Skip to content

Commit dc39ad0

Browse files
author
puhui999
committed
fix: 完善拼团活动 CRUD
1 parent 3fd9f1b commit dc39ad0

File tree

5 files changed

+89
-32
lines changed

5 files changed

+89
-32
lines changed

src/api/mall/promotion/combination/combinationactivity.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Sku, Spu } from '@/api/mall/product/spu'
44
export interface CombinationActivityVO {
55
id?: number
66
name?: string
7-
spuIds?: number[]
7+
spuId?: number
88
totalLimitCount?: number
99
singleLimitCount?: number
1010
startTime?: Date
@@ -27,7 +27,7 @@ export interface CombinationProductVO {
2727
}
2828

2929
// 扩展 Sku 配置
30-
type SkuExtension = Sku & {
30+
export type SkuExtension = Sku & {
3131
productConfig: CombinationProductVO
3232
}
3333

@@ -59,8 +59,3 @@ export const updateCombinationActivity = async (data: CombinationActivityVO) =>
5959
export const deleteCombinationActivity = async (id: number) => {
6060
return await request.delete({ url: '/promotion/combination-activity/delete?id=' + id })
6161
}
62-
63-
// 导出拼团活动 Excel
64-
export const exportCombinationActivity = async (params) => {
65-
return await request.download({ url: '/promotion/combination-activity/export-excel', params })
66-
}

src/views/mall/promotion/combination/activity/CombinationActivityForm.vue

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
:rules="rules"
88
:schema="allSchemas.formSchema"
99
>
10-
<template #spuIds>
10+
<template #spuId>
1111
<el-button @click="spuSelectRef.open()">选择商品</el-button>
1212
<SpuAndSkuList
1313
ref="spuAndSkuListRef"
@@ -34,7 +34,7 @@
3434
<el-button @click="dialogVisible = false">取 消</el-button>
3535
</template>
3636
</Dialog>
37-
<SpuSelect ref="spuSelectRef" @confirm="selectSpu" />
37+
<SpuSelect ref="spuSelectRef" :isSelectSku="true" @confirm="selectSpu" />
3838
</template>
3939
<script lang="ts" setup>
4040
import * as CombinationActivityApi from '@/api/mall/promotion/combination/combinationactivity'
@@ -43,7 +43,7 @@ import { allSchemas, rules } from './combinationActivity.data'
4343
import { SpuAndSkuList, SpuProperty, SpuSelect } from '@/views/mall/promotion/components'
4444
import { getPropertyList, RuleConfig } from '@/views/mall/product/spu/components'
4545
import * as ProductSpuApi from '@/api/mall/product/spu'
46-
import { convertToInteger } from '@/utils'
46+
import { convertToInteger, formatToFraction } from '@/utils'
4747
4848
defineOptions({ name: 'PromotionCombinationActivityForm' })
4949
@@ -71,32 +71,51 @@ const ruleConfig: RuleConfig[] = [
7171
]
7272
const selectSpu = (spuId: number, skuIds: number[]) => {
7373
formRef.value.setValues({ spuId })
74-
getSpuDetails([spuId])
75-
console.log(skuIds)
74+
getSpuDetails(spuId, skuIds)
7675
}
7776
/**
7877
* 获取 SPU 详情
79-
* @param spuIds
8078
*/
81-
const getSpuDetails = async (spuIds: number[]) => {
79+
const getSpuDetails = async (
80+
spuId: number,
81+
skuIds: number[] | undefined,
82+
products?: CombinationProductVO[]
83+
) => {
8284
const spuProperties: SpuProperty<CombinationActivityApi.SpuExtension>[] = []
83-
const res = (await ProductSpuApi.getSpuDetailList(
84-
spuIds
85-
)) as CombinationActivityApi.SpuExtension[]
85+
const res = (await ProductSpuApi.getSpuDetailList([
86+
spuId
87+
])) as CombinationActivityApi.SpuExtension[]
88+
if (res.length == 0) {
89+
return
90+
}
8691
spuList.value = []
87-
res?.forEach((spu) => {
88-
// 初始化每个 sku 秒杀配置
89-
spu.skus?.forEach((sku) => {
90-
const config: CombinationActivityApi.CombinationProductVO = {
91-
spuId: spu.id!,
92-
skuId: sku.id!,
93-
activePrice: 0
92+
// 因为只能选择一个
93+
const spu = res[0]
94+
const selectSkus =
95+
typeof skuIds === 'undefined' ? spu?.skus : spu?.skus?.filter((sku) => skuIds.includes(sku.id!))
96+
selectSkus?.forEach((sku) => {
97+
let config: CombinationProductVO = {
98+
spuId: spu.id!,
99+
skuId: sku.id!,
100+
activePrice: 0
101+
}
102+
if (typeof products !== 'undefined') {
103+
const product = products.find((item) => item.skuId === sku.id)
104+
if (product) {
105+
// 分转元
106+
product.activePrice = formatToFraction(product.activePrice)
94107
}
95-
sku.productConfig = config
96-
})
97-
spuProperties.push({ spuId: spu.id!, spuDetail: spu, propertyList: getPropertyList(spu) })
108+
config = product || config
109+
}
110+
sku.productConfig = config
98111
})
99-
spuList.value.push(...res)
112+
spu.skus = selectSkus as CombinationActivityApi.SkuExtension[]
113+
spuProperties.push({
114+
spuId: spu.id!,
115+
spuDetail: spu,
116+
propertyList: getPropertyList(spu)
117+
})
118+
spuList.value.push(spu)
100119
spuPropertyList.value = spuProperties
101120
}
102121
@@ -107,11 +126,19 @@ const open = async (type: string, id?: number) => {
107126
dialogVisible.value = true
108127
dialogTitle.value = t('action.' + type)
109128
formType.value = type
129+
await resetForm()
110130
// 修改时,设置数据
111131
if (id) {
112132
formLoading.value = true
113133
try {
114-
const data = await CombinationActivityApi.getCombinationActivity(id)
134+
const data = (await CombinationActivityApi.getCombinationActivity(
135+
id
136+
)) as CombinationActivityApi.CombinationActivityVO
137+
await getSpuDetails(
138+
data.spuId!,
139+
data.products?.map((sku) => sku.skuId),
140+
data.products
141+
)
115142
formRef.value.setValues(data)
116143
} finally {
117144
formLoading.value = false
@@ -120,6 +147,14 @@ const open = async (type: string, id?: number) => {
120147
}
121148
defineExpose({ open }) // 提供 open 方法,用于打开弹窗
122149
150+
/** 重置表单 */
151+
const resetForm = async () => {
152+
spuList.value = []
153+
spuPropertyList.value = []
154+
await nextTick()
155+
formRef.value.getElFormRef().resetFields()
156+
}
157+
123158
/** 提交表单 */
124159
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
125160
const submitForm = async () => {

src/views/mall/promotion/combination/activity/combinationActivity.data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ const crudSchemas = reactive<CrudSchema[]>([
134134
},
135135
{
136136
label: '拼团商品',
137-
field: 'spuIds',
137+
field: 'spuId',
138138
isSearch: false,
139139
form: {
140140
colProps: {

src/views/mall/promotion/combination/activity/index.vue

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929
total: tableObject.total
3030
}"
3131
>
32+
<template #spuId="{ row }">
33+
<el-image
34+
:src="row.picUrl"
35+
class="w-30px h-30px align-middle mr-5px"
36+
@click="imagePreview(row.picUrl)"
37+
/>
38+
<span class="align-middle">{{ row.spuName }}</span>
39+
</template>
3240
<template #action="{ row }">
3341
<el-button
3442
v-hasPermi="['promotion:combination-activity:update']"
@@ -57,6 +65,8 @@
5765
import { allSchemas } from './combinationActivity.data'
5866
import * as CombinationActivityApi from '@/api/mall/promotion/combination/combinationactivity'
5967
import CombinationActivityForm from './CombinationActivityForm.vue'
68+
import { cloneDeep } from 'lodash-es'
69+
import { createImageViewer } from '@/components/ImageViewer'
6070
6171
defineOptions({ name: 'PromotionCombinationActivity' })
6272
@@ -70,6 +80,13 @@ const { tableObject, tableMethods } = useTable({
7080
// 获得表格的各种操作
7181
const { getList, setSearchParams } = tableMethods
7282
83+
/** 商品图预览 */
84+
const imagePreview = (imgUrl: string) => {
85+
createImageViewer({
86+
urlList: [imgUrl]
87+
})
88+
}
89+
7390
/** 添加/修改操作 */
7491
const formRef = ref()
7592
const openForm = (type: string, id?: number) => {
@@ -83,6 +100,17 @@ const handleDelete = (id: number) => {
83100
84101
/** 初始化 **/
85102
onMounted(() => {
103+
/*
104+
TODO
105+
后面准备封装成一个函数来操作 tableColumns 重新排列:比如说需求是表单上商品选择是在后面的而列表展示的时候需要调到位置。
106+
封装效果支持批量操作,给出 field 和需要插入的位置,例:[{field:'spuId',index: 1}] 效果为把 field 为 spuId 的 column 移动到第一个位置
107+
*/
108+
// 处理一下表格列让商品往前
109+
const index = allSchemas.tableColumns.findIndex((item) => item.field === 'spuId')
110+
const column = cloneDeep(allSchemas.tableColumns[index])
111+
allSchemas.tableColumns.splice(index, 1)
112+
// 添加到开头
113+
allSchemas.tableColumns.unshift(column)
86114
getList()
87115
})
88116
</script>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ const selectSpu = (spuId: number, skuIds: number[]) => {
8787
}
8888
/**
8989
* 获取 SPU 详情
90-
* @param spuIds
9190
*/
9291
const getSpuDetails = async (
9392
spuId: number,
@@ -113,7 +112,7 @@ const getSpuDetails = async (
113112
if (typeof products !== 'undefined') {
114113
const product = products.find((item) => item.skuId === sku.id)
115114
if (product) {
116-
// 元转分
115+
// 分转元
117116
product.seckillPrice = formatToFraction(product.seckillPrice)
118117
}
119118
config = product || config

0 commit comments

Comments
 (0)