Skip to content

Commit 7bb482a

Browse files
author
puhui999
committed
【代码优化】商城: 限时折扣活动 sku 自动计算对应的满减/折扣
1 parent 2e207dd commit 7bb482a

File tree

2 files changed

+71
-41
lines changed

2 files changed

+71
-41
lines changed

src/views/mall/promotion/components/SpuAndSkuList.vue

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030
<el-table-column align="center" label="销量" min-width="90" prop="salesCount" />
3131
<el-table-column align="center" label="库存" min-width="90" prop="stock" />
3232
<el-table-column
33-
v-if="spuData.length > 1 && isDelete"
33+
v-if="spuData.length > 1 && deletable"
3434
align="center"
3535
label="操作"
3636
min-width="90"
3737
>
3838
<template #default="scope">
39-
<el-button type="primary" link @click="deleteSpu(scope.row.id)"> 删除 </el-button>
39+
<el-button link type="primary" @click="deleteSpu(scope.row.id)"> 删除</el-button>
4040
</template>
4141
</el-table-column>
4242
</el-table>
@@ -56,13 +56,13 @@ const props = defineProps<{
5656
spuList: T[]
5757
ruleConfig: RuleConfig[]
5858
spuPropertyListP: SpuProperty<T>[]
59-
isDelete?: boolean // SPU 是否可删除;TODO deletable 换成这个名字好点。
59+
deletable?: boolean // SPU 是否可删除;
6060
}>()
6161
6262
const spuData = ref<Spu[]>([]) // spu 详情数据列表
6363
const skuListRef = ref() // 商品属性列表Ref
6464
const spuPropertyList = ref<SpuProperty<T>[]>([]) // spuId 对应的 sku 的属性列表
65-
const expandRowKeys = ref<number[]>() // 控制展开行需要设置 row-key 属性才能使用,该属性为展开行的 keys 数组。
65+
const expandRowKeys = ref<string[]>([]) // 控制展开行需要设置 row-key 属性才能使用,该属性为展开行的 keys 数组。
6666
6767
/**
6868
* 获取所有 sku 活动配置
@@ -71,10 +71,10 @@ const expandRowKeys = ref<number[]>() // 控制展开行需要设置 row-key 属
7171
*/
7272
const getSkuConfigs = (extendedAttribute: string) => {
7373
skuListRef.value.validateSku()
74-
const seckillProducts = []
74+
const seckillProducts: any[] = []
7575
spuPropertyList.value.forEach((item) => {
76-
item.spuDetail.skus.forEach((sku) => {
77-
seckillProducts.push(sku[extendedAttribute])
76+
item.spuDetail.skus?.forEach((sku: any) => {
77+
seckillProducts.push(sku[extendedAttribute] as any)
7878
})
7979
})
8080
return seckillProducts
@@ -124,10 +124,10 @@ watch(
124124
() => props.spuPropertyListP,
125125
(data) => {
126126
if (!data) return
127-
spuPropertyList.value = data as SpuProperty<T>[]
127+
spuPropertyList.value = data as SpuProperty<T>[] as any
128128
// 解决如果之前选择的是单规格 spu 的话后面选择多规格 sku 多规格属性信息不展示的问题。解决方法:让 SkuList 组件重新渲染(行折叠会干掉包含的组件展开时会重新加载)
129129
setTimeout(() => {
130-
expandRowKeys.value = data.map((item) => item.spuId)
130+
expandRowKeys.value = data.map((item) => item.spuId + '')
131131
}, 200)
132132
},
133133
{

src/views/mall/promotion/discountActivity/DiscountActivityForm.vue

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,40 @@
88
:schema="allSchemas.formSchema"
99
>
1010
<!-- 先选择 -->
11-
<!-- TODO @zhangshuai:商品允许选择多个 -->
12-
<!-- TODO @zhangshuai:选择后的 SKU,需要后面加个【删除】按钮 -->
13-
<!-- TODO @zhangshuai:展示的金额,貌似不对,大了 100 倍,需要看下 -->
14-
<!-- TODO @zhangshuai:“优惠类型”,是每个 SKU 可以自定义已设置哈。因为每个商品 SKU 的折扣和减少价格,可能不同。具体交互,可以注册一个 youzan.com 看看;它的交互方式是,如果设置了“优惠金额”,则算“减价”;如果再次设置了“折扣百分比”,就算“打折”;这样形成一个互斥的优惠类型 -->
1511
<template #spuId>
1612
<el-button @click="spuSelectRef.open()">选择商品</el-button>
1713
<SpuAndSkuList
1814
ref="spuAndSkuListRef"
15+
:deletable="true"
1916
:rule-config="ruleConfig"
2017
:spu-list="spuList"
2118
:spu-property-list-p="spuPropertyList"
22-
:isDelete="true"
2319
@delete="deleteSpu"
2420
>
2521
<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+
/>
2832
</template>
2933
</el-table-column>
3034
<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+
/>
3345
</template>
3446
</el-table-column>
3547
</SpuAndSkuList>
@@ -45,11 +57,12 @@
4557
<script lang="ts" setup>
4658
import { SpuAndSkuList, SpuProperty, SpuSelect } from '../components'
4759
import { allSchemas, rules } from './discountActivity.data'
48-
import { cloneDeep } from 'lodash-es'
60+
import { cloneDeep, debounce } from 'lodash-es'
4961
import * as DiscountActivityApi from '@/api/mall/promotion/discount/discountActivity'
5062
import * as ProductSpuApi from '@/api/mall/product/spu'
5163
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'
5366
5467
defineOptions({ name: 'PromotionDiscountActivityForm' })
5568
@@ -101,21 +114,20 @@ const getSpuDetails = async (
101114
selectSkus?.forEach((sku) => {
102115
let config: DiscountActivityApi.DiscountProductVO = {
103116
skuId: sku.id!,
104-
spuId: spu.id,
117+
spuId: spu.id!,
105118
discountType: 1,
106119
discountPercent: 0,
107120
discountPrice: 0
108121
}
109122
if (typeof products !== 'undefined') {
110123
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+
}
111128
config = product || config
112129
}
113130
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)
119131
})
120132
spu.skus = selectSkus as DiscountActivityApi.SkuExtension[]
121133
spuPropertyList.value.push({
@@ -168,25 +180,13 @@ const submitForm = async () => {
168180
// 提交请求
169181
formLoading.value = true
170182
try {
171-
const data = formRef.value.formModel as DiscountActivityApi.DiscountActivityVO
172183
// 获取折扣商品配置
173184
const products = cloneDeep(spuAndSkuListRef.value.getSkuConfigs('productConfig'))
174-
// 校验优惠金额、折扣百分比,是否正确
175-
// TODO @puhui999:这个交互,可以参考下 youzan 的
176-
let discountInvalid = false
177185
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)
185188
})
186-
if (discountInvalid) {
187-
message.error('优惠金额和折扣百分比需要填写一个')
188-
return
189-
}
189+
const data = cloneDeep(formRef.value.formModel) as DiscountActivityApi.DiscountActivityVO
190190
data.products = products
191191
// 真正提交
192192
if (formType.value === 'create') {
@@ -204,6 +204,36 @@ const submitForm = async () => {
204204
}
205205
}
206206
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+
207237
/** 重置表单 */
208238
const resetForm = async () => {
209239
spuList.value = []

0 commit comments

Comments
 (0)