8
8
:schema =" allSchemas.formSchema"
9
9
>
10
10
<!-- 先选择 -->
11
- <!-- TODO @zhangshuai:商品允许选择多个 -->
12
- <!-- TODO @zhangshuai:选择后的 SKU,需要后面加个【删除】按钮 -->
13
- <!-- TODO @zhangshuai:展示的金额,貌似不对,大了 100 倍,需要看下 -->
14
- <!-- TODO @zhangshuai:“优惠类型”,是每个 SKU 可以自定义已设置哈。因为每个商品 SKU 的折扣和减少价格,可能不同。具体交互,可以注册一个 youzan.com 看看;它的交互方式是,如果设置了“优惠金额”,则算“减价”;如果再次设置了“折扣百分比”,就算“打折”;这样形成一个互斥的优惠类型 -->
15
11
<template #spuId >
16
12
<el-button @click =" spuSelectRef.open()" >选择商品</el-button >
17
13
<SpuAndSkuList
18
14
ref =" spuAndSkuListRef"
15
+ :deletable =" true"
19
16
:rule-config =" ruleConfig"
20
17
:spu-list =" spuList"
21
18
:spu-property-list-p =" spuPropertyList"
22
- :isDelete =" true"
23
19
@delete =" deleteSpu"
24
20
>
25
21
<el-table-column align =" center" label =" 优惠金额" min-width =" 168" >
26
- <template #default =" { row: sku } " >
27
- <el-input-number v-model =" sku.productConfig.discountPrice" :min =" 0" class =" w-100%" />
22
+ <template #default =" { row } " >
23
+ <el-input-number
24
+ v-model =" row.productConfig.discountPrice"
25
+ :max =" parseFloat(fenToYuan(row.price))"
26
+ :min =" 0"
27
+ :precision =" 2"
28
+ :step =" 0.1"
29
+ class =" w-100%"
30
+ @change =" handleSkuDiscountPriceChange(row)"
31
+ />
28
32
</template >
29
33
</el-table-column >
30
34
<el-table-column align =" center" label =" 折扣百分比(%)" min-width =" 168" >
31
- <template #default =" { row: sku } " >
32
- <el-input-number v-model =" sku.productConfig.discountPercent" class =" w-100%" />
35
+ <template #default =" { row } " >
36
+ <el-input-number
37
+ v-model =" row.productConfig.discountPercent"
38
+ :max =" 100"
39
+ :min =" 0"
40
+ :precision =" 2"
41
+ :step =" 0.1"
42
+ class =" w-100%"
43
+ @change =" handleSkuDiscountPercentChange(row)"
44
+ />
33
45
</template >
34
46
</el-table-column >
35
47
</SpuAndSkuList >
45
57
<script lang="ts" setup>
46
58
import { SpuAndSkuList , SpuProperty , SpuSelect } from ' ../components'
47
59
import { allSchemas , rules } from ' ./discountActivity.data'
48
- import { cloneDeep } from ' lodash-es'
60
+ import { cloneDeep , debounce } from ' lodash-es'
49
61
import * as DiscountActivityApi from ' @/api/mall/promotion/discount/discountActivity'
50
62
import * as ProductSpuApi from ' @/api/mall/product/spu'
51
63
import { getPropertyList , RuleConfig } from ' @/views/mall/product/spu/components'
52
- import { formatToFraction } from ' @/utils'
64
+ import { convertToInteger , erpCalculatePercentage , fenToYuan , yuanToFen } from ' @/utils'
65
+ import { PromotionDiscountTypeEnum } from ' @/utils/constants'
53
66
54
67
defineOptions ({ name: ' PromotionDiscountActivityForm' })
55
68
@@ -65,11 +78,19 @@ const formRef = ref() // 表单 Ref
65
78
66
79
const spuSelectRef = ref () // 商品和属性选择 Ref
67
80
const spuAndSkuListRef = ref () // sku 限时折扣 配置组件Ref
68
- const ruleConfig: RuleConfig [] = []
81
+ const ruleConfig: RuleConfig [] = [
82
+ {
83
+ name: ' productConfig.discountPrice' ,
84
+ rule : (arg ) => arg > 0 ,
85
+ message: ' 商品优惠金额不能为 0 !!!'
86
+ }
87
+ ]
69
88
const spuList = ref <DiscountActivityApi .SpuExtension []>([]) // 选择的 spu
70
89
const spuPropertyList = ref <SpuProperty <DiscountActivityApi .SpuExtension >[]>([])
71
90
const spuIds = ref <number []>([])
72
91
const selectSpu = (spuId : number , skuIds : number []) => {
92
+ // TODO puhui999: 艿艿现在限时折扣活动可以选择多个 spu ,那么 spuId 是不是得改成 spuIds 来存放多个?🤣
93
+ formRef .value .setValues ({ spuId })
73
94
getSpuDetails (spuId , skuIds )
74
95
}
75
96
/**
@@ -101,21 +122,20 @@ const getSpuDetails = async (
101
122
selectSkus ?.forEach ((sku ) => {
102
123
let config: DiscountActivityApi .DiscountProductVO = {
103
124
skuId: sku .id ! ,
104
- spuId: spu .id ,
125
+ spuId: spu .id ! ,
105
126
discountType: 1 ,
106
127
discountPercent: 0 ,
107
128
discountPrice: 0
108
129
}
109
130
if (typeof products !== ' undefined' ) {
110
131
const product = products .find ((item ) => item .skuId === sku .id )
132
+ if (product ) {
133
+ product .discountPercent = fenToYuan (product .discountPercent ) as any
134
+ product .discountPrice = fenToYuan (product .discountPrice ) as any
135
+ }
111
136
config = product || config
112
137
}
113
138
sku .productConfig = config
114
- sku .price = formatToFraction (sku .price )
115
- sku .marketPrice = formatToFraction (sku .marketPrice )
116
- sku .costPrice = formatToFraction (sku .costPrice )
117
- sku .firstBrokeragePrice = formatToFraction (sku .firstBrokeragePrice )
118
- sku .secondBrokeragePrice = formatToFraction (sku .secondBrokeragePrice )
119
139
})
120
140
spu .skus = selectSkus as DiscountActivityApi .SkuExtension []
121
141
spuPropertyList .value .push ({
@@ -168,25 +188,13 @@ const submitForm = async () => {
168
188
// 提交请求
169
189
formLoading .value = true
170
190
try {
171
- const data = formRef .value .formModel as DiscountActivityApi .DiscountActivityVO
172
191
// 获取折扣商品配置
173
192
const products = cloneDeep (spuAndSkuListRef .value .getSkuConfigs (' productConfig' ))
174
- // 校验优惠金额、折扣百分比,是否正确
175
- // TODO @puhui999:这个交互,可以参考下 youzan 的
176
- let discountInvalid = false
177
193
products .forEach ((item : DiscountActivityApi .DiscountProductVO ) => {
178
- if (item .discountPrice != null && item .discountPrice > 0 ) {
179
- item .discountType = 1
180
- } else if (item .discountPercent != null && item .discountPercent > 0 ) {
181
- item .discountType = 2
182
- } else {
183
- discountInvalid = true
184
- }
194
+ item .discountPercent = convertToInteger (item .discountPercent )
195
+ item .discountPrice = convertToInteger (item .discountPrice )
185
196
})
186
- if (discountInvalid ) {
187
- message .error (' 优惠金额和折扣百分比需要填写一个' )
188
- return
189
- }
197
+ const data = cloneDeep (formRef .value .formModel ) as DiscountActivityApi .DiscountActivityVO
190
198
data .products = products
191
199
// 真正提交
192
200
if (formType .value === ' create' ) {
@@ -204,6 +212,36 @@ const submitForm = async () => {
204
212
}
205
213
}
206
214
215
+ /** 处理 sku 优惠金额变动 */
216
+ const handleSkuDiscountPriceChange = debounce ((row : any ) => {
217
+ // 校验边界
218
+ if (row .productConfig .discountPrice <= 0 ) {
219
+ return
220
+ }
221
+
222
+ // 设置优惠类型:满减
223
+ row .productConfig .discountType = PromotionDiscountTypeEnum .PRICE .type
224
+ // 设置折扣
225
+ row .productConfig .discountPercent = erpCalculatePercentage (
226
+ row .price - yuanToFen (row .productConfig .discountPrice ),
227
+ row .price
228
+ )
229
+ }, 200 )
230
+ /** 处理 sku 优惠折扣变动 */
231
+ const handleSkuDiscountPercentChange = debounce ((row : any ) => {
232
+ // 校验边界
233
+ if (row .productConfig .discountPercent <= 0 || row .productConfig .discountPercent >= 100 ) {
234
+ return
235
+ }
236
+
237
+ // 设置优惠类型:折扣
238
+ row .productConfig .discountType = PromotionDiscountTypeEnum .PERCENT .type
239
+ // 设置满减金额
240
+ row .productConfig .discountPrice = fenToYuan (
241
+ row .price - row .price * (row .productConfig .discountPercent / 100.0 || 0 )
242
+ )
243
+ }, 200 )
244
+
207
245
/** 重置表单 */
208
246
const resetForm = async () => {
209
247
spuList .value = []
0 commit comments