Skip to content

Commit e7c9ca0

Browse files
authored
Merge pull request #71 from GoldenZqqq/GoldenZqqq/issue47
【功能优化】添加商品属性时允许选择已有的属性值
2 parents aaa5e14 + 2937f3c commit e7c9ca0

File tree

5 files changed

+97
-12
lines changed

5 files changed

+97
-12
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
>
2525
<template #default="{ row }">
2626
<span style="font-weight: bold; color: #40aaff">
27-
{{ row.properties[index]?.valueName }}
27+
{{ row.properties?.[index]?.valueName }}
2828
</span>
2929
</template>
3030
</el-table-column>
@@ -168,7 +168,7 @@
168168
>
169169
<template #default="{ row }">
170170
<span style="font-weight: bold; color: #40aaff">
171-
{{ row.properties[index]?.valueName }}
171+
{{ row.properties?.[index]?.valueName }}
172172
</span>
173173
</template>
174174
</el-table-column>
@@ -248,7 +248,7 @@
248248
>
249249
<template #default="{ row }">
250250
<span style="font-weight: bold; color: #40aaff">
251-
{{ row.properties[index]?.valueName }}
251+
{{ row.properties?.[index]?.valueName }}
252252
</span>
253253
</template>
254254
</el-table-column>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ interface PropertyAndValues {
55
id: number
66
name: string
77
values?: PropertyAndValues[]
8+
propertyOpts?: PropertyAndValues[]
89
}
910

