【代码评审】商城:限时折扣活动

pull/533/head
YunaiV 2024-08-26 22:09:56 +08:00
parent af1bec2a8c
commit c4f7aa760b
2 changed files with 45 additions and 33 deletions

View File

@ -29,15 +29,14 @@
</el-table-column>
<el-table-column align="center" label="销量" min-width="90" prop="salesCount" />
<el-table-column align="center" label="库存" min-width="90" prop="stock" />
<el-table-column v-if="spuData.length > 1 && isDelete" align="center" label="操作" min-width="90" >
<el-table-column
v-if="spuData.length > 1 && isDelete"
align="center"
label="操作"
min-width="90"
>
<template #default="scope">
<el-button
type="primary"
link
@click="deleteSpu(scope.row.id)"
>
删除
</el-button>
<el-button type="primary" link @click="deleteSpu(scope.row.id)"> </el-button>
</template>
</el-table-column>
</el-table>
@ -57,7 +56,7 @@ const props = defineProps<{
spuList: T[]
ruleConfig: RuleConfig[]
spuPropertyListP: SpuProperty<T>[]
isDelete?: boolean //spu
isDelete?: boolean // SPU TODO deletable
}>()
const spuData = ref<Spu[]>([]) // spu
@ -96,12 +95,12 @@ const emits = defineEmits<{
(e: 'delete', spuId: number): void
}>()
/** 多选时可以删除spu **/
/** 多选时可以删除 SPU **/
const deleteSpu = async (spuId: number) => {
await message.confirm('是否删除商品编号为' + spuId + '的数据?')
let index = spuData.value.findIndex((item) => item.id == spuId)
spuData.value.splice(index,1);
emits('delete',spuId)
const index = spuData.value.findIndex((item) => item.id == spuId)
spuData.value.splice(index, 1)
emits('delete', spuId)
}
/**

View File

@ -49,7 +49,7 @@ import { cloneDeep } from 'lodash-es'
import * as DiscountActivityApi from '@/api/mall/promotion/discount/discountActivity'
import * as ProductSpuApi from '@/api/mall/product/spu'
import { getPropertyList, RuleConfig } from '@/views/mall/product/spu/components'
import {formatToFraction} from "@/utils";
import { formatToFraction } from '@/utils'
defineOptions({ name: 'PromotionDiscountActivityForm' })
@ -68,7 +68,7 @@ const spuAndSkuListRef = ref() // sku 限时折扣 配置组件Ref
const ruleConfig: RuleConfig[] = []
const spuList = ref<DiscountActivityApi.SpuExtension[]>([]) // spu
const spuPropertyList = ref<SpuProperty<DiscountActivityApi.SpuExtension>[]>([])
const spuIds = ref<number[]>([]);
const spuIds = ref<number[]>([])
const selectSpu = (spuId: number, skuIds: number[]) => {
getSpuDetails(spuId, skuIds)
}
@ -81,12 +81,12 @@ const getSpuDetails = async (
products?: DiscountActivityApi.DiscountProductVO[],
type?: string
) => {
//spu
if(spuIds.value.includes(spuId)){
if(type !== "load"){
message.error("数据重复选择!")
// SPU
if (spuIds.value.includes(spuId)) {
if (type !== 'load') {
message.error('数据重复选择!')
}
return;
return
}
spuIds.value.push(spuId)
const res = (await ProductSpuApi.getSpuDetailList([spuId])) as DiscountActivityApi.SpuExtension[]
@ -143,7 +143,12 @@ const open = async (type: string, id?: number) => {
)) as DiscountActivityApi.DiscountActivityVO
for (let productsKey in data.products) {
const supId = data.products[productsKey].spuId
await getSpuDetails(supId!, data.products?.map((sku) => sku.skuId), data.products,"load")
await getSpuDetails(
supId!,
data.products?.map((sku) => sku.skuId),
data.products,
'load'
)
}
formRef.value.setValues(data)
} finally {
@ -164,21 +169,23 @@ const submitForm = async () => {
formLoading.value = true
try {
const data = formRef.value.formModel as DiscountActivityApi.DiscountActivityVO
//
//
const products = cloneDeep(spuAndSkuListRef.value.getSkuConfigs('productConfig'))
let timp = false;
//
// TODO @puhui999 youzan
let discountInvalid = false
products.forEach((item: DiscountActivityApi.DiscountProductVO) => {
if(item.discountPrice != null && item.discountPrice > 0){
if (item.discountPrice != null && item.discountPrice > 0) {
item.discountType = 1
}else if(item.discountPercent != null && item.discountPercent > 0){
} else if (item.discountPercent != null && item.discountPercent > 0) {
item.discountType = 2
}else{
timp = true
} else {
discountInvalid = true
}
})
if(timp){
message.error("优惠金额和折扣百分比需要填写一个");
return;
if (discountInvalid) {
message.error('优惠金额和折扣百分比需要填写一个')
return
}
data.products = products
//
@ -207,10 +214,16 @@ const resetForm = async () => {
}
/**
* 删除spu
* 删除 SPU
*/
const deleteSpu = (spuId: number) => {
spuIds.value.splice(spuIds.value.findIndex((item) => item == spuId), 1)
spuPropertyList.value.splice(spuPropertyList.value.findIndex((item) => item.spuId == spuId), 1)
spuIds.value.splice(
spuIds.value.findIndex((item) => item == spuId),
1
)
spuPropertyList.value.splice(
spuPropertyList.value.findIndex((item) => item.spuId == spuId),
1
)
}
</script>