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 1783dddb..2b52893d 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 @@ -14,6 +14,7 @@ import { PlusOutlined, MinusOutlined } from '@ant-design/icons-vue'; import { Alert, + AutoComplete, Button, Card, Cascader, @@ -228,8 +229,6 @@ const skuRows = ref([]); const specCatalog = ref([]); /** 用户已添加的规格轴(默认一条「款式」) */ const specAxes = ref([]); -/** 各规格行「输入规格值」草稿 */ -const specValueDraftByRow = ref>({}); let specAxisRowSeq = 1; /** 规格表容器:新增后滚到表体底部 */ const skuTableWrapRef = ref(null); @@ -614,7 +613,6 @@ async function applyCatSelection(catId: number, pathIds?: string, pathLabel?: st clearSkuRows(); specAxes.value = []; specCatalog.value = []; - specValueDraftByRow.value = {}; await refreshCatAuth(path); if (!form.pddShopId || !form.catId) return; if (catAuthAuthorized.value === false) { @@ -729,8 +727,7 @@ watch( clearSkuRows(); specAxes.value = []; specCatalog.value = []; - specValueDraftByRow.value = {}; - logisticsOptions.value = []; + logisticsOptions.value = []; form.goodsPropValues = {}; catSearchHitOptions.value = []; catCascaderSearch.value = ''; @@ -806,7 +803,8 @@ function createAxisFromCatalog(item: SpecCatalogItem): SpecAxis { templatePid: item.templatePid, valueUnit: item.valueUnit, presetOptions: item.presetOptions.map((o) => ({ ...o })), - selectedValues: [], + // 默认一条空输入,随时可编辑 + selectedValues: [''], }; } @@ -822,7 +820,6 @@ function findDefaultCatalogItem(catalog: SpecCatalogItem[]) { function rebuildSpecCatalogAndSeed() { const catalog = buildSpecCatalog(); specCatalog.value = catalog; - specValueDraftByRow.value = {}; const def = findDefaultCatalogItem(catalog); specAxes.value = def ? [createAxisFromCatalog(def)] : []; syncSkuRowsFromSpecs(); @@ -851,7 +848,7 @@ function applyCatalogToAxis(axis: SpecAxis, catalogKey: string) { axis.templatePid = item.templatePid; axis.valueUnit = item.valueUnit; axis.presetOptions = item.presetOptions.map((o) => ({ ...o })); - axis.selectedValues = []; + axis.selectedValues = ['']; } function onAxisCatalogChange(rowId: number, catalogKey: string) { @@ -892,51 +889,98 @@ function removeSpecAxis(rowId: number) { zIndex: 2100, onOk() { specAxes.value = specAxes.value.filter((a) => a.rowId !== rowId); - const drafts = { ...specValueDraftByRow.value }; - delete drafts[rowId]; - specValueDraftByRow.value = drafts; syncSkuRowsFromSpecs(); }, }); } -function unusedPresetOptions(axis: SpecAxis) { - const selected = new Set(axis.selectedValues); - return axis.presetOptions - .filter((o) => !selected.has(o.value)) - .map((o) => ({ label: o.label, value: o.value })); +/** 参与笛卡尔积的有效规格值(去掉空白) */ +function effectiveSpecValues(axis: SpecAxis) { + return axis.selectedValues.map((v) => v.trim()).filter(Boolean); } -function addSpecValue(rowId: number, raw: string) { - const axis = specAxes.value.find((a) => a.rowId === rowId); - if (!axis) return; +/** AutoComplete 展示文案:预设用 label,自定义原样 */ +function specValueDisplay(axis: SpecAxis, index: number) { + const raw = axis.selectedValues[index] ?? ''; + return labelOfAxisValue(axis, raw); +} + +/** 下拉选项:未占用的预设 + 当前项(用 label 作 value,避免输入框出现 vid) */ +function specValueAutoOptions(axis: SpecAxis, index: number) { + const current = axis.selectedValues[index] ?? ''; + const used = new Set( + axis.selectedValues + .map((v, i) => (i === index ? '' : v.trim())) + .filter(Boolean), + ); + return axis.presetOptions + .filter((o) => !used.has(o.value) || o.value === current) + .map((o) => ({ label: o.label, value: o.label })); +} + +function resolveSpecValueToken(axis: SpecAxis, raw: string) { const text = raw.trim(); - if (!text) return; + if (!text) return ''; const matched = axis.presetOptions.find( (o) => o.value === text || o.label === text, ); - const value = matched?.value ?? text; - if (axis.selectedValues.includes(value)) { - message.warning('该规格值已添加'); + return matched?.value ?? text; +} + +function onSpecValueInput(rowId: number, index: number, text: string) { + const axis = specAxes.value.find((a) => a.rowId === rowId); + if (!axis || index < 0 || index >= axis.selectedValues.length) return; + // 输入过程保留原文,失焦再归一成预设 value + axis.selectedValues[index] = text; +} + +function onSpecValueCommit(rowId: number, index: number) { + const axis = specAxes.value.find((a) => a.rowId === rowId); + if (!axis || index < 0 || index >= axis.selectedValues.length) return; + const value = resolveSpecValueToken(axis, axis.selectedValues[index] ?? ''); + if ( + value && + axis.selectedValues.some((v, i) => i !== index && v.trim() === value) + ) { + message.warning('该规格值已存在'); + axis.selectedValues[index] = ''; + syncSkuRowsFromSpecs(); return; } - axis.selectedValues.push(value); - specValueDraftByRow.value = { - ...specValueDraftByRow.value, - [rowId]: '', - }; + axis.selectedValues[index] = value; syncSkuRowsFromSpecs(); } -function commitSpecValueDraft(rowId: number) { - addSpecValue(rowId, specValueDraftByRow.value[rowId] || ''); +/** 回车:提交当前值并追加一个新的空输入,并聚焦到新框 */ +function onSpecValueEnter(rowId: number, index: number) { + onSpecValueCommit(rowId, index); + const axis = specAxes.value.find((a) => a.rowId === rowId); + if (!axis) return; + const current = (axis.selectedValues[index] ?? '').trim(); + if (!current) return; + const next = axis.selectedValues[index + 1]; + if (next != null && !String(next).trim()) { + focusSpecValueInput(rowId, index + 1); + return; + } + axis.selectedValues.splice(index + 1, 0, ''); + focusSpecValueInput(rowId, index + 1); } -function setSpecValueDraft(rowId: number, value: string) { - specValueDraftByRow.value = { - ...specValueDraftByRow.value, - [rowId]: value, - }; +function focusSpecValueInput(rowId: number, index: number) { + void nextTick(() => { + const root = document.querySelector( + `[data-spec-value-input="${rowId}_${index}"]`, + ); + const input = root?.querySelector('input') as HTMLInputElement | null; + input?.focus(); + }); +} + +function addSpecValueSlot(rowId: number) { + const axis = specAxes.value.find((a) => a.rowId === rowId); + if (!axis) return; + axis.selectedValues.push(''); } function removeSpecValue(rowId: number, index: number) { @@ -944,6 +988,24 @@ function removeSpecValue(rowId: number, index: number) { if (!axis) return; const raw = axis.selectedValues[index]; if (raw == null) return; + + const doRemove = () => { + const current = specAxes.value.find((a) => a.rowId === rowId); + if (!current) return; + if (index < 0 || index >= current.selectedValues.length) return; + if (current.selectedValues.length <= 1) { + current.selectedValues[0] = ''; + } else { + current.selectedValues.splice(index, 1); + } + syncSkuRowsFromSpecs(); + }; + + if (!String(raw).trim()) { + doRemove(); + return; + } + const label = labelOfAxisValue(axis, raw); AntModal.confirm({ title: '确认删除规格值?', @@ -953,15 +1015,7 @@ function removeSpecValue(rowId: number, index: number) { cancelText: '取消', centered: true, zIndex: 2100, - onOk() { - const current = specAxes.value.find((a) => a.rowId === rowId); - if (!current) return; - // 确认期间取值可能已变,按当时值定位删除 - const idx = current.selectedValues.indexOf(raw); - if (idx < 0) return; - current.selectedValues.splice(idx, 1); - syncSkuRowsFromSpecs(); - }, + onOk: doRemove, }); } @@ -1002,12 +1056,13 @@ function syncSkuRowsFromSpecs() { clearSkuRows(); return; } - if (axes.some((a) => a.selectedValues.length === 0)) { + const valueLists = axes.map((a) => effectiveSpecValues(a)); + if (valueLists.some((list) => list.length === 0)) { clearSkuRows(); return; } - const combos = cartesianProduct(axes.map((a) => a.selectedValues)); + const combos = cartesianProduct(valueLists); const maxSku = Number(catTemplate.value?.maxSkuNum ?? 0); if (maxSku > 0 && combos.length > maxSku) { message.warning( @@ -1111,8 +1166,7 @@ async function onCatChange(path: number[]) { clearSkuRows(); specAxes.value = []; specCatalog.value = []; - specValueDraftByRow.value = {}; - return; + return; } const searching = localCatReady.value && !!catCascaderSearch.value.trim(); @@ -1132,8 +1186,7 @@ async function onCatChange(path: number[]) { clearSkuRows(); specAxes.value = []; specCatalog.value = []; - specValueDraftByRow.value = {}; - return; + return; } const catId = path[path.length - 1]!; const hit = @@ -1404,8 +1457,8 @@ function validateForm(): boolean { return false; } for (const axis of specAxes.value) { - if (axis.selectedValues.length === 0) { - message.warning(`请为规格「${axis.name}」选择或填写至少一个取值`); + if (effectiveSpecValues(axis).length === 0) { + message.warning(`请为规格「${axis.name}」填写至少一个取值`); return false; } } @@ -1625,7 +1678,6 @@ function resetAll() { clearSkuRows(); specAxes.value = []; specCatalog.value = []; - specValueDraftByRow.value = {}; catTemplate.value = null; parentSpecs.value = []; rootCatOptions.value = []; @@ -2004,7 +2056,7 @@ onBeforeUnmount(() => { type="info" show-icon class="mb-3" - message="默认一条「款式」;最多两种规格。规格值横向填写,组合自动生成下方 SKU。" + message="默认一条「款式」;最多两种规格。规格值始终可编辑,失焦后更新下方 SKU 组合。" /> { :image="Empty.PRESENTED_IMAGE_SIMPLE" />
- - +
+
+ + +
+ +
-
+