📖 MALL:商品编辑 => 优化 DescriptionForm 表单

(cherry picked from commit 10afbe8e2f)
This commit is contained in:
YunaiV 2024-01-12 21:01:10 +08:00 committed by shizhong
parent 890d31ff5f
commit 92d3564cae
6 changed files with 116 additions and 128 deletions

View File

@ -75,7 +75,6 @@ watch(
/** 表单校验 */
const emit = defineEmits(['update:activeName'])
const validate = async () => {
//
if (!formRef) return
try {
await unref(formRef)?.validate()

View File

@ -1,11 +1,6 @@
<!-- 商品发布 - 商品详情 -->
<template>
<el-form
ref="descriptionFormRef"
:model="formData"
:rules="rules"
label-width="120px"
:disabled="isDetail"
>
<el-form ref="formRef" :model="formData" :rules="rules" label-width="120px" :disabled="isDetail">
<!--富文本编辑器组件-->
<el-form-item label="商品详情" prop="description">
<Editor v-model:modelValue="formData.description" />
@ -19,7 +14,7 @@ import { PropType } from 'vue'
import { propTypes } from '@/utils/propTypes'
import { copyValueToTarget } from '@/utils'
defineOptions({ name: 'DescriptionForm' })
defineOptions({ name: 'ProductDescriptionForm' })
const message = useMessage() //
@ -31,7 +26,7 @@ const props = defineProps({
activeName: propTypes.string.def(''),
isDetail: propTypes.bool.def(false) //
})
const descriptionFormRef = ref() // Ref
const formRef = ref() // Ref
const formData = ref<Spu>({
description: '' //
})
@ -39,9 +34,8 @@ const formData = ref<Spu>({
const rules = reactive({
description: [required]
})
/**
* 富文本编辑器如果输入过再清空会有残留需再重置一次
*/
/** 富文本编辑器如果输入过再清空会有残留,需再重置一次 */
watch(
() => formData.value.description,
(newValue) => {
@ -54,9 +48,8 @@ watch(
immediate: true
}
)
/**
* 将传进来的值赋值给formData
*/
/** 将传进来的值赋值给 formData */
watch(
() => props.propFormData,
(data) => {
@ -70,24 +63,19 @@ watch(
}
)
/**
* 表单校验
*/
/** 表单校验 */
const emit = defineEmits(['update:activeName'])
const validate = async () => {
//
if (!descriptionFormRef) return
return await unref(descriptionFormRef).validate((valid) => {
if (!valid) {
message.warning('商品详情为完善!!')
emit('update:activeName', 'description')
//
throw new Error('商品详情为完善!!')
} else {
//
Object.assign(props.propFormData, formData.value)
}
})
if (!formRef) return
try {
await unref(formRef)?.validate()
//
Object.assign(props.propFormData, formData)
} catch (e) {
message.error('【商品详情】不完善,请填写相关信息')
emit('update:activeName', 'description')
throw e //
}
}
defineExpose({ validate })
</script>

View File

@ -120,7 +120,6 @@ watch(
/** 表单校验 */
const emit = defineEmits(['update:activeName'])
const validate = async () => {
//
if (!formRef) return
try {
await unref(formRef)?.validate()

View File

@ -0,0 +1,91 @@
<!-- 商品发布 - 其它设置 -->
<template>
<el-form ref="formRef" :model="formData" :rules="rules" label-width="120px" :disabled="isDetail">
<el-form-item label="商品排序" prop="sort">
<el-input-number
v-model="formData.sort"
:min="0"
placeholder="请输入商品排序"
class="w-80!"
/>
</el-form-item>
<el-form-item label="赠送积分" prop="giveIntegral">
<el-input-number
v-model="formData.giveIntegral"
:min="0"
placeholder="请输入赠送积分"
class="w-80!"
/>
</el-form-item>
<el-form-item label="虚拟销量" prop="virtualSalesCount">
<el-input-number
v-model="formData.virtualSalesCount"
:min="0"
placeholder="请输入虚拟销量"
class="w-80!"
/>
</el-form-item>
</el-form>
</template>
<script lang="ts" setup>
import type { Spu } from '@/api/mall/product/spu'
import { PropType } from 'vue'
import { propTypes } from '@/utils/propTypes'
import { copyValueToTarget } from '@/utils'
defineOptions({ name: 'ProductOtherForm' })
const message = useMessage() //
const props = defineProps({
propFormData: {
type: Object as PropType<Spu>,
default: () => {}
},
isDetail: propTypes.bool.def(false) //
})
const formRef = ref() // Ref
//
const formData = ref<Spu>({
sort: 0, //
giveIntegral: 0, //
virtualSalesCount: 0 //
})
//
const rules = reactive({
sort: [required],
giveIntegral: [required],
virtualSalesCount: [required]
})
/** 将传进来的值赋值给 formData */
watch(
() => props.propFormData,
(data) => {
if (!data) {
return
}
copyValueToTarget(formData.value, data)
},
{
immediate: true
}
)
/** 表单校验 */
const emit = defineEmits(['update:activeName'])
const validate = async () => {
if (!formRef) return
try {
await unref(formRef)?.validate()
//
Object.assign(props.propFormData, formData)
} catch (e) {
message.error('【其它设置】不完善,请填写相关信息')
emit('update:activeName', 'other')
throw e //
}
}
defineExpose({ validate })
</script>

View File

@ -1,89 +0,0 @@
<template>
<el-form
ref="otherSettingsFormRef"
:model="formData"
:rules="rules"
label-width="120px"
:disabled="isDetail"
>
<el-form-item label="商品排序" prop="sort">
<el-input-number v-model="formData.sort" :min="0" />
</el-form-item>
<el-form-item label="赠送积分" prop="giveIntegral">
<el-input-number v-model="formData.giveIntegral" :min="0" />
</el-form-item>
<el-form-item label="虚拟销量" prop="virtualSalesCount">
<el-input-number v-model="formData.virtualSalesCount" :min="0" placeholder="请输入虚拟销量" />
</el-form-item>
</el-form>
</template>
<script lang="ts" setup>
import type { Spu } from '@/api/mall/product/spu'
import { PropType } from 'vue'
import { propTypes } from '@/utils/propTypes'
import { copyValueToTarget } from '@/utils'
defineOptions({ name: 'OtherSettingsForm' })
const message = useMessage() //
const props = defineProps({
propFormData: {
type: Object as PropType<Spu>,
default: () => {}
},
activeName: propTypes.string.def(''),
isDetail: propTypes.bool.def(false) //
})
const otherSettingsFormRef = ref() // Ref
//
const formData = ref<Spu>({
sort: 1, //
giveIntegral: 1, //
virtualSalesCount: 1 //
})
//
const rules = reactive({
sort: [required],
giveIntegral: [required],
virtualSalesCount: [required]
})
/**
* 将传进来的值赋值给formData
*/
watch(
() => props.propFormData,
(data) => {
if (!data) {
return
}
copyValueToTarget(formData.value, data)
},
{
immediate: true
}
)
/**
* 表单校验
*/
const emit = defineEmits(['update:activeName'])
const validate = async () => {
//
if (!otherSettingsFormRef) return
return await unref(otherSettingsFormRef).validate((valid) => {
if (!valid) {
message.warning('商品其他设置未完善!!')
emit('update:activeName', 'otherSettings')
//
throw new Error('商品其他设置未完善!!')
} else {
//
Object.assign(props.propFormData, formData.value)
}
})
}
defineExpose({ validate })
</script>

View File

@ -33,9 +33,9 @@
:propFormData="formData"
/>
</el-tab-pane>
<el-tab-pane label="其它设置" name="otherSettings">
<OtherSettingsForm
ref="otherSettingsRef"
<el-tab-pane label="其它设置" name="other">
<OtherForm
ref="otherRef"
v-model:activeName="activeName"
:is-detail="isDetail"
:propFormData="formData"
@ -58,7 +58,7 @@ import { useTagsViewStore } from '@/store/modules/tagsView'
import * as ProductSpuApi from '@/api/mall/product/spu'
import InfoForm from './InfoForm.vue'
import DescriptionForm from './DescriptionForm.vue'
import OtherSettingsForm from './OtherSettingsForm.vue'
import OtherForm from './OtherForm.vue'
import SkuForm from './SkuForm.vue'
import DeliveryForm from './DeliveryForm.vue'
import { convertToInteger, floatToFixed2, formatToFraction } from '@/utils'
@ -78,7 +78,7 @@ const infoRef = ref() // 商品信息 Ref
const skuRef = ref() // Ref
const deliveryRef = ref() // Ref
const descriptionRef = ref() // Ref
const otherSettingsRef = ref() // Ref
const otherRef = ref() // Ref
// SPU
const formData = ref<ProductSpuApi.Spu>({
name: '', //
@ -155,7 +155,7 @@ const submitForm = async () => {
await unref(skuRef)?.validate()
await unref(deliveryRef)?.validate()
await unref(descriptionRef)?.validate()
await unref(otherSettingsRef)?.validate()
await unref(otherRef)?.validate()
// , server
const deepCopyFormData = cloneDeep(unref(formData.value)) as ProductSpuApi.Spu
deepCopyFormData.skus!.forEach((item) => {