Skip to content

Commit e9bb940

Browse files
author
puhui999
committed
【优化】spu:新增商品属性属性值为空校验
1 parent 96a499a commit e9bb940

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

src/views/mall/product/spu/components/SkuList.vue

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ import { createImageViewer } from '@/components/ImageViewer'
292292
import { RuleConfig } from '@/views/mall/product/spu/components/index'
293293
import { PropertyAndValues } from './index'
294294
import { ElTable } from 'element-plus'
295+
import { isEmpty } from '@/utils/is'
295296
296297
defineOptions({ name: 'SkuList' })
297298
const message = useMessage() // 消息弹窗
@@ -340,11 +341,22 @@ const imagePreview = (imgUrl: string) => {
340341
341342
/** 批量添加 */
342343
const batchAdd = () => {
344+
validateProperty()
343345
formData.value!.skus!.forEach((item) => {
344346
copyValueToTarget(item, skuList.value[0])
345347
})
346348
}
347-
349+
/** 校验商品属性属性值 */
350+
const validateProperty = () => {
351+
// 校验商品属性属性值是否为空,有一个为空都不给过
352+
const warningInfo = '存在属性属性值为空,请先检查完善属性值后重试!!!'
353+
for (const item of props.propertyList) {
354+
if (!item.values || isEmpty(item.values)) {
355+
message.warning(warningInfo)
356+
throw new Error(warningInfo)
357+
}
358+
}
359+
}
348360
/** 删除 sku */
349361
const deleteSku = (row) => {
350362
const index = formData.value!.skus!.findIndex(
@@ -358,6 +370,7 @@ const tableHeaders = ref<{ prop: string; label: string }[]>([]) // 多属性表
358370
* 保存时,每个商品规格的表单要校验下。例如说,销售金额最低是 0.01 这种。
359371
*/
360372
const validateSku = () => {
373+
validateProperty()
361374
let warningInfo = '请检查商品各行相关属性配置,'
362375
let validate = true // 默认通过
363376
for (const sku of formData.value!.skus!) {
@@ -421,7 +434,7 @@ watch(
421434
const generateTableData = (propertyList: any[]) => {
422435
// 构建数据结构
423436
const propertyValues = propertyList.map((item) =>
424-
item.values.map((v) => ({
437+
item.values.map((v: any) => ({
425438
propertyId: item.id,
426439
propertyName: item.name,
427440
valueId: v.id,
@@ -464,15 +477,14 @@ const generateTableData = (propertyList: any[]) => {
464477
*/
465478
const validateData = (propertyList: any[]) => {
466479
const skuPropertyIds: number[] = []
467-
formData.value!.skus!.forEach(
468-
(sku) =>
469-
sku.properties
470-
?.map((property) => property.propertyId)
471-
?.forEach((propertyId) => {
472-
if (skuPropertyIds.indexOf(propertyId!) === -1) {
473-
skuPropertyIds.push(propertyId!)
474-
}
475-
})
480+
formData.value!.skus!.forEach((sku) =>
481+
sku.properties
482+
?.map((property) => property.propertyId)
483+
?.forEach((propertyId) => {
484+
if (skuPropertyIds.indexOf(propertyId!) === -1) {
485+
skuPropertyIds.push(propertyId!)
486+
}
487+
})
476488
)
477489
const propertyIds = propertyList.map((item) => item.id)
478490
return skuPropertyIds.length === propertyIds.length
@@ -543,7 +555,7 @@ watch(
543555
return
544556
}
545557
// 添加新属性没有属性值也不做处理
546-
if (propertyList.some((item) => item.values!.length === 0)) {
558+
if (propertyList.some((item) => !item.values || isEmpty(item.values))) {
547559
return
548560
}
549561
// 生成 table 数据,即 sku 列表

src/views/mall/product/spu/form/ProductAttributes.vue

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<el-col v-for="(item, index) in attributeList" :key="index">
44
<div>
55
<el-text class="mx-1">属性名:</el-text>
6-
<el-tag class="mx-1" :closable="!isDetail" type="success" @close="handleCloseProperty(index)">
6+
<el-tag :closable="!isDetail" class="mx-1" type="success" @close="handleCloseProperty(index)">
77
{{ item.name }}
88
</el-tag>
99
</div>
@@ -12,8 +12,8 @@
1212
<el-tag
1313
v-for="(value, valueIndex) in item.values"
1414
:key="value.id"
15-
class="mx-1"
1615
:closable="!isDetail"
16+
class="mx-1"
1717
@close="handleCloseValue(index, valueIndex)"
1818
>
1919
{{ value.name }}
@@ -44,7 +44,6 @@
4444
<script lang="ts" setup>
4545
import { ElInput } from 'element-plus'
4646
import * as PropertyApi from '@/api/mall/product/property'
47-
import { PropertyVO } from '@/api/mall/product/property'
4847
import { PropertyAndValues } from '@/views/mall/product/spu/components'
4948
import { propTypes } from '@/utils/propTypes'
5049
@@ -59,9 +58,9 @@ const inputVisible = computed(() => (index: number) => {
5958
if (attributeIndex.value === null) return false
6059
if (attributeIndex.value === index) return true
6160
})
62-
const inputRef = ref([]) //标签输入框Ref
61+
const inputRef = ref<any[]>([]) //标签输入框Ref
6362
/** 解决 ref 在 v-for 中的获取问题*/
64-
const setInputRef = (el) => {
63+
const setInputRef = (el: any) => {
6564
if (el === null || typeof el === 'undefined') return
6665
// 如果不存在 id 相同的元素才添加
6766
if (!inputRef.value.some((item) => item.input?.attributes.id === el.input?.attributes.id)) {
@@ -81,7 +80,7 @@ watch(
8180
() => props.propertyList,
8281
(data) => {
8382
if (!data) return
84-
attributeList.value = data
83+
attributeList.value = data as any
8584
},
8685
{
8786
deep: true,
@@ -97,6 +96,7 @@ const handleCloseValue = (index: number, valueIndex: number) => {
9796
/** 删除属性*/
9897
const handleCloseProperty = (index: number) => {
9998
attributeList.value?.splice(index, 1)
99+
emit('success', attributeList.value)
100100
}
101101
102102
/** 显示输入框并获取焦点 */

src/views/mall/product/spu/form/SkuForm.vue

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<!-- 商品发布 - 库存价格 -->
22
<template>
3-
<el-form ref="formRef" :model="formData" :rules="rules" label-width="120px" :disabled="isDetail">
3+
<el-form ref="formRef" :disabled="isDetail" :model="formData" :rules="rules" label-width="120px">
44
<el-form-item label="分销类型" props="subCommissionType">
55
<el-radio-group
66
v-model="formData.subCommissionType"
7-
@change="changeSubCommissionType"
87
class="w-80"
8+
@change="changeSubCommissionType"
99
>
1010
<el-radio :label="false">默认设置</el-radio>
1111
<el-radio :label="true" class="radio">单独设置</el-radio>
1212
</el-radio-group>
1313
</el-form-item>
1414
<el-form-item label="商品规格" props="specType">
15-
<el-radio-group v-model="formData.specType" @change="onChangeSpec" class="w-80">
15+
<el-radio-group v-model="formData.specType" class="w-80" @change="onChangeSpec">
1616
<el-radio :label="false" class="radio">单规格</el-radio>
1717
<el-radio :label="true">多规格</el-radio>
1818
</el-radio-group>
@@ -29,22 +29,22 @@
2929
<el-form-item v-if="formData.specType" label="商品属性">
3030
<el-button class="mb-10px mr-15px" @click="attributesAddFormRef.open">添加属性</el-button>
3131
<ProductAttributes
32+
:is-detail="isDetail"
3233
:property-list="propertyList"
3334
@success="generateSkus"
34-
:is-detail="isDetail"
3535
/>
3636
</el-form-item>
3737
<template v-if="formData.specType && propertyList.length > 0">
38-
<el-form-item label="批量设置" v-if="!isDetail">
38+
<el-form-item v-if="!isDetail" label="批量设置">
3939
<SkuList :is-batch="true" :prop-form-data="formData" :property-list="propertyList" />
4040
</el-form-item>
4141
<el-form-item label="规格列表">
4242
<SkuList
4343
ref="skuListRef"
44+
:is-detail="isDetail"
4445
:prop-form-data="formData"
4546
:property-list="propertyList"
4647
:rule-config="ruleConfig"
47-
:is-detail="isDetail"
4848
/>
4949
</el-form-item>
5050
</template>
@@ -181,7 +181,7 @@ const onChangeSpec = () => {
181181
}
182182
183183
/** 调用 SkuList generateTableData 方法*/
184-
const generateSkus = (propertyList) => {
184+
const generateSkus = (propertyList: any[]) => {
185185
skuListRef.value.generateTableData(propertyList)
186186
}
187187
</script>

0 commit comments

Comments
 (0)