feat:【ele】【mall】seckill 代码迁移
This commit is contained in:
@@ -4,6 +4,117 @@ import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
|||||||
import { DICT_TYPE } from '@vben/constants';
|
import { DICT_TYPE } from '@vben/constants';
|
||||||
import { getDictOptions } from '@vben/hooks';
|
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[] {
|
export function useGridFormSchema(): VbenFormSchema[] {
|
||||||
return [
|
return [
|
||||||
|
|||||||
@@ -8,12 +8,16 @@ import { useVbenModal } from '@vben/common-ui';
|
|||||||
import { ElMessage } from 'element-plus';
|
import { ElMessage } from 'element-plus';
|
||||||
|
|
||||||
import { useVbenForm } from '#/adapter/form';
|
import { useVbenForm } from '#/adapter/form';
|
||||||
|
import { getSpu } from '#/api/mall/product/spu';
|
||||||
import {
|
import {
|
||||||
createSeckillActivity,
|
createSeckillActivity,
|
||||||
getSeckillActivity,
|
getSeckillActivity,
|
||||||
updateSeckillActivity,
|
updateSeckillActivity,
|
||||||
} from '#/api/mall/promotion/seckill/seckillActivity';
|
} from '#/api/mall/promotion/seckill/seckillActivity';
|
||||||
import { $t } from '#/locales';
|
import { $t } from '#/locales';
|
||||||
|
import { SpuSkuSelect } from '#/views/mall/product/spu/components';
|
||||||
|
|
||||||
|
import { useFormSchema } from '../data';
|
||||||
|
|
||||||
const emit = defineEmits(['success']);
|
const emit = defineEmits(['success']);
|
||||||
const formData = ref<MallSeckillActivityApi.SeckillActivity>();
|
const formData = ref<MallSeckillActivityApi.SeckillActivity>();
|
||||||
@@ -23,51 +27,47 @@ const getTitle = computed(() => {
|
|||||||
: $t('ui.actionTitle.create', ['秒杀活动']);
|
: $t('ui.actionTitle.create', ['秒杀活动']);
|
||||||
});
|
});
|
||||||
|
|
||||||
// 简化的表单配置,实际项目中应该有完整的字段配置
|
const spuId = ref<number>();
|
||||||
const formSchema = [
|
const spuName = ref<string>('');
|
||||||
{
|
const skuTableData = ref<any[]>([]);
|
||||||
fieldName: 'id',
|
const spuSkuSelectRef = ref();
|
||||||
component: 'Input',
|
|
||||||
dependencies: {
|
const handleSelectProduct = () => {
|
||||||
triggerFields: [''],
|
spuSkuSelectRef.value?.open();
|
||||||
show: () => false,
|
};
|
||||||
},
|
|
||||||
},
|
async function handleSpuSelected(selectedSpuId: number, skuIds?: number[]) {
|
||||||
{
|
const spu = await getSpu(selectedSpuId);
|
||||||
fieldName: 'name',
|
if (!spu) return;
|
||||||
label: '活动名称',
|
|
||||||
component: 'Input',
|
spuId.value = spu.id;
|
||||||
componentProps: {
|
spuName.value = spu.name || '';
|
||||||
placeholder: '请输入活动名称',
|
|
||||||
},
|
const selectedSkus = skuIds
|
||||||
rules: 'required',
|
? spu.skus?.filter((sku) => skuIds.includes(sku.id!))
|
||||||
},
|
: spu.skus;
|
||||||
{
|
|
||||||
fieldName: 'status',
|
skuTableData.value =
|
||||||
label: '活动状态',
|
selectedSkus?.map((sku) => ({
|
||||||
component: 'Select',
|
skuId: sku.id!,
|
||||||
componentProps: {
|
skuName: sku.name || '',
|
||||||
placeholder: '请选择活动状态',
|
picUrl: sku.picUrl || spu.picUrl || '',
|
||||||
options: [
|
price: sku.price || 0,
|
||||||
{ label: '开启', value: 0 },
|
stock: 0,
|
||||||
{ label: '关闭', value: 1 },
|
seckillPrice: 0,
|
||||||
],
|
})) || [];
|
||||||
},
|
}
|
||||||
rules: 'required',
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const [Form, formApi] = useVbenForm({
|
const [Form, formApi] = useVbenForm({
|
||||||
commonConfig: {
|
commonConfig: {
|
||||||
componentProps: {
|
componentProps: {
|
||||||
class: 'w-full',
|
class: 'w-full',
|
||||||
},
|
},
|
||||||
formItemClass: 'col-span-2',
|
|
||||||
labelWidth: 80,
|
|
||||||
},
|
},
|
||||||
layout: 'horizontal',
|
layout: 'horizontal',
|
||||||
schema: formSchema,
|
schema: useFormSchema(),
|
||||||
showDefaultActions: false,
|
showDefaultActions: false,
|
||||||
|
wrapperClass: 'grid-cols-2',
|
||||||
});
|
});
|
||||||
|
|
||||||
const [Modal, modalApi] = useVbenModal({
|
const [Modal, modalApi] = useVbenModal({
|
||||||
@@ -76,15 +76,38 @@ const [Modal, modalApi] = useVbenModal({
|
|||||||
if (!valid) {
|
if (!valid) {
|
||||||
return;
|
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();
|
modalApi.lock();
|
||||||
// 提交表单
|
|
||||||
const data =
|
|
||||||
(await formApi.getValues()) as MallSeckillActivityApi.SeckillActivity;
|
|
||||||
try {
|
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
|
await (formData.value?.id
|
||||||
? updateSeckillActivity(data)
|
? updateSeckillActivity(data)
|
||||||
: createSeckillActivity(data));
|
: createSeckillActivity(data));
|
||||||
// 关闭并提示
|
|
||||||
await modalApi.close();
|
await modalApi.close();
|
||||||
emit('success');
|
emit('success');
|
||||||
ElMessage.success($t('ui.actionMessage.operationSuccess'));
|
ElMessage.success($t('ui.actionMessage.operationSuccess'));
|
||||||
@@ -95,9 +118,11 @@ const [Modal, modalApi] = useVbenModal({
|
|||||||
async onOpenChange(isOpen: boolean) {
|
async onOpenChange(isOpen: boolean) {
|
||||||
if (!isOpen) {
|
if (!isOpen) {
|
||||||
formData.value = undefined;
|
formData.value = undefined;
|
||||||
|
spuId.value = undefined;
|
||||||
|
spuName.value = '';
|
||||||
|
skuTableData.value = [];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 加载数据
|
|
||||||
const data = modalApi.getData<MallSeckillActivityApi.SeckillActivity>();
|
const data = modalApi.getData<MallSeckillActivityApi.SeckillActivity>();
|
||||||
if (!data || !data.id) {
|
if (!data || !data.id) {
|
||||||
return;
|
return;
|
||||||
@@ -105,8 +130,29 @@ const [Modal, modalApi] = useVbenModal({
|
|||||||
modalApi.lock();
|
modalApi.lock();
|
||||||
try {
|
try {
|
||||||
formData.value = await getSeckillActivity(data.id);
|
formData.value = await getSeckillActivity(data.id);
|
||||||
// 设置到 values
|
|
||||||
await formApi.setValues(formData.value);
|
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 {
|
} finally {
|
||||||
modalApi.unlock();
|
modalApi.unlock();
|
||||||
}
|
}
|
||||||
@@ -115,7 +161,73 @@ const [Modal, modalApi] = useVbenModal({
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<Modal class="w-2/5" :title="getTitle">
|
<Modal class="w-4/5" :title="getTitle">
|
||||||
<Form class="mx-4" />
|
<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>
|
</Modal>
|
||||||
|
<SpuSkuSelect
|
||||||
|
ref="spuSkuSelectRef"
|
||||||
|
:is-select-sku="true"
|
||||||
|
@select="handleSpuSelected"
|
||||||
|
/>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
Reference in New Issue
Block a user