feat: mall seckill activity
parent
a4f0a48bfe
commit
df396f3441
|
@ -0,0 +1,126 @@
|
||||||
|
import type { VbenFormSchema } from '#/adapter/form';
|
||||||
|
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
||||||
|
|
||||||
|
import { DICT_TYPE, getDictOptions } from '#/utils';
|
||||||
|
|
||||||
|
/** 列表的搜索表单 */
|
||||||
|
export function useGridFormSchema(): VbenFormSchema[] {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
fieldName: 'name',
|
||||||
|
label: '活动名称',
|
||||||
|
component: 'Input',
|
||||||
|
componentProps: {
|
||||||
|
placeholder: '请输入活动名称',
|
||||||
|
clearable: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
fieldName: 'status',
|
||||||
|
label: '活动状态',
|
||||||
|
component: 'Select',
|
||||||
|
componentProps: {
|
||||||
|
placeholder: '请选择活动状态',
|
||||||
|
clearable: true,
|
||||||
|
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 列表的字段 */
|
||||||
|
export function useGridColumns(): VxeTableGridOptions['columns'] {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
field: 'id',
|
||||||
|
title: '活动编号',
|
||||||
|
minWidth: 80,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'name',
|
||||||
|
title: '活动名称',
|
||||||
|
minWidth: 140,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'configIds',
|
||||||
|
title: '秒杀时段',
|
||||||
|
width: 220,
|
||||||
|
slots: { default: 'configIds' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'startTime',
|
||||||
|
title: '活动时间',
|
||||||
|
minWidth: 210,
|
||||||
|
slots: { default: 'timeRange' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'picUrl',
|
||||||
|
title: '商品图片',
|
||||||
|
minWidth: 80,
|
||||||
|
cellRender: {
|
||||||
|
name: 'CellImage',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'spuName',
|
||||||
|
title: '商品标题',
|
||||||
|
minWidth: 300,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'marketPrice',
|
||||||
|
title: '原价',
|
||||||
|
minWidth: 100,
|
||||||
|
formatter: ({ row }) => `¥${(row.marketPrice / 100).toFixed(2)}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'seckillPrice',
|
||||||
|
title: '秒杀价',
|
||||||
|
minWidth: 100,
|
||||||
|
formatter: ({ row }) => {
|
||||||
|
if (!(row.products || row.products.length === 0)) {
|
||||||
|
return '¥0.00';
|
||||||
|
}
|
||||||
|
const seckillPrice = Math.min(
|
||||||
|
...row.products.map((item: any) => item.seckillPrice),
|
||||||
|
);
|
||||||
|
return `¥${(seckillPrice / 100).toFixed(2)}`;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'status',
|
||||||
|
title: '活动状态',
|
||||||
|
align: 'center',
|
||||||
|
minWidth: 100,
|
||||||
|
cellRender: {
|
||||||
|
name: 'CellDict',
|
||||||
|
props: { type: DICT_TYPE.COMMON_STATUS },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'stock',
|
||||||
|
title: '库存',
|
||||||
|
align: 'center',
|
||||||
|
minWidth: 80,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'totalStock',
|
||||||
|
title: '总库存',
|
||||||
|
align: 'center',
|
||||||
|
minWidth: 80,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
field: 'createTime',
|
||||||
|
title: '创建时间',
|
||||||
|
align: 'center',
|
||||||
|
width: 180,
|
||||||
|
formatter: 'formatDateTime',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: '操作',
|
||||||
|
align: 'center',
|
||||||
|
width: 150,
|
||||||
|
fixed: 'right',
|
||||||
|
slots: { default: 'actions' },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
import { formatDate } from '@vben/utils';
|
||||||
|
|
||||||
|
// 全局变量,用于存储配置列表
|
||||||
|
let configList: any[] = [];
|
||||||
|
|
||||||
|
/** 设置配置列表 */
|
||||||
|
export function setConfigList(list: any[]) {
|
||||||
|
configList = list;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 格式化配置名称 */
|
||||||
|
export function formatConfigNames(configId: number): string {
|
||||||
|
const config = configList.find((item) => item.id === configId);
|
||||||
|
return config === null || config === undefined
|
||||||
|
? ''
|
||||||
|
: `${config.name}[${config.startTime} ~ ${config.endTime}]`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 格式化秒杀价格 */
|
||||||
|
export function formatSeckillPrice(products: any[]): string {
|
||||||
|
if (!products || products.length === 0) {
|
||||||
|
return '¥0.00';
|
||||||
|
}
|
||||||
|
const seckillPrice = Math.min(...products.map((item) => item.seckillPrice));
|
||||||
|
return `¥${(seckillPrice / 100).toFixed(2)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 格式化活动时间范围 */
|
||||||
|
export function formatTimeRange(
|
||||||
|
startTime: Date | string,
|
||||||
|
endTime: Date | string,
|
||||||
|
): string {
|
||||||
|
return `${formatDate(startTime, 'YYYY-MM-DD')} ~ ${formatDate(endTime, 'YYYY-MM-DD')}`;
|
||||||
|
}
|
|
@ -1,32 +1,198 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { DocAlert, Page } from '@vben/common-ui';
|
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
|
||||||
|
import type { MallSeckillActivityApi } from '#/api/mall/promotion/seckill/seckillActivity';
|
||||||
|
|
||||||
import { Button } from 'ant-design-vue';
|
import { onMounted } from 'vue';
|
||||||
|
|
||||||
|
import { DocAlert, Page, useVbenModal } from '@vben/common-ui';
|
||||||
|
import { $t } from '@vben/locales';
|
||||||
|
|
||||||
|
import { message, Tag } from 'ant-design-vue';
|
||||||
|
|
||||||
|
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||||
|
import {
|
||||||
|
closeSeckillActivity,
|
||||||
|
deleteSeckillActivity,
|
||||||
|
getSeckillActivityPage,
|
||||||
|
} from '#/api/mall/promotion/seckill/seckillActivity';
|
||||||
|
import { getSimpleSeckillConfigList } from '#/api/mall/promotion/seckill/seckillConfig';
|
||||||
|
|
||||||
|
import { useGridColumns, useGridFormSchema } from './data';
|
||||||
|
import { formatConfigNames, formatTimeRange, setConfigList } from './formatter';
|
||||||
|
import Form from './modules/form.vue';
|
||||||
|
|
||||||
|
defineOptions({ name: 'SeckillActivity' });
|
||||||
|
|
||||||
|
const [FormModal, formModalApi] = useVbenModal({
|
||||||
|
connectedComponent: Form,
|
||||||
|
destroyOnClose: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
/** 刷新表格 */
|
||||||
|
function onRefresh() {
|
||||||
|
gridApi.query();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 编辑活动 */
|
||||||
|
function handleEdit(row: MallSeckillActivityApi.SeckillActivity) {
|
||||||
|
formModalApi.setData(row).open();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 创建活动 */
|
||||||
|
function handleCreate() {
|
||||||
|
formModalApi.setData(null).open();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 关闭活动 */
|
||||||
|
async function handleClose(row: MallSeckillActivityApi.SeckillActivity) {
|
||||||
|
const hideLoading = message.loading({
|
||||||
|
content: $t('ui.actionMessage.closing', [row.name]),
|
||||||
|
key: 'action_key_msg',
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
await closeSeckillActivity(row.id as number);
|
||||||
|
message.success({
|
||||||
|
content: '关闭成功',
|
||||||
|
key: 'action_key_msg',
|
||||||
|
});
|
||||||
|
onRefresh();
|
||||||
|
} finally {
|
||||||
|
hideLoading();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 删除活动 */
|
||||||
|
async function handleDelete(row: MallSeckillActivityApi.SeckillActivity) {
|
||||||
|
const hideLoading = message.loading({
|
||||||
|
content: $t('ui.actionMessage.deleting', [row.name]),
|
||||||
|
key: 'action_key_msg',
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
await deleteSeckillActivity(row.id as number);
|
||||||
|
message.success({
|
||||||
|
content: $t('ui.actionMessage.deleteSuccess', [row.name]),
|
||||||
|
key: 'action_key_msg',
|
||||||
|
});
|
||||||
|
onRefresh();
|
||||||
|
} finally {
|
||||||
|
hideLoading();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const [Grid, gridApi] = useVbenVxeGrid({
|
||||||
|
formOptions: {
|
||||||
|
schema: useGridFormSchema(),
|
||||||
|
},
|
||||||
|
gridOptions: {
|
||||||
|
columns: useGridColumns(),
|
||||||
|
height: 'auto',
|
||||||
|
keepSource: true,
|
||||||
|
proxyConfig: {
|
||||||
|
ajax: {
|
||||||
|
query: async ({ page }, formValues) => {
|
||||||
|
return await getSeckillActivityPage({
|
||||||
|
pageNo: page.currentPage,
|
||||||
|
pageSize: page.pageSize,
|
||||||
|
...formValues,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
rowConfig: {
|
||||||
|
keyField: 'id',
|
||||||
|
isHover: true,
|
||||||
|
},
|
||||||
|
toolbarConfig: {
|
||||||
|
refresh: { code: 'query' },
|
||||||
|
search: true,
|
||||||
|
},
|
||||||
|
} as VxeTableGridOptions<MallSeckillActivityApi.SeckillActivity>,
|
||||||
|
});
|
||||||
|
|
||||||
|
/** 初始化 */
|
||||||
|
onMounted(async () => {
|
||||||
|
// 获得秒杀时间段配置
|
||||||
|
const configList = await getSimpleSeckillConfigList();
|
||||||
|
setConfigList(configList);
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<Page>
|
<Page auto-content-height>
|
||||||
|
<template #doc>
|
||||||
<DocAlert
|
<DocAlert
|
||||||
title="【营销】秒杀活动"
|
title="【营销】秒杀活动"
|
||||||
url="https://doc.iocoder.cn/mall/promotion-seckill/"
|
url="https://doc.iocoder.cn/mall/promotion-seckill/"
|
||||||
/>
|
/>
|
||||||
<Button
|
</template>
|
||||||
danger
|
|
||||||
type="link"
|
<FormModal @success="onRefresh" />
|
||||||
target="_blank"
|
<Grid table-title="秒杀活动列表">
|
||||||
href="https://github.com/yudaocode/yudao-ui-admin-vue3"
|
<template #toolbar-tools>
|
||||||
|
<TableAction
|
||||||
|
:actions="[
|
||||||
|
{
|
||||||
|
label: $t('ui.actionTitle.create', ['秒杀活动']),
|
||||||
|
type: 'primary',
|
||||||
|
icon: ACTION_ICON.ADD,
|
||||||
|
auth: ['promotion:seckill-activity:create'],
|
||||||
|
onClick: handleCreate,
|
||||||
|
},
|
||||||
|
]"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<template #configIds="{ row }">
|
||||||
|
<div class="flex flex-wrap gap-1">
|
||||||
|
<Tag
|
||||||
|
v-for="(configId, index) in row.configIds"
|
||||||
|
:key="index"
|
||||||
|
class="mr-1"
|
||||||
>
|
>
|
||||||
该功能支持 Vue3 + element-plus 版本!
|
{{ formatConfigNames(configId) }}
|
||||||
</Button>
|
</Tag>
|
||||||
<br />
|
</div>
|
||||||
<Button
|
</template>
|
||||||
type="link"
|
|
||||||
target="_blank"
|
<template #timeRange="{ row }">
|
||||||
href="https://github.com/yudaocode/yudao-ui-admin-vue3/blob/master/src/views/mall/promotion/seckill/activity/index"
|
{{ formatTimeRange(row.startTime, row.endTime) }}
|
||||||
>
|
</template>
|
||||||
可参考
|
|
||||||
https://github.com/yudaocode/yudao-ui-admin-vue3/blob/master/src/views/mall/promotion/seckill/activity/index
|
<template #actions="{ row }">
|
||||||
代码,pull request 贡献给我们!
|
<TableAction
|
||||||
</Button>
|
:actions="[
|
||||||
|
{
|
||||||
|
label: $t('common.edit'),
|
||||||
|
type: 'link',
|
||||||
|
icon: ACTION_ICON.EDIT,
|
||||||
|
auth: ['promotion:seckill-activity:update'],
|
||||||
|
onClick: handleEdit.bind(null, row),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: '关闭',
|
||||||
|
type: 'link',
|
||||||
|
danger: true,
|
||||||
|
auth: ['promotion:seckill-activity:close'],
|
||||||
|
ifShow: row.status === 0,
|
||||||
|
popConfirm: {
|
||||||
|
title: '确认关闭该秒杀活动吗?',
|
||||||
|
confirm: handleClose.bind(null, row),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: $t('common.delete'),
|
||||||
|
type: 'link',
|
||||||
|
danger: true,
|
||||||
|
auth: ['promotion:seckill-activity:delete'],
|
||||||
|
ifShow: row.status !== 0,
|
||||||
|
popConfirm: {
|
||||||
|
title: $t('ui.actionMessage.deleteConfirm', [row.name]),
|
||||||
|
confirm: handleDelete.bind(null, row),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
</Grid>
|
||||||
</Page>
|
</Page>
|
||||||
</template>
|
</template>
|
||||||
|
|
|
@ -0,0 +1,121 @@
|
||||||
|
<script lang="ts" setup>
|
||||||
|
import type { MallSeckillActivityApi } from '#/api/mall/promotion/seckill/seckillActivity';
|
||||||
|
|
||||||
|
import { computed, ref } from 'vue';
|
||||||
|
|
||||||
|
import { useVbenModal } from '@vben/common-ui';
|
||||||
|
|
||||||
|
import { message } from 'ant-design-vue';
|
||||||
|
|
||||||
|
import { useVbenForm } from '#/adapter/form';
|
||||||
|
import {
|
||||||
|
createSeckillActivity,
|
||||||
|
getSeckillActivity,
|
||||||
|
updateSeckillActivity,
|
||||||
|
} from '#/api/mall/promotion/seckill/seckillActivity';
|
||||||
|
import { $t } from '#/locales';
|
||||||
|
|
||||||
|
const emit = defineEmits(['success']);
|
||||||
|
const formData = ref<MallSeckillActivityApi.SeckillActivity>();
|
||||||
|
const getTitle = computed(() => {
|
||||||
|
return formData.value?.id
|
||||||
|
? $t('ui.actionTitle.edit', ['秒杀活动'])
|
||||||
|
: $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 [Form, formApi] = useVbenForm({
|
||||||
|
commonConfig: {
|
||||||
|
componentProps: {
|
||||||
|
class: 'w-full',
|
||||||
|
},
|
||||||
|
formItemClass: 'col-span-2',
|
||||||
|
labelWidth: 80,
|
||||||
|
},
|
||||||
|
layout: 'horizontal',
|
||||||
|
schema: formSchema,
|
||||||
|
showDefaultActions: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
const [Modal, modalApi] = useVbenModal({
|
||||||
|
async onConfirm() {
|
||||||
|
const { valid } = await formApi.validate();
|
||||||
|
if (!valid) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
modalApi.lock();
|
||||||
|
// 提交表单
|
||||||
|
const data =
|
||||||
|
(await formApi.getValues()) as MallSeckillActivityApi.SeckillActivity;
|
||||||
|
try {
|
||||||
|
await (formData.value?.id
|
||||||
|
? updateSeckillActivity(data)
|
||||||
|
: createSeckillActivity(data));
|
||||||
|
// 关闭并提示
|
||||||
|
await modalApi.close();
|
||||||
|
emit('success');
|
||||||
|
message.success($t('ui.actionMessage.operationSuccess'));
|
||||||
|
} finally {
|
||||||
|
modalApi.unlock();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async onOpenChange(isOpen: boolean) {
|
||||||
|
if (!isOpen) {
|
||||||
|
formData.value = undefined;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 加载数据
|
||||||
|
const data = modalApi.getData<MallSeckillActivityApi.SeckillActivity>();
|
||||||
|
if (!data || !data.id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
modalApi.lock();
|
||||||
|
try {
|
||||||
|
formData.value = await getSeckillActivity(data.id as number);
|
||||||
|
// 设置到 values
|
||||||
|
await formApi.setValues(formData.value);
|
||||||
|
} finally {
|
||||||
|
modalApi.unlock();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<Modal class="w-2/5" :title="getTitle">
|
||||||
|
<Form class="mx-4" />
|
||||||
|
</Modal>
|
||||||
|
</template>
|
Loading…
Reference in New Issue