feat: mall seckill config

pull/142/MERGE
xingyu4j 2025-06-14 14:36:28 +08:00
parent 5fefb334af
commit 122b835fb7
3 changed files with 392 additions and 25 deletions

View File

@ -0,0 +1,150 @@
import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MallSeckillConfigApi } from '#/api/mall/promotion/seckill/seckillConfig';
import { DICT_TYPE, getDictOptions, getIntDictOptions } from '#/utils';
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
return [
{
component: 'Input',
fieldName: 'id',
dependencies: {
triggerFields: [''],
show: () => false,
},
},
{
fieldName: 'name',
label: '秒杀时段名称',
component: 'Input',
rules: 'required',
},
{
fieldName: 'startTime',
label: '开始时间点',
component: 'TimePicker',
componentProps: {
format: 'HH:mm',
valueFormat: 'HH:mm',
placeholder: '请选择开始时间点',
},
rules: 'required',
},
{
fieldName: 'endTime',
label: '结束时间点',
component: 'TimePicker',
componentProps: {
format: 'HH:mm',
valueFormat: 'HH:mm',
placeholder: '请选择结束时间点',
},
rules: 'required',
},
{
fieldName: 'sliderPicUrls',
label: '秒杀轮播图',
component: 'ImageUpload',
componentProps: {
multiple: true,
maxNumber: 5,
},
},
{
fieldName: 'status',
label: '状态',
component: 'RadioGroup',
componentProps: {
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
buttonStyle: 'solid',
optionType: 'button',
},
rules: 'required',
},
];
}
/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
return [
{
fieldName: 'name',
label: '秒杀时段名称',
component: 'Input',
componentProps: {
placeholder: '请输入秒杀时段名称',
},
},
{
fieldName: 'status',
label: '状态',
component: 'Select',
componentProps: {
placeholder: '请选择状态',
options: getIntDictOptions(DICT_TYPE.COMMON_STATUS),
},
},
];
}
/** 表格列配置 */
export function useGridColumns<T = MallSeckillConfigApi.SeckillConfig>(
onStatusChange?: (
newStatus: number,
row: T,
) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions['columns'] {
return [
{
title: '秒杀时段名称',
field: 'name',
width: 200,
},
{
title: '开始时间点',
field: 'startTime',
width: 120,
},
{
title: '结束时间点',
field: 'endTime',
width: 120,
},
{
title: '秒杀轮播图',
field: 'sliderPicUrls',
cellRender: {
name: 'CellImages',
},
},
{
title: '活动状态',
field: 'status',
width: 100,
cellRender: {
attrs: { beforeChange: onStatusChange },
name: 'CellSwitch',
props: {
checkedValue: 1,
checkedChildren: '启用',
unCheckedValue: 0,
unCheckedChildren: '禁用',
},
},
},
{
title: '创建时间',
field: 'createTime',
width: 180,
formatter: 'formatDateTime',
},
{
title: '操作',
width: 180,
fixed: 'right',
slots: { default: 'actions' },
},
];
}

View File

@ -1,32 +1,159 @@
<script lang="ts" setup>
import { DocAlert, Page } from '@vben/common-ui';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MallSeckillConfigApi } from '#/api/mall/promotion/seckill/seckillConfig';
import { Button } from 'ant-design-vue';
import { confirm, Page, useVbenModal } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import {
deleteSeckillConfig,
getSeckillConfigPage,
updateSeckillConfigStatus,
} from '#/api/mall/promotion/seckill/seckillConfig';
import { $t } from '#/locales';
import { useGridColumns, useGridFormSchema } from './data';
import Form from './modules/form.vue';
const [FormModal, formModalApi] = useVbenModal({
connectedComponent: Form,
destroyOnClose: true,
});
/** 刷新表格 */
function onRefresh() {
gridApi.query();
}
/** 创建秒杀时段 */
function handleCreate() {
formModalApi.setData(null).open();
}
/** 编辑秒杀时段 */
function handleEdit(row: MallSeckillConfigApi.SeckillConfig) {
formModalApi.setData(row).open();
}
/** 删除秒杀时段 */
async function handleDelete(row: MallSeckillConfigApi.SeckillConfig) {
const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting', [row.name]),
key: 'action_key_msg',
});
try {
await deleteSeckillConfig(row.id as number);
message.success({
content: $t('ui.actionMessage.deleteSuccess', [row.name]),
key: 'action_key_msg',
});
onRefresh();
} finally {
hideLoading();
}
}
/** 修改状态 */
async function handleStatusChange(
newStatus: number,
row: MallSeckillConfigApi.SeckillConfig,
): Promise<boolean | undefined> {
return new Promise((resolve, reject) => {
//
const text = row.status === 0 ? '启用' : '停用';
confirm({
content: `确认要${text + row.name}吗?`,
})
.then(async () => {
//
const res = await updateSeckillConfigStatus(row.id, newStatus);
if (res) {
//
message.success(`${text}成功`);
resolve(true);
} else {
reject(new Error('操作失败'));
}
})
.catch(() => {
reject(new Error('取消操作'));
});
});
}
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useGridFormSchema(),
},
gridOptions: {
columns: useGridColumns(handleStatusChange),
height: 'auto',
keepSource: true,
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
return await getSeckillConfigPage({
pageNo: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
rowConfig: {
keyField: 'id',
},
toolbarConfig: {
refresh: { code: 'query' },
search: true,
},
} as VxeTableGridOptions<MallSeckillConfigApi.SeckillConfig>,
});
</script>
<template>
<Page>
<DocAlert
title="【营销】秒杀活动"
url="https://doc.iocoder.cn/mall/promotion-seckill/"
<Page auto-content-height>
<FormModal @success="onRefresh" />
<Grid table-title="">
<template #toolbar-tools>
<TableAction
:actions="[
{
label: $t('ui.actionTitle.create', ['秒杀时段']),
type: 'primary',
icon: ACTION_ICON.ADD,
auth: ['promotion:seckill-config:create'],
onClick: handleCreate,
},
]"
/>
<Button
danger
type="link"
target="_blank"
href="https://github.com/yudaocode/yudao-ui-admin-vue3"
>
该功能支持 Vue3 + element-plus 版本
</Button>
<br />
<Button
type="link"
target="_blank"
href="https://github.com/yudaocode/yudao-ui-admin-vue3/blob/master/src/views/mall/promotion/seckill/config/index"
>
可参考
https://github.com/yudaocode/yudao-ui-admin-vue3/blob/master/src/views/mall/promotion/seckill/config/index
代码pull request 贡献给我们
</Button>
</template>
<template #actions="{ row }">
<TableAction
:actions="[
{
label: $t('common.edit'),
type: 'link',
icon: ACTION_ICON.EDIT,
auth: ['promotion:seckill-config:update'],
onClick: handleEdit.bind(null, row),
},
{
label: $t('common.delete'),
type: 'link',
danger: true,
icon: ACTION_ICON.DELETE,
auth: ['promotion:seckill-config:delete'],
popConfirm: {
title: $t('ui.actionMessage.deleteConfirm', [row.name]),
confirm: handleDelete.bind(null, row),
},
},
]"
/>
</template>
</Grid>
</Page>
</template>

