feat: mall banner

pull/142/MERGE
xingyu4j 2025-06-14 13:32:59 +08:00
parent 8691b17cc2
commit fb4af81e3f
3 changed files with 390 additions and 25 deletions

View File

@ -0,0 +1,183 @@
import type { VbenFormSchema } from '#/adapter/form';
import type { VxeGridPropTypes } from '#/adapter/vxe-table';
import {
DICT_TYPE,
getDictOptions,
getIntDictOptions,
getRangePickerDefaultProps,
} from '#/utils';
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
return [
{
component: 'Input',
fieldName: 'id',
dependencies: {
triggerFields: [''],
show: () => false,
},
},
{
fieldName: 'title',
label: 'Banner标题',
component: 'Input',
rules: 'required',
},
{
fieldName: 'picUrl',
label: '图片地址',
component: 'ImageUpload',
componentProps: {
maxSize: 1,
},
rules: 'required',
},
{
fieldName: 'position',
label: '定位',
component: 'RadioGroup',
componentProps: {
options: getDictOptions(DICT_TYPE.PROMOTION_BANNER_POSITION, 'number'),
buttonStyle: 'solid',
optionType: 'button',
},
rules: 'required',
},
{
fieldName: 'url',
label: '跳转地址',
component: 'Input',
rules: 'required',
},
{
fieldName: 'sort',
label: '排序',
component: 'InputNumber',
componentProps: {
min: 0,
controlsPosition: 'right',
placeholder: '请输入排序',
},
rules: 'required',
},
{
fieldName: 'status',
label: '状态',
component: 'RadioGroup',
componentProps: {
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
buttonStyle: 'solid',
optionType: 'button',
},
rules: 'required',
},
{
fieldName: 'memo',
label: '描述',
component: 'Textarea',
componentProps: {
rows: 4,
placeholder: '请输入描述',
},
},
];
}
/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
return [
{
fieldName: 'title',
label: 'Banner标题',
component: 'Input',
componentProps: {
placeholder: '请输入Banner标题',
},
},
{
fieldName: 'status',
label: '状态',
component: 'Select',
componentProps: {
placeholder: '请选择状态',
options: getIntDictOptions(DICT_TYPE.COMMON_STATUS),
},
},
{
fieldName: 'createTime',
label: '创建时间',
component: 'RangePicker',
componentProps: {
...getRangePickerDefaultProps(),
allowClear: true,
},
},
];
}
/** 表格列配置 */
export function useGridColumns(): VxeGridPropTypes.Columns {
return [
{
title: 'Banner标题',
field: 'title',
},
{
title: '图片',
field: 'picUrl',
width: 80,
cellRender: {
name: 'CellImage',
},
},
{
title: '状态',
field: 'status',
width: 150,
cellRender: {
name: 'CellDictTag',
props: {
dictType: DICT_TYPE.COMMON_STATUS,
},
},
},
{
title: '定位',
field: 'position',
width: 150,
cellRender: {
name: 'CellDictTag',
props: {
dictType: DICT_TYPE.PROMOTION_BANNER_POSITION,
},
},
},
{
title: '跳转地址',
field: 'url',
},
{
title: '创建时间',
field: 'createTime',
width: 180,
formatter: 'formatDateTime',
},
{
title: '排序',
field: 'sort',
width: 100,
},
{
title: '描述',
field: 'memo',
},
{
title: '操作',
width: 180,
fixed: 'right',
slots: { default: 'actions' },
},
];
}

View File

@ -1,32 +1,127 @@
<script lang="ts" setup>
import { DocAlert, Page } from '@vben/common-ui';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MallBannerApi } from '#/api/mall/market/banner';
import { Button } from 'ant-design-vue';
import { Page, useVbenModal } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { deleteBanner, getBannerPage } from '#/api/mall/market/banner';
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();
}
/** 创建Banner */
function handleCreate() {
formModalApi.setData(null).open();
}
/** 编辑Banner */
function handleEdit(row: MallBannerApi.Banner) {
formModalApi.setData(row).open();
}
/** 删除Banner */
async function handleDelete(row: MallBannerApi.Banner) {
const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting', [row.title]),
key: 'action_key_msg',
});
try {
await deleteBanner(row.id as number);
message.success({
content: $t('ui.actionMessage.deleteSuccess', [row.title]),
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 getBannerPage({
pageNo: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
rowConfig: {
keyField: 'id',
},
toolbarConfig: {
refresh: { code: 'query' },
search: true,
},
} as VxeTableGridOptions<MallBannerApi.Banner>,
});
</script>
<template>
<Page>
<DocAlert
title="【营销】内容管理"
url="https://doc.iocoder.cn/mall/promotion-content/"
/>
<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/banner/index"
>
可参考
https://github.com/yudaocode/yudao-ui-admin-vue3/blob/master/src/views/mall/promotion/banner/index
代码pull request 贡献给我们
</Button>
<Page auto-content-height>
<FormModal @success="onRefresh" />
<Grid table-title="Banner">
<template #toolbar-tools>
<TableAction
:actions="[
{
label: $t('ui.actionTitle.create', ['Banner']),
type: 'primary',
icon: ACTION_ICON.ADD,
auth: ['promotion:banner:create'],
onClick: handleCreate,
},
]"
/>
</template>
<template #actions="{ row }">
<TableAction
:actions="[
{
label: $t('common.edit'),
type: 'link',
icon: ACTION_ICON.EDIT,
auth: ['promotion:banner:update'],
onClick: handleEdit.bind(null, row),
},
{
label: $t('common.delete'),
type: 'link',
danger: true,
icon: ACTION_ICON.DELETE,
auth: ['promotion:banner:delete'],
popConfirm: {
title: $t('ui.actionMessage.deleteConfirm', [row.title]),
confirm: handleDelete.bind(null, row),
},
},
]"
/>
</template>
</Grid>
</Page>
</template>

View File

@ -0,0 +1,87 @@
<script lang="ts" setup>
import type { MallBannerApi } from '#/api/mall/market/banner';
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import {
createBanner,
getBanner,
updateBanner,
} from '#/api/mall/market/banner';
import { $t } from '#/locales';
import { useFormSchema } from '../data';
const emit = defineEmits(['success']);
const formData = ref<MallBannerApi.Banner>();
const getTitle = computed(() => {
return formData.value?.id
? $t('ui.actionTitle.edit', ['Banner'])
: $t('ui.actionTitle.create', ['Banner']);
});
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 MallBannerApi.Banner;
try {
await (formData.value?.id ? updateBanner(data) : createBanner(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<MallBannerApi.Banner>();
if (!data || !data.id) {
return;
}
modalApi.lock();
try {
formData.value = await getBanner(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>