fix(mall): 【antd/ele】修复商品 SKU 名称校验失败的问题

前端创建/编辑商品时,SKU 对象缺少 name 字段初始化,导致提交时后端校验 "商品 SKU 名字不能为空" 失败。

修改内容:
- form/index.vue: 初始化 SKU 添加 name 字段,提交前校验商品名称并赋值给 SKU
- sku-list.vue: createEmptySku 函数添加 name 字段

影响范围:web-antd、web-ele 两个版本
This commit is contained in:
puhui999
2026-01-28 17:45:20 +08:00
parent 0441afc24f
commit 548da70f9f
4 changed files with 22 additions and 3 deletions

View File

@@ -56,6 +56,7 @@ const tableHeaders = ref<{ label: string; prop: string }[]>([]);
/** 创建空 SKU 数据 */ /** 创建空 SKU 数据 */
function createEmptySku(): MallSpuApi.Sku { function createEmptySku(): MallSpuApi.Sku {
return { return {
name: '', // SKU 名称,提交时会自动使用 SPU 名称
price: 0, price: 0,
marketPrice: 0, marketPrice: 0,
costPrice: 0, costPrice: 0,

View File

@@ -50,6 +50,7 @@ const formData = ref<MallSpuApi.Spu>({
subCommissionType: false, subCommissionType: false,
skus: [ skus: [
{ {
name: '', // SKU 名称,提交时会自动使用 SPU 名称
price: 0, price: 0,
marketPrice: 0, marketPrice: 0,
costPrice: 0, costPrice: 0,
@@ -181,6 +182,11 @@ async function handleSubmit() {
.merge(otherFormApi) .merge(otherFormApi)
.submitAllForm(true); .submitAllForm(true);
values.skus = formData.value.skus; values.skus = formData.value.skus;
// 校验商品名称不能为空(用于 SKU name
if (!values.name || values.name.trim() === '') {
message.error('商品名称不能为空');
return;
}
if (values.skus) { if (values.skus) {
try { try {
// 校验 sku // 校验 sku
@@ -190,6 +196,8 @@ async function handleSubmit() {
return; return;
} }
values.skus.forEach((item) => { values.skus.forEach((item) => {
// 给 sku name 赋值(使用商品名称作为 SKU 名称)
item.name = values.name;
// 金额转换:元转分 // 金额转换:元转分
item.price = convertToInteger(item.price); item.price = convertToInteger(item.price);
item.marketPrice = convertToInteger(item.marketPrice); item.marketPrice = convertToInteger(item.marketPrice);
@@ -277,6 +285,7 @@ function handleChangeSpec() {
// 重置 sku 列表 // 重置 sku 列表
formData.value.skus = [ formData.value.skus = [
{ {
name: '', // SKU 名称,提交时会自动使用 SPU 名称
price: 0, price: 0,
marketPrice: 0, marketPrice: 0,
costPrice: 0, costPrice: 0,

View File

@@ -62,6 +62,7 @@ const tableHeaders = ref<{ label: string; prop: string }[]>([]);
/** 创建空 SKU 数据 */ /** 创建空 SKU 数据 */
function createEmptySku(): MallSpuApi.Sku { function createEmptySku(): MallSpuApi.Sku {
return { return {
name: '', // SKU 名称,提交时会自动使用 SPU 名称
price: 0, price: 0,
marketPrice: 0, marketPrice: 0,
costPrice: 0, costPrice: 0,

View File

@@ -50,6 +50,7 @@ const formData = ref<MallSpuApi.Spu>({
subCommissionType: false, subCommissionType: false,
skus: [ skus: [
{ {
name: '', // SKU 名称,提交时会自动使用 SPU 名称
price: 0, price: 0,
marketPrice: 0, marketPrice: 0,
costPrice: 0, costPrice: 0,
@@ -168,8 +169,8 @@ const [OtherForm, otherFormApi] = useVbenForm({
}); });
/** tab 切换 */ /** tab 切换 */
function handleTabChange(key: string) { function handleTabChange(key: number | string) {
activeTabName.value = key; activeTabName.value = key as string;
} }
/** 提交表单 */ /** 提交表单 */
@@ -181,6 +182,11 @@ async function handleSubmit() {
.merge(otherFormApi) .merge(otherFormApi)
.submitAllForm(true); .submitAllForm(true);
values.skus = formData.value.skus; values.skus = formData.value.skus;
// 校验商品名称不能为空(用于 SKU name
if (!values.name || values.name.trim() === '') {
ElMessage.error('商品名称不能为空');
return;
}
if (values.skus) { if (values.skus) {
try { try {
// 校验 sku // 校验 sku
@@ -190,6 +196,8 @@ async function handleSubmit() {
return; return;
} }
values.skus.forEach((item) => { values.skus.forEach((item) => {
// 给 sku name 赋值(使用商品名称作为 SKU 名称)
item.name = values.name;
// 金额转换:元转分 // 金额转换:元转分
item.price = convertToInteger(item.price); item.price = convertToInteger(item.price);
item.marketPrice = convertToInteger(item.marketPrice); item.marketPrice = convertToInteger(item.marketPrice);
@@ -277,6 +285,7 @@ function handleChangeSpec() {
// 重置 sku 列表 // 重置 sku 列表
formData.value.skus = [ formData.value.skus = [
{ {
name: '', // SKU 名称,提交时会自动使用 SPU 名称
price: 0, price: 0,
marketPrice: 0, marketPrice: 0,
costPrice: 0, costPrice: 0,
@@ -320,7 +329,6 @@ onMounted(async () => {
<ElCard class="h-full w-full" v-loading="formLoading"> <ElCard class="h-full w-full" v-loading="formLoading">
<template #header> <template #header>
<div class="flex items-center justify-between"> <div class="flex items-center justify-between">
<!-- @puhui999idea 这边会有告警 -->
<ElTabs v-model="activeTabName" @tab-change="handleTabChange"> <ElTabs v-model="activeTabName" @tab-change="handleTabChange">
<ElTabPane label="基础设置" name="info" /> <ElTabPane label="基础设置" name="info" />
<ElTabPane label="价格库存" name="sku" /> <ElTabPane label="价格库存" name="sku" />