【代码优化】MALL: 满减送表单重构

pull/521/head
puhui999 2024-08-22 16:39:52 +08:00
parent c45a5228e1
commit 45fc9957af
4 changed files with 211 additions and 229 deletions

View File

@ -1,34 +1,37 @@
import request from '@/config/axios'
export interface DiscountActivityVO {
export interface RewardActivityVO {
id?: number
name?: string
startTime?: Date
endTime?: Date
startAndEndTime?: Date[] // 只前端使用
remark?: string
conditionType?: number
productScope?: number
productSpuIds?: number[]
rules?: DiscountProductVO[]
rules?: RewardRule[]
}
// 优惠规则
export interface DiscountProductVO {
limit: number
discountPrice: number
freeDelivery: boolean
point: number
couponIds: number[]
couponCounts: number[]
export interface RewardRule {
limit?: number
discountPrice?: number
freeDelivery?: boolean
givePoint?: boolean
point?: number
giveCoupon?: boolean
couponIds?: number[]
couponCounts?: number[]
}
// 新增满减送活动
export const createRewardActivity = async (data: DiscountActivityVO) => {
export const createRewardActivity = async (data: RewardActivityVO) => {
return await request.post({ url: '/promotion/reward-activity/create', data })
}
// 更新满减送活动
export const updateRewardActivity = async (data: DiscountActivityVO) => {
export const updateRewardActivity = async (data: RewardActivityVO) => {
return await request.put({ url: '/promotion/reward-activity/update', data })
}

View File

@ -1,11 +1,11 @@
<template>
<Dialog :title="dialogTitle" v-model="dialogVisible">
<Dialog v-model="dialogVisible" :title="dialogTitle">
<el-form
ref="formRef"
v-loading="formLoading"
:model="formData"
:rules="formRules"
label-width="80px"
v-loading="formLoading"
>
<el-form-item label="活动名称" prop="name">
<el-input v-model="formData.name" placeholder="请输入活动名称" />
@ -13,10 +13,10 @@
<el-form-item label="活动时间" prop="startAndEndTime">
<el-date-picker
v-model="formData.startAndEndTime"
type="datetimerange"
range-separator="-"
:start-placeholder="t('common.startTimeText')"
:end-placeholder="t('common.endTimeText')"
:start-placeholder="t('common.startTimeText')"
range-separator="-"
type="datetimerange"
/>
</el-form-item>
<el-form-item label="条件类型" prop="conditionType">
@ -31,79 +31,7 @@
</el-radio-group>
</el-form-item>
<el-form-item label="优惠设置">
<template v-for="(item, index) in formData.rules" :key="index">
<el-row type="flex">
<el-col :span="24" style="font-weight: bold; display: flex">
活动层级{{ index + 1 }}
<el-button
link
type="danger"
style="margin-left: auto"
v-if="index != 0"
@click="deleteActivityRule(index)"
>
删除
</el-button>
</el-col>
<e-form :ref="'formRef' + index" :model="item">
<el-form-item
label="优惠门槛:"
prop="limit"
label-width="100px"
style="padding-left: 50px"
>
<el-input
style="width: 150px; padding: 0 10px"
v-model="item.limit"
type="number"
placeholder=""
/>
</el-form-item>
<el-form-item label="优惠内容:" label-width="100px" style="padding-left: 50px">
<el-checkbox-group v-model="activityRules[index]" style="width: 100%">
<el-col :span="24">
<el-checkbox label="订单金额优惠" name="type" />
<el-form-item v-if="activityRules[index].includes('订单金额优惠')">
<el-input
style="width: 150px; padding: 0 20px"
v-model="item.discountPrice"
type="number"
placeholder=""
/>
</el-form-item>
</el-col>
<el-col :span="24">
<el-checkbox v-model="item.freeDelivery" label="包邮" name="type" />
</el-col>
<el-col :span="24">
<el-checkbox label="送积分" name="type" />
<el-form-item v-if="activityRules[index].includes('送积分')">
<el-input
style="width: 150px; padding: 0 20px"
v-model="item.point"
type="number"
placeholder=""
/>
积分
</el-form-item>
</el-col>
<!-- 优惠券待处理 也可以参考优惠劵的SpuShowcase-->
<!-- TODO 待实现-->
<el-col :span="24">
<el-checkbox label="送优惠券" name="type" />
</el-col>
</el-checkbox-group>
</el-form-item>
</e-form>
</el-row>
</template>
<!-- TODO 实现建议改成放在每一个活动层级的下面有点类似主子表 -->
<el-button type="primary" @click="addActivityStratum"></el-button>
<RewardRule v-model="formData" />
</el-form-item>
<el-form-item label="活动商品" prop="productScope">
<el-radio-group v-model="formData.productScope">
@ -123,17 +51,16 @@
>
<el-select
v-model="formData.productSpuIds"
placeholder="请选择活动商品"
clearable
size="small"
multiple
filterable
style="width: 400px"
multiple
placeholder="请选择活动商品"
size="small"
>
<el-option v-for="item in productSpus" :key="item.id" :label="item.name" :value="item.id">
<span style="float: left">{{ item.name }}</span>
<span style="float: right; font-size: 13px; color: #8492a6">
{{ (item.price / 100.0).toFixed(2) }}
{{ fenToYuan(item.price) }}
</span>
</el-option>
</el-select>
@ -143,55 +70,33 @@
</el-form-item>
</el-form>
<template #footer>
<el-button @click="submitForm" type="primary" :disabled="formLoading"> </el-button>
<el-button :disabled="formLoading" type="primary" @click="submitForm"> </el-button>
<el-button @click="dialogVisible = false"> </el-button>
</template>
</Dialog>
</template>
<script lang="ts" setup>
import RewardRule from './components/RewardRule.vue'
import { getSpuSimpleList } from '@/api/mall/product/spu'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import * as RewardActivityApi from '@/api/mall/promotion/reward/rewardActivity'
import { PromotionConditionTypeEnum, PromotionProductScopeEnum } from '@/utils/constants'
import { fenToYuan } from '@/utils'
/** 初始化 **/
onMounted(() => {
getSpuSimpleList().then((response) => {
productSpus.value = response
})
})
defineOptions({ name: 'ProductBrandForm' })
const { t } = useI18n() //
const message = useMessage() //
const productSpus = ref<any[]>([]) //
const dialogVisible = ref(false) //
const dialogTitle = ref('') //
const formLoading = ref(false) // 12
const formType = ref('') // create - update -
const formData = ref({
id: undefined,
name: undefined,
startAndEndTime: undefined,
startTime: undefined,
endTime: undefined,
const formData = ref<RewardActivityApi.RewardActivityVO>({
conditionType: PromotionConditionTypeEnum.PRICE.type,
remark: undefined,
productScope: PromotionProductScopeEnum.ALL.scope,
productSpuIds: undefined,
rules: [
{
limit: undefined,
discountPrice: undefined,
freeDelivery: undefined,
point: undefined,
couponIds: [],
couponCounts: []
}
]
})
const activityRules = reactive([]) // []
rules: []
} as RewardActivityApi.RewardActivityVO)
const formRules = reactive({
name: [{ required: true, message: '活动名称不能为空', trigger: 'blur' }],
startAndEndTime: [{ required: true, message: '活动时间不能为空', trigger: 'blur' }],
@ -211,23 +116,8 @@ const open = async (type: string, id?: number) => {
if (id) {
formLoading.value = true
try {
let data = await RewardActivityApi.getReward(id)
data.startAndEndTime = [new Date(data.startTime), new Date(data.endTime)]
activityRules.splice(0, activityRules.length)
data.rules.forEach((item) => {
// TODO reactive []
let array: string[] = reactive([])
if (item.freeDelivery) {
array.push('包邮')
}
if (item.point) {
array.push('送积分')
}
if (item.discountPrice) {
array.push('订单金额优惠')
}
activityRules.push(array)
})
const data = await RewardActivityApi.getReward(id)
data.startAndEndTime = [data.startTime, data.endTime]
formData.value = data
} finally {
formLoading.value = false
@ -241,25 +131,15 @@ const emit = defineEmits(['success']) // 定义 success 事件,用于操作成
const submitForm = async () => {
//
if (!formRef) return
const valid = await formRef.value.validate()
if (!valid) return
//
formData.value.startTime = +new Date(formData.value.startAndEndTime[0])
formData.value.endTime = +new Date(formData.value.startAndEndTime[1])
activityRules.forEach((item, index) => {
formData.value.rules[index].freeDelivery = !!item.includes('包邮')
if (!item.includes('送积分')) {
formData.value.rules[index].point = undefined
}
if (!item.includes('订单金额优惠')) {
formData.value.rules[index].discountPrice = undefined
}
})
// TODO puhui999:
// const valid = await formRef.value.validate()
// if (!valid) return
// TODO puhui999:
//
formLoading.value = true
try {
const data = formData.value as RewardActivityApi.DiscountActivityVO
const data = formData.value
if (formType.value === 'create') {
await RewardActivityApi.createRewardActivity(data)
message.success(t('common.createSuccess'))
@ -275,51 +155,18 @@ const submitForm = async () => {
}
}
const addActivityStratum = () => {
formData.value.rules.push({
limit: undefined,
discountPrice: undefined,
freeDelivery: undefined,
point: undefined,
couponIds: [],
couponCounts: []
})
activityRules.push([])
}
const deleteActivityRule = (index) => {
formData.value.rules.splice(index, 1)
activityRules.splice(index, 1)
}
/** 重置表单 */
const resetForm = () => {
formData.value = {
id: undefined,
name: undefined,
startAndEndTime: undefined,
startTime: undefined,
endTime: undefined,
conditionType: PromotionConditionTypeEnum.PRICE.type,
remark: undefined,
productScope: PromotionProductScopeEnum.ALL.scope,
productSpuIds: undefined,
rules: [
{
limit: undefined,
discountPrice: undefined,
freeDelivery: undefined,
point: undefined,
couponIds: [],
couponCounts: []
}
]
}
activityRules.splice(0, activityRules.length)
activityRules.push(reactive([]))
//
nextTick(() => {
formRef.value?.resetFields()
})
rules: []
} as RewardActivityApi.RewardActivityVO
}
/** 初始化 **/
const productSpus = ref<any[]>([]) //
onMounted(async () => {
productSpus.value = await getSpuSimpleList()
})
</script>

View File

@ -0,0 +1,125 @@
<template>
<!-- 满减送活动规则组件 -->
<el-row>
<template v-if="formData.rules">
<div v-for="(rule, index) in formData.rules" :key="index">
<el-col :span="24">
<span class="font-bold">活动层级{{ index + 1 }}</span>
<el-button v-if="index !== 0" link type="danger" @click="deleteRule(index)">
删除
</el-button>
</el-col>
<el-form ref="formRef" :model="rule">
<el-form-item label="优惠门槛:" label-width="100px" prop="limit">
<el-input
v-model="rule.limit"
:min="0"
class="w-150px! p-x-20px!"
placeholder=""
type="number"
/>
{{ PromotionConditionTypeEnum.PRICE.type === formData.conditionType ? '元' : '件' }}
</el-form-item>
<el-form-item label="优惠内容:" label-width="100px">
<el-col :span="24">
订单金额优惠
<el-form-item>
<el-input
v-model="rule.discountPrice"
class="w-150px! p-x-20px!"
placeholder=""
type="number"
/>
</el-form-item>
</el-col>
<el-col :span="24">
<span>包邮</span>
<el-switch
v-model="rule.freeDelivery"
active-text="是"
inactive-text="否"
inline-prompt
/>
</el-col>
<el-col :span="24">
<span>送积分</span>
<el-switch
v-model="rule.givePoint"
active-text="是"
inactive-text="否"
inline-prompt
/>
<el-form-item v-if="rule.givePoint">
<el-input
v-model="rule.point"
class="w-150px! p-x-20px!"
placeholder=""
type="number"
/>
积分
</el-form-item>
</el-col>
<!-- 优惠券待处理 也可以参考优惠劵的SpuShowcase-->
<!-- TODO 待实现-->
<el-col :span="24">
<span>送优惠券</span>
<el-switch
v-model="rule.giveCoupon"
active-text="是"
inactive-text="否"
inline-prompt
/>
</el-col>
</el-form-item>
</el-form>
</div>
</template>
<el-col :span="24">
<el-button type="primary" @click="addRule"></el-button>
</el-col>
</el-row>
</template>
<script lang="ts" setup>
import { RewardActivityVO } from '@/api/mall/promotion/reward/rewardActivity'
import { PromotionConditionTypeEnum } from '@/utils/constants'
import { useVModel } from '@vueuse/core'
defineOptions({ name: 'RewardRule' })
const props = defineProps<{
modelValue: RewardActivityVO
}>()
const emits = defineEmits<{
(e: 'update:modelValue', v: any): void
(e: 'deleteRule', v: number): void
}>()
const formData = useVModel(props, 'modelValue', emits) //
/** 删除优惠规则 */
const deleteRule = (ruleIndex: number) => {
formData.value.rules.splice(ruleIndex, 1)
}
/** 添加优惠规则 */
const addRule = () => {
formData.value.rules.push({
limit: 0,
discountPrice: 0,
freeDelivery: false,
givePoint: false,
point: 0,
giveCoupon: false,
couponIds: [],
couponCounts: []
})
}
</script>
<style lang="scss" scoped></style>

View File

@ -4,27 +4,27 @@
<!-- 搜索工作栏 -->
<ContentWrap>
<el-form
class="-mb-15px"
:model="queryParams"
ref="queryFormRef"
:inline="true"
:model="queryParams"
class="-mb-15px"
label-width="68px"
>
<el-form-item label="活动名称" prop="name">
<el-input
v-model="queryParams.name"
placeholder="请输入活动名称"
clearable
@keyup.enter="handleQuery"
class="!w-240px"
clearable
placeholder="请输入活动名称"
@keyup.enter="handleQuery"
/>
</el-form-item>
<el-form-item label="活动状态" prop="status">
<el-select
v-model="queryParams.status"
placeholder="请选择活动状态"
clearable
class="!w-240px"
clearable
placeholder="请选择活动状态"
>
<el-option
v-for="dict in getIntDictOptions(DICT_TYPE.PROMOTION_ACTIVITY_STATUS)"
@ -37,24 +37,31 @@
<el-form-item label="活动时间" prop="createTime">
<el-date-picker
v-model="queryParams.createTime"
value-format="YYYY-MM-DD HH:mm:ss"
type="daterange"
start-placeholder="活动开始日期"
end-placeholder="活动结束日期"
:default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
class="!w-240px"
end-placeholder="活动结束日期"
start-placeholder="活动开始日期"
type="daterange"
value-format="YYYY-MM-DD HH:mm:ss"
/>
</el-form-item>
<el-form-item>
<el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
<el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
<el-button @click="handleQuery">
<Icon class="mr-5px" icon="ep:search" />
搜索
</el-button>
<el-button @click="resetQuery">
<Icon class="mr-5px" icon="ep:refresh" />
重置
</el-button>
<el-button
type="primary"
plain
@click="openForm('create')"
v-hasPermi="['product:brand:create']"
plain
type="primary"
@click="openForm('create')"
>
<Icon icon="ep:plus" class="mr-5px" /> 新增
<Icon class="mr-5px" icon="ep:plus" />
新增
</el-button>
</el-form-item>
</el-form>
@ -62,47 +69,47 @@
<!-- 列表 -->
<ContentWrap>
<el-table v-loading="loading" :data="list" row-key="id" default-expand-all>
<el-table v-loading="loading" :data="list" default-expand-all row-key="id">
<el-table-column label="活动名称" prop="name" />
<el-table-column
label="活动开始时间"
align="center"
prop="startTime"
:formatter="dateFormatter"
align="center"
label="活动开始时间"
prop="startTime"
/>
<el-table-column
label="活动结束时间"
align="center"
prop="endTime"
:formatter="dateFormatter"
align="center"
label="活动结束时间"
prop="endTime"
/>
<el-table-column label="状态" align="center" prop="status">
<el-table-column align="center" label="状态" prop="status">
<template #default="scope">
<dict-tag :type="DICT_TYPE.PROMOTION_ACTIVITY_STATUS" :value="scope.row.status" />
</template>
</el-table-column>
<el-table-column
label="创建时间"
:formatter="dateFormatter"
align="center"
label="创建时间"
prop="createTime"
width="180"
:formatter="dateFormatter"
/>
<el-table-column label="操作" align="center">
<el-table-column align="center" label="操作">
<template #default="scope">
<el-button
v-hasPermi="['product:brand:update']"
link
type="primary"
@click="openForm('update', scope.row.id)"
v-hasPermi="['product:brand:update']"
>
编辑
</el-button>
<el-button
v-hasPermi="['product:brand:delete']"
link
type="danger"
@click="handleDelete(scope.row.id)"
v-hasPermi="['product:brand:delete']"
>
删除
</el-button>
@ -111,9 +118,9 @@
</el-table>
<!-- 分页 -->
<Pagination
:total="total"
v-model:page="queryParams.pageNo"
v-model:limit="queryParams.pageSize"
v-model:page="queryParams.pageNo"
:total="total"
@pagination="getList"
/>
</ContentWrap>
@ -168,9 +175,9 @@ const resetQuery = () => {
}
/** 添加/修改操作 */
const formRef = ref()
const formRef = ref<InstanceType<typeof RewardForm>>()
const openForm = (type: string, id?: number) => {
formRef.value.open(type, id)
formRef.value?.open(type, id)
}
/** 删除按钮操作 */