fix: 修复 OtherSettingsForm TODO 优化相关算法,解决上一版本遗留的bug

This commit is contained in:
puhui999 2023-05-17 01:41:30 +08:00
parent 9c06832d7e
commit 3c4a39df01

View File

@ -1,32 +1,34 @@
<template> <template>
<el-form ref="OtherSettingsFormRef" :model="formData" :rules="rules" label-width="120px"> <el-form ref="OtherSettingsFormRef" :model="formData" :rules="rules" label-width="120px">
<el-row> <el-row>
<!-- TODO @puhui999横着三个哈 --> <!-- TODO @puhui999横着三个哈 fix-->
<el-col :span="24"> <el-col :span="24">
<el-col :span="8"> <el-row :gutter="20">
<el-form-item label="商品排序" prop="sort"> <el-col :span="8">
<el-input-number v-model="formData.sort" :min="0" /> <el-form-item label="商品排序" prop="sort">
</el-form-item> <el-input-number v-model="formData.sort" :min="0" />
</el-col> </el-form-item>
<el-col :span="8"> </el-col>
<el-form-item label="赠送积分" prop="giveIntegral"> <el-col :span="8">
<el-input-number v-model="formData.giveIntegral" :min="0" /> <el-form-item label="赠送积分" prop="giveIntegral">
</el-form-item> <el-input-number v-model="formData.giveIntegral" :min="0" />
</el-col> </el-form-item>
<el-col :span="8"> </el-col>
<el-form-item label="虚拟销量" prop="virtualSalesCount"> <el-col :span="8">
<el-input-number <el-form-item label="虚拟销量" prop="virtualSalesCount">
v-model="formData.virtualSalesCount" <el-input-number
:min="0" v-model="formData.virtualSalesCount"
placeholder="请输入虚拟销量" :min="0"
/> placeholder="请输入虚拟销量"
</el-form-item> />
</el-col> </el-form-item>
</el-col>
</el-row>
</el-col> </el-col>
<el-col :span="24"> <el-col :span="24">
<el-form-item label="商品推荐"> <el-form-item label="商品推荐">
<el-checkbox-group v-model="checkboxGroup" @change="onChangeGroup"> <el-checkbox-group v-model="checkboxGroup" @change="onChangeGroup">
<el-checkbox v-for="(item, index) in recommend" :key="index" :label="item.value"> <el-checkbox v-for="(item, index) in recommendOptions" :key="index" :label="item.value">
{{ item.name }} {{ item.name }}
</el-checkbox> </el-checkbox>
</el-checkbox-group> </el-checkbox-group>
@ -51,7 +53,7 @@
</el-form> </el-form>
</template> </template>
<script lang="ts" name="OtherSettingsForm" setup> <script lang="ts" name="OtherSettingsForm" setup>
import type { SpuType } from '@/api/mall/product/management/type/spuType' import type { SpuType } from '@/api/mall/product/spu'
import { PropType } from 'vue' import { PropType } from 'vue'
import { copyValueToTarget } from '@/utils' import { copyValueToTarget } from '@/utils'
import { propTypes } from '@/utils/propTypes' import { propTypes } from '@/utils/propTypes'
@ -65,34 +67,7 @@ const props = defineProps({
}, },
activeName: propTypes.string.def('') activeName: propTypes.string.def('')
}) })
// TODO @puhui999 recommendOptions
const recommend = [
{ name: '是否热卖', value: 'recommendHot' },
{ name: '是否优惠', value: 'recommendBenefit' },
{ name: '是否精品', value: 'recommendBest' },
{ name: '是否新品', value: 'recommendNew' },
{ name: '是否优品', value: 'recommendGood' }
]
const checkboxGroup = ref<string[]>(['recommendHot']) //
/** 选择商品后赋值 */
const onChangeGroup = () => {
// TODO @puhui999 recommend
checkboxGroup.value.includes('recommendHot')
? (formData.value.recommendHot = true)
: (formData.value.recommendHot = false)
checkboxGroup.value.includes('recommendBenefit')
? (formData.value.recommendBenefit = true)
: (formData.value.recommendBenefit = false)
checkboxGroup.value.includes('recommendBest')
? (formData.value.recommendBest = true)
: (formData.value.recommendBest = false)
checkboxGroup.value.includes('recommendNew')
? (formData.value.recommendNew = true)
: (formData.value.recommendNew = false)
checkboxGroup.value.includes('recommendGood')
? (formData.value.recommendGood = true)
: (formData.value.recommendGood = false)
}
const OtherSettingsFormRef = ref() // Ref const OtherSettingsFormRef = ref() // Ref
// //
const formData = ref<SpuType>({ const formData = ref<SpuType>({
@ -111,6 +86,23 @@ const rules = reactive({
giveIntegral: [required], giveIntegral: [required],
virtualSalesCount: [required] virtualSalesCount: [required]
}) })
// TODO @puhui999 recommendOptions fix
const recommendOptions = [
{ name: '是否热卖', value: 'recommendHot' },
{ name: '是否优惠', value: 'recommendBenefit' },
{ name: '是否精品', value: 'recommendBest' },
{ name: '是否新品', value: 'recommendNew' },
{ name: '是否优品', value: 'recommendGood' }
] //
const checkboxGroup = ref<string[]>([]) //
/** 选择商品后赋值 */
const onChangeGroup = () => {
// TODO @puhui999 recommendfix
recommendOptions.forEach(({ value }) => {
formData.value[value] = checkboxGroup.value.includes(value) ? true : false
})
}
/** /**
* 将传进来的值赋值给formData * 将传进来的值赋值给formData
@ -120,13 +112,6 @@ watch(
(data) => { (data) => {
if (!data) return if (!data) return
copyValueToTarget(formData.value, data) copyValueToTarget(formData.value, data)
// TODO
checkboxGroup.value = []
formData.value.recommendHot ? checkboxGroup.value.push('recommendHot') : ''
formData.value.recommendBenefit ? checkboxGroup.value.push('recommendBenefit') : ''
formData.value.recommendBest ? checkboxGroup.value.push('recommendBest') : ''
formData.value.recommendNew ? checkboxGroup.value.push('recommendNew') : ''
formData.value.recommendGood ? checkboxGroup.value.push('recommendGood') : ''
}, },
{ {
deep: true, deep: true,
@ -154,4 +139,14 @@ const validate = async () => {
}) })
} }
defineExpose({ validate }) defineExpose({ validate })
onMounted(async () => {
await nextTick()
// TODO fix:
checkboxGroup.value = []
recommendOptions.forEach(({ value }) => {
if (formData.value[value]) {
checkboxGroup.value.push(value)
}
})
})
</script> </script>