Skip to content

Commit 38545e8

Browse files
author
puhui999
committed
fix: 优化相关逻辑,修复遗留bug
1 parent 0c42b76 commit 38545e8

File tree

5 files changed

+76
-51
lines changed

5 files changed

+76
-51
lines changed

src/views/mall/product/spu/addForm.vue

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import { useTagsViewStore } from '@/store/modules/tagsView'
3737
import { BasicInfoForm, DescriptionForm, OtherSettingsForm } from './components'
3838
// 业务api
3939
import * as ProductSpuApi from '@/api/mall/product/spu'
40-
import * as PropertyApi from '@/api/mall/product/property'
4140
import { convertToInteger, formatToFraction } from '@/utils'
4241
4342
const { t } = useI18n() // 国际化
@@ -105,25 +104,6 @@ const getDetail = async () => {
105104
item.subCommissionSecondPrice = formatToFraction(item.subCommissionSecondPrice)
106105
})
107106
formData.value = res
108-
// 只有是多规格才处理
109-
if (res.specType) {
110-
// TODO @puhui999:可以直接拿 propertyName 拼接处规格 id + 属性,可以看下商品 uniapp 详情的做法
111-
// fix: 考虑到 sku 数量和通过属性算出来的sku不一致的情况
112-
const propertyIds = []
113-
res.skus.forEach((sku) =>
114-
sku.properties
115-
?.map((property) => property.propertyId)
116-
.forEach((propertyId) => {
117-
if (propertyIds.indexOf(propertyId) === -1) {
118-
propertyIds.push(propertyId)
119-
}
120-
})
121-
)
122-
const properties = await PropertyApi.getPropertyListAndValue({ propertyIds })
123-
await nextTick()
124-
// 回显商品属性
125-
basicInfoRef.value.addAttribute(properties)
126-
}
127107
} finally {
128108
formLoading.value = false
129109
}
@@ -141,7 +121,7 @@ const submitForm = async () => {
141121
await unref(descriptionRef)?.validate()
142122
await unref(otherSettingsRef)?.validate()
143123
const deepCopyFormData = cloneDeep(unref(formData.value)) // 深拷贝一份 fix:这样最终 server 端不满足,不需要恢复,
144-
// TODO 兜底处理 sku 空数据详见 SkuList TODO
124+
// TODO 兜底处理 sku 空数据
145125
formData.value.skus.forEach((sku) => {
146126
// 因为是空数据这里判断一下商品条码是否为空就行
147127
if (sku.barCode === '') {

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@
9898
<el-form-item v-if="formData.specType" label="商品属性">
9999
<!-- TODO @puhui999:参考 https://admin.java.crmeb.net/store/list/creatProduct 添加规格好做么?添加的时候,不用输入备注哈 fix-->
100100
<el-button class="mr-15px mb-10px" @click="attributesAddFormRef.open">添加规格</el-button>
101-
<ProductAttributes :propertyList="propertyList" />
101+
<ProductAttributes :propertyList="propertyList" @success="generateSkus" />
102102
</el-form-item>
103103
<template v-if="formData.specType && propertyList.length > 0">
104104
<el-form-item label="批量设置">
105105
<SkuList :is-batch="true" :prop-form-data="formData" :propertyList="propertyList" />
106106
</el-form-item>
107107
<el-form-item label="属性列表">
108-
<SkuList :prop-form-data="formData" :propertyList="propertyList" />
108+
<SkuList ref="skuListRef" :prop-form-data="formData" :propertyList="propertyList" />
109109
</el-form-item>
110110
</template>
111111
<el-form-item v-if="!formData.specType">
@@ -114,7 +114,7 @@
114114
</el-col>
115115
</el-row>
116116
</el-form>
117-
<ProductAttributesAddForm ref="attributesAddFormRef" @success="addAttribute" />
117+
<ProductAttributesAddForm ref="attributesAddFormRef" :propertyList="propertyList" />
118118
</template>
119119
<script lang="ts" name="ProductSpuBasicInfoForm" setup>
120120
import { PropType } from 'vue'
@@ -141,15 +141,11 @@ const attributesAddFormRef = ref() // 添加商品属性表单 TODO @puhui999:
141141
const productSpuBasicInfoRef = ref() // 表单Ref TODO @puhui999:小写开头哈 fix
142142
// TODO @puhui999:attributeList 改成 propertyList,会更统一一点 fix
143143
const propertyList = ref([]) // 商品属性列表
144-
/** 添加商品属性 */
145-
// TODO @puhui999:propFormData 算出来 fix: 因为ProductAttributesAddForm添加属性成功回调得使用不能完全依赖于propFormData
146-
const addAttribute = (property: any) => {
147-
Array.isArray(property) ? (propertyList.value = property) : propertyList.value.push(property)
148-
}
144+
const skuListRef = ref() // 商品属性列表Ref
149145
/** 调用 SkuList generateTableData 方法*/
150-
// const generateSkus(propertyList){
151-
// skuList.value.generateTableData()
152-
// }
146+
const generateSkus = (propertyList) => {
147+
skuListRef.value.generateTableData(propertyList)
148+
}
153149
const formData = reactive<SpuType>({
154150
name: '', // 商品名称
155151
categoryId: null, // 商品分类
@@ -191,6 +187,26 @@ watch(
191187
formData.sliderPicUrls = data['sliderPicUrls'].map((item) => ({
192188
url: item
193189
}))
190+
// 只有是多规格才处理
191+
if (formData.specType) {
192+
// TODO @puhui999:可以直接拿 propertyName 拼接处规格 id + 属性,可以看下商品 uniapp 详情的做法
193+
// fix: 直接拿返回的 skus 属性逆向生成出 propertyList
194+
const properties = []
195+
formData.skus.forEach((sku) => {
196+
sku.properties.forEach(({ propertyId, propertyName, valueId, valueName }) => {
197+
// 添加属性
198+
if (!properties.some((item) => item.id === propertyId)) {
199+
properties.push({ id: propertyId, name: propertyName, values: [] })
200+
}
201+
// 添加属性值
202+
const index = properties.findIndex((item) => item.id === propertyId)
203+
if (!properties[index].values.some((value) => value.id === valueId)) {
204+
properties[index].values.push({ id: valueId, name: valueName })
205+
}
206+
})
207+
})
208+
propertyList.value = properties
209+
}
194210
},
195211
{
196212
// fix: 去掉深度监听只有对象引用发生改变的时候才执行,解决改一动多的问题
@@ -217,7 +233,7 @@ const validate = async () => {
217233
}
218234
})
219235
}
220-
defineExpose({ validate, addAttribute })
236+
defineExpose({ validate })
221237
222238
/** 分销类型 */
223239
const changeSubCommissionType = () => {

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22
<el-col v-for="(item, index) in attributeList" :key="index">
33
<div>
44
<el-text class="mx-1">属性名:</el-text>
5-
<el-text class="mx-1">{{ item.name }}</el-text>
5+
<el-tag class="mx-1" closable type="success" @close="handleCloseProperty(index)"
6+
>{{ item.name }}
7+
</el-tag>
68
</div>
79
<div>
810
<el-text class="mx-1">属性值:</el-text>
911
<el-tag
1012
v-for="(value, valueIndex) in item.values"
1113
:key="value.id"
12-
:disable-transitions="false"
1314
class="mx-1"
1415
closable
15-
@close="handleClose(index, valueIndex)"
16+
@close="handleCloseValue(index, valueIndex)"
1617
>
1718
{{ value.name }}
1819
</el-tag>
1920
<el-input
2021
v-show="inputVisible(index)"
21-
ref="InputRef"
22+
:id="`input${index}`"
23+
:ref="setInputRef"
2224
v-model="inputValue"
2325
class="!w-20"
2426
size="small"
@@ -51,7 +53,15 @@ const inputVisible = computed(() => (index) => {
5153
if (attributeIndex.value === null) return false
5254
if (attributeIndex.value === index) return true
5355
})
54-
const InputRef = ref() //标签输入框Ref
56+
const inputRef = ref([]) //标签输入框Ref
57+
/** 解决 ref 在 v-for 中的获取问题*/
58+
const setInputRef = (el) => {
59+
if (el === null || typeof el === 'undefined') return
60+
// 如果不存在id相同的元素才添加
61+
if (!inputRef.value.some((item) => item.input?.attributes.id === el.input?.attributes.id)) {
62+
inputRef.value.push(el)
63+
}
64+
}
5565
const attributeList = ref([]) // 商品属性列表
5666
const props = defineProps({
5767
propertyList: {
@@ -72,19 +82,22 @@ watch(
7282
}
7383
)
7484
75-
/** 删除标签 tagValue 标签值*/
76-
const handleClose = (index, valueIndex) => {
85+
/** 删除属性值*/
86+
const handleCloseValue = (index, valueIndex) => {
7787
attributeList.value[index].values?.splice(valueIndex, 1)
7888
}
79-
89+
/** 删除属性*/
90+
const handleCloseProperty = (index) => {
91+
attributeList.value?.splice(index, 1)
92+
}
8093
/** 显示输入框并获取焦点 */
8194
const showInput = async (index) => {
8295
attributeIndex.value = index
83-
// TODO 嗯!!!自动获取焦点还是有点问题,后续继续改进
84-
// 因为组件在ref中所以需要用索引获取对应的Ref
85-
InputRef.value[index]!.input!.focus()
96+
inputRef.value[index].focus()
8697
}
8798
99+
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
100+
88101
/** 输入框失去焦点或点击回车时触发 */
89102
const handleInputConfirm = async (index, propertyId) => {
90103
if (inputValue.value) {
@@ -93,6 +106,7 @@ const handleInputConfirm = async (index, propertyId) => {
93106
const id = await PropertyApi.createPropertyValue({ propertyId, name: inputValue.value })
94107
attributeList.value[index].values.push({ id, name: inputValue.value })
95108
message.success(t('common.createSuccess'))
109+
emit('success', attributeList.value)
96110
} catch {
97111
message.error('添加失败,请重试') // TODO 缺少国际化
98112
}

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,25 @@ const formRules = reactive({
3333
name: [{ required: true, message: '名称不能为空', trigger: 'blur' }]
3434
})
3535
const formRef = ref() // 表单 Ref
36+
const attributeList = ref([]) // 商品属性列表
37+
const props = defineProps({
38+
propertyList: {
39+
type: Array,
40+
default: () => {}
41+
}
42+
})
3643
44+
watch(
45+
() => props.propertyList,
46+
(data) => {
47+
if (!data) return
48+
attributeList.value = data
49+
},
50+
{
51+
deep: true,
52+
immediate: true
53+
}
54+
)
3755
/** 打开弹窗 */
3856
const open = async () => {
3957
dialogVisible.value = true
@@ -42,7 +60,6 @@ const open = async () => {
4260
defineExpose({ open }) // 提供 open 方法,用于打开弹窗
4361
4462
/** 提交表单 */
45-
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
4663
const submitForm = async () => {
4764
// 校验表单
4865
if (!formRef) return
@@ -56,12 +73,12 @@ const submitForm = async () => {
5673
const res = await PropertyApi.getPropertyListAndValue({ name: data.name })
5774
if (res.length === 0) {
5875
const propertyId = await PropertyApi.createProperty(data)
59-
emit('success', { id: propertyId, ...formData.value, values: [] })
76+
attributeList.value.push({ id: propertyId, ...formData.value, values: [] })
6077
} else {
6178
if (res[0].values === null) {
6279
res[0].values = []
6380
}
64-
emit('success', res[0]) // 因为只用一个
81+
attributeList.value.push(res[0]) // 因为只用一个
6582
}
6683
message.success(t('common.createSuccess'))
6784
dialogVisible.value = false

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,6 @@ const generateTableData = (propertyList: any[]) => {
212212
if (index !== -1) {
213213
continue
214214
}
215-
/**
216-
* TODO 嗯。。有一个问题回显数据时已删除的 sku 会被重新添加暂时没想到好办法,保存时先手动重新删除一下因为是一条空数据很好辨别 不手动删也没是提交表单时会检测删除空sku来兜底
217-
*
218-
*/
219215
formData.value.skus.push(row)
220216
}
221217
}
@@ -302,4 +298,6 @@ watch(
302298
immediate: true
303299
}
304300
)
301+
// 暴露出生成 sku 方法给添加属性成功时调用 fix: 为了在只有一个属性下 spu 回显 skus 属性和和商品属性个数一致的情况下 添加属性值时添加 sku
302+
defineExpose({ generateTableData })
305303
</script>

0 commit comments

Comments
 (0)