feat: 发布商品规格值改为 AutoComplete 内联编辑

This commit is contained in:
2026-09-12 09:47:19 +08:00
parent d2e887e61a
commit f61fa56b29
@@ -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<SkuRow[]>([]);
const specCatalog = ref<SpecCatalogItem[]>([]);
/** 用户已添加的规格轴(默认一条「款式」) */
const specAxes = ref<SpecAxis[]>([]);
/** 各规格行「输入规格值」草稿 */
const specValueDraftByRow = ref<Record<number, string>>({});
let specAxisRowSeq = 1;
/** 规格表容器:新增后滚到表体底部 */
const skuTableWrapRef = ref<HTMLElement | null>(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 组合。"
/>
<Empty
v-if="specCatalog.length === 0"
@@ -2012,55 +2064,75 @@ onBeforeUnmount(() => {
:image="Empty.PRESENTED_IMAGE_SIMPLE"
/>
<div v-else class="space-y-3">
<Space
<div
v-for="axis in specAxes"
:key="axis.rowId"
wrap
class="w-full"
:size="[8, 8]"
class="flex flex-wrap items-start gap-2 rounded-md border border-border/60 bg-muted/20 p-3"
>
<Select
:value="axis.catalogKey"
class="w-32"
class="w-28 shrink-0"
:options="catalogOptionsForAxis(axis.rowId)"
@change="
(v: any) => onAxisCatalogChange(axis.rowId, String(v))
"
/>
<Tag
v-for="(val, vi) in axis.selectedValues"
:key="`${axis.rowId}_${val}_${vi}`"
closable
class="!m-0 !inline-flex !h-8 !items-center !px-3 !text-sm"
@close.prevent="removeSpecValue(axis.rowId, vi)"
>
{{ labelOfAxisValue(axis, val) }}
</Tag>
<Select
v-if="unusedPresetOptions(axis).length > 0"
:key="`${axis.rowId}_preset_${axis.selectedValues.length}`"
class="w-32"
placeholder="选预设值"
:options="unusedPresetOptions(axis)"
@change="(v: any) => addSpecValue(axis.rowId, String(v))"
/>
<Input
:value="specValueDraftByRow[axis.rowId] || ''"
class="w-40"
placeholder="输入后回车"
allow-clear
@update:value="(v: string) => setSpecValueDraft(axis.rowId, v)"
@press-enter="commitSpecValueDraft(axis.rowId)"
/>
<div class="flex min-w-0 flex-1 flex-wrap items-center gap-2">
<div
v-for="(_val, vi) in axis.selectedValues"
:key="`${axis.rowId}_val_${vi}`"
class="flex items-center gap-1"
:data-spec-value-input="`${axis.rowId}_${vi}`"
>
<AutoComplete
:value="specValueDisplay(axis, vi)"
class="w-36"
:options="specValueAutoOptions(axis, vi)"
placeholder="输入或选预设,回车新增"
allow-clear
@update:value="
(v) =>
onSpecValueInput(
axis.rowId,
vi,
v == null ? '' : String(v),
)
"
@blur="onSpecValueCommit(axis.rowId, vi)"
@select="() => onSpecValueCommit(axis.rowId, vi)"
@keydown.enter.prevent="
onSpecValueEnter(axis.rowId, vi)
"
/>
<Button
type="text"
danger
size="small"
title="删除规格值"
@click="removeSpecValue(axis.rowId, vi)"
>
<template #icon>
<MinusOutlined />
</template>
</Button>
</div>
<Button type="dashed" @click="addSpecValueSlot(axis.rowId)">
<template #icon>
<PlusOutlined />
</template>
规格值
</Button>
</div>
<Button
v-if="specAxes.length > 1"
type="link"
danger
class="shrink-0"
@click="removeSpecAxis(axis.rowId)"
>
删除
删除规格
</Button>
</Space>
</div>
<Button
v-if="canAddSpecAxis"
type="dashed"