fix: 优化相关逻辑,修复遗留bug

This commit is contained in:
puhui999 2023-05-20 12:05:18 +08:00
parent 0c42b76a52
commit 38545e89c6
5 changed files with 76 additions and 51 deletions

View File

@ -37,7 +37,6 @@ import { useTagsViewStore } from '@/store/modules/tagsView'
import { BasicInfoForm, DescriptionForm, OtherSettingsForm } from './components'
// api
import * as ProductSpuApi from '@/api/mall/product/spu'
import * as PropertyApi from '@/api/mall/product/property'
import { convertToInteger, formatToFraction } from '@/utils'
const { t } = useI18n() //
@ -105,25 +104,6 @@ const getDetail = async () => {
item.subCommissionSecondPrice = formatToFraction(item.subCommissionSecondPrice)
})
formData.value = res
//
if (res.specType) {
// TODO @puhui999 propertyName id + uniapp
// fix: sku sku
const propertyIds = []
res.skus.forEach((sku) =>
sku.properties
?.map((property) => property.propertyId)
.forEach((propertyId) => {
if (propertyIds.indexOf(propertyId) === -1) {
propertyIds.push(propertyId)
}
})
)
const properties = await PropertyApi.getPropertyListAndValue({ propertyIds })
await nextTick()
//
basicInfoRef.value.addAttribute(properties)
}
} finally {
formLoading.value = false
}
@ -141,7 +121,7 @@ const submitForm = async () => {
await unref(descriptionRef)?.validate()
await unref(otherSettingsRef)?.validate()
const deepCopyFormData = cloneDeep(unref(formData.value)) // fix: server
// TODO sku SkuList TODO
// TODO sku
formData.value.skus.forEach((sku) => {
//
if (sku.barCode === '') {

View File

@ -98,14 +98,14 @@
<el-form-item v-if="formData.specType" label="商品属性">
<!-- TODO @puhui999参考 https://admin.java.crmeb.net/store/list/creatProduct fix-->
<el-button class="mr-15px mb-10px" @click="attributesAddFormRef.open">添加规格</el-button>
<ProductAttributes :propertyList="propertyList" />
<ProductAttributes :propertyList="propertyList" @success="generateSkus" />
</el-form-item>
<template v-if="formData.specType && propertyList.length > 0">
<el-form-item label="批量设置">
<SkuList :is-batch="true" :prop-form-data="formData" :propertyList="propertyList" />
</el-form-item>
<el-form-item label="属性列表">
<SkuList :prop-form-data="formData" :propertyList="propertyList" />
<SkuList ref="skuListRef" :prop-form-data="formData" :propertyList="propertyList" />
</el-form-item>
</template>
<el-form-item v-if="!formData.specType">
@ -114,7 +114,7 @@
</el-col>
</el-row>
</el-form>
<ProductAttributesAddForm ref="attributesAddFormRef" @success="addAttribute" />
<ProductAttributesAddForm ref="attributesAddFormRef" :propertyList="propertyList" />
</template>
<script lang="ts" name="ProductSpuBasicInfoForm" setup>
import { PropType } from 'vue'
@ -141,15 +141,11 @@ const attributesAddFormRef = ref() // 添加商品属性表单 TODO @puhui999
const productSpuBasicInfoRef = ref() // Ref TODO @puhui999 fix
// TODO @puhui999attributeList propertyList fix
const propertyList = ref([]) //
/** 添加商品属性 */
// TODO @puhui999propFormData fix: ProductAttributesAddForm使propFormData
const addAttribute = (property: any) => {
Array.isArray(property) ? (propertyList.value = property) : propertyList.value.push(property)
}
const skuListRef = ref() // Ref
/** 调用 SkuList generateTableData 方法*/
// const generateSkus(propertyList){
// skuList.value.generateTableData()
// }
const generateSkus = (propertyList) => {
skuListRef.value.generateTableData(propertyList)
}
const formData = reactive<SpuType>({
name: '', //
categoryId: null, //
@ -191,6 +187,26 @@ watch(
formData.sliderPicUrls = data['sliderPicUrls'].map((item) => ({
url: item
}))
//
if (formData.specType) {
// TODO @puhui999 propertyName id + uniapp
// fix: skus propertyList
const properties = []
formData.skus.forEach((sku) => {
sku.properties.forEach(({ propertyId, propertyName, valueId, valueName }) => {
//
if (!properties.some((item) => item.id === propertyId)) {
properties.push({ id: propertyId, name: propertyName, values: [] })
}
//
const index = properties.findIndex((item) => item.id === propertyId)
if (!properties[index].values.some((value) => value.id === valueId)) {
properties[index].values.push({ id: valueId, name: valueName })
}
})
})
propertyList.value = properties
}
},
{
// fix: ,
@ -217,7 +233,7 @@ const validate = async () => {
}
})
}
defineExpose({ validate, addAttribute })
defineExpose({ validate })
/** 分销类型 */
const changeSubCommissionType = () => {

View File

@ -2,23 +2,25 @@
<el-col v-for="(item, index) in attributeList" :key="index">
<div>
<el-text class="mx-1">属性名</el-text>
<el-text class="mx-1">{{ item.name }}</el-text>
<el-tag class="mx-1" closable type="success" @close="handleCloseProperty(index)"
>{{ item.name }}
</el-tag>
</div>
<div>
<el-text class="mx-1">属性值</el-text>
<el-tag
v-for="(value, valueIndex) in item.values"
:key="value.id"
:disable-transitions="false"
class="mx-1"
closable
@close="handleClose(index, valueIndex)"
@close="handleCloseValue(index, valueIndex)"
>
{{ value.name }}
</el-tag>
<el-input
v-show="inputVisible(index)"
ref="InputRef"
:id="`input${index}`"
:ref="setInputRef"
v-model="inputValue"
class="!w-20"
size="small"
@ -51,7 +53,15 @@ const inputVisible = computed(() => (index) => {
if (attributeIndex.value === null) return false
if (attributeIndex.value === index) return true
})
const InputRef = ref() //Ref
const inputRef = ref([]) //Ref
/** 解决 ref 在 v-for 中的获取问题*/
const setInputRef = (el) => {
if (el === null || typeof el === 'undefined') return
// id
if (!inputRef.value.some((item) => item.input?.attributes.id === el.input?.attributes.id)) {
inputRef.value.push(el)
}
}
const attributeList = ref([]) //
const props = defineProps({
propertyList: {
@ -72,19 +82,22 @@ watch(
}
)
/** 删除标签 tagValue 标签值*/
const handleClose = (index, valueIndex) => {
/** 删除属性值*/
const handleCloseValue = (index, valueIndex) => {
attributeList.value[index].values?.splice(valueIndex, 1)
}
/** 删除属性*/
const handleCloseProperty = (index) => {
attributeList.value?.splice(index, 1)
}
/** 显示输入框并获取焦点 */
const showInput = async (index) => {
attributeIndex.value = index
// TODO
// refRef
InputRef.value[index]!.input!.focus()
inputRef.value[index].focus()
}
const emit = defineEmits(['success']) // success
/** 输入框失去焦点或点击回车时触发 */
const handleInputConfirm = async (index, propertyId) => {
if (inputValue.value) {
@ -93,6 +106,7 @@ const handleInputConfirm = async (index, propertyId) => {
const id = await PropertyApi.createPropertyValue({ propertyId, name: inputValue.value })
attributeList.value[index].values.push({ id, name: inputValue.value })
message.success(t('common.createSuccess'))
emit('success', attributeList.value)
} catch {
message.error('添加失败,请重试') // TODO
}

View File

@ -33,7 +33,25 @@ const formRules = reactive({
name: [{ required: true, message: '名称不能为空', trigger: 'blur' }]
})
const formRef = ref() // Ref
const attributeList = ref([]) //
const props = defineProps({
propertyList: {
type: Array,
default: () => {}
}
})
watch(
() => props.propertyList,
(data) => {
if (!data) return
attributeList.value = data
},
{
deep: true,
immediate: true
}
)
/** 打开弹窗 */
const open = async () => {
dialogVisible.value = true
@ -42,7 +60,6 @@ const open = async () => {
defineExpose({ open }) // open
/** 提交表单 */
const emit = defineEmits(['success']) // success
const submitForm = async () => {
//
if (!formRef) return
@ -56,12 +73,12 @@ const submitForm = async () => {
const res = await PropertyApi.getPropertyListAndValue({ name: data.name })
if (res.length === 0) {
const propertyId = await PropertyApi.createProperty(data)
emit('success', { id: propertyId, ...formData.value, values: [] })
attributeList.value.push({ id: propertyId, ...formData.value, values: [] })
} else {
if (res[0].values === null) {
res[0].values = []
}
emit('success', res[0]) //
attributeList.value.push(res[0]) //
}
message.success(t('common.createSuccess'))
dialogVisible.value = false

View File

@ -212,10 +212,6 @@ const generateTableData = (propertyList: any[]) => {
if (index !== -1) {
continue
}
/**
* TODO 有一个问题回显数据时已删除的 sku 会被重新添加暂时没想到好办法保存时先手动重新删除一下因为是一条空数据很好辨别 不手动删也没是提交表单时会检测删除空sku来兜底
*
*/
formData.value.skus.push(row)
}
}
@ -302,4 +298,6 @@ watch(
immediate: true
}
)
// sku fix: spu skus sku
defineExpose({ generateTableData })
</script>