feat:【ele】【mall】seckill 代码迁移

This commit is contained in:
puhui999
2025-12-15 16:16:50 +08:00
parent 5417b19a8b
commit e8526674c5
2 changed files with 267 additions and 44 deletions

View File

@@ -4,6 +4,117 @@ import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { z } from '#/adapter/form';
import { getSimpleSeckillConfigList } from '#/api/mall/promotion/seckill/seckillConfig';
/** 新增/编辑的表单 */
export function useFormSchema(): VbenFormSchema[] {
return [
{
component: 'Input',
fieldName: 'id',
dependencies: {
triggerFields: [''],
show: () => false,
},
},
{
fieldName: 'name',
label: '秒杀活动名称',
component: 'Input',
componentProps: {
placeholder: '请输入活动名称',
},
rules: 'required',
formItemClass: 'col-span-2',
},
{
fieldName: 'startTime',
label: '活动开始时间',
component: 'DatePicker',
componentProps: {
placeholder: '请选择活动开始时间',
showTime: false,
format: 'YYYY-MM-DD',
valueFormat: 'x',
class: 'w-full',
},
rules: 'required',
},
{
fieldName: 'endTime',
label: '活动结束时间',
component: 'DatePicker',
componentProps: {
placeholder: '请选择活动结束时间',
showTime: false,
format: 'YYYY-MM-DD',
valueFormat: 'x',
class: 'w-full',
},
rules: 'required',
},
{
fieldName: 'configIds',
label: '秒杀时段',
component: 'ApiSelect',
componentProps: {
placeholder: '请选择秒杀时段',
mode: 'multiple',
api: getSimpleSeckillConfigList,
labelField: 'name',
valueField: 'id',
class: 'w-full',
},
rules: 'required',
formItemClass: 'col-span-2',
},
{
fieldName: 'totalLimitCount',
label: '总限购数量',
component: 'InputNumber',
componentProps: {
placeholder: '请输入总限购数量',
min: 0,
class: 'w-full',
},
rules: z.number().min(0).default(0),
},
{
fieldName: 'singleLimitCount',
label: '单次限购数量',
component: 'InputNumber',
componentProps: {
placeholder: '请输入单次限购数量',
min: 0,
class: 'w-full',
},
rules: z.number().min(0).default(0),
},
{
fieldName: 'sort',
label: '排序',
component: 'InputNumber',
componentProps: {
placeholder: '请输入排序',
min: 0,
class: 'w-full',
},
rules: z.number().min(0).default(0),
},
{
fieldName: 'remark',
label: '备注',
component: 'Textarea',
componentProps: {
placeholder: '请输入备注',
rows: 4,
},
formItemClass: 'col-span-2',
},
];
}
/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
return [

View File

@@ -8,12 +8,16 @@ import { useVbenModal } from '@vben/common-ui';
import { ElMessage } from 'element-plus';
import { useVbenForm } from '#/adapter/form';
import { getSpu } from '#/api/mall/product/spu';
import {
createSeckillActivity,
getSeckillActivity,
updateSeckillActivity,
} from '#/api/mall/promotion/seckill/seckillActivity';
import { $t } from '#/locales';
import { SpuSkuSelect } from '#/views/mall/product/spu/components';
import { useFormSchema } from '../data';
const emit = defineEmits(['success']);
const formData = ref<MallSeckillActivityApi.SeckillActivity>();
@@ -23,51 +27,47 @@ const getTitle = computed(() => {
: $t('ui.actionTitle.create', ['秒杀活动']);
});
// 简化的表单配置,实际项目中应该有完整的字段配置
const formSchema = [
{
fieldName: 'id',
component: 'Input',
dependencies: {
triggerFields: [''],
show: () => false,
},
},
{
fieldName: 'name',
label: '活动名称',
component: 'Input',
componentProps: {
placeholder: '请输入活动名称',
},
rules: 'required',
},
{
fieldName: 'status',
label: '活动状态',
component: 'Select',
componentProps: {
placeholder: '请选择活动状态',
options: [
{ label: '开启', value: 0 },
{ label: '关闭', value: 1 },
],
},
rules: 'required',
},
];
const spuId = ref<number>();
const spuName = ref<string>('');
const skuTableData = ref<any[]>([]);
const spuSkuSelectRef = ref();
const handleSelectProduct = () => {
spuSkuSelectRef.value?.open();
};
async function handleSpuSelected(selectedSpuId: number, skuIds?: number[]) {
const spu = await getSpu(selectedSpuId);
if (!spu) return;
spuId.value = spu.id;
spuName.value = spu.name || '';
const selectedSkus = skuIds
? spu.skus?.filter((sku) => skuIds.includes(sku.id!))
: spu.skus;
skuTableData.value =
selectedSkus?.map((sku) => ({
skuId: sku.id!,
skuName: sku.name || '',
picUrl: sku.picUrl || spu.picUrl || '',
price: sku.price || 0,
stock: 0,
seckillPrice: 0,
})) || [];
}
const [Form, formApi] = useVbenForm({
commonConfig: {
componentProps: {
class: 'w-full',
},
formItemClass: 'col-span-2',
labelWidth: 80,
},
layout: 'horizontal',
schema: formSchema,
schema: useFormSchema(),
showDefaultActions: false,
wrapperClass: 'grid-cols-2',
});
const [Modal, modalApi] = useVbenModal({
@@ -76,15 +76,38 @@ const [Modal, modalApi] = useVbenModal({
if (!valid) {
return;
}
if (!spuId.value) {
ElMessage.error('请选择秒杀商品');
return;
}
if (skuTableData.value.length === 0) {
ElMessage.error('请至少配置一个 SKU');
return;
}
const hasInvalidSku = skuTableData.value.some(
(sku) => sku.stock < 1 || sku.seckillPrice < 0.01,
);
if (hasInvalidSku) {
ElMessage.error('请正确配置 SKU 的秒杀库存≥1和秒杀价格≥0.01');
return;
}
modalApi.lock();
// 提交表单
const data =
(await formApi.getValues()) as MallSeckillActivityApi.SeckillActivity;
try {
const values = await formApi.getValues();
const data: any = {
...values,
spuId: spuId.value,
products: skuTableData.value.map((sku) => ({
skuId: sku.skuId,
stock: sku.stock,
seckillPrice: Math.round(sku.seckillPrice * 100),
})),
};
await (formData.value?.id
? updateSeckillActivity(data)
: createSeckillActivity(data));
// 关闭并提示
await modalApi.close();
emit('success');
ElMessage.success($t('ui.actionMessage.operationSuccess'));
@@ -95,9 +118,11 @@ const [Modal, modalApi] = useVbenModal({
async onOpenChange(isOpen: boolean) {
if (!isOpen) {
formData.value = undefined;
spuId.value = undefined;
spuName.value = '';
skuTableData.value = [];
return;
}
// 加载数据
const data = modalApi.getData<MallSeckillActivityApi.SeckillActivity>();
if (!data || !data.id) {
return;
@@ -105,8 +130,29 @@ const [Modal, modalApi] = useVbenModal({
modalApi.lock();
try {
formData.value = await getSeckillActivity(data.id);
// 设置到 values
await formApi.setValues(formData.value);
if (formData.value.spuId) {
const spu = await getSpu(formData.value.spuId);
if (spu) {
spuId.value = spu.id;
spuName.value = spu.name || '';
const products = formData.value.products || [];
skuTableData.value =
spu.skus
?.filter((sku) => products.some((p) => p.skuId === sku.id))
.map((sku) => {
const product = products.find((p) => p.skuId === sku.id);
return {
skuId: sku.id!,
skuName: sku.name || '',
picUrl: sku.picUrl || spu.picUrl || '',
price: sku.price || 0,
stock: product?.stock || 0,
seckillPrice: (product?.seckillPrice || 0) / 100,
};
}) || [];
}
}
} finally {
modalApi.unlock();
}
@@ -115,7 +161,73 @@ const [Modal, modalApi] = useVbenModal({
</script>
<template>
<Modal class="w-2/5" :title="getTitle">
<Form class="mx-4" />
<Modal class="w-4/5" :title="getTitle">
<div class="mx-4">
<Form />
<div class="mt-4">
<div class="mb-2 flex items-center">
<span class="text-sm font-medium">秒杀活动商品:</span>
<el-button class="ml-2" type="primary" @click="handleSelectProduct">
选择商品
</el-button>
<span v-if="spuName" class="ml-4 text-sm text-gray-600">
已选择: {{ spuName }}
</span>
</div>
<div v-if="skuTableData.length > 0" class="mt-4">
<table class="w-full border-collapse border border-gray-300">
<thead>
<tr class="bg-gray-100">
<th class="border border-gray-300 px-4 py-2">商品图片</th>
<th class="border border-gray-300 px-4 py-2">SKU 名称</th>
<th class="border border-gray-300 px-4 py-2">原价()</th>
<th class="border border-gray-300 px-4 py-2">秒杀库存</th>
<th class="border border-gray-300 px-4 py-2">秒杀价格()</th>
</tr>
</thead>
<tbody>
<tr v-for="(sku, index) in skuTableData" :key="index">
<td class="border border-gray-300 px-4 py-2 text-center">
<img
v-if="sku.picUrl"
:src="sku.picUrl"
alt="商品图片"
class="h-16 w-16 object-cover"
/>
</td>
<td class="border border-gray-300 px-4 py-2">
{{ sku.skuName }}
</td>
<td class="border border-gray-300 px-4 py-2 text-center">
¥{{ (sku.price / 100).toFixed(2) }}
</td>
<td class="border border-gray-300 px-4 py-2">
<input
v-model.number="sku.stock"
type="number"
min="0"
class="w-full rounded border border-gray-300 px-2 py-1"
/>
</td>
<td class="border border-gray-300 px-4 py-2">
<input
v-model.number="sku.seckillPrice"
type="number"
min="0"
step="0.01"
class="w-full rounded border border-gray-300 px-2 py-1"
/>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</Modal>
<SpuSkuSelect
ref="spuSkuSelectRef"
:is-select-sku="true"
@select="handleSpuSelected"
/>
</template>