1011
interface RuleConfig {

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,28 @@
1818
>
1919
{{ value.name }}
2020
</el-tag>
21-
<el-input
22-
v-show="inputVisible(index)"
21+
<el-select
2322
:id="`input${index}`"
2423
:ref="setInputRef"
24+
v-show="inputVisible(index)"
2525
v-model="inputValue"
26-
class="!w-20"
26+
filterable
27+
allow-create
28+
default-first-option
29+
:reserve-keyword="false"
2730
size="small"
31+
class="!w-30"
2832
@blur="handleInputConfirm(index, item.id)"
2933
@keyup.enter="handleInputConfirm(index, item.id)"
30-
/>
34+
@change="handleInputConfirm(index, item.id)"
35+
>
36+
<el-option
37+
v-for="item2 in item.propertyOpts"
38+
:key="item2.id"
39+
:label="item2.name"
40+
:value="item2.id"
41+
/>
42+
</el-select>
3143
<el-button
3244
v-show="!inputVisible(index)"
3345
class="button-new-tag ml-1"
@@ -42,10 +54,10 @@
4254
</template>
4355

4456
<script lang="ts" setup>
45-
import { ElInput } from 'element-plus'
4657
import * as PropertyApi from '@/api/mall/product/property'
4758
import { PropertyAndValues } from '@/views/mall/product/spu/components'
4859
import { propTypes } from '@/utils/propTypes'
60+
import { isNumber } from '@/utils/is'
4961
5062
defineOptions({ name: 'ProductAttributes' })
5163
@@ -109,6 +121,15 @@ const showInput = async (index) => {
109121
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
110122
const handleInputConfirm = async (index: number, propertyId: number) => {
111123
if (inputValue.value) {
124+
// 重复添加校验
125+
if (isNumber(inputValue.value)) {
126+
if (attributeList.value[index].values?.some((item) => item.id === inputValue.value)) {
127+
message.warning('已存在相同属性值,请重试')
128+
attributeIndex.value = null
129+
inputValue.value = ''
130+
return
131+
}
132+
}
112133
// 保存属性值
113134
try {
114135
const id = await PropertyApi.createPropertyValue({ propertyId, name: inputValue.value })

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,22 @@
1010
@keydown.enter.prevent="submitForm"
1111
>
1212
<el-form-item label="属性名称" prop="name">
13-
<el-input v-model="formData.name" placeholder="请输入名称" />
13+
<el-select
14+
v-model="formData.name"
15+
filterable
16+
allow-create
17+
default-first-option
18+
:reserve-keyword="false"
19+
placeholder="请选择属性名称"
20+
style="width: 240px"
21+
>
22+
<el-option
23+
v-for="item in attrOption"
24+
:key="item.id"
25+
:label="item.name"
26+
:value="item.name"
27+
/>
28+
</el-select>
1429
</el-form-item>
1530
</el-form>
1631
<template #footer>
@@ -24,6 +39,7 @@ import * as PropertyApi from '@/api/mall/product/property'
2439
2540
defineOptions({ name: 'ProductPropertyForm' })
2641
42+
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
2743
const { t } = useI18n() // 国际化
2844
const message = useMessage() // 消息弹窗
2945
@@ -37,6 +53,7 @@ const formRules = reactive({
3753
})
3854
const formRef = ref() // 表单 Ref
3955
const attributeList = ref([]) // 商品属性列表
56+
const attrOption = ref([]) // 属性名称下拉框
4057
const props = defineProps({
4158
propertyList: {
4259
type: Array,
@@ -59,6 +76,7 @@ watch(
5976
/** 打开弹窗 */
6077
const open = async () => {
6178
dialogVisible.value = true
79+
getAttrOption()
6280
resetForm()
6381
}
6482
defineExpose({ open }) // 提供 open 方法,用于打开弹窗
@@ -80,6 +98,15 @@ const submitForm = async () => {
8098
...formData.value,
8199
values: []
82100
})
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+
}
83110
message.success(t('common.createSuccess'))
84111
dialogVisible.value = false
85112
} finally {
@@ -94,4 +121,16 @@ const resetForm = () => {
94121
}
95122
formRef.value?.resetFields()
96123
}
124+
125+
/** 获取商品属性下拉选项 */
126+
const getAttrOption = async () => {
127+
formLoading.value = true
128+
try {
129+
// TODO @芋艿:需要增加一个全列表接口
130+
const data = await PropertyApi.getPropertyPage({ pageNo: 1, pageSize: 100 })
131+
attrOption.value = data.list
132+
} finally {
133+
formLoading.value = false
134+
}
135+
}
97136
</script>

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
<!-- 商品发布 - 库存价格 -->
22
<template>
3-
<el-form ref="formRef" :disabled="isDetail" :model="formData" :rules="rules" label-width="120px">
3+
<el-form
4+
ref="formRef"
5+
:disabled="isDetail"
6+
:model="formData"
7+
:rules="rules"
8+
label-width="120px"
9+
v-loading="formLoading"
10+
>
411
<el-form-item label="分销类型" props="subCommissionType">
512
<el-radio-group
613
v-model="formData.subCommissionType"
@@ -51,9 +58,14 @@
5158
</el-form>
5259

5360
<!-- 商品属性添加 Form 表单 -->
54-
<ProductPropertyAddForm ref="attributesAddFormRef" :propertyList="propertyList" />
61+
<ProductPropertyAddForm
62+
ref="attributesAddFormRef"
63+
:propertyList="propertyList"
64+
@success="getPropertyValueList"
65+
/>
5566
</template>
5667
<script lang="ts" setup>
68+
import * as PropertyApi from '@/api/mall/product/property'
5769
import { PropType } from 'vue'
5870
import { copyValueToTarget } from '@/utils'
5971
import { propTypes } from '@/utils/propTypes'
@@ -94,7 +106,7 @@ const ruleConfig: RuleConfig[] = [
94106
]
95107
96108
const message = useMessage() // 消息弹窗
97-
109+
const formLoading = ref(false)
98110
const props = defineProps({
99111
propFormData: {
100112
type: Object as PropType<Spu>,
@@ -184,4 +196,16 @@ const onChangeSpec = () => {
184196
const generateSkus = (propertyList: any[]) => {
185197
skuListRef.value.generateTableData(propertyList)
186198
}
199+
200+
/* 获取属性值列表 */
201+
const getPropertyValueList = async (id, propertyId) => {
202+
formLoading.value = true
203+
try {
204+
// TODO @芋艿:需要增加一个全列表接口
205+
const data = await PropertyApi.getPropertyValuePage({ pageNo: 1, pageSize: 100, propertyId })
206+
propertyList.value.find((item) => item.id === id).propertyOpts = data.list
207+
} finally {
208+
formLoading.value = false
209+
}
210+
}
187211
</script>

0 commit comments

Comments
 (0)