Skip to content

Commit bcb4fc3

Browse files
committed
【功能优化】添加商品属性时允许选择已有的属性值
1 parent e7c9ca0 commit bcb4fc3

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

src/api/mall/product/property.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ export const getPropertyPage = (params: PageParam) => {
6565
return request.get({ url: '/product/property/page', params })
6666
}
6767

68+
// 获得属性项精简列表
69+
export const getPropertySimpleList = (): Promise<PropertyVO[]> => {
70+
return request.get({ url: '/product/property/simple-list' })
71+
}
72+
6873
// ------------------------ 属性值 -------------------
6974

7075
// 获得属性值分页

src/views/mall/product/spu/components/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ interface PropertyAndValues {
55
id: number
66
name: string
77
values?: PropertyAndValues[]
8-
propertyOpts?: PropertyAndValues[]
8+
propertyOpts?: PropertyAndValues[] // TODO @GoldenZqqq:建议直接复用 values;
99
}
1010

1111
interface RuleConfig {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ const emit = defineEmits(['success']) // 定义 success 事件,用于操作成
122122
const handleInputConfirm = async (index: number, propertyId: number) => {
123123
if (inputValue.value) {
124124
// 重复添加校验
125+
// TODO @芋艿:需要测试下
125126
if (isNumber(inputValue.value)) {
126127
if (attributeList.value[index].values?.some((item) => item.id === inputValue.value)) {
127128
message.warning('已存在相同属性值,请重试')

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

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
allow-create
1717
default-first-option
1818
:reserve-keyword="false"
19-
placeholder="请选择属性名称"
20-
style="width: 240px"
19+
placeholder="请选择属性名称。如果不存在,可手动输入选择"
20+
class="!w-360px"
2121
>
2222
<el-option
23-
v-for="item in attrOption"
23+
v-for="item in attributeOptions"
2424
:key="item.id"
2525
:label="item.name"
2626
:value="item.name"
@@ -53,7 +53,7 @@ const formRules = reactive({
5353
})
5454
const formRef = ref() // 表单 Ref
5555
const attributeList = ref([]) // 商品属性列表
56-
const attrOption = ref([]) // 属性名称下拉框
56+
const attributeOptions = ref([] as PropertyApi.PropertyVO[]) // 商品属性名称下拉框
5757
const props = defineProps({
5858
propertyList: {
5959
type: Array,
@@ -76,13 +76,32 @@ watch(
7676
/** 打开弹窗 */
7777
const open = async () => {
7878
dialogVisible.value = true
79-
getAttrOption()
8079
resetForm()
80+
// 加载列表
81+
await getAttributeOptions()
8182
}
8283
defineExpose({ open }) // 提供 open 方法,用于打开弹窗
8384
8485
/** 提交表单 */
8586
const submitForm = async () => {
87+
// 情况一:如果是已存在的属性,直接结束,不提交表单新增
88+
for (const option of attributeOptions.value) {
89+
if (option.name === formData.value.name) {
90+
// 添加到属性列表
91+
attributeList.value.push({
92+
id: option.id,
93+
...formData.value,
94+
values: []
95+
})
96+
// 触发属性列表的加载
97+
emit('success', option.id, option.id)
98+
// 关闭弹窗
99+
dialogVisible.value = false
100+
return
101+
}
102+
}
103+
104+
// 情况二:如果是不存在的属性,则需要执行新增
86105
// 校验表单
87106
if (!formRef) return
88107
const valid = await formRef.value.validate()
@@ -98,15 +117,7 @@ const submitForm = async () => {
98117
...formData.value,
99118
values: []
100119
})
101-
// 判断最终提交的属性名称是否是选择的 自己手动输入的属性名称不执行emit
102-
for (const element of attrOption.value) {
103-
if (element.name === formData.value.name) {
104-
emit('success', propertyId, element.id)
105-
message.success(t('common.createSuccess'))
106-
dialogVisible.value = false
107-
return
108-
}
109-
}
120+
// 关闭弹窗
110121
message.success(t('common.createSuccess'))
111122
dialogVisible.value = false
112123
} finally {
@@ -123,12 +134,10 @@ const resetForm = () => {
123134
}
124135
125136
/** 获取商品属性下拉选项 */
126-
const getAttrOption = async () => {
137+
const getAttributeOptions = async () => {
127138
formLoading.value = true
128139
try {
129-
// TODO @芋艿:需要增加一个全列表接口
130-
const data = await PropertyApi.getPropertyPage({ pageNo: 1, pageSize: 100 })
131-
attrOption.value = data.list
140+
attributeOptions.value = await PropertyApi.getPropertySimpleList()
132141
} finally {
133142
formLoading.value = false
134143
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ const generateSkus = (propertyList: any[]) => {
197197
skuListRef.value.generateTableData(propertyList)
198198
}
199199
200+
// TODO @GoldenZqqq:这里不建议使用 success 去刷新。而是改成点击【属性值】的【添加】后,进行加载列表。后端提供了 getPropertyValueSimpleList 接口哈。
200201
/* 获取属性值列表 */
201202
const getPropertyValueList = async (id, propertyId) => {
202203
formLoading.value = true

0 commit comments

Comments
 (0)