feat: 新增 ele 邮件管理模块

This commit is contained in:
puhui999
2025-05-12 00:31:59 +08:00
parent e8e3d020f4
commit d84b72e2a6
10 changed files with 1384 additions and 0 deletions

View File

@@ -0,0 +1,207 @@
import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemMailAccountApi } from '#/api/system/mail/account';
import { useAccess } from '@vben/access';
import { z } from '#/adapter/form';
import { DICT_TYPE, getDictOptions } from '#/utils';
const { hasAccessByCodes } = useAccess();
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
return [
{
fieldName: 'id',
component: 'Input',
dependencies: {
triggerFields: [''],
show: () => false,
},
},
{
fieldName: 'mail',
label: '邮箱',
component: 'Input',
componentProps: {
placeholder: '请输入邮箱',
},
rules: 'required',
},
{
fieldName: 'username',
label: '用户名',
component: 'Input',
componentProps: {
placeholder: '请输入用户名',
},
rules: 'required',
},
{
fieldName: 'password',
label: '密码',
component: 'InputPassword',
componentProps: {
placeholder: '请输入密码',
},
rules: 'required',
},
{
fieldName: 'host',
label: 'SMTP 服务器域名',
component: 'Input',
componentProps: {
placeholder: '请输入 SMTP 服务器域名',
},
rules: 'required',
},
{
fieldName: 'port',
label: 'SMTP 服务器端口',
component: 'InputNumber',
componentProps: {
placeholder: '请输入 SMTP 服务器端口',
min: 0,
max: 65_535,
},
rules: 'required',
},
{
fieldName: 'sslEnable',
label: '是否开启 SSL',
component: 'RadioGroup',
componentProps: {
options: getDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING, 'boolean'),
buttonStyle: 'solid',
optionType: 'button',
},
rules: z.boolean().default(true),
},
{
fieldName: 'starttlsEnable',
label: '是否开启 STARTTLS',
component: 'RadioGroup',
componentProps: {
options: getDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING, 'boolean'),
buttonStyle: 'solid',
optionType: 'button',
},
rules: z.boolean().default(false),
},
{
fieldName: 'remark',
label: '备注',
component: 'Textarea',
componentProps: {
placeholder: '请输入备注',
},
},
];
}
/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
return [
{
fieldName: 'mail',
label: '邮箱',
component: 'Input',
componentProps: {
placeholder: '请输入邮箱',
clearable: true,
},
},
{
fieldName: 'username',
label: '用户名',
component: 'Input',
componentProps: {
placeholder: '请输入用户名',
clearable: true,
},
},
];
}
/** 列表的字段 */
export function useGridColumns<T = SystemMailAccountApi.MailAccount>(
onActionClick: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
return [
{
field: 'id',
title: '编号',
minWidth: 100,
},
{
field: 'mail',
title: '邮箱',
minWidth: 160,
},
{
field: 'username',
title: '用户名',
minWidth: 160,
},
{
field: 'host',
title: 'SMTP 服务器域名',
minWidth: 150,
},
{
field: 'port',
title: 'SMTP 服务器端口',
minWidth: 130,
},
{
field: 'sslEnable',
title: '是否开启 SSL',
minWidth: 120,
cellRender: {
name: 'CellDict',
props: { type: DICT_TYPE.INFRA_BOOLEAN_STRING },
},
},
{
field: 'starttlsEnable',
title: '是否开启 STARTTLS',
minWidth: 145,
cellRender: {
name: 'CellDict',
props: { type: DICT_TYPE.INFRA_BOOLEAN_STRING },
},
},
{
field: 'createTime',
title: '创建时间',
minWidth: 180,
formatter: 'formatDateTime',
},
{
field: 'operation',
title: '操作',
minWidth: 130,
align: 'center',
fixed: 'right',
cellRender: {
attrs: {
nameField: 'mail',
nameTitle: '邮箱账号',
onClick: onActionClick,
},
name: 'CellOperation',
options: [
{
code: 'edit',
show: hasAccessByCodes(['system:mail-account:update']),
},
{
code: 'delete',
show: hasAccessByCodes(['system:mail-account:delete']),
},
],
},
},
];
}

