Skip to content

Commit e187c55

Browse files
YunaiVgitee-org
authored andcommitted
!283 mall模块
Merge pull request !283 from zxiaoxiu/discount
2 parents 2457665 + 9f77514 commit e187c55

File tree

4 files changed

+590
-0
lines changed

4 files changed

+590
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import request from '@/config/axios'
2+
import { Sku, Spu } from '@/api/mall/product/spu'
3+
4+
export interface DiscountActivityVO {
5+
id?: number
6+
spuId?: number
7+
name?: string
8+
status?: number
9+
remark?: string
10+
startTime?: Date
11+
endTime?: Date
12+
products?: DiscountProductVO[]
13+
}
14+
// 限时折扣相关 属性
15+
export interface DiscountProductVO {
16+
spuId: number
17+
skuId: number
18+
discountType: number
19+
discountPercent: number
20+
discountPrice: number
21+
}
22+
23+
// 扩展 Sku 配置
24+
export type SkuExtension = Sku & {
25+
productConfig: DiscountProductVO
26+
}
27+
28+
export interface SpuExtension extends Spu {
29+
skus: SkuExtension[] // 重写类型
30+
}
31+
32+
// 查询限时折扣活动列表
33+
export const getDiscountActivityPage = async (params) => {
34+
return await request.get({ url: '/promotion/discount-activity/page', params })
35+
}
36+
37+
// 查询限时折扣活动详情
38+
export const getDiscountActivity = async (id: number) => {
39+
return await request.get({ url: '/promotion/discount-activity/get?id=' + id })
40+
}
41+
42+
// 新增限时折扣活动
43+
export const createDiscountActivity = async (data: DiscountActivityVO) => {
44+
return await request.post({ url: '/promotion/discount-activity/create', data })
45+
}
46+
47+
// 修改限时折扣活动
48+
export const updateDiscountActivity = async (data: DiscountActivityVO) => {
49+
return await request.put({ url: '/promotion/discount-activity/update', data })
50+
}
51+
52+
// 关闭限时折扣活动
53+
export const closeDiscountActivity = async (id: number) => {
54+
return await request.put({ url: '/promotion/discount-activity/close?id=' + id })
55+
}
56+
57+
// 删除限时折扣活动
58+
export const deleteDiscountActivity = async (id: number) => {
59+
return await request.delete({ url: '/promotion/discount-activity/delete?id=' + id })
60+
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<template>
2+
<Dialog v-model="dialogVisible" :title="dialogTitle" width="65%">
3+
<Form
4+
ref="formRef"
5+
v-loading="formLoading"
6+
:isCol="true"
7+
:rules="rules"
8+
:schema="allSchemas.formSchema"
9+
>
10+
<!-- 先选择 -->
11+
<template #spuId>
12+
<el-button @click="spuSelectRef.open()">选择商品</el-button>
13+
<SpuAndSkuList
14+
ref="spuAndSkuListRef"
15+
:rule-config="ruleConfig"
16+
:spu-list="spuList"
17+
:spu-property-list-p="spuPropertyList"
18+
>
19+
<el-table-column align="center" label="优惠金额" min-width="168">
20+
<template #default="{ row: sku }">
21+
<el-input-number v-model="sku.productConfig.discountPrice" :min="0" class="w-100%" />
22+
</template>
23+
</el-table-column>
24+
<el-table-column align="center" label="折扣百分比(%)" min-width="168">
25+
<template #default="{ row: sku }">
26+
<el-input-number v-model="sku.productConfig.discountPercent" class="w-100%" />
27+
</template>
28+
</el-table-column>
29+
</SpuAndSkuList>
30+
</template>
31+
</Form>
32+
<template #footer>
33+
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
34+
<el-button @click="dialogVisible = false">取 消</el-button>
35+
</template>
36+
</Dialog>
37+
<SpuSelect ref="spuSelectRef" :isSelectSku="true" @confirm="selectSpu" />
38+
</template>
39+
<script lang="ts" setup>
40+
import { SpuAndSkuList, SpuProperty, SpuSelect } from '../components'
41+
import { allSchemas, rules } from './discountActivity.data'
42+
import { cloneDeep } from 'lodash-es'
43+
import * as DiscountActivityApi from '@/api/mall/promotion/discount/discountActivity'
44+
import * as ProductSpuApi from '@/api/mall/product/spu'
45+
import { getPropertyList, RuleConfig } from '@/views/mall/product/spu/components'
46+
47+
defineOptions({ name: 'PromotionDiscountActivityForm' })
48+
49+
const { t } = useI18n() // 国际化
50+
const message = useMessage() // 消息弹窗
51+
52+
const dialogVisible = ref(false) // 弹窗的是否展示
53+
const dialogTitle = ref('') // 弹窗的标题
54+
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
55+
const formType = ref('') // 表单的类型:create - 新增;update - 修改
56+
const formRef = ref() // 表单 Ref
57+
// ================= 商品选择相关 =================
58+
59+
const spuSelectRef = ref() // 商品和属性选择 Ref
60+
const spuAndSkuListRef = ref() // sku 限时折扣 配置组件Ref
61+
const ruleConfig: RuleConfig[] = []
62+
const spuList = ref<DiscountActivityApi.SpuExtension[]>([]) // 选择的 spu
63+
const spuPropertyList = ref<SpuProperty<DiscountActivityApi.SpuExtension>[]>([])
64+
const selectSpu = (spuId: number, skuIds: number[]) => {
65+
formRef.value.setValues({ spuId })
66+
getSpuDetails(spuId, skuIds)
67+
}
68+
/**
69+
* 获取 SPU 详情
70+
*/
71+
const getSpuDetails = async (
72+
spuId: number,
73+
skuIds: number[] | undefined,
74+
products?: DiscountActivityApi.DiscountProductVO[]
75+
) => {
76+
const spuProperties: SpuProperty<DiscountActivityApi.SpuExtension>[] = []
77+
const res = (await ProductSpuApi.getSpuDetailList([spuId])) as DiscountActivityApi.SpuExtension[]
78+
if (res.length == 0) {
79+
return
80+
}
81+
spuList.value = []
82+
// 因为只能选择一个
83+
const spu = res[0]
84+
const selectSkus =
85+
typeof skuIds === 'undefined' ? spu?.skus : spu?.skus?.filter((sku) => skuIds.includes(sku.id!))
86+
selectSkus?.forEach((sku) => {
87+
let config: DiscountActivityApi.DiscountProductVO = {
88+
skuId: sku.id!,
89+
spuId: spu.id,
90+
discountType: 1,
91+
discountPercent: 0,
92+
discountPrice: 0
93+
}
94+
if (typeof products !== 'undefined') {
95+
const product = products.find((item) => item.skuId === sku.id)
96+
config = product || config
97+
}
98+
sku.productConfig = config
99+
})
100+
spu.skus = selectSkus as DiscountActivityApi.SkuExtension[]
101+
spuProperties.push({
102+
spuId: spu.id!,
103+
spuDetail: spu,
104+
propertyList: getPropertyList(spu)
105+
})
106+
spuList.value.push(spu)
107+
spuPropertyList.value = spuProperties
108+
}
109+
110+
// ================= end =================
111+
112+
/** 打开弹窗 */
113+
const open = async (type: string, id?: number) => {
114+
dialogVisible.value = true
115+
dialogTitle.value = t('action.' + type)
116+
formType.value = type
117+
await resetForm()
118+
// 修改时,设置数据
119+
if (id) {
120+
formLoading.value = true
121+
try {
122+
const data = (await DiscountActivityApi.getDiscountActivity(
123+
id
124+
)) as DiscountActivityApi.DiscountActivityVO
125+
const supId = data.products[0].spuId
126+
await getSpuDetails(supId!, data.products?.map((sku) => sku.skuId), data.products)
127+
formRef.value.setValues(data)
128+
} finally {
129+
formLoading.value = false
130+
}
131+
}
132+
}
133+
defineExpose({ open }) // 提供 open 方法,用于打开弹窗
134+
135+
/** 提交表单 */
136+
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
137+
const submitForm = async () => {
138+
// 校验表单
139+
if (!formRef) return
140+
const valid = await formRef.value.getElFormRef().validate()
141+
if (!valid) return
142+
// 提交请求
143+
formLoading.value = true
144+
try {
145+
const data = formRef.value.formModel as DiscountActivityApi.DiscountActivityVO
146+
// 获取 折扣商品配置
147+
const products = cloneDeep(spuAndSkuListRef.value.getSkuConfigs('productConfig'))
148+
products.forEach((item: DiscountActivityApi.DiscountProductVO) => {
149+
item.discountType = data['discountType']
150+
})
151+
data.products = products
152+
// 真正提交
153+
if (formType.value === 'create') {
154+
await DiscountActivityApi.createDiscountActivity(data)
155+
message.success(t('common.createSuccess'))
156+
} else {
157+
await DiscountActivityApi.updateDiscountActivity(data)
158+
message.success(t('common.updateSuccess'))
159+
}
160+
dialogVisible.value = false
161+
// 发送操作成功的事件
162+
emit('success')
163+
} finally {
164+
formLoading.value = false
165+
}
166+
}
167+
168+
/** 重置表单 */
169+
const resetForm = async () => {
170+
spuList.value = []
171+
spuPropertyList.value = []
172+
await nextTick()
173+
formRef.value.getElFormRef().resetFields()
174+
}
175+
</script>
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import type { CrudSchema } from '@/hooks/web/useCrudSchemas'
2+
import { dateFormatter2 } from '@/utils/formatTime'
3+
4+
// 表单校验
5+
export const rules = reactive({
6+
spuId: [required],
7+
name: [required],
8+
startTime: [required],
9+
endTime: [required],
10+
discountType: [required]
11+
})
12+
13+
// CrudSchema https://doc.iocoder.cn/vue3/crud-schema/
14+
const crudSchemas = reactive<CrudSchema[]>([
15+
{
16+
label: '活动名称',
17+
field: 'name',
18+
isSearch: true,
19+
form: {
20+
colProps: {
21+
span: 24
22+
}
23+
},
24+
table: {
25+
width: 120
26+
}
27+
},
28+
{
29+
label: '活动开始时间',
30+
field: 'startTime',
31+
formatter: dateFormatter2,
32+
isSearch: true,
33+
search: {
34+
component: 'DatePicker',
35+
componentProps: {
36+
valueFormat: 'YYYY-MM-DD',
37+
type: 'daterange'
38+
}
39+
},
40+
form: {
41+
component: 'DatePicker',
42+
componentProps: {
43+
type: 'date',
44+
valueFormat: 'x'
45+
}
46+
},
47+
table: {
48+
width: 120
49+
}
50+
},
51+
{
52+
label: '活动结束时间',
53+
field: 'endTime',
54+
formatter: dateFormatter2,
55+
isSearch: true,
56+
search: {
57+
component: 'DatePicker',
58+
componentProps: {
59+
valueFormat: 'YYYY-MM-DD',
60+
type: 'daterange'
61+
}
62+
},
63+
form: {
64+
component: 'DatePicker',
65+
componentProps: {
66+
type: 'date',
67+
valueFormat: 'x'
68+
}
69+
},
70+
table: {
71+
width: 120
72+
}
73+
},
74+
{
75+
label: '优惠类型',
76+
field: 'discountType',
77+
dictType: DICT_TYPE.PROMOTION_DISCOUNT_TYPE,
78+
dictClass: 'number',
79+
isSearch: true,
80+
form: {
81+
component: 'Radio',
82+
value: 1
83+
}
84+
},
85+
{
86+
label: '活动商品',
87+
field: 'spuId',
88+
isTable: true,
89+
isSearch: false,
90+
form: {
91+
colProps: {
92+
span: 24
93+
}
94+
},
95+
table: {
96+
width: 300
97+
}
98+
},
99+
{
100+
label: '备注',
101+
field: 'remark',
102+
isSearch: false,
103+
form: {
104+
component: 'Input',
105+
componentProps: {
106+
type: 'textarea',
107+
rows: 4
108+
},
109+
colProps: {
110+
span: 24
111+
}
112+
},
113+
table: {
114+
width: 300
115+
}
116+
}
117+
])
118+
export const { allSchemas } = useCrudSchemas(crudSchemas)

0 commit comments

Comments
 (0)