File tree Expand file tree Collapse file tree 2 files changed +31
-15
lines changed
src/views/mall/product/spu/form Expand file tree Collapse file tree 2 files changed +31
-15
lines changed Original file line number Diff line number Diff line change @@ -123,15 +123,25 @@ const showInput = async (index: number) => {
123
123
const emit = defineEmits ([' success' ]) // 定义 success 事件,用于操作成功后的回调
124
124
const handleInputConfirm = async (index : number , propertyId : number ) => {
125
125
if (inputValue .value ) {
126
- // 重复添加校验
127
- // TODO @芋艿:需要测试下
126
+ // 1. 重复添加校验
128
127
if (attributeList .value [index ].values .find ((item ) => item .name === inputValue .value )) {
129
128
message .warning (' 已存在相同属性值,请重试' )
130
129
attributeIndex .value = null
131
130
inputValue .value = ' '
132
131
return
133
132
}
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 情况二:新属性值,则进行保存
135
145
try {
136
146
const id = await PropertyApi .createPropertyValue ({ propertyId , name: inputValue .value })
137
147
attributeList .value [index ].values .push ({ id , name: inputValue .value })
Original file line number Diff line number Diff line change @@ -83,18 +83,32 @@ defineExpose({ open }) // 提供 open 方法,用于打开弹窗
83
83
84
84
/** 提交表单 */
85
85
const submitForm = async () => {
86
- // 情况一:如果是已存在的属性,直接结束,不提交表单新增
86
+ // 1.1 重复添加校验
87
87
for (const attrItem of attributeList .value ) {
88
88
if (attrItem .name === formData .value .name ) {
89
89
return message .error (' 该属性已存在,请勿重复添加' )
90
90
}
91
91
}
92
-
93
- // 情况二:如果是不存在的属性,则需要执行新增
94
- // 校验表单
92
+ // 1.2 校验表单
95
93
if (! formRef ) return
96
94
const valid = await formRef .value .validate ()
97
95
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 情况二:如果是不存在的属性,则需要执行新增
98
112
// 提交请求
99
113
formLoading .value = true
100
114
try {
@@ -106,14 +120,6 @@ const submitForm = async () => {
106
120
... formData .value ,
107
121
values: []
108
122
})
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
- }
117
123
// 关闭弹窗
118
124
message .success (t (' common.createSuccess' ))
119
125
dialogVisible .value = false
You can’t perform that action at this time.
0 commit comments