Skip to content

Commit ce3ca7c

Browse files
author
puhui999
committed
fix: mall SeckillActivity
1 parent 36c0bce commit ce3ca7c

File tree

5 files changed

+64
-31
lines changed

5 files changed

+64
-31
lines changed

src/api/mall/promotion/seckill/seckillActivity.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ import request from '@/config/axios'
22
import { Sku, Spu } from '@/api/mall/product/spu'
33

44
export interface SeckillActivityVO {
5-
id: number
6-
spuIds: number[]
7-
name: string
8-
status: number
9-
remark: string
10-
startTime: Date
11-
endTime: Date
12-
sort: number
13-
configIds: string
14-
orderCount: number
15-
userCount: number
16-
totalPrice: number
17-
totalLimitCount: number
18-
singleLimitCount: number
19-
stock: number
20-
totalStock: number
21-
products: SeckillProductVO[]
5+
id?: number
6+
spuId?: number
7+
name?: string
8+
status?: number
9+
remark?: string
10+
startTime?: Date
11+
endTime?: Date
12+
sort?: number
13+
configIds?: string
14+
orderCount?: number
15+
userCount?: number
16+
totalPrice?: number
17+
totalLimitCount?: number
18+
singleLimitCount?: number
19+
stock?: number
20+
totalStock?: number
21+
products?: SeckillProductVO[]
2222
}
2323

2424
// 秒杀活动所需属性

