Skip to content

Commit ba327cd

Browse files
committed
Vue3 重构:邮件模版的新增和修改操作
1 parent 4d03e46 commit ba327cd

File tree

5 files changed

+98
-28
lines changed

5 files changed

+98
-28
lines changed

src/api/system/mail/template/index.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,38 @@ export interface MailTemplateVO {
1313
remark: string
1414
}
1515

16-
export interface MailTemplatePageReqVO extends PageParam {
17-
name?: string
18-
code?: string
19-
accountId?: number
20-
status?: number
21-
createTime?: Date[]
22-
}
23-
2416
export interface MailSendReqVO {
2517
mail: string
2618
templateCode: string
2719
templateParams: Map<String, Object>
2820
}
2921

3022
// 查询邮件模版列表
31-
export const getMailTemplatePageApi = async (params: MailTemplatePageReqVO) => {
23+
export const getMailTemplatePage = async (params: PageParam) => {
3224
return await request.get({ url: '/system/mail-template/page', params })
3325
}
3426

3527
// 查询邮件模版详情
36-
export const getMailTemplateApi = async (id: number) => {
28+
export const getMailTemplate = async (id: number) => {
3729
return await request.get({ url: '/system/mail-template/get?id=' + id })
3830
}
3931

4032
// 新增邮件模版
41-
export const createMailTemplateApi = async (data: MailTemplateVO) => {
33+
export const createMailTemplate = async (data: MailTemplateVO) => {
4234
return await request.post({ url: '/system/mail-template/create', data })
4335
}
4436

4537
// 修改邮件模版
46-
export const updateMailTemplateApi = async (data: MailTemplateVO) => {
38+
export const updateMailTemplate = async (data: MailTemplateVO) => {
4739
return await request.put({ url: '/system/mail-template/update', data })
4840
}
4941

5042
// 删除邮件模版
51-
export const deleteMailTemplateApi = async (id: number) => {
43+
export const deleteMailTemplate = async (id: number) => {
5244
return await request.delete({ url: '/system/mail-template/delete?id=' + id })
5345
}
5446

5547
// 发送邮件
56-
export const sendMailApi = (data: MailSendReqVO) => {
48+
export const sendMail = (data: MailSendReqVO) => {
5749
return request.post({ url: '/system/mail-template/send-mail', data })
5850
}

src/views/system/mail/account/form.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const openModal = async (type: string, id?: number) => {
2525
modelVisible.value = true
2626
modelTitle.value = t('action.' + type)
2727
formType.value = type
28-
// resetForm()
2928
// 修改时,设置数据
3029
if (id) {
3130
formLoading.value = true
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<template>
2+
<Dialog :title="modelTitle" v-model="modelVisible" :scroll="true" :width="800" :max-height="500">
3+
<Form ref="formRef" :schema="allSchemas.formSchema" :rules="rules" v-loading="formLoading" />
4+
<template #footer>
5+
<el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
6+
<el-button @click="modelVisible = false">取 消</el-button>
7+
</template>
8+
</Dialog>
9+
</template>
10+
<script setup lang="ts">
11+
import * as MailTemplateApi from '@/api/system/mail/template'
12+
import { rules, allSchemas } from './template.data'
13+
14+
const { t } = useI18n() // 国际化
15+
const message = useMessage() // 消息弹窗
16+
17+
const modelVisible = ref(false) // 弹窗的是否展示
18+
const modelTitle = ref('') // 弹窗的标题
19+
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
20+
const formType = ref('') // 表单的类型:create - 新增;update - 修改
21+
const formRef = ref() // 表单 Ref
22+
23+
/** 打开弹窗 */
24+
const openModal = async (type: string, id?: number) => {
25+
modelVisible.value = true
26+
modelTitle.value = t('action.' + type)
27+
formType.value = type
28+
// 修改时,设置数据
29+
if (id) {
30+
formLoading.value = true
31+
try {
32+
const data = await MailTemplateApi.getMailTemplate(id)
33+
formRef.value.setValues(data)
34+
} finally {
35+
formLoading.value = false
36+
}
37+
}
38+
}
39+
defineExpose({ openModal }) // 提供 openModal 方法,用于打开弹窗
40+
41+
/** 提交表单 */
42+
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
43+
const submitForm = async () => {
44+
// 校验表单
45+
if (!formRef) return
46+
const valid = await formRef.value.getElFormRef().validate()
47+
if (!valid) return
48+
// 提交请求
49+
formLoading.value = true
50+
try {
51+
const data = formRef.value.formModel as MailTemplateApi.MailTemplateVO
52+
if (formType.value === 'create') {
53+
await MailTemplateApi.createMailTemplate(data)
54+
message.success(t('common.createSuccess'))
55+
} else {
56+
await MailTemplateApi.updateMailTemplate(data)
57+
message.success(t('common.updateSuccess'))
58+
}
59+
modelVisible.value = false
60+
// 发送操作成功的事件
61+
emit('success')
62+
} finally {
63+
formLoading.value = false
64+
}
65+
}
66+
</script>

src/views/system/mail/template/index.vue

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,28 @@
4949
</content-wrap>
5050

5151
<!-- 表单弹窗:添加/修改 -->
52-
<!-- <mail-template-form ref="modalRef" @success="getList" />-->
52+
<mail-template-form ref="modalRef" @success="getList" />
5353
</template>
5454
<script setup lang="ts" name="MailTemplate">
5555
import { allSchemas } from './template.data'
5656
import * as MailTemplateApi from '@/api/system/mail/template'
57-
// import MailAccountForm from './form.vue'
57+
import MailTemplateForm from './form.vue'
5858
5959
// tableObject:表格的属性对象,可获得分页大小、条数等属性
6060
// tableMethods:表格的操作对象,可进行获得分页、删除记录等操作
6161
// 详细可见:https://kailong110120130.gitee.io/vue-element-plus-admin-doc/components/table.html#usetable
6262
const { tableObject, tableMethods } = useTable({
63-
getListApi: MailTemplateApi.getMailTemplatePageApi, // 分页接口
64-
delListApi: MailTemplateApi.deleteMailTemplateApi // 删除接口
63+
getListApi: MailTemplateApi.getMailTemplatePage, // 分页接口
64+
delListApi: MailTemplateApi.deleteMailTemplate // 删除接口
6565
})
6666
// 获得表格的各种操作
6767
const { getList, setSearchParams } = tableMethods
6868
6969
/** 添加/修改操作 */
70-
// const modalRef = ref()
71-
// const openModal = (type: string, id?: number) => {
72-
// modalRef.value.openModal(type, id)
73-
// }
70+
const modalRef = ref()
71+
const openModal = (type: string, id?: number) => {
72+
modalRef.value.openModal(type, id)
73+
}
7474
7575
/** 删除按钮操作 */
7676
const handleDelete = (id: number) => {

src/views/system/mail/template/template.data.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,32 @@ const crudSchemas = reactive<CrudSchema[]>([
4242
span: 24
4343
},
4444
componentProps: {
45-
valueHtml: ''
45+
valueHtml: '',
46+
height: 200
4647
}
4748
}
4849
},
4950
{
5051
label: '邮箱账号',
5152
field: 'accountId',
52-
isSearch: true,
5353
width: '200px',
5454
formatter: (_: Recordable, __: TableColumn, cellValue: number) => {
5555
return accounts.find((account) => account.id === cellValue)?.mail
5656
},
5757
search: {
5858
show: true,
5959
component: 'Select',
60-
api: () => {
61-
return accounts
62-
},
60+
api: () => accounts,
61+
componentProps: {
62+
optionsAlias: {
63+
labelField: 'mail',
64+
valueField: 'id'
65+
}
66+
}
67+
},
68+
form: {
69+
component: 'Select',
70+
api: () => accounts,
6371
componentProps: {
6472
optionsAlias: {
6573
labelField: 'mail',
@@ -104,6 +112,11 @@ const crudSchemas = reactive<CrudSchema[]>([
104112
defaultTime: [new Date('1 00:00:00'), new Date('1 23:59:59')]
105113
}
106114
}
115+
},
116+
{
117+
label: '操作',
118+
field: 'action',
119+
isForm: false
107120
}
108121
])
109122
export const { allSchemas } = useCrudSchemas(crudSchemas)

0 commit comments

Comments
 (0)