This commit is contained in:
xingyu4j
2025-10-23 09:47:08 +08:00
8 changed files with 55 additions and 105 deletions

View File

@@ -2,4 +2,4 @@ export { default as SpuAndSkuList } from './spu-and-sku-list.vue';
export { default as SpuSkuSelect } from './spu-sku-select.vue';
export type * from './types';
// TODO @puhui999这个要不要也放到 product/spu/components 下?相当于各种商品的 select 能力,统一由 spu 提供组件化能力!

View File

@@ -15,14 +15,10 @@ import { TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { SkuList } from '#/views/mall/product/spu/form';
interface SpuAndSkuListProps {
/** SPU 列表 */
spuList: T[];
/** 规则配置 */
ruleConfig: RuleConfig[];
/** SPU 属性列表 */
spuPropertyList: SpuProperty<T>[];
/** 是否可删除 */
deletable?: boolean;
spuList: T[]; // SPU 列表
ruleConfig: RuleConfig[]; // 规则配置
spuPropertyList: SpuProperty<T>[]; // SPU 属性列表
deletable?: boolean; // 是否可删除
}
const props = withDefaults(defineProps<SpuAndSkuListProps>(), {
@@ -33,13 +29,11 @@ const emit = defineEmits<{
delete: [spuId: number];
}>();
// 内部数据
const spuData = ref<MallSpuApi.Spu[]>([]);
const spuPropertyListData = ref<SpuProperty<T>[]>([]);
const expandedRowKeys = ref<number[]>([]);
const skuListRef = ref<InstanceType<typeof SkuList>>();
// 列配置(动态计算)
const columns = computed<VxeGridProps['columns']>(() => {
const cols: VxeGridProps['columns'] = [
{
@@ -103,7 +97,6 @@ const columns = computed<VxeGridProps['columns']>(() => {
return cols;
});
// 初始化表格
const [Grid, gridApi] = useVbenVxeGrid({
gridOptions: {
columns: columns.value,
@@ -121,14 +114,12 @@ const [Grid, gridApi] = useVbenVxeGrid({
},
});
/**
* 删除 SPU
*/
/** 删除 SPU */
async function handleDelete(row: MallSpuApi.Spu) {
// TODO @puhui999可以使用 TableAction 的 popConfirm 哈?
await confirm({
content: `是否删除商品编号为 ${row.id} 的数据?`,
});
const index = spuData.value.findIndex((item) => item.id === row.id);
if (index !== -1) {
spuData.value.splice(index, 1);
@@ -138,6 +129,7 @@ async function handleDelete(row: MallSpuApi.Spu) {
/**
* 获取所有 SKU 活动配置
*
* @param extendedAttribute 在 SKU 上扩展的属性名称
*/
function getSkuConfigs(extendedAttribute: string) {
@@ -157,32 +149,36 @@ function getSkuConfigs(extendedAttribute: string) {
return configs;
}
// 暴露方法给父组件
defineExpose({
getSkuConfigs,
});
// 监听 spuList 变化
/** 监听 spuList 变化 */
watch(
() => props.spuList,
(data) => {
if (!data) return;
if (!data) {
return;
}
spuData.value = data as MallSpuApi.Spu[];
// 更新表格列配置和数据
gridApi.grid.reloadData(spuData.value);
gridApi.grid.reloadColumn(columns.value);
gridApi.grid.reloadColumn(columns.value as any[]);
},
{ deep: true, immediate: true },
);
// 监听 spuPropertyList 变化
/** 监听 spuPropertyList 变化 */
watch(
() => props.spuPropertyList,
(data) => {
if (!data) return;
if (!data) {
return;
}
spuPropertyListData.value = data as SpuProperty<T>[];
// 延迟展开所有行,确保 SKU 列表正确渲染
// TODO @puhui999只能 setTimeout 么await 之类可以么?
setTimeout(() => {
expandedRowKeys.value = data.map((item) => item.spuId);
// 手动展开每一行
@@ -222,7 +218,6 @@ watch(
</SkuList>
</div>
</template>
<!-- 操作列 -->
<template
v-if="props.deletable && props.spuList.length > 1"

View File

@@ -18,10 +18,8 @@ import { getRangePickerDefaultProps } from '#/utils';
import { getPropertyList, SkuList } from '#/views/mall/product/spu/form';
interface SpuSkuSelectProps {
/** 是否选择 SKU */
isSelectSku?: boolean;
/** 是否单选 SKU */
radio?: boolean;
isSelectSku?: boolean; // 是否选择 SKU
radio?: boolean; // 是否单选 SKU
}
const props = withDefaults(defineProps<SpuSkuSelectProps>(), {
@@ -33,33 +31,17 @@ const emit = defineEmits<{
confirm: [spuId: number, skuIds?: number[]];
}>();
// 单选:选中的 SPU ID
const selectedSpuId = ref<number>();
// 选中的 SKU ID 列表
const selectedSkuIds = ref<number[]>([]);
// SKU 列表数据
const selectedSpuId = ref<number>(); // 单选:选中的 SPU ID
const selectedSkuIds = ref<number[]>([]); // 选中的 SKU ID 列表
const spuData = ref<MallSpuApi.Spu>();
const propertyList = ref<any[]>([]);
const isExpand = ref(false);
const expandRowKeys = ref<number[]>([]);
const skuListRef = ref<InstanceType<typeof SkuList>>();
// 分类列表(扁平)
const categoryList = ref<any[]>([]);
// 分类树
const categoryTreeList = ref<any[]>([]);
const categoryList = ref<any[]>([]); // 分类列表(扁平)
const categoryTreeList = ref<any[]>([]); // 分类树
// 初始化分类数据
onMounted(async () => {
try {
categoryList.value = await getCategoryList({});
categoryTreeList.value = handleTree(categoryList.value, 'id', 'parentId');
} catch (error) {
console.error('加载分类数据失败:', error);
}
});
// 搜索表单配置
const formSchema = computed<VbenFormSchema[]>(() => {
return [
{
@@ -100,10 +82,8 @@ const formSchema = computed<VbenFormSchema[]>(() => {
];
});
// 列配置
const gridColumns = computed<VxeGridProps['columns']>(() => {
const columns: VxeGridProps['columns'] = [];
// 如果需要选择 SKU添加展开列
if (props.isSelectSku) {
columns.push({
@@ -112,7 +92,6 @@ const gridColumns = computed<VxeGridProps['columns']>(() => {
slots: { content: 'expand-content' },
});
}
// 单选列
columns.push(
{
@@ -152,11 +131,9 @@ const gridColumns = computed<VxeGridProps['columns']>(() => {
},
},
);
return columns;
});
// 初始化表格
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: formSchema.value,
@@ -175,6 +152,7 @@ const [Grid, gridApi] = useVbenVxeGrid({
ajax: {
async query({ page }: any, formValues: any) {
try {
// TODO @puhui9991try catch 可以去掉2params 直接放到 getSpuPage 里哇?
const params = {
pageNo: page.currentPage,
pageSize: page.pageSize,
@@ -200,23 +178,21 @@ const [Grid, gridApi] = useVbenVxeGrid({
},
});
// 单选:处理选中 SPU
/** 单选:处理选中 SPU */
async function handleSingleSelected(row: MallSpuApi.Spu) {
selectedSpuId.value = row.id;
// 如果需要选择 SKU展开行
if (props.isSelectSku) {
await expandChange(row, [row]);
}
}
// 选择 SKU
/** 选择 SKU */
function selectSku(skus: MallSpuApi.Sku[]) {
if (!selectedSpuId.value) {
message.warning('请先选择商品再选择相应的规格!');
return;
}
if (skus.length === 0) {
selectedSkuIds.value = [];
return;
@@ -226,7 +202,7 @@ function selectSku(skus: MallSpuApi.Sku[]) {
: (selectedSkuIds.value = skus.map((sku) => sku.id!));
}
// 展开行,加载 SKU 列表
/** 展开行,加载 SKU 列表 */
async function expandChange(
row: MallSpuApi.Spu,
expandedRows?: MallSpuApi.Spu[],
@@ -237,7 +213,6 @@ async function expandChange(
expandRowKeys.value = [selectedSpuId.value];
return;
}
// 如果已经展开且是同一个 SPU不重复加载
if (isExpand.value && spuData.value?.id === row.id) {
return;
@@ -247,7 +222,6 @@ async function expandChange(
spuData.value = undefined;
propertyList.value = [];
isExpand.value = false;
if (!expandedRows || expandedRows.length === 0) {
expandRowKeys.value = [];
return;
@@ -261,13 +235,12 @@ async function expandChange(
expandRowKeys.value = [row.id!];
}
// 确认选择
/** 确认选择 */
function handleConfirm() {
if (!selectedSpuId.value) {
message.warning('没有选择任何商品');
return;
}
if (props.isSelectSku && selectedSkuIds.value.length === 0) {
message.warning('没有选择任何商品属性');
return;
@@ -279,12 +252,10 @@ function handleConfirm() {
selectedSpuId.value,
props.isSelectSku ? selectedSkuIds.value : undefined,
);
// 关闭弹窗
modalApi.close();
}
// 初始化弹窗
const [Modal, modalApi] = useVbenModal({
onConfirm: handleConfirm,
async onOpenChange(isOpen: boolean) {
@@ -307,6 +278,11 @@ defineExpose({
open: modalApi.open,
close: modalApi.close,
});
/** 初始化分类数据 */
onMounted(async () => {
categoryList.value = await getCategoryList({});
categoryTreeList.value = handleTree(categoryList.value, 'id', 'parentId');
});
</script>
<template>

View File

@@ -5,11 +5,7 @@ import type { PropertyAndValues } from '#/views/mall/product/spu/form';
* 用于活动商品选择中,关联 SPU 和其属性列表
*/
export interface SpuProperty<T = any> {
/** SPU ID */
spuId: number;
/** SPU 详情 */
spuDetail: T;
/** 属性列表 */
propertyList: PropertyAndValues[];
spuId: number; // SPU ID
spuDetail: T; // SPU 详情
propertyList: PropertyAndValues[]; // 属性列表
}