mirror of
https://gitee.com/yudaocode/yudao-ui-admin-vben.git
synced 2025-12-30 02:22:25 +00:00
feat:【ele】【mall】seckill 代码对齐 antd
This commit is contained in:
@@ -59,8 +59,8 @@ export function useFormSchema(): VbenFormSchema[] {
|
||||
component: 'DatePicker',
|
||||
componentProps: {
|
||||
placeholder: '请选择活动开始时间',
|
||||
showTime: false,
|
||||
format: 'YYYY-MM-DD',
|
||||
type: 'datetime',
|
||||
format: 'YYYY-MM-DD HH:mm:ss',
|
||||
valueFormat: 'x',
|
||||
class: 'w-full',
|
||||
},
|
||||
@@ -72,8 +72,8 @@ export function useFormSchema(): VbenFormSchema[] {
|
||||
component: 'DatePicker',
|
||||
componentProps: {
|
||||
placeholder: '请选择活动结束时间',
|
||||
showTime: false,
|
||||
format: 'YYYY-MM-DD',
|
||||
type: 'datetime',
|
||||
format: 'YYYY-MM-DD HH:mm:ss',
|
||||
valueFormat: 'x',
|
||||
class: 'w-full',
|
||||
},
|
||||
@@ -85,7 +85,7 @@ export function useFormSchema(): VbenFormSchema[] {
|
||||
component: 'ApiSelect',
|
||||
componentProps: {
|
||||
placeholder: '请选择秒杀时段',
|
||||
mode: 'multiple',
|
||||
multiple: true,
|
||||
api: getSimpleSeckillConfigList,
|
||||
labelField: 'name',
|
||||
valueField: 'id',
|
||||
@@ -137,6 +137,12 @@ export function useFormSchema(): VbenFormSchema[] {
|
||||
},
|
||||
formItemClass: 'col-span-2',
|
||||
},
|
||||
{
|
||||
fieldName: 'spuId',
|
||||
label: '秒杀商品',
|
||||
component: 'Input',
|
||||
formItemClass: 'col-span-2',
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -4,8 +4,7 @@ import type { MallSeckillActivityApi } from '#/api/mall/promotion/seckill/seckil
|
||||
|
||||
import { onMounted } from 'vue';
|
||||
|
||||
import { confirm, DocAlert, Page, useVbenModal } from '@vben/common-ui';
|
||||
import { $t } from '@vben/locales';
|
||||
import { DocAlert, Page, useVbenModal } from '@vben/common-ui';
|
||||
|
||||
import { ElLoading, ElMessage, ElTag } from 'element-plus';
|
||||
|
||||
@@ -16,6 +15,7 @@ import {
|
||||
getSeckillActivityPage,
|
||||
} from '#/api/mall/promotion/seckill/seckillActivity';
|
||||
import { getSimpleSeckillConfigList } from '#/api/mall/promotion/seckill/seckillConfig';
|
||||
import { $t } from '#/locales';
|
||||
|
||||
import { useGridColumns, useGridFormSchema } from './data';
|
||||
import { formatConfigNames, formatTimeRange, setConfigList } from './formatter';
|
||||
@@ -45,10 +45,16 @@ function handleCreate() {
|
||||
|
||||
/** 关闭活动 */
|
||||
async function handleClose(row: MallSeckillActivityApi.SeckillActivity) {
|
||||
await confirm('确认关闭该秒杀活动吗?');
|
||||
await closeSeckillActivity(row.id as number);
|
||||
ElMessage.success('关闭成功');
|
||||
handleRefresh();
|
||||
const loadingInstance = ElLoading.service({
|
||||
text: '活动关闭中...',
|
||||
});
|
||||
try {
|
||||
await closeSeckillActivity(row.id as number);
|
||||
ElMessage.success('关闭成功');
|
||||
handleRefresh();
|
||||
} finally {
|
||||
loadingInstance.close();
|
||||
}
|
||||
}
|
||||
|
||||
/** 删除活动 */
|
||||
@@ -84,10 +90,6 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
||||
},
|
||||
},
|
||||
},
|
||||
// TODO @puhui999:antd 没有,是不是要统一哈。
|
||||
cellConfig: {
|
||||
height: 250,
|
||||
},
|
||||
rowConfig: {
|
||||
keyField: 'id',
|
||||
isHover: true,
|
||||
@@ -165,8 +167,10 @@ onMounted(async () => {
|
||||
link: true,
|
||||
auth: ['promotion:seckill-activity:close'],
|
||||
ifShow: row.status === 0,
|
||||
// TODO @puhui999:antd 这里是 popConfirm,看看要不要统一
|
||||
onClick: handleClose.bind(null, row),
|
||||
popConfirm: {
|
||||
title: '确认关闭该秒杀活动吗?',
|
||||
confirm: handleClose.bind(null, row),
|
||||
},
|
||||
},
|
||||
{
|
||||
label: $t('common.delete'),
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
<script lang="ts" setup>
|
||||
import type { MallSpuApi } from '#/api/mall/product/spu';
|
||||
import type { MallSeckillActivityApi } from '#/api/mall/promotion/seckill/seckillActivity';
|
||||
import type {
|
||||
RuleConfig,
|
||||
SpuProperty,
|
||||
} from '#/views/mall/product/spu/components';
|
||||
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { useVbenModal } from '@vben/common-ui';
|
||||
import { cloneDeep, convertToInteger, formatToFraction } from '@vben/utils';
|
||||
|
||||
import { ElMessage } from 'element-plus';
|
||||
import { ElButton, ElInputNumber, ElMessage } from 'element-plus';
|
||||
|
||||
import { useVbenForm } from '#/adapter/form';
|
||||
import { VxeColumn } from '#/adapter/vxe-table';
|
||||
import { getSpu } from '#/api/mall/product/spu';
|
||||
import {
|
||||
createSeckillActivity,
|
||||
@@ -15,11 +22,18 @@ import {
|
||||
updateSeckillActivity,
|
||||
} from '#/api/mall/promotion/seckill/seckillActivity';
|
||||
import { $t } from '#/locales';
|
||||
import { SpuSkuSelect } from '#/views/mall/product/spu/components';
|
||||
import {
|
||||
getPropertyList,
|
||||
SpuAndSkuList,
|
||||
SpuSkuSelect,
|
||||
} from '#/views/mall/product/spu/components';
|
||||
|
||||
import { useFormSchema } from '../data';
|
||||
|
||||
defineOptions({ name: 'PromotionSeckillActivityForm' });
|
||||
|
||||
const emit = defineEmits(['success']);
|
||||
|
||||
const formData = ref<MallSeckillActivityApi.SeckillActivity>();
|
||||
const getTitle = computed(() => {
|
||||
return formData.value?.id
|
||||
@@ -27,94 +41,140 @@ const getTitle = computed(() => {
|
||||
: $t('ui.actionTitle.create', ['秒杀活动']);
|
||||
});
|
||||
|
||||
// ================= 商品选择相关 =================
|
||||
const spuId = ref<number>();
|
||||
const spuName = ref<string>('');
|
||||
const skuTableData = ref<any[]>([]);
|
||||
|
||||
const spuSkuSelectRef = ref(); // 商品选择弹窗 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 || '';
|
||||
|
||||
// 筛选指定的 SKU
|
||||
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,
|
||||
})) || [];
|
||||
}
|
||||
|
||||
// ================= end =================
|
||||
|
||||
const [Form, formApi] = useVbenForm({
|
||||
commonConfig: {
|
||||
componentProps: {
|
||||
class: 'w-full',
|
||||
},
|
||||
labelWidth: 120,
|
||||
},
|
||||
layout: 'horizontal',
|
||||
schema: useFormSchema(),
|
||||
showDefaultActions: false,
|
||||
wrapperClass: 'grid-cols-2',
|
||||
});
|
||||
|
||||
// ================= 商品选择相关 =================
|
||||
|
||||
const spuSkuSelectRef = ref(); // 商品和属性选择 Ref
|
||||
const spuAndSkuListRef = ref(); // SPU 和 SKU 列表组件 Ref
|
||||
|
||||
const ruleConfig: RuleConfig[] = [
|
||||
{
|
||||
name: 'productConfig.stock',
|
||||
rule: (arg) => arg >= 1,
|
||||
message: '商品秒杀库存必须大于等于 1 !!!',
|
||||
},
|
||||
{
|
||||
name: 'productConfig.seckillPrice',
|
||||
rule: (arg) => arg >= 0.01,
|
||||
message: '商品秒杀价格必须大于等于 0.01 !!!',
|
||||
},
|
||||
];
|
||||
|
||||
const spuList = ref<MallSpuApi.Spu[]>([]); // 选择的 SPU 列表
|
||||
const spuPropertyList = ref<SpuProperty<MallSpuApi.Spu>[]>([]); // SPU 属性列表
|
||||
|
||||
/** 打开商品选择器 */
|
||||
function openSpuSelect() {
|
||||
spuSkuSelectRef.value?.open();
|
||||
}
|
||||
|
||||
/** 选择商品后的回调 */
|
||||
async function handleSpuSelected(spuId: number, skuIds?: number[]) {
|
||||
await formApi.setFieldValue('spuId', spuId);
|
||||
await getSpuDetails(spuId, skuIds);
|
||||
}
|
||||
|
||||
/** 获取 SPU 详情 */
|
||||
async function getSpuDetails(
|
||||
spuId: number,
|
||||
skuIds?: number[],
|
||||
products?: MallSeckillActivityApi.SeckillProduct[],
|
||||
) {
|
||||
const res = await getSpu(spuId);
|
||||
if (!res) {
|
||||
return;
|
||||
}
|
||||
|
||||
spuList.value = [];
|
||||
|
||||
// 筛选指定的 SKU
|
||||
const selectSkus =
|
||||
skuIds === undefined
|
||||
? res.skus
|
||||
: res.skus?.filter((sku) => skuIds.includes(sku.id!));
|
||||
|
||||
// 为每个 SKU 配置秒杀活动相关的配置
|
||||
selectSkus?.forEach((sku) => {
|
||||
let config: MallSeckillActivityApi.SeckillProduct = {
|
||||
spuId: res.id!,
|
||||
skuId: sku.id!,
|
||||
stock: 0,
|
||||
seckillPrice: 0,
|
||||
};
|
||||
// 如果是编辑模式,回填已有配置
|
||||
if (products !== undefined) {
|
||||
const product = products.find((item) => item.skuId === sku.id);
|
||||
if (product) {
|
||||
// 分转元
|
||||
product.seckillPrice = formatToFraction(
|
||||
product.seckillPrice,
|
||||
) as unknown as number;
|
||||
}
|
||||
config = product || config;
|
||||
}
|
||||
// 动态添加 productConfig 属性到 SKU
|
||||
(
|
||||
sku as MallSpuApi.Sku & {
|
||||
productConfig: MallSeckillActivityApi.SeckillProduct;
|
||||
}
|
||||
).productConfig = config;
|
||||
});
|
||||
res.skus = selectSkus;
|
||||
|
||||
const spuProperties: SpuProperty<MallSpuApi.Spu>[] = [
|
||||
{
|
||||
spuId: res.id!,
|
||||
spuDetail: res,
|
||||
propertyList: getPropertyList(res),
|
||||
},
|
||||
];
|
||||
|
||||
// 直接赋值,因为秒杀活动只选择一个 SPU
|
||||
spuList.value = [res];
|
||||
spuPropertyList.value = spuProperties;
|
||||
}
|
||||
|
||||
// ================= end =================
|
||||
|
||||
const [Modal, modalApi] = useVbenModal({
|
||||
async onConfirm() {
|
||||
const { valid } = await formApi.validate();
|
||||
if (!valid) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 验证商品和 SKU 配置
|
||||
if (!spuId.value) {
|
||||
ElMessage.error('请选择秒杀商品');
|
||||
return;
|
||||
}
|
||||
if (skuTableData.value.length === 0) {
|
||||
ElMessage.error('请至少配置一个 SKU');
|
||||
return;
|
||||
}
|
||||
// 验证 SKU 配置
|
||||
const hasInvalidSku = skuTableData.value.some(
|
||||
(sku) => sku.stock < 1 || sku.seckillPrice < 0.01,
|
||||
);
|
||||
if (hasInvalidSku) {
|
||||
ElMessage.error('请正确配置 SKU 的秒杀库存(≥1)和秒杀价格(≥0.01)');
|
||||
return;
|
||||
}
|
||||
|
||||
// 提交表单
|
||||
modalApi.lock();
|
||||
try {
|
||||
// 获取秒杀商品配置(深拷贝避免直接修改原对象)
|
||||
const products: MallSeckillActivityApi.SeckillProduct[] = cloneDeep(
|
||||
spuAndSkuListRef.value?.getSkuConfigs('productConfig') || [],
|
||||
);
|
||||
if (products.length === 0) {
|
||||
ElMessage.error('请选择秒杀商品');
|
||||
return;
|
||||
}
|
||||
// 价格需要转为分
|
||||
products.forEach((item) => {
|
||||
item.seckillPrice = convertToInteger(item.seckillPrice);
|
||||
});
|
||||
|
||||
// 提交表单
|
||||
const values = await formApi.getValues();
|
||||
const data: any = {
|
||||
const data = {
|
||||
...values,
|
||||
spuId: spuId.value,
|
||||
products: skuTableData.value.map((sku) => ({
|
||||
skuId: sku.skuId,
|
||||
stock: sku.stock,
|
||||
seckillPrice: Math.round(sku.seckillPrice * 100), // 转换为分
|
||||
})),
|
||||
};
|
||||
products,
|
||||
} as MallSeckillActivityApi.SeckillActivity;
|
||||
|
||||
await (formData.value?.id
|
||||
? updateSeckillActivity(data)
|
||||
: createSeckillActivity(data));
|
||||
@@ -129,9 +189,8 @@ const [Modal, modalApi] = useVbenModal({
|
||||
async onOpenChange(isOpen: boolean) {
|
||||
if (!isOpen) {
|
||||
formData.value = undefined;
|
||||
spuId.value = undefined;
|
||||
spuName.value = '';
|
||||
skuTableData.value = [];
|
||||
spuList.value = [];
|
||||
spuPropertyList.value = [];
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -140,36 +199,18 @@ const [Modal, modalApi] = useVbenModal({
|
||||
if (!data || !data.id) {
|
||||
return;
|
||||
}
|
||||
// 加载数据
|
||||
modalApi.lock();
|
||||
try {
|
||||
formData.value = await getSeckillActivity(data.id);
|
||||
// TODO @puhui999:这里需要 nextTick 么?因为 antd 有
|
||||
// 对齐活动商品处理结构
|
||||
await getSpuDetails(
|
||||
formData.value.spuId!,
|
||||
formData.value.products?.map((sku) => sku.skuId),
|
||||
formData.value.products,
|
||||
);
|
||||
// 设置表单值
|
||||
await formApi.setValues(formData.value);
|
||||
// 加载商品和 SKU 信息
|
||||
// TODO @puhui999:if return 简化括号层级
|
||||
if (formData.value.spuId) {
|
||||
const spu = await getSpu(formData.value.spuId);
|
||||
if (spu) {
|
||||
spuId.value = spu.id;
|
||||
spuName.value = spu.name || '';
|
||||
// 回填 SKU 配置
|
||||
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();
|
||||
}
|
||||
@@ -178,81 +219,63 @@ const [Modal, modalApi] = useVbenModal({
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Modal class="w-4/5" :title="getTitle">
|
||||
<div class="mx-4">
|
||||
<Form />
|
||||
<div>
|
||||
<Modal :title="getTitle" class="w-[70%]">
|
||||
<Form class="mx-4">
|
||||
<!-- 商品选择 -->
|
||||
<template #spuId>
|
||||
<div class="w-full">
|
||||
<ElButton
|
||||
v-if="!formData?.id"
|
||||
type="primary"
|
||||
@click="openSpuSelect"
|
||||
>
|
||||
选择商品
|
||||
</ElButton>
|
||||
|
||||
<!-- 商品选择区域 -->
|
||||
<div class="mt-4">
|
||||
<div class="mb-2 flex items-center">
|
||||
<span class="text-sm font-medium">秒杀活动商品:</span>
|
||||
<!-- TODO @puhui999:使用 ElButton 这种哈 -->
|
||||
<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>
|
||||
<!-- SPU 和 SKU 列表展示 -->
|
||||
<SpuAndSkuList
|
||||
ref="spuAndSkuListRef"
|
||||
:rule-config="ruleConfig"
|
||||
:spu-list="spuList"
|
||||
:spu-property-list-p="spuPropertyList"
|
||||
class="mt-4"
|
||||
>
|
||||
<!-- 扩展列:秒杀活动特有配置 -->
|
||||
<template #default>
|
||||
<VxeColumn align="center" min-width="168" title="秒杀库存">
|
||||
<template #default="{ row: sku }">
|
||||
<ElInputNumber
|
||||
v-model="sku.productConfig.stock"
|
||||
:max="sku.stock"
|
||||
:min="0"
|
||||
class="w-full"
|
||||
/>
|
||||
</template>
|
||||
</VxeColumn>
|
||||
<VxeColumn align="center" min-width="168" title="秒杀价格(元)">
|
||||
<template #default="{ row: sku }">
|
||||
<ElInputNumber
|
||||
v-model="sku.productConfig.seckillPrice"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
:step="0.1"
|
||||
class="w-full"
|
||||
/>
|
||||
</template>
|
||||
</VxeColumn>
|
||||
</template>
|
||||
</SpuAndSkuList>
|
||||
</div>
|
||||
</template>
|
||||
</Form>
|
||||
</Modal>
|
||||
|
||||
<!-- SKU 配置表格 -->
|
||||
<div v-if="skuTableData.length > 0" class="mt-4">
|
||||
<!-- TODO @puhui999:Grid?或者 VXETable 哇? -->
|
||||
<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"
|
||||
/>
|
||||
<!-- 商品选择器弹窗 -->
|
||||
<SpuSkuSelect
|
||||
ref="spuSkuSelectRef"
|
||||
:is-select-sku="true"
|
||||
@select="handleSpuSelected"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user