|
| 1 | +<template> |
| 2 | + <Dialog v-model="dialogVisible" :title="dialogTitle" width="65%"> |
| 3 | + <Form |
| 4 | + ref="formRef" |
| 5 | + v-loading="formLoading" |
| 6 | + :isCol="true" |
| 7 | + :rules="rules" |
| 8 | + :schema="allSchemas.formSchema" |
| 9 | + > |
| 10 | + <!-- 先选择 --> |
| 11 | + <template #spuId> |
| 12 | + <el-button @click="spuSelectRef.open()">选择商品</el-button> |
| 13 | + <SpuAndSkuList |
| 14 | + ref="spuAndSkuListRef" |
| 15 | + :rule-config="ruleConfig" |
| 16 | + :spu-list="spuList" |
| 17 | + :spu-property-list-p="spuPropertyList" |
| 18 | + > |
| 19 | + <el-table-column align="center" label="优惠金额" min-width="168"> |
| 20 | + <template #default="{ row: sku }"> |
| 21 | + <el-input-number v-model="sku.productConfig.discountPrice" :min="0" class="w-100%" /> |
| 22 | + </template> |
| 23 | + </el-table-column> |
| 24 | + <el-table-column align="center" label="折扣百分比(%)" min-width="168"> |
| 25 | + <template #default="{ row: sku }"> |
| 26 | + <el-input-number v-model="sku.productConfig.discountPercent" class="w-100%" /> |
| 27 | + </template> |
| 28 | + </el-table-column> |
| 29 | + </SpuAndSkuList> |
| 30 | + </template> |
| 31 | + </Form> |
| 32 | + <template #footer> |
| 33 | + <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button> |
| 34 | + <el-button @click="dialogVisible = false">取 消</el-button> |
| 35 | + </template> |
| 36 | + </Dialog> |
| 37 | + <SpuSelect ref="spuSelectRef" :isSelectSku="true" @confirm="selectSpu" /> |
| 38 | +</template> |
| 39 | +<script lang="ts" setup> |
| 40 | +import { SpuAndSkuList, SpuProperty, SpuSelect } from '../components' |
| 41 | +import { allSchemas, rules } from './discountActivity.data' |
| 42 | +import { cloneDeep } from 'lodash-es' |
| 43 | +import * as DiscountActivityApi from '@/api/mall/promotion/discount/discountActivity' |
| 44 | +import * as ProductSpuApi from '@/api/mall/product/spu' |
| 45 | +import { getPropertyList, RuleConfig } from '@/views/mall/product/spu/components' |
| 46 | +
|
| 47 | +defineOptions({ name: 'PromotionDiscountActivityForm' }) |
| 48 | +
|
| 49 | +const { t } = useI18n() // 国际化 |
| 50 | +const message = useMessage() // 消息弹窗 |
| 51 | +
|
| 52 | +const dialogVisible = ref(false) // 弹窗的是否展示 |
| 53 | +const dialogTitle = ref('') // 弹窗的标题 |
| 54 | +const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
| 55 | +const formType = ref('') // 表单的类型:create - 新增;update - 修改 |
| 56 | +const formRef = ref() // 表单 Ref |
| 57 | +// ================= 商品选择相关 ================= |
| 58 | +
|
| 59 | +const spuSelectRef = ref() // 商品和属性选择 Ref |
| 60 | +const spuAndSkuListRef = ref() // sku 限时折扣 配置组件Ref |
| 61 | +const ruleConfig: RuleConfig[] = [] |
| 62 | +const spuList = ref<DiscountActivityApi.SpuExtension[]>([]) // 选择的 spu |
| 63 | +const spuPropertyList = ref<SpuProperty<DiscountActivityApi.SpuExtension>[]>([]) |
| 64 | +const selectSpu = (spuId: number, skuIds: number[]) => { |
| 65 | + formRef.value.setValues({ spuId }) |
| 66 | + getSpuDetails(spuId, skuIds) |
| 67 | +} |
| 68 | +/** |
| 69 | + * 获取 SPU 详情 |
| 70 | + */ |
| 71 | +const getSpuDetails = async ( |
| 72 | + spuId: number, |
| 73 | + skuIds: number[] | undefined, |
| 74 | + products?: DiscountActivityApi.DiscountProductVO[] |
| 75 | +) => { |
| 76 | + const spuProperties: SpuProperty<DiscountActivityApi.SpuExtension>[] = [] |
| 77 | + const res = (await ProductSpuApi.getSpuDetailList([spuId])) as DiscountActivityApi.SpuExtension[] |
| 78 | + if (res.length == 0) { |
| 79 | + return |
| 80 | + } |
| 81 | + spuList.value = [] |
| 82 | + // 因为只能选择一个 |
| 83 | + const spu = res[0] |
| 84 | + const selectSkus = |
| 85 | + typeof skuIds === 'undefined' ? spu?.skus : spu?.skus?.filter((sku) => skuIds.includes(sku.id!)) |
| 86 | + selectSkus?.forEach((sku) => { |
| 87 | + let config: DiscountActivityApi.DiscountProductVO = { |
| 88 | + skuId: sku.id!, |
| 89 | + spuId: spu.id, |
| 90 | + discountType: 1, |
| 91 | + discountPercent: 0, |
| 92 | + discountPrice: 0 |
| 93 | + } |
| 94 | + if (typeof products !== 'undefined') { |
| 95 | + const product = products.find((item) => item.skuId === sku.id) |
| 96 | + config = product || config |
| 97 | + } |
| 98 | + sku.productConfig = config |
| 99 | + }) |
| 100 | + spu.skus = selectSkus as DiscountActivityApi.SkuExtension[] |
| 101 | + spuProperties.push({ |
| 102 | + spuId: spu.id!, |
| 103 | + spuDetail: spu, |
| 104 | + propertyList: getPropertyList(spu) |
| 105 | + }) |
| 106 | + spuList.value.push(spu) |
| 107 | + spuPropertyList.value = spuProperties |
| 108 | +} |
| 109 | +
|
| 110 | +// ================= end ================= |
| 111 | +
|
| 112 | +/** 打开弹窗 */ |
| 113 | +const open = async (type: string, id?: number) => { |
| 114 | + dialogVisible.value = true |
| 115 | + dialogTitle.value = t('action.' + type) |
| 116 | + formType.value = type |
| 117 | + await resetForm() |
| 118 | + // 修改时,设置数据 |
| 119 | + if (id) { |
| 120 | + formLoading.value = true |
| 121 | + try { |
| 122 | + const data = (await DiscountActivityApi.getDiscountActivity( |
| 123 | + id |
| 124 | + )) as DiscountActivityApi.DiscountActivityVO |
| 125 | + const supId = data.products[0].spuId |
| 126 | + await getSpuDetails(supId!, data.products?.map((sku) => sku.skuId), data.products) |
| 127 | + formRef.value.setValues(data) |
| 128 | + } finally { |
| 129 | + formLoading.value = false |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | +defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
| 134 | +
|
| 135 | +/** 提交表单 */ |
| 136 | +const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
| 137 | +const submitForm = async () => { |
| 138 | + // 校验表单 |
| 139 | + if (!formRef) return |
| 140 | + const valid = await formRef.value.getElFormRef().validate() |
| 141 | + if (!valid) return |
| 142 | + // 提交请求 |
| 143 | + formLoading.value = true |
| 144 | + try { |
| 145 | + const data = formRef.value.formModel as DiscountActivityApi.DiscountActivityVO |
| 146 | + // 获取 折扣商品配置 |
| 147 | + const products = cloneDeep(spuAndSkuListRef.value.getSkuConfigs('productConfig')) |
| 148 | + products.forEach((item: DiscountActivityApi.DiscountProductVO) => { |
| 149 | + item.discountType = data['discountType'] |
| 150 | + }) |
| 151 | + data.products = products |
| 152 | + // 真正提交 |
| 153 | + if (formType.value === 'create') { |
| 154 | + await DiscountActivityApi.createDiscountActivity(data) |
| 155 | + message.success(t('common.createSuccess')) |
| 156 | + } else { |
| 157 | + await DiscountActivityApi.updateDiscountActivity(data) |
| 158 | + message.success(t('common.updateSuccess')) |
| 159 | + } |
| 160 | + dialogVisible.value = false |
| 161 | + // 发送操作成功的事件 |
| 162 | + emit('success') |
| 163 | + } finally { |
| 164 | + formLoading.value = false |
| 165 | + } |
| 166 | +} |
| 167 | +
|
| 168 | +/** 重置表单 */ |
| 169 | +const resetForm = async () => { |
| 170 | + spuList.value = [] |
| 171 | + spuPropertyList.value = [] |
| 172 | + await nextTick() |
| 173 | + formRef.value.getElFormRef().resetFields() |
| 174 | +} |
| 175 | +</script> |
0 commit comments