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
@@ -101,21 +114,20 @@ const getSpuDetails = async (
101
114
selectSkus ?.forEach ((sku ) => {
102
115
let config: DiscountActivityApi .DiscountProductVO = {
103
116
skuId: sku .id ! ,
104
- spuId: spu .id ,
117
+ spuId: spu .id ! ,
105
118
discountType: 1 ,
106
119
discountPercent: 0 ,
107
120
discountPrice: 0
108
121
}
109
122
if (typeof products !== ' undefined' ) {
110
123
const product = products .find ((item ) => item .skuId === sku .id )
124
+ if (product ) {
125
+ product .discountPercent = fenToYuan (product .discountPercent ) as any
126
+ product .discountPrice = fenToYuan (product .discountPrice ) as any
127
+ }
111
128
config = product || config
112
129
}
113
130
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
131
})
120
132
spu .skus = selectSkus as DiscountActivityApi .SkuExtension []
121
133
spuPropertyList .value .push ({
@@ -168,25 +180,13 @@ const submitForm = async () => {
168
180
// 提交请求
169
181
formLoading .value = true
170
182
try {
171
- const data = formRef .value .formModel as DiscountActivityApi .DiscountActivityVO
172
183
// 获取折扣商品配置
173
184
const products = cloneDeep (spuAndSkuListRef .value .getSkuConfigs (' productConfig' ))
174
- // 校验优惠金额、折扣百分比,是否正确
175
- // TODO @puhui999:这个交互,可以参考下 youzan 的
176
- let discountInvalid = false
177
185
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
- }
186
+ item .discountPercent = convertToInteger (item .discountPercent )
187
+ item .discountPrice = convertToInteger (item .discountPrice )
185
188
})
186
- if (discountInvalid ) {
187
- message .error (' 优惠金额和折扣百分比需要填写一个' )
188
- return
189
- }
189
+ const data = cloneDeep (formRef .value .formModel ) as DiscountActivityApi .DiscountActivityVO
190
190
data .products = products
191
191
// 真正提交
192
192
if (formType .value === ' create' ) {
@@ -204,6 +204,36 @@ const submitForm = async () => {
204
204
}
205
205
}
206
206
207
+ /** 处理 sku 优惠金额变动 */
208
+ const handleSkuDiscountPriceChange = debounce ((row : any ) => {
209
+ // 校验边界
210
+ if (row .productConfig .discountPrice <= 0 ) {
211
+ return
212
+ }
213
+
214
+ // 设置优惠类型:满减
215
+ row .productConfig .discountType = PromotionDiscountTypeEnum .PRICE .type
216
+ // 设置折扣
217
+ row .productConfig .discountPercent = erpCalculatePercentage (
218
+ row .price - yuanToFen (row .productConfig .discountPrice ),
219
+ row .price
220
+ )
221
+ }, 200 )
222
+ /** 处理 sku 优惠折扣变动 */
223
+ const handleSkuDiscountPercentChange = debounce ((row : any ) => {
224
+ // 校验边界
225
+ if (row .productConfig .discountPercent <= 0 || row .productConfig .discountPercent >= 100 ) {
226
+ return
227
+ }
228
+
229
+ // 设置优惠类型:折扣
230
+ row .productConfig .discountType = PromotionDiscountTypeEnum .PERCENT .type
231
+ // 设置满减金额
232
+ row .productConfig .discountPrice = fenToYuan (
233
+ row .price - row .price * (row .productConfig .discountPercent / 100.0 || 0 )
234
+ )
235
+ }, 200 )
236
+
207
237
/** 重置表单 */
208
238
const resetForm = async () => {
209
239
spuList .value = []
0 commit comments