2023-10-20 20:51:18 +08:00
|
|
|
|
<template>
|
|
|
|
|
<Dialog v-model="dialogVisible" :title="dialogTitle" width="65%">
|
|
|
|
|
<Form
|
|
|
|
|
ref="formRef"
|
|
|
|
|
v-loading="formLoading"
|
|
|
|
|
:isCol="true"
|
|
|
|
|
:rules="rules"
|
|
|
|
|
:schema="allSchemas.formSchema"
|
|
|
|
|
>
|
|
|
|
|
<!-- 先选择 -->
|
|
|
|
|
<template #spuId>
|
|
|
|
|
<el-button @click="spuSelectRef.open()">选择商品</el-button>
|
|
|
|
|
<SpuAndSkuList
|
|
|
|
|
ref="spuAndSkuListRef"
|
2024-09-08 22:29:35 +08:00
|
|
|
|
:deletable="true"
|
2023-10-20 20:51:18 +08:00
|
|
|
|
:rule-config="ruleConfig"
|
|
|
|
|
:spu-list="spuList"
|
|
|
|
|
:spu-property-list-p="spuPropertyList"
|
2024-08-26 17:28:05 +08:00
|
|
|
|
@delete="deleteSpu"
|
2023-10-20 20:51:18 +08:00
|
|
|
|
>
|
|
|
|
|
<el-table-column align="center" label="优惠金额" min-width="168">
|
2024-09-08 22:29:35 +08:00
|
|
|
|
<template #default="{ row }">
|
|
|
|
|
<el-input-number
|
|
|
|
|
v-model="row.productConfig.discountPrice"
|
|
|
|
|
:max="parseFloat(fenToYuan(row.price))"
|
|
|
|
|
:min="0"
|
|
|
|
|
:precision="2"
|
|
|
|
|
:step="0.1"
|
|
|
|
|
class="w-100%"
|
|
|
|
|
@change="handleSkuDiscountPriceChange(row)"
|
|
|
|
|
/>
|
2023-10-20 20:51:18 +08:00
|
|
|
|
</template>
|
|
|
|
|
</el-table-column>
|
|
|
|
|
<el-table-column align="center" label="折扣百分比(%)" min-width="168">
|
2024-09-08 22:29:35 +08:00
|
|
|
|
<template #default="{ row }">
|
|
|
|
|
<el-input-number
|
|
|
|
|
v-model="row.productConfig.discountPercent"
|
|
|
|
|
:max="100"
|
|
|
|
|
:min="0"
|
|
|
|
|
:precision="2"
|
|
|
|
|
:step="0.1"
|
|
|
|
|
class="w-100%"
|
|
|
|
|
@change="handleSkuDiscountPercentChange(row)"
|
|
|
|
|
/>
|
2023-10-20 20:51:18 +08:00
|
|
|
|
</template>
|
|
|
|
|
</el-table-column>
|
|
|
|
|
</SpuAndSkuList>
|
|
|
|
|
</template>
|
|
|
|
|
</Form>
|
|
|
|
|
<template #footer>
|
|
|
|
|
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
|
|
|
|
|
<el-button @click="dialogVisible = false">取 消</el-button>
|
|
|
|
|
</template>
|
|
|
|
|
</Dialog>
|
|
|
|
|
<SpuSelect ref="spuSelectRef" :isSelectSku="true" @confirm="selectSpu" />
|
|
|
|
|
</template>
|
|
|
|
|
<script lang="ts" setup>
|
|
|
|
|
import { SpuAndSkuList, SpuProperty, SpuSelect } from '../components'
|
|
|
|
|
import { allSchemas, rules } from './discountActivity.data'
|
2024-09-08 22:29:35 +08:00
|
|
|
|
import { cloneDeep, debounce } from 'lodash-es'
|
2023-10-20 20:51:18 +08:00
|
|
|
|
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'
|
2024-09-08 22:29:35 +08:00
|
|
|
|
import { convertToInteger, erpCalculatePercentage, fenToYuan, yuanToFen } from '@/utils'
|
|
|
|
|
import { PromotionDiscountTypeEnum } from '@/utils/constants'
|
2023-10-20 20:51:18 +08:00
|
|
|
|
|
|
|
|
|
defineOptions({ name: 'PromotionDiscountActivityForm' })
|
|
|
|
|
|
|
|
|
|
const { t } = useI18n() // 国际化
|
|
|
|
|
const message = useMessage() // 消息弹窗
|
|
|
|
|
|
|
|
|
|
const dialogVisible = ref(false) // 弹窗的是否展示
|
|
|
|
|
const dialogTitle = ref('') // 弹窗的标题
|
|
|
|
|
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
|
|
|
|
|
const formType = ref('') // 表单的类型:create - 新增;update - 修改
|
|
|
|
|
const formRef = ref() // 表单 Ref
|
|
|
|
|
// ================= 商品选择相关 =================
|
|
|
|
|
|
|
|
|
|
const spuSelectRef = ref() // 商品和属性选择 Ref
|
|
|
|
|
const spuAndSkuListRef = ref() // sku 限时折扣 配置组件Ref
|
2024-09-08 22:40:47 +08:00
|
|
|
|
const ruleConfig: RuleConfig[] = [
|
|
|
|
|
{
|
|
|
|
|
name: 'productConfig.discountPrice',
|
|
|
|
|
rule: (arg) => arg > 0,
|
|
|
|
|
message: '商品优惠金额不能为 0 !!!'
|
|
|
|
|
}
|
|
|
|
|
]
|
2023-10-20 20:51:18 +08:00
|
|
|
|
const spuList = ref<DiscountActivityApi.SpuExtension[]>([]) // 选择的 spu
|
|
|
|
|
const spuPropertyList = ref<SpuProperty<DiscountActivityApi.SpuExtension>[]>([])
|
2024-08-26 22:09:56 +08:00
|
|
|
|
const spuIds = ref<number[]>([])
|
2023-10-20 20:51:18 +08:00
|
|
|
|
const selectSpu = (spuId: number, skuIds: number[]) => {
|
2024-09-08 22:40:47 +08:00
|
|
|
|
// TODO puhui999: 艿艿现在限时折扣活动可以选择多个 spu ,那么 spuId 是不是得改成 spuIds 来存放多个?🤣
|
|
|
|
|
formRef.value.setValues({ spuId })
|
2023-10-20 20:51:18 +08:00
|
|
|
|
getSpuDetails(spuId, skuIds)
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 获取 SPU 详情
|
|
|
|
|
*/
|
|
|
|
|
const getSpuDetails = async (
|
|
|
|
|
spuId: number,
|
|
|
|
|
skuIds: number[] | undefined,
|
2024-08-26 17:28:05 +08:00
|
|
|
|
products?: DiscountActivityApi.DiscountProductVO[],
|
|
|
|
|
type?: string
|
2023-10-20 20:51:18 +08:00
|
|
|
|
) => {
|
2024-08-26 22:09:56 +08:00
|
|
|
|
// 如果已经包含 SPU 则跳过
|
|
|
|
|
if (spuIds.value.includes(spuId)) {
|
|
|
|
|
if (type !== 'load') {
|
|
|
|
|
message.error('数据重复选择!')
|
2024-08-26 17:28:05 +08:00
|
|
|
|
}
|
2024-08-26 22:09:56 +08:00
|
|
|
|
return
|
2024-08-26 17:28:05 +08:00
|
|
|
|
}
|
|
|
|
|
spuIds.value.push(spuId)
|
2023-10-20 20:51:18 +08:00
|
|
|
|
const res = (await ProductSpuApi.getSpuDetailList([spuId])) as DiscountActivityApi.SpuExtension[]
|
|
|
|
|
if (res.length == 0) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-08-26 17:28:05 +08:00
|
|
|
|
//spuList.value = []
|
2023-10-20 20:51:18 +08:00
|
|
|
|
// 因为只能选择一个
|
|
|
|
|
const spu = res[0]
|
|
|
|
|
const selectSkus =
|
|
|
|
|
typeof skuIds === 'undefined' ? spu?.skus : spu?.skus?.filter((sku) => skuIds.includes(sku.id!))
|
|
|
|
|
selectSkus?.forEach((sku) => {
|
|
|
|
|
let config: DiscountActivityApi.DiscountProductVO = {
|
|
|
|
|
skuId: sku.id!,
|
2024-09-08 22:29:35 +08:00
|
|
|
|
spuId: spu.id!,
|
2023-10-20 20:51:18 +08:00
|
|
|
|
discountType: 1,
|
|
|
|
|
discountPercent: 0,
|
|
|
|
|
discountPrice: 0
|
|
|
|
|
}
|
|
|
|
|
if (typeof products !== 'undefined') {
|
|
|
|
|
const product = products.find((item) => item.skuId === sku.id)
|
2024-09-08 22:29:35 +08:00
|
|
|
|
if (product) {
|
|
|
|
|
product.discountPercent = fenToYuan(product.discountPercent) as any
|
|
|
|
|
product.discountPrice = fenToYuan(product.discountPrice) as any
|
|
|
|
|
}
|
2023-10-20 20:51:18 +08:00
|
|
|
|
config = product || config
|
|
|
|
|
}
|
|
|
|
|
sku.productConfig = config
|
|
|
|
|
})
|
|
|
|
|
spu.skus = selectSkus as DiscountActivityApi.SkuExtension[]
|
2024-08-26 17:28:05 +08:00
|
|
|
|
spuPropertyList.value.push({
|
2023-10-20 20:51:18 +08:00
|
|
|
|
spuId: spu.id!,
|
|
|
|
|
spuDetail: spu,
|
|
|
|
|
propertyList: getPropertyList(spu)
|
|
|
|
|
})
|
|
|
|
|
spuList.value.push(spu)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ================= end =================
|
|
|
|
|
|
|
|
|
|
/** 打开弹窗 */
|
|
|
|
|
const open = async (type: string, id?: number) => {
|
|
|
|
|
dialogVisible.value = true
|
|
|
|
|
dialogTitle.value = t('action.' + type)
|
|
|
|
|
formType.value = type
|
|
|
|
|
await resetForm()
|
|
|
|
|
// 修改时,设置数据
|
|
|
|
|
if (id) {
|
|
|
|
|
formLoading.value = true
|
|
|
|
|
try {
|
|
|
|
|
const data = (await DiscountActivityApi.getDiscountActivity(
|
|
|
|
|
id
|
|
|
|
|
)) as DiscountActivityApi.DiscountActivityVO
|
2024-08-26 17:28:05 +08:00
|
|
|
|
for (let productsKey in data.products) {
|
|
|
|
|
const supId = data.products[productsKey].spuId
|
2024-08-26 22:09:56 +08:00
|
|
|
|
await getSpuDetails(
|
|
|
|
|
supId!,
|
|
|
|
|
data.products?.map((sku) => sku.skuId),
|
|
|
|
|
data.products,
|
|
|
|
|
'load'
|
|
|
|
|
)
|
2024-08-26 17:28:05 +08:00
|
|
|
|
}
|
2023-10-20 20:51:18 +08:00
|
|
|
|
formRef.value.setValues(data)
|
|
|
|
|
} finally {
|
|
|
|
|
formLoading.value = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
defineExpose({ open }) // 提供 open 方法,用于打开弹窗
|
|
|
|
|
|
|
|
|
|
/** 提交表单 */
|
|
|
|
|
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
|
|
|
|
|
const submitForm = async () => {
|
|
|
|
|
// 校验表单
|
|
|
|
|
if (!formRef) return
|
|
|
|
|
const valid = await formRef.value.getElFormRef().validate()
|
|
|
|
|
if (!valid) return
|
|
|
|
|
// 提交请求
|
|
|
|
|
formLoading.value = true
|
|
|
|
|
try {
|
2024-08-26 22:09:56 +08:00
|
|
|
|
// 获取折扣商品配置
|
2023-10-20 20:51:18 +08:00
|
|
|
|
const products = cloneDeep(spuAndSkuListRef.value.getSkuConfigs('productConfig'))
|
|
|
|
|
products.forEach((item: DiscountActivityApi.DiscountProductVO) => {
|
2024-09-08 22:29:35 +08:00
|
|
|
|
item.discountPercent = convertToInteger(item.discountPercent)
|
|
|
|
|
item.discountPrice = convertToInteger(item.discountPrice)
|
2023-10-20 20:51:18 +08:00
|
|
|
|
})
|
2024-09-08 22:29:35 +08:00
|
|
|
|
const data = cloneDeep(formRef.value.formModel) as DiscountActivityApi.DiscountActivityVO
|
2023-10-20 20:51:18 +08:00
|
|
|
|
data.products = products
|
|
|
|
|
// 真正提交
|
|
|
|
|
if (formType.value === 'create') {
|
|
|
|
|
await DiscountActivityApi.createDiscountActivity(data)
|
|
|
|
|
message.success(t('common.createSuccess'))
|
|
|
|
|
} else {
|
|
|
|
|
await DiscountActivityApi.updateDiscountActivity(data)
|
|
|
|
|
message.success(t('common.updateSuccess'))
|
|
|
|
|
}
|
|
|
|
|
dialogVisible.value = false
|
|
|
|
|
// 发送操作成功的事件
|
|
|
|
|
emit('success')
|
|
|
|
|
} finally {
|
|
|
|
|
formLoading.value = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-08 22:29:35 +08:00
|
|
|
|
/** 处理 sku 优惠金额变动 */
|
|
|
|
|
const handleSkuDiscountPriceChange = debounce((row: any) => {
|
|
|
|
|
// 校验边界
|
|
|
|
|
if (row.productConfig.discountPrice <= 0) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置优惠类型:满减
|
|
|
|
|
row.productConfig.discountType = PromotionDiscountTypeEnum.PRICE.type
|
|
|
|
|
// 设置折扣
|
|
|
|
|
row.productConfig.discountPercent = erpCalculatePercentage(
|
|
|
|
|
row.price - yuanToFen(row.productConfig.discountPrice),
|
|
|
|
|
row.price
|
|
|
|
|
)
|
|
|
|
|
}, 200)
|
|
|
|
|
/** 处理 sku 优惠折扣变动 */
|
|
|
|
|
const handleSkuDiscountPercentChange = debounce((row: any) => {
|
|
|
|
|
// 校验边界
|
|
|
|
|
if (row.productConfig.discountPercent <= 0 || row.productConfig.discountPercent >= 100) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置优惠类型:折扣
|
|
|
|
|
row.productConfig.discountType = PromotionDiscountTypeEnum.PERCENT.type
|
|
|
|
|
// 设置满减金额
|
|
|
|
|
row.productConfig.discountPrice = fenToYuan(
|
|
|
|
|
row.price - row.price * (row.productConfig.discountPercent / 100.0 || 0)
|
|
|
|
|
)
|
|
|
|
|
}, 200)
|
|
|
|
|
|
2023-10-20 20:51:18 +08:00
|
|
|
|
/** 重置表单 */
|
|
|
|
|
const resetForm = async () => {
|
|
|
|
|
spuList.value = []
|
|
|
|
|
spuPropertyList.value = []
|
2024-08-26 17:28:05 +08:00
|
|
|
|
spuIds.value = []
|
2023-10-20 20:51:18 +08:00
|
|
|
|
await nextTick()
|
|
|
|
|
formRef.value.getElFormRef().resetFields()
|
|
|
|
|
}
|
2024-08-26 17:28:05 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2024-08-26 22:09:56 +08:00
|
|
|
|
* 删除 SPU
|
2024-08-26 17:28:05 +08:00
|
|
|
|
*/
|
|
|
|
|
const deleteSpu = (spuId: number) => {
|
2024-08-26 22:09:56 +08:00
|
|
|
|
spuIds.value.splice(
|
|
|
|
|
spuIds.value.findIndex((item) => item == spuId),
|
|
|
|
|
1
|
|
|
|
|
)
|
|
|
|
|
spuPropertyList.value.splice(
|
|
|
|
|
spuPropertyList.value.findIndex((item) => item.spuId == spuId),
|
|
|
|
|
1
|
|
|
|
|
)
|
2024-08-26 17:28:05 +08:00
|
|
|
|
}
|
2023-10-20 20:51:18 +08:00
|
|
|
|
</script>
|