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 { import {
Alert, Alert,
AutoComplete,
Button, Button,
Card, Card,
Cascader, Cascader,
@@ -228,8 +229,6 @@ const skuRows = ref<SkuRow[]>([]);
const specCatalog = ref<SpecCatalogItem[]>([]); const specCatalog = ref<SpecCatalogItem[]>([]);
/** 用户已添加的规格轴(默认一条「款式」) */ /** 用户已添加的规格轴(默认一条「款式」) */
const specAxes = ref<SpecAxis[]>([]); const specAxes = ref<SpecAxis[]>([]);
/** 各规格行「输入规格值」草稿 */
const specValueDraftByRow = ref<Record<number, string>>({});
let specAxisRowSeq = 1; let specAxisRowSeq = 1;
/** 规格表容器:新增后滚到表体底部 */ /** 规格表容器:新增后滚到表体底部 */
const skuTableWrapRef = ref<HTMLElement | null>(null); const skuTableWrapRef = ref<HTMLElement | null>(null);
@@ -614,7 +613,6 @@ async function applyCatSelection(catId: number, pathIds?: string, pathLabel?: st
clearSkuRows(); clearSkuRows();
specAxes.value = []; specAxes.value = [];
specCatalog.value = []; specCatalog.value = [];
specValueDraftByRow.value = {};
await refreshCatAuth(path); await refreshCatAuth(path);
if (!form.pddShopId || !form.catId) return; if (!form.pddShopId || !form.catId) return;
if (catAuthAuthorized.value === false) { if (catAuthAuthorized.value === false) {
@@ -729,7 +727,6 @@ watch(
clearSkuRows(); clearSkuRows();
specAxes.value = []; specAxes.value = [];
specCatalog.value = []; specCatalog.value = [];
specValueDraftByRow.value = {};
logisticsOptions.value = []; logisticsOptions.value = [];
form.goodsPropValues = {}; form.goodsPropValues = {};
catSearchHitOptions.value = []; catSearchHitOptions.value = [];
@@ -806,7 +803,8 @@ function createAxisFromCatalog(item: SpecCatalogItem): SpecAxis {
templatePid: item.templatePid, templatePid: item.templatePid,
valueUnit: item.valueUnit, valueUnit: item.valueUnit,
presetOptions: item.presetOptions.map((o) => ({ ...o })), presetOptions: item.presetOptions.map((o) => ({ ...o })),
selectedValues: [], // 默认一条空输入,随时可编辑
selectedValues: [''],
}; };
} }
@@ -822,7 +820,6 @@ function findDefaultCatalogItem(catalog: SpecCatalogItem[]) {
function rebuildSpecCatalogAndSeed() { function rebuildSpecCatalogAndSeed() {
const catalog = buildSpecCatalog(); const catalog = buildSpecCatalog();
specCatalog.value = catalog; specCatalog.value = catalog;
specValueDraftByRow.value = {};
const def = findDefaultCatalogItem(catalog); const def = findDefaultCatalogItem(catalog);
specAxes.value = def ? [createAxisFromCatalog(def)] : []; specAxes.value = def ? [createAxisFromCatalog(def)] : [];
syncSkuRowsFromSpecs(); syncSkuRowsFromSpecs();
@@ -851,7 +848,7 @@ function applyCatalogToAxis(axis: SpecAxis, catalogKey: string) {
axis.templatePid = item.templatePid; axis.templatePid = item.templatePid;
axis.valueUnit = item.valueUnit; axis.valueUnit = item.valueUnit;
axis.presetOptions = item.presetOptions.map((o) => ({ ...o })); axis.presetOptions = item.presetOptions.map((o) => ({ ...o }));
axis.selectedValues = []; axis.selectedValues = [''];
} }
function onAxisCatalogChange(rowId: number, catalogKey: string) { function onAxisCatalogChange(rowId: number, catalogKey: string) {
@@ -892,51 +889,98 @@ function removeSpecAxis(rowId: number) {
zIndex: 2100, zIndex: 2100,
onOk() { onOk() {
specAxes.value = specAxes.value.filter((a) => a.rowId !== rowId); specAxes.value = specAxes.value.filter((a) => a.rowId !== rowId);
const drafts = { ...specValueDraftByRow.value };
delete drafts[rowId];
specValueDraftByRow.value = drafts;
syncSkuRowsFromSpecs(); syncSkuRowsFromSpecs();
}, },
}); });
} }
function unusedPresetOptions(axis: SpecAxis) { /** 参与笛卡尔积的有效规格值(去掉空白) */
const selected = new Set(axis.selectedValues); function effectiveSpecValues(axis: SpecAxis) {
return axis.presetOptions return axis.selectedValues.map((v) => v.trim()).filter(Boolean);
.filter((o) => !selected.has(o.value))
.map((o) => ({ label: o.label, value: o.value }));
} }
function addSpecValue(rowId: number, raw: string) { /** AutoComplete 展示文案:预设用 label,自定义原样 */
const axis = specAxes.value.find((a) => a.rowId === rowId); function specValueDisplay(axis: SpecAxis, index: number) {
if (!axis) return; 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(); const text = raw.trim();
if (!text) return; if (!text) return '';
const matched = axis.presetOptions.find( const matched = axis.presetOptions.find(
(o) => o.value === text || o.label === text, (o) => o.value === text || o.label === text,
); );
const value = matched?.value ?? text; return matched?.value ?? text;
if (axis.selectedValues.includes(value)) { }
message.warning('该规格值已添加');
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; return;
} }
axis.selectedValues.push(value); axis.selectedValues[index] = value;
specValueDraftByRow.value = {
...specValueDraftByRow.value,
[rowId]: '',
};
syncSkuRowsFromSpecs(); 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) { function focusSpecValueInput(rowId: number, index: number) {
specValueDraftByRow.value = { void nextTick(() => {
...specValueDraftByRow.value, const root = document.querySelector(
[rowId]: value, `[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) { function removeSpecValue(rowId: number, index: number) {
@@ -944,6 +988,24 @@ function removeSpecValue(rowId: number, index: number) {
if (!axis) return; if (!axis) return;
const raw = axis.selectedValues[index]; const raw = axis.selectedValues[index];
if (raw == null) return; 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); const label = labelOfAxisValue(axis, raw);
AntModal.confirm({ AntModal.confirm({
title: '确认删除规格值?', title: '确认删除规格值?',
@@ -953,15 +1015,7 @@ function removeSpecValue(rowId: number, index: number) {
cancelText: '取消', cancelText: '取消',
centered: true, centered: true,
zIndex: 2100, zIndex: 2100,
onOk() { onOk: doRemove,
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();
},
}); });
} }
@@ -1002,12 +1056,13 @@ function syncSkuRowsFromSpecs() {
clearSkuRows(); clearSkuRows();
return; return;
} }
if (axes.some((a) => a.selectedValues.length === 0)) { const valueLists = axes.map((a) => effectiveSpecValues(a));
if (valueLists.some((list) => list.length === 0)) {
clearSkuRows(); clearSkuRows();
return; return;
} }
const combos = cartesianProduct(axes.map((a) => a.selectedValues)); const combos = cartesianProduct(valueLists);
const maxSku = Number(catTemplate.value?.maxSkuNum ?? 0); const maxSku = Number(catTemplate.value?.maxSkuNum ?? 0);
if (maxSku > 0 && combos.length > maxSku) { if (maxSku > 0 && combos.length > maxSku) {
message.warning( message.warning(
@@ -1111,7 +1166,6 @@ async function onCatChange(path: number[]) {
clearSkuRows(); clearSkuRows();
specAxes.value = []; specAxes.value = [];
specCatalog.value = []; specCatalog.value = [];
specValueDraftByRow.value = {};
return; return;
} }
const searching = const searching =
@@ -1132,7 +1186,6 @@ async function onCatChange(path: number[]) {
clearSkuRows(); clearSkuRows();
specAxes.value = []; specAxes.value = [];
specCatalog.value = []; specCatalog.value = [];
specValueDraftByRow.value = {};
return; return;
} }
const catId = path[path.length - 1]!; const catId = path[path.length - 1]!;
@@ -1404,8 +1457,8 @@ function validateForm(): boolean {
return false; return false;
} }
for (const axis of specAxes.value) { for (const axis of specAxes.value) {
if (axis.selectedValues.length === 0) { if (effectiveSpecValues(axis).length === 0) {
message.warning(`请为规格「${axis.name}」选择或填写至少一个取值`); message.warning(`请为规格「${axis.name}」填写至少一个取值`);
return false; return false;
} }
} }
@@ -1625,7 +1678,6 @@ function resetAll() {
clearSkuRows(); clearSkuRows();
specAxes.value = []; specAxes.value = [];
specCatalog.value = []; specCatalog.value = [];
specValueDraftByRow.value = {};
catTemplate.value = null; catTemplate.value = null;
parentSpecs.value = []; parentSpecs.value = [];
rootCatOptions.value = []; rootCatOptions.value = [];
@@ -2004,7 +2056,7 @@ onBeforeUnmount(() => {
type="info" type="info"
show-icon show-icon
class="mb-3" class="mb-3"
message="默认一条「款式」;最多两种规格。规格值横向填写,组合自动生成下方 SKU。" message="默认一条「款式」;最多两种规格。规格值始终可编辑,失焦后更新下方 SKU 组合。"
/> />
<Empty <Empty
v-if="specCatalog.length === 0" v-if="specCatalog.length === 0"
@@ -2012,55 +2064,75 @@ onBeforeUnmount(() => {
:image="Empty.PRESENTED_IMAGE_SIMPLE" :image="Empty.PRESENTED_IMAGE_SIMPLE"
/> />
<div v-else class="space-y-3"> <div v-else class="space-y-3">
<Space <div
v-for="axis in specAxes" v-for="axis in specAxes"
:key="axis.rowId" :key="axis.rowId"
wrap class="flex flex-wrap items-start gap-2 rounded-md border border-border/60 bg-muted/20 p-3"
class="w-full"
:size="[8, 8]"
> >
<Select <Select
:value="axis.catalogKey" :value="axis.catalogKey"
class="w-32" class="w-28 shrink-0"
:options="catalogOptionsForAxis(axis.rowId)" :options="catalogOptionsForAxis(axis.rowId)"
@change=" @change="
(v: any) => onAxisCatalogChange(axis.rowId, String(v)) (v: any) => onAxisCatalogChange(axis.rowId, String(v))
" "
/> />
<Tag <div class="flex min-w-0 flex-1 flex-wrap items-center gap-2">
v-for="(val, vi) in axis.selectedValues" <div
:key="`${axis.rowId}_${val}_${vi}`" v-for="(_val, vi) in axis.selectedValues"
closable :key="`${axis.rowId}_val_${vi}`"
class="!m-0 !inline-flex !h-8 !items-center !px-3 !text-sm" class="flex items-center gap-1"
@close.prevent="removeSpecValue(axis.rowId, vi)" :data-spec-value-input="`${axis.rowId}_${vi}`"
> >
{{ labelOfAxisValue(axis, val) }} <AutoComplete
</Tag> :value="specValueDisplay(axis, vi)"
<Select class="w-36"
v-if="unusedPresetOptions(axis).length > 0" :options="specValueAutoOptions(axis, vi)"
:key="`${axis.rowId}_preset_${axis.selectedValues.length}`" placeholder="输入或选预设,回车新增"
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 allow-clear
@update:value="(v: string) => setSpecValueDraft(axis.rowId, v)" @update:value="
@press-enter="commitSpecValueDraft(axis.rowId)" (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 <Button
v-if="specAxes.length > 1" v-if="specAxes.length > 1"
type="link" type="link"
danger danger
class="shrink-0"
@click="removeSpecAxis(axis.rowId)" @click="removeSpecAxis(axis.rowId)"
> >
删除 删除规格
</Button> </Button>
</Space> </div>
<Button <Button
v-if="canAddSpecAxis" v-if="canAddSpecAxis"
type="dashed" type="dashed"