View File

@@ -0,0 +1,127 @@
<script lang="ts" setup>
import type {
OnActionClickParams,
VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { SystemMailAccountApi } from '#/api/system/mail/account';
import { Page, useVbenModal } from '@vben/common-ui';
import { Plus } from '@vben/icons';
import { ElButton, ElLoading, ElMessage } from 'element-plus';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import {
deleteMailAccount,
getMailAccountPage,
} from '#/api/system/mail/account';
import { DocAlert } from '#/components/doc-alert';
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 onCreate() {
formModalApi.setData(null).open();
}
/** 编辑邮箱账号 */
function onEdit(row: SystemMailAccountApi.MailAccount) {
formModalApi.setData(row).open();
}
/** 删除邮箱账号 */
async function onDelete(row: SystemMailAccountApi.MailAccount) {
const loadingInstance = ElLoading.service({
text: $t('ui.actionMessage.deleting', [row.mail]),
fullscreen: true,
});
try {
await deleteMailAccount(row.id as number);
ElMessage.success($t('ui.actionMessage.deleteSuccess', [row.mail]));
onRefresh();
} catch {
// 异常处理
} finally {
loadingInstance.close();
}
}
/** 表格操作按钮的回调函数 */
function onActionClick({
code,
row,
}: OnActionClickParams<SystemMailAccountApi.MailAccount>) {
switch (code) {
case 'delete': {
onDelete(row);
break;
}
case 'edit': {
onEdit(row);
break;
}
}
}
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useGridFormSchema(),
},
gridOptions: {
columns: useGridColumns(onActionClick),
height: 'auto',
keepSource: true,
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
return await getMailAccountPage({
pageNo: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
rowConfig: {
keyField: 'id',
},
toolbarConfig: {
refresh: { code: 'query' },
search: true,
},
} as VxeTableGridOptions<SystemMailAccountApi.MailAccount>,
});
</script>
<template>
<Page auto-content-height>
<template #doc>
<DocAlert title="邮件配置" url="https://doc.iocoder.cn/mail" />
</template>
<FormModal @success="onRefresh" />
<Grid table-title="邮箱账号列表">
<template #toolbar-tools>
<ElButton
type="primary"
@click="onCreate"
v-access:code="['system:mail-account:create']"
>
<Plus class="size-5" />
{{ $t('ui.actionTitle.create', ['邮箱账号']) }}
</ElButton>
</template>
</Grid>
</Page>
</template>

View File

@@ -0,0 +1,89 @@
<script lang="ts" setup>
import type { SystemMailAccountApi } from '#/api/system/mail/account';
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { ElMessage } from 'element-plus';
import { useVbenForm } from '#/adapter/form';
import {
createMailAccount,
getMailAccount,
updateMailAccount,
} from '#/api/system/mail/account';
import { $t } from '#/locales';
import { useFormSchema } from '../data';
const emit = defineEmits(['success']);
const formData = ref<SystemMailAccountApi.MailAccount>();
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: 80,
},
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 SystemMailAccountApi.MailAccount;
try {
await (formData.value?.id
? updateMailAccount(data)
: createMailAccount(data));
// 关闭并提示
await modalApi.close();
emit('success');
ElMessage.success($t('ui.actionMessage.operationSuccess'));
} finally {
modalApi.unlock();
}
},
async onOpenChange(isOpen: boolean) {
if (!isOpen) {
formData.value = undefined;
return;
}
// 加载数据
const data = modalApi.getData<SystemMailAccountApi.MailAccount>();
if (!data || !data.id) {
return;
}
modalApi.lock();
try {
formData.value = await getMailAccount(data.id);
// 设置到 values
await formApi.setValues(formData.value);
} finally {
modalApi.unlock();
}
},
});
</script>
<template>
<Modal :title="getTitle">
<Form class="mx-4" />
</Modal>
</template>