View File

@ -0,0 +1,90 @@
<script lang="ts" setup>
import type { MallSeckillConfigApi } from '#/api/mall/promotion/seckill/seckillConfig';
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import {
createSeckillConfig,
getSeckillConfig,
updateSeckillConfig,
} from '#/api/mall/promotion/seckill/seckillConfig';
import { $t } from '#/locales';
import { useFormSchema } from '../data';
const emit = defineEmits(['success']);
const formData = ref<MallSeckillConfigApi.SeckillConfig>();
const getTitle = computed(() => {
return formData.value?.id
? $t('ui.actionTitle.edit', ['秒杀时段'])
: $t('ui.actionTitle.create', ['秒杀时段']);
});
const [Form, formApi] = useVbenForm({
commonConfig: {
componentProps: {
class: 'w-full',
},
formItemClass: 'col-span-2',
labelWidth: 100,
},
layout: 'horizontal',
schema: useFormSchema(),
showDefaultActions: false,
});
const [Modal, modalApi] = useVbenModal({
async onConfirm() {
const { valid } = await formApi.validate();
if (!valid) {
return;
}
modalApi.lock();
//
const data =
(await formApi.getValues()) as MallSeckillConfigApi.SeckillConfig;
try {
await (formData.value?.id
? updateSeckillConfig(data)
: createSeckillConfig(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<MallSeckillConfigApi.SeckillConfig>();
if (!data || !data.id) {
return;
}
modalApi.lock();
try {
formData.value = await getSeckillConfig(data.id as number);
// values
await formApi.setValues(formData.value);
} finally {
modalApi.unlock();
}
},
});
</script>
<template>
<Modal class="w-[40%]" :title="getTitle">
<Form class="mx-4" />
</Modal>
</template>