Skip to content

Commit 2dc738a

Browse files
committed
【功能优化】添加商品属性时允许选择已有的属性值,点击「选择」后,获取 value 列表
1 parent 521ae46 commit 2dc738a

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,25 @@ const showInput = async (index: number) => {
123123
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
124124
const handleInputConfirm = async (index: number, propertyId: number) => {
125125
if (inputValue.value) {
126-
// 重复添加校验
127-
// TODO @芋艿:需要测试下
126+
// 1. 重复添加校验
128127
if (attributeList.value[index].values.find((item) => item.name === inputValue.value)) {
129128
message.warning('已存在相同属性值,请重试')
130129
attributeIndex.value = null
131130
inputValue.value = ''
132131
return
133132
}
134-
// 保存属性值
133+
134+
// 2.1 情况一:属性值已存在,则直接使用并结束
135+
const existValue = attributeOptions.value.find((item) => item.name === inputValue.value)
136+
if (existValue) {
137+
attributeIndex.value = null
138+
inputValue.value = ''
139+
attributeList.value[index].values.push({ id: existValue.id, name: existValue.name })
140+
emit('success', attributeList.value)
141+
return
142+
}
143+
144+
// 2.2 情况二:新属性值,则进行保存
135145
try {
136146
const id = await PropertyApi.createPropertyValue({ propertyId, name: inputValue.value })
137147
attributeList.value[index].values.push({ id, name: inputValue.value })

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,32 @@ defineExpose({ open }) // 提供 open 方法,用于打开弹窗
8383
8484
/** 提交表单 */
8585
const submitForm = async () => {
86-
// 情况一:如果是已存在的属性,直接结束,不提交表单新增
86+
// 1.1 重复添加校验
8787
for (const attrItem of attributeList.value) {
8888
if (attrItem.name === formData.value.name) {
8989
return message.error('该属性已存在,请勿重复添加')
9090
}
9191
}
92-
93-
// 情况二:如果是不存在的属性,则需要执行新增
94-
// 校验表单
92+
// 1.2 校验表单
9593
if (!formRef) return
9694
const valid = await formRef.value.validate()
9795
if (!valid) return
96+
97+
// 2.1 情况一:属性名已存在,则直接使用并结束
98+
const existProperty = attributeOptions.value.find((item) => item.name === formData.value.name)
99+
if (existProperty) {
100+
// 添加到属性列表
101+
attributeList.value.push({
102+
id: existProperty.id,
103+
...formData.value,
104+
values: []
105+
})
106+
// 关闭弹窗
107+
dialogVisible.value = false
108+
return
109+
}
110+
111+
// 2.2 情况二:如果是不存在的属性,则需要执行新增
98112
// 提交请求
99113
formLoading.value = true
100114
try {
@@ -106,14 +120,6 @@ const submitForm = async () => {
106120
...formData.value,
107121
values: []
108122
})
109-
// 判断最终提交的属性名称是否是用户下拉选择的 自己手动输入的属性名称就不执行emit获取该属性名下属性值列表
110-
for (const element of attributeOptions.value) {
111-
if (element.name === formData.value.name) {
112-
message.success(t('common.createSuccess'))
113-
dialogVisible.value = false
114-
return
115-
}
116-
}
117123
// 关闭弹窗
118124
message.success(t('common.createSuccess'))
119125
dialogVisible.value = false

0 commit comments

Comments
 (0)