src/views/mall/product/spu/components/SkuList.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
<!-- 情况二:详情 -->
114114
<el-table
115115
v-if="isDetail"
116+
ref="activitySkuListRef"
116117
:data="formData!.skus"
117118
border
118119
max-height="500"
@@ -194,7 +195,6 @@
194195
<!-- 情况三:作为活动组件 -->
195196
<el-table
196197
v-if="isActivityComponent"
197-
ref="activitySkuListRef"
198198
:data="formData!.skus"
199199
border
200200
max-height="500"
@@ -261,6 +261,7 @@ import type { Property, Sku, Spu } from '@/api/mall/product/spu'
261261
import { createImageViewer } from '@/components/ImageViewer'
262262
import { RuleConfig } from '@/views/mall/product/spu/components/index'
263263
import { Properties } from './index'
264+
import { ElTable } from 'element-plus'
264265
265266
defineOptions({ name: 'SkuList' })
266267
const message = useMessage() // 消息弹窗
@@ -515,7 +516,6 @@ watch(
515516
// name加属性项index区分属性值
516517
tableHeaders.value.push({ prop: `name${index}`, label: item.name })
517518
})
518-
519519
// 如果回显的 sku 属性和添加的属性一致则不处理
520520
if (validateData(propertyList)) {
521521
return
@@ -532,6 +532,10 @@ watch(
532532
immediate: true
533533
}
534534
)
535+
const activitySkuListRef = ref<InstanceType<typeof ElTable>>()
536+
const clearSelection = () => {
537+
activitySkuListRef.value.clearSelection()
538+
}
535539
// 暴露出生成 sku 方法,给添加属性成功时调用
536-
defineExpose({ generateTableData, validateSku })
540+
defineExpose({ generateTableData, validateSku, clearSelection })
537541
</script>

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<el-table :data="spuData" :default-expand-all="true">
2+
<el-table :data="spuData" :expand-row-keys="expandRowKeys" row-key="id">
33
<el-table-column type="expand" width="30">
44
<template #default="{ row }">
55
<SkuList
@@ -48,9 +48,8 @@ const props = defineProps<{
4848
4949
const spuData = ref<Spu[]>([]) // spu 详情数据列表
5050
const skuListRef = ref() // 商品属性列表Ref
51-
5251
const spuPropertyList = ref<SpuProperty<T>[]>([]) // spuId 对应的 sku 的属性列表
53-
52+
const expandRowKeys = ref<number[]>() // 控制展开行需要设置 row-key 属性才能使用,该属性为展开行的 keys 数组。
5453
/**
5554
* 获取所有 sku 活动配置
5655
* @param extendedAttribute 在 sku 上扩展的属性,例:秒杀活动 sku 扩展属性 productConfig 请参考 seckillActivity.ts
@@ -98,6 +97,10 @@ watch(
9897
(data) => {
9998
if (!data) return
10099
spuPropertyList.value = data as SpuProperty<T>[]
100+
// 解决如果之前选择的是单规格 spu 的话后面选择多规格 sku 多规格属性信息不展示的问题。解决方法:让 SkuList 组件重新渲染(行折叠会干掉包含的组件展开时会重新加载)
101+
setTimeout(() => {
102+
expandRowKeys.value = data.map((item) => item.spuId)
103+
}, 200)
101104
},
102105
{
103106
deep: true,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<template #default>
5858
<SkuList
5959
v-if="isExpand"
60+
ref="skuListRef"
6061
:isComponent="true"
6162
:isDetail="true"
6263
:prop-form-data="spuData"
@@ -145,6 +146,7 @@ const queryParams = ref({
145146
}) // 查询参数
146147
const propertyList = ref<Properties[]>([]) // 商品属性列表
147148
const spuListRef = ref<InstanceType<typeof ElTable>>()
149+
const skuListRef = ref() // 商品属性选择 Ref
148150
const spuData = ref<ProductSpuApi.Spu>() // 商品详情
149151
const isExpand = ref(false) // 控制 SKU 列表显示
150152
const expandRowKeys = ref<number[]>() // 控制展开行需要设置 row-key 属性才能使用,该属性为展开行的 keys 数组。
@@ -154,6 +156,8 @@ const selectedSpuId = ref<number>(0) // 选中的商品 spuId
154156
const selectedSkuIds = ref<number[]>([]) // 选中的商品 skuIds
155157
const selectSku = (val: ProductSpuApi.Sku[]) => {
156158
if (selectedSpuId.value === 0) {
159+
message.warning('请先选择商品再选择相应的规格!!!')
160+
skuListRef.value.clearSelection()
157161
return
158162
}
159163
selectedSkuIds.value = val.map((sku) => sku.id!)
@@ -233,6 +237,8 @@ const confirm = () => {
233237
: emits('confirm', selectedSpuId.value)
234238
// 关闭弹窗
235239
dialogVisible.value = false
240+
selectedSpuId.value = 0
241+
selectedSkuIds.value = []
236242
}
237243
238244
/** 打开弹窗 */

src/views/mall/promotion/seckill/activity/SeckillActivityForm.vue

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import * as SeckillActivityApi from '@/api/mall/promotion/seckill/seckillActivit
5050
import { SeckillProductVO } from '@/api/mall/promotion/seckill/seckillActivity'
5151
import * as ProductSpuApi from '@/api/mall/product/spu'
5252
import { getPropertyList, RuleConfig } from '@/views/mall/product/spu/components'
53-
import { convertToInteger } from '@/utils'
53+
import { convertToInteger, formatToFraction } from '@/utils'
5454
5555
defineOptions({ name: 'PromotionSeckillActivityForm' })
5656
@@ -89,7 +89,11 @@ const selectSpu = (spuId: number, skuIds: number[]) => {
8989
* 获取 SPU 详情
9090
* @param spuIds
9191
*/
92-
const getSpuDetails = async (spuId: number, skuIds: number[]) => {
92+
const getSpuDetails = async (
93+
spuId: number,
94+
skuIds: number[] | undefined,
95+
products?: SeckillProductVO[]
96+
) => {
9397
const spuProperties: SpuProperty<SeckillActivityApi.SpuExtension>[] = []
9498
const res = (await ProductSpuApi.getSpuDetailList([spuId])) as SeckillActivityApi.SpuExtension[]
9599
if (res.length == 0) {
@@ -98,13 +102,22 @@ const getSpuDetails = async (spuId: number, skuIds: number[]) => {
98102
spuList.value = []
99103
// 因为只能选择一个
100104
const spu = res[0]
101-
const selectSkus = spu?.skus?.filter((sku) => skuIds.includes(sku.id!))
105+
const selectSkus =
106+
typeof skuIds === 'undefined' ? spu?.skus : spu?.skus?.filter((sku) => skuIds.includes(sku.id!))
102107
selectSkus?.forEach((sku) => {
103-
const config: SeckillActivityApi.SeckillProductVO = {
108+
let config: SeckillActivityApi.SeckillProductVO = {
104109
skuId: sku.id!,
105110
stock: 0,
106111
seckillPrice: 0
107112
}
113+
if (typeof products !== 'undefined') {
114+
const product = products.find((item) => item.skuId === sku.id)
115+
if (product) {
116+
// 元转分
117+
product.seckillPrice = formatToFraction(product.seckillPrice)
118+
}
119+
config = product || config
120+
}
108121
sku.productConfig = config
109122
})
110123
spu.skus = selectSkus as SeckillActivityApi.SkuExtension[]
@@ -113,7 +126,7 @@ const getSpuDetails = async (spuId: number, skuIds: number[]) => {
113126
spuDetail: spu,
114127
propertyList: getPropertyList(spu)
115128
})
116-
spuList.value.push(...res)
129+
spuList.value.push(spu)
117130
spuPropertyList.value = spuProperties
118131
}
119132
@@ -125,11 +138,18 @@ const open = async (type: string, id?: number) => {
125138
dialogTitle.value = t('action.' + type)
126139
formType.value = type
127140
await resetForm()
128-
// 修改时,设置数据 TODO 没测试估计有问题
141+
// 修改时,设置数据
129142
if (id) {
130143
formLoading.value = true
131144
try {
132-
const data = await SeckillActivityApi.getSeckillActivity(id)
145+
const data = (await SeckillActivityApi.getSeckillActivity(
146+
id
147+
)) as SeckillActivityApi.SeckillActivityVO
148+
await getSpuDetails(
149+
data.spuId!,
150+
data.products?.map((sku) => sku.skuId),
151+
data.products
152+
)
133153
formRef.value.setValues(data)
134154
} finally {
135155
formLoading.value = false
@@ -162,7 +182,7 @@ const submitForm = async () => {
162182
item.seckillPrice = convertToInteger(item.seckillPrice)
163183
})
164184
// 获取秒杀商品配置
165-
data.products = spuAndSkuListRef.value.getSkuConfigs('productConfig')
185+
data.products = products
166186
if (formType.value === 'create') {
167187
await SeckillActivityApi.createSeckillActivity(data)
168188
message.success(t('common.createSuccess'))

0 commit comments

Comments
 (0)