From 6246bd22334501f9842e27c7e1a9a45e8b398fb3 Mon Sep 17 00:00:00 2001 From: ah-max150 Date: Sat, 12 Sep 2026 10:44:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8F=91=E5=B8=83=E5=BC=B9=E7=AA=97?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=A7=84=E6=A0=BC=E6=8B=96=E6=8B=BD=E3=80=81?= =?UTF-8?q?SKU=E8=A7=84=E6=A0=BC=E5=86=85=E8=81=94=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E4=B8=8E=E5=95=86=E5=AE=B6=E7=BC=96=E7=A0=81=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E5=85=B3=E8=81=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../goods/components/PddGoodsPublishModal.vue | 679 +++++++++++++++--- 1 file changed, 581 insertions(+), 98 deletions(-) diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/channel/pdd/goods/components/PddGoodsPublishModal.vue b/ecom-dmb-frontend/apps/web-antd/src/views/channel/pdd/goods/components/PddGoodsPublishModal.vue index 2b52893d..9e00442f 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/channel/pdd/goods/components/PddGoodsPublishModal.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/channel/pdd/goods/components/PddGoodsPublishModal.vue @@ -9,7 +9,11 @@ import type { CascaderProps, UploadFile, UploadProps } from 'ant-design-vue'; import { computed, nextTick, onBeforeUnmount, reactive, ref, watch } from 'vue'; +import type { Sortable } from '@vben/hooks'; + import { useVbenModal } from '@vben/common-ui'; +import { useSortable } from '@vben/hooks'; +import { createIconifyIcon } from '@vben/icons'; import { PlusOutlined, MinusOutlined } from '@ant-design/icons-vue'; import { @@ -52,12 +56,18 @@ import { } from '#/api/channel/pdd/cat'; import { getPddShopPageApi } from '#/api/channel/pdd/shop'; import { getProductPageApi } from '#/api/ec/product'; -import { getProductSkuPageApi } from '#/api/ec/product/sku'; +import { + getProductSkuBySkuCodeApi, + getProductSkuPageApi, +} from '#/api/ec/product/sku'; import { FileCategory } from '#/constants/file-category'; import { uploadFile } from '#/utils/upload-file'; defineOptions({ name: 'PddGoodsPublishModal' }); +/** 规格轴 / 规格值拖拽手柄 */ +const GripVerticalIcon = createIconifyIcon('lucide:grip-vertical'); + const props = defineProps<{ open: boolean; presetPddShopId?: number | null; @@ -157,6 +167,13 @@ type SkuRow = { comboKey: string; internalSkuId?: number; productName?: string; + /** 关联的内部 SKU 名称(展示用) */ + internalSkuName?: string; + /** + * 商家编码是否命中内部 SKU: + * idle=未校验/空;checking=请求中;matched=是;unmatched=非内部SKU + */ + codeMatchStatus?: 'idle' | 'checking' | 'matched' | 'unmatched'; skuName: string; outSkuSn: string; priceYuan?: number; @@ -230,6 +247,19 @@ const specCatalog = ref([]); /** 用户已添加的规格轴(默认一条「款式」) */ const specAxes = ref([]); let specAxisRowSeq = 1; +/** 规格轴列表容器(拖拽排序) */ +const specAxesListRef = ref(null); +let axesSortable: null | Sortable = null; +/** 各轴规格值容器的 Sortable 实例 */ +const valueSortables = new Map(); +/** SKU 规格列内联编辑草稿:`${rowKey}_${axisKey}` → 输入中文案 */ +const skuSpecDraft = ref>({}); +/** 上方「内部 SKU 拉入」多选 */ +const bindPickSkuIds = ref([]); +/** 商家编码校验防抖定时器(按行) */ +const outSkuSnTimers = new Map>(); +/** 商家编码校验序号,防止乱序回写 */ +const outSkuSnValidateSeq = new Map(); /** 规格表容器:新增后滚到表体底部 */ const skuTableWrapRef = ref(null); @@ -279,12 +309,13 @@ const skuTableColumns = computed(() => [ ...specAxes.value.map((axis) => ({ title: axis.name, key: `spec_${axis.key}`, - width: 120, + width: 140, customCell: (_record: SkuRow, rowIndex?: number) => ({ rowSpan: skuSpecRowSpans.value.get(axis.key)?.[rowIndex ?? 0] ?? 1, }), })), - { title: '商家编码', key: 'code', width: 300 }, + { title: '编码', key: 'code', width: 140 }, + { title: '内部SKU', key: 'internalSku', width: 160 }, { title: '单买价', key: 'price', width: 120 }, { title: '拼单价', key: 'multi', width: 120 }, { title: '库存', key: 'qty', width: 100 }, @@ -293,7 +324,7 @@ const skuTableColumns = computed(() => [ ]); const skuTableScrollX = computed( - () => 740 + specAxes.value.length * 120, + () => 740 + specAxes.value.length * 140, ); const logisticsOptions = ref<{ label: string; value: number }[]>([]); @@ -1019,6 +1050,111 @@ function removeSpecValue(rowId: number, index: number) { }); } +/** 销毁规格轴/规格值 Sortable,避免泄漏 */ +function destroySpecSortables() { + axesSortable?.destroy(); + axesSortable = null; + for (const s of valueSortables.values()) { + s.destroy(); + } + valueSortables.clear(); +} + +/** 规格轴卡片拖拽:交换维度顺序后重建 SKU 行序 */ +async function initAxesSortable() { + axesSortable?.destroy(); + axesSortable = null; + await nextTick(); + const el = specAxesListRef.value; + if (!el || specAxes.value.length < 2) return; + const { initializeSortable } = useSortable(el, { + animation: 200, + delay: 0, + handle: '.spec-axis-drag-handle', + draggable: '.spec-axis-card', + onEnd(evt) { + const { oldIndex, newIndex } = evt; + if ( + oldIndex == null || + newIndex == null || + oldIndex === newIndex || + Number.isNaN(oldIndex) || + Number.isNaN(newIndex) + ) { + return; + } + const list = [...specAxes.value]; + const [moved] = list.splice(oldIndex, 1); + if (!moved) return; + list.splice(newIndex, 0, moved); + specAxes.value = list; + syncSkuRowsFromSpecs(); + void refreshSpecSortables(); + }, + }); + axesSortable = await initializeSortable(); +} + +/** 轴内规格值 Chip 拖拽:只调同轴取值顺序 */ +async function initValueSortables() { + for (const s of valueSortables.values()) { + s.destroy(); + } + valueSortables.clear(); + await nextTick(); + for (const axis of specAxes.value) { + const el = document.querySelector( + `[data-spec-values="${axis.rowId}"]`, + ) as HTMLElement | null; + if (!el || axis.selectedValues.length < 2) continue; + const rowId = axis.rowId; + const { initializeSortable } = useSortable(el, { + animation: 150, + delay: 0, + handle: '.spec-value-drag-handle', + draggable: '.spec-value-chip', + onEnd(evt) { + const { oldIndex, newIndex } = evt; + if ( + oldIndex == null || + newIndex == null || + oldIndex === newIndex || + Number.isNaN(oldIndex) || + Number.isNaN(newIndex) + ) { + return; + } + const target = specAxes.value.find((a) => a.rowId === rowId); + if (!target) return; + const vals = [...target.selectedValues]; + const [moved] = vals.splice(oldIndex, 1); + if (moved === undefined) return; + vals.splice(newIndex, 0, moved); + target.selectedValues = vals; + syncSkuRowsFromSpecs(); + void refreshSpecSortables(); + }, + }); + valueSortables.set(rowId, await initializeSortable()); + } +} + +async function refreshSpecSortables() { + await initAxesSortable(); + await initValueSortables(); +} + +/** 规格轴数量或取值槽位数变化时重建拖拽 */ +watch( + () => + specAxes.value + .map((a) => `${a.rowId}:${a.selectedValues.length}`) + .join('|'), + () => { + void refreshSpecSortables(); + }, +); + function cartesianProduct(lists: string[][]): string[][] { if (lists.length === 0) return []; return lists.reduce( @@ -1047,6 +1183,72 @@ function skuSpecColumnLabel( return labelOfAxisValue(axis, raw); } +function skuSpecDraftKey(rowKey: number, axisKey: string) { + return `${rowKey}_${axisKey}`; +} + +/** 规格列展示:优先草稿(输入中),否则轴上取值的 label */ +function skuSpecCellDisplay(record: Record, axisKey: string) { + const rowKey = Number(record.key); + const dk = skuSpecDraftKey(rowKey, axisKey); + if (Object.prototype.hasOwnProperty.call(skuSpecDraft.value, dk)) { + return skuSpecDraft.value[dk] ?? ''; + } + return skuSpecColumnLabel(record, `spec_${axisKey}`); +} + +function skuSpecCellAutoOptions(record: Record, axisKey: string) { + const axis = specAxes.value.find((a) => a.key === axisKey); + if (!axis) return []; + const current = (record.saleValues?.[axisKey] as string | undefined) ?? ''; + const index = axis.selectedValues.findIndex((v) => v === current); + if (index < 0) { + return axis.presetOptions.map((o) => ({ + label: o.label, + value: o.label, + })); + } + return specValueAutoOptions(axis, index); +} + +function onSkuSpecCellInput(rowKey: number, axisKey: string, text: string) { + skuSpecDraft.value = { + ...skuSpecDraft.value, + [skuSpecDraftKey(rowKey, axisKey)]: text, + }; +} + +/** + * 合并单元格内改规格值 = 重命名该轴上对应取值(整组同值),再同步笛卡尔积。 + */ +function onSkuSpecCellCommit(rowKey: number, axisKey: string) { + const dk = skuSpecDraftKey(rowKey, axisKey); + if (!Object.prototype.hasOwnProperty.call(skuSpecDraft.value, dk)) return; + const draft = skuSpecDraft.value[dk] ?? ''; + const nextDraft = { ...skuSpecDraft.value }; + delete nextDraft[dk]; + skuSpecDraft.value = nextDraft; + + const row = skuRows.value.find((r) => r.key === rowKey); + const axis = specAxes.value.find((a) => a.key === axisKey); + if (!row || !axis) return; + + const oldToken = row.saleValues[axisKey] ?? ''; + const newToken = resolveSpecValueToken(axis, draft); + if (!newToken || newToken === oldToken) return; + + const vi = axis.selectedValues.findIndex((v) => v === oldToken); + if (vi < 0) return; + if ( + axis.selectedValues.some((v, i) => i !== vi && v.trim() === newToken) + ) { + message.warning('该规格值已存在'); + return; + } + axis.selectedValues[vi] = newToken; + syncSkuRowsFromSpecs(); +} + /** * 规格取值变化后按笛卡尔积生成 SKU;同组合键保留已填价格/编码/图。 */ @@ -1119,6 +1321,8 @@ function bindInternalSku(rowKey: number, skuId?: number) { if (skuId == null) { row.internalSkuId = undefined; row.productName = undefined; + row.internalSkuName = undefined; + row.codeMatchStatus = 'idle'; return; } if ( @@ -1136,11 +1340,182 @@ function bindInternalSku(rowKey: number, skuId?: number) { } row.internalSkuId = sku.id; row.productName = sku.productName; + row.internalSkuName = sku.name; + row.codeMatchStatus = 'matched'; if (sku.skuCode) { row.outSkuSn = sku.skuCode; } } +/** 商家编码写入后清除该行待校验定时器 */ +function clearOutSkuSnTimer(rowKey: number) { + const t = outSkuSnTimers.get(rowKey); + if (t) { + clearTimeout(t); + outSkuSnTimers.delete(rowKey); + } +} + +function clearAllOutSkuSnTimers() { + for (const t of outSkuSnTimers.values()) { + clearTimeout(t); + } + outSkuSnTimers.clear(); + outSkuSnValidateSeq.clear(); +} + +/** 商家编码变更:400ms 防抖后校验是否内部 SKU */ +function onOutSkuSnChange(rowKey: number, value: string) { + const row = skuRows.value.find((r) => r.key === rowKey); + if (!row) return; + row.outSkuSn = value; + // 编码改动后暂视为未校验,避免仍显示旧关联 + if (row.codeMatchStatus === 'matched' || row.codeMatchStatus === 'unmatched') { + row.codeMatchStatus = 'idle'; + } + clearOutSkuSnTimer(rowKey); + outSkuSnTimers.set( + rowKey, + setTimeout(() => { + outSkuSnTimers.delete(rowKey); + void validateOutSkuSnByCode(rowKey); + }, 400), + ); +} + +/** 失焦立即校验(取消防抖) */ +function onOutSkuSnBlur(rowKey: number) { + clearOutSkuSnTimer(rowKey); + void validateOutSkuSnByCode(rowKey); +} + +/** + * 按商家编码反查内部 SKU;命中则关联,否则标「非内部SKU」。 + */ +async function validateOutSkuSnByCode(rowKey: number) { + const row = skuRows.value.find((r) => r.key === rowKey); + if (!row) return; + + const code = row.outSkuSn.trim(); + if (!code) { + row.internalSkuId = undefined; + row.productName = undefined; + row.internalSkuName = undefined; + row.codeMatchStatus = 'idle'; + return; + } + + // 已关联且编码未变:跳过请求 + if ( + row.codeMatchStatus === 'matched' && + row.internalSkuId != null && + row.outSkuSn.trim() === code + ) { + const bound = skuOptions.value.find((s) => s.id === row.internalSkuId); + if (bound?.skuCode && bound.skuCode === code) return; + } + + const seq = (outSkuSnValidateSeq.get(rowKey) ?? 0) + 1; + outSkuSnValidateSeq.set(rowKey, seq); + row.codeMatchStatus = 'checking'; + + try { + const sku = await getProductSkuBySkuCodeApi(code); + if (outSkuSnValidateSeq.get(rowKey) !== seq) return; + if (!sku?.id) { + row.internalSkuId = undefined; + row.productName = undefined; + row.internalSkuName = undefined; + row.codeMatchStatus = 'unmatched'; + return; + } + if ( + skuRows.value.some( + (r) => r.key !== row.key && r.internalSkuId === sku.id, + ) + ) { + message.warning('该内部 SKU 已绑定到其他规格组合'); + row.internalSkuId = undefined; + row.productName = sku.productName; + row.internalSkuName = sku.name; + row.codeMatchStatus = 'matched'; + return; + } + row.internalSkuId = sku.id; + row.productName = sku.productName; + row.internalSkuName = sku.name; + row.codeMatchStatus = 'matched'; + // 缓存到下拉候选,便于同弹窗内复用 + if (!skuOptions.value.some((s) => s.id === sku.id)) { + skuOptions.value = [sku, ...skuOptions.value]; + } + } catch { + if (outSkuSnValidateSeq.get(rowKey) !== seq) return; + row.internalSkuId = undefined; + row.productName = undefined; + row.internalSkuName = undefined; + row.codeMatchStatus = 'unmatched'; + } +} + +/** 按当前 SKU 表行序依次关联上方多选的内部 SKU */ +function applyInternalSkusInOrder() { + const ids = bindPickSkuIds.value; + if (ids.length === 0) { + message.warning('请先选择内部 SKU'); + return; + } + const rows = skuRows.value; + if (rows.length === 0) { + message.warning('请先生成 SKU 组合'); + return; + } + const n = Math.min(ids.length, rows.length); + const idSet = new Set(ids.slice(0, n)); + for (const r of rows) { + if (r.internalSkuId != null && idSet.has(r.internalSkuId)) { + r.internalSkuId = undefined; + r.productName = undefined; + r.internalSkuName = undefined; + r.codeMatchStatus = 'idle'; + } + } + for (let i = 0; i < n; i++) { + const row = rows[i]!; + const skuId = ids[i]!; + const sku = skuOptions.value.find((s) => s.id === skuId); + if (!sku) continue; + clearOutSkuSnTimer(row.key); + row.internalSkuId = sku.id; + row.productName = sku.productName; + row.internalSkuName = sku.name; + row.codeMatchStatus = 'matched'; + if (sku.skuCode) { + row.outSkuSn = sku.skuCode; + } + } + if (ids.length > rows.length) { + message.info(`已按顺序填入 ${rows.length} 行,多余选择已忽略`); + } else if (ids.length < rows.length) { + message.info(`已填入前 ${ids.length} 行,其余行未改动`); + } else { + message.success('已按顺序关联内部 SKU'); + } +} + +/** 清空全部行的内部 SKU 关联(不改商家编码) */ +function clearInternalSkuBindings() { + if (skuRows.value.length === 0) return; + for (const row of skuRows.value) { + clearOutSkuSnTimer(row.key); + row.internalSkuId = undefined; + row.productName = undefined; + row.internalSkuName = undefined; + row.codeMatchStatus = 'idle'; + } + message.success('已清空内部 SKU 关联'); +} + /** 新增规格后滚到表体底部,方便继续编辑新行 */ async function scrollSkuTableToBottom() { await nextTick(); @@ -1678,6 +2053,10 @@ function resetAll() { clearSkuRows(); specAxes.value = []; specCatalog.value = []; + skuSpecDraft.value = {}; + bindPickSkuIds.value = []; + clearAllOutSkuSnTimers(); + destroySpecSortables(); catTemplate.value = null; parentSpecs.value = []; rootCatOptions.value = []; @@ -1693,6 +2072,8 @@ function resetAll() { onBeforeUnmount(() => { document.removeEventListener('pointerdown', onOverlayPointerDown, true); clearLocalImages(); + clearAllOutSkuSnTimers(); + destroySpecSortables(); if (catSearchTimer) clearTimeout(catSearchTimer); }); @@ -2056,7 +2437,7 @@ onBeforeUnmount(() => { type="info" show-icon class="mb-3" - message="默认一条「款式」;最多两种规格。规格值始终可编辑,失焦后更新下方 SKU 组合。" + message="默认一条「款式」;最多两种规格。拖拽手柄可调规格轴与规格值顺序;失焦后更新下方 SKU 组合。" /> { :image="Empty.PRESENTED_IMAGE_SIMPLE" />
-
- +
+
+
+ + + + + +
+
+
-
-