feat:【antd】【mall】 spu 相关公共组件迁移

pull/277/head
puhui999 2025-11-24 15:07:00 +08:00
parent 390d9d7ca7
commit 26416b0acd
2 changed files with 71 additions and 28 deletions

View File

@ -14,7 +14,7 @@ import { getRangePickerDefaultProps } from '#/utils';
* @description:
*/
export function useGridFormSchema(
categoryTreeList: Ref<MallCategoryApi.Category[]>,
categoryTreeList: Ref<MallCategoryApi.Category[] | unknown[]>,
): VbenFormSchema[] {
return [
{

View File

@ -2,6 +2,7 @@
import type { PropertyAndValues } from './type';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MallCategoryApi } from '#/api/mall/product/category';
import type { MallSpuApi } from '#/api/mall/product/spu';
import { computed, onMounted, ref } from 'vue';
@ -38,12 +39,8 @@ interface SpuSelectProps {
}
// ============ ============
const categoryList = ref<Array<{ id: number; name: string; parentId: number }>>(
[],
); //
const categoryTreeList = ref<
Array<{ id: number; name: string; parentId: number }>
>([]); //
const categoryList = ref<MallCategoryApi.Category[]>([]); //
const categoryTreeList = ref<MallCategoryApi.Category[]>([]); //
const propertyList = ref<PropertyAndValues[]>([]); //
const spuData = ref<MallSpuApi.Spu>(); //
const isExpand = ref(false); // SKU
@ -67,16 +64,24 @@ function selectSku(val: MallSpuApi.Sku[]) {
}
if (props.radio) {
//
selectedSkuIds.value = [val.map((sku) => sku.id!)[0]];
const firstId = val[0]?.id;
if (firstId !== undefined) {
selectedSkuIds.value = [firstId];
}
// 1
if (val.length > 1) {
//
skuTable?.clearSelection();
//
skuTable?.toggleRowSelection(val.pop(), true);
const lastItem = val.pop();
if (lastItem) {
skuTable?.toggleRowSelection(lastItem, true);
}
}
} else {
selectedSkuIds.value = val.map((sku) => sku.id!);
selectedSkuIds.value = val
.map((sku) => sku.id!)
.filter((id): id is number => id !== undefined);
}
}
@ -87,7 +92,10 @@ function selectSpu(val: MallSpuApi.Spu[]) {
return;
}
//
selectedSpuId.value = val.map((spu) => spu.id!)[0];
const firstId = val[0]?.id;
if (firstId !== undefined) {
selectedSpuId.value = firstId;
}
// spu sku , sku spu
if (selectedSkuIds.value.length > 0) {
selectedSkuIds.value = [];
@ -119,8 +127,17 @@ async function expandChange(
if (selectedSpuId.value !== 0) {
if (row.id !== selectedSpuId.value) {
message.warning('你已选择商品请先取消');
//
await gridApi.grid.setExpandRowKeys([selectedSpuId.value]);
//
if (row.id !== undefined) {
const tableData = gridApi.grid.getTableData().fullData;
const selectedRow = tableData.find(
(item: MallSpuApi.Spu) => item.id === selectedSpuId.value,
);
if (selectedRow) {
//
gridApi.grid.setRowExpand(selectedRow, true);
}
}
return;
}
// skuList spu skuList
@ -132,28 +149,36 @@ async function expandChange(
propertyList.value = [];
isExpand.value = false;
if (expandedRows?.length === 0) {
// 0
expandRowKeys.value = [];
// 0
return;
}
// SPU
const res = (await getSpu(row.id as number)) as MallSpuApi.Spu;
if (row.id === undefined) {
return;
}
const res = (await getSpu(row.id)) as MallSpuApi.Spu;
// API
// API
res.skus?.forEach((item) => {
if (item.price) item.price = Math.round(item.price * 100);
if (item.marketPrice) item.marketPrice = Math.round(item.marketPrice * 100);
if (item.costPrice) item.costPrice = Math.round(item.costPrice * 100);
if (item.firstBrokeragePrice)
if (typeof item.price === 'number') {
item.price = Math.round(item.price * 100);
}
if (typeof item.marketPrice === 'number') {
item.marketPrice = Math.round(item.marketPrice * 100);
}
if (typeof item.costPrice === 'number') {
item.costPrice = Math.round(item.costPrice * 100);
}
if (typeof item.firstBrokeragePrice === 'number') {
item.firstBrokeragePrice = Math.round(item.firstBrokeragePrice * 100);
if (item.secondBrokeragePrice)
}
if (typeof item.secondBrokeragePrice === 'number') {
item.secondBrokeragePrice = Math.round(item.secondBrokeragePrice * 100);
}
});
propertyList.value = getPropertyList(res);
spuData.value = res;
isExpand.value = true;
// grid API
await gridApi.grid.setExpandRowKeys([row.id!]);
}
/** 确认选择时的触发事件 */
@ -222,11 +247,21 @@ const [Grid, gridApi] = useVbenVxeGrid({
},
},
gridEvents: {
checkboxChange: ({ records }) => {
checkboxChange: ({ records }: { records: unknown[] }) => {
selectSpu(records as MallSpuApi.Spu[]);
},
expandChange: ({ row, expandedRows }) => {
expandChange(row as MallSpuApi.Spu, expandedRows as MallSpuApi.Spu[]);
toggleRowExpand: ({
row,
expanded,
}: {
expanded: boolean;
row: unknown;
}) => {
if (expanded) {
expandChange(row as MallSpuApi.Spu, [row as MallSpuApi.Spu]);
} else {
expandChange(row as MallSpuApi.Spu, []);
}
},
},
});
@ -238,7 +273,11 @@ const [Modal, modalApi] = useVbenModal({
async onOpenChange(isOpen: boolean) {
if (!isOpen) {
await gridApi.grid.clearCheckboxRow();
await gridApi.grid.setExpandRowKeys([]);
//
const tableData = gridApi.grid.getTableData().fullData;
tableData.forEach((row: MallSpuApi.Spu) => {
gridApi.grid.setRowExpand(row, false);
});
selectedSpuId.value = 0;
selectedSkuIds.value = [];
spuData.value = undefined;
@ -261,7 +300,11 @@ defineExpose({
/** 初始化分类数据 */
onMounted(async () => {
categoryList.value = await getCategoryList({});
categoryTreeList.value = handleTree(categoryList.value, 'id', 'parentId');
categoryTreeList.value = handleTree(
categoryList.value,
'id',
'parentId',
) as MallCategoryApi.Category[];
});
</script>