重构VUE3【站内信-模板管理】

This commit is contained in:
咱哥丶 2023-04-15 12:25:11 +08:00
parent c461800eea
commit 776eb3a7b0
4 changed files with 407 additions and 273 deletions

View File

@ -1,13 +1,14 @@
import request from '@/config/axios'
export interface NotifyTemplateVO {
id: number
id: number | null
name: string
nickname: string
code: string
content: string
type: number
type: number | null
params: string
status: number
status: number | null
remark: string
}
@ -19,7 +20,7 @@ export interface NotifyTemplatePageReqVO extends PageParam {
}
export interface NotifySendReqVO {
userId: number
userId: number | null
templateCode: string
templateParams: Map<String, Object>
}

View File

@ -0,0 +1,140 @@
<template>
<Dialog :title="dialogTitle" v-model="dialogVisible">
<el-form
ref="formRef"
:model="formData"
:rules="formRules"
label-width="140px"
v-loading="formLoading"
>
<el-form-item label="模版编码" prop="code">
<el-input v-model="formData.code" placeholder="请输入模版编码" />
</el-form-item>
<el-form-item label="模板名称" prop="name">
<el-input v-model="formData.name" placeholder="请输入模版名称" />
</el-form-item>
<el-form-item label="发件人名称" prop="nickname">
<el-input v-model="formData.nickname" placeholder="请输入发件人名称" />
</el-form-item>
<el-form-item label="模板内容" prop="content">
<el-input type="textarea" v-model="formData.content" placeholder="请输入模板内容" />
</el-form-item>
<el-form-item label="类型" prop="type">
<el-select v-model="formData.type" placeholder="请选择类型">
<el-option
v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_NOTIFY_TEMPLATE_TYPE)"
:key="dict.value"
:label="dict.label"
:value="parseInt(dict.value)"
/>
</el-select>
</el-form-item>
<el-form-item label="开启状态" prop="status">
<el-radio-group v-model="formData.status">
<el-radio
v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
:key="dict.value"
:label="parseInt(dict.value as string)"
>
{{ dict.label }}
</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="备注" prop="remark">
<el-input v-model="formData.remark" placeholder="请输入备注" />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="submitForm" type="primary" :disabled="formLoading"> </el-button>
<el-button @click="dialogVisible = false"> </el-button>
</template>
</Dialog>
</template>
<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import * as NotifyTemplateApi from '@/api/system/notify/template'
import { CommonStatusEnum } from '@/utils/constants'
const message = useMessage() //
const dialogVisible = ref(false) //
const dialogTitle = ref('') //
const formLoading = ref(false) // 12
const formType = ref('') //
const formData = ref<NotifyTemplateApi.NotifyTemplateVO>({
id: null,
name: '',
nickname: '',
code: '',
content: '',
type: null,
params: '',
status: CommonStatusEnum.ENABLE,
remark: ''
})
const formRules = reactive({
type: [{ required: true, message: '消息类型不能为空', trigger: 'change' }],
status: [{ required: true, message: '开启状态不能为空', trigger: 'blur' }],
code: [{ required: true, message: '模板编码不能为空', trigger: 'blur' }],
name: [{ required: true, message: '模板名称不能为空', trigger: 'blur' }],
nickname: [{ required: true, message: '发件人姓名不能为空', trigger: 'blur' }],
content: [{ required: true, message: '模板内容不能为空', trigger: 'blur' }]
})
const formRef = ref() // Ref
const open = async (type: string, id?: number) => {
dialogVisible.value = true
dialogTitle.value = type
formType.value = type
resetForm()
//
if (id) {
formLoading.value = true
try {
formData.value = await NotifyTemplateApi.getNotifyTemplateApi(id)
} finally {
formLoading.value = false
}
}
}
defineExpose({ open }) // open
/** 提交表单 */
const emit = defineEmits(['success']) // success
const submitForm = async () => {
//
if (!formRef) return
const valid = await formRef.value.validate()
if (!valid) return
formLoading.value = true
try {
const data = formData.value as NotifyTemplateApi.NotifyTemplateVO
if (formType.value === 'create') {
await NotifyTemplateApi.createNotifyTemplateApi(data)
message.success('新增成功')
} else {
await NotifyTemplateApi.updateNotifyTemplateApi(data)
message.success('修改成功')
}
dialogVisible.value = false
//
emit('success')
} finally {
formLoading.value = false
}
}
/** 重置表单 */
const resetForm = () => {
formData.value = {
id: null,
name: '',
nickname: '',
code: '',
content: '',
type: null,
params: '',
status: CommonStatusEnum.ENABLE,
remark: ''
}
formRef.value?.resetFields()
}
</script>

View File

@ -1,87 +1,169 @@
<template>
<doc-alert title="站内信配置" url="https://doc.iocoder.cn/notify/" />
<ContentWrap>
<!-- 列表 -->
<XTable @register="registerTable">
<template #toolbar_buttons>
<!-- 操作新增 -->
<XButton
<!-- 搜索工作栏 -->
<el-form
class="-mb-15px"
:model="queryParams"
ref="queryFormRef"
:inline="true"
label-width="68px"
>
<el-form-item label="模板名称" prop="name">
<el-input
v-model="queryParams.name"
placeholder="请输入模板名称"
clearable
@keyup.enter="handleQuery"
class="!w-240px"
/>
</el-form-item>
<el-form-item label="模板编号" prop="code">
<el-input
v-model="queryParams.code"
placeholder="请输入模版编码"
clearable
@keyup.enter="handleQuery"
class="!w-240px"
/>
</el-form-item>
<el-form-item label="状态" prop="status">
<el-select
v-model="queryParams.status"
placeholder="请选择开启状态"
clearable
class="!w-240px"
>
<el-option
v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
:key="dict.value"
:label="dict.label"
:value="dict.value"
/>
</el-select>
</el-form-item>
<el-form-item label="创建时间" prop="createTime">
<el-date-picker
v-model="queryParams.createTime"
value-format="YYYY-MM-DD HH:mm:ss"
type="daterange"
start-placeholder="开始日期"
end-placeholder="结束日期"
:default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
class="!w-240px"
/>
</el-form-item>
<el-form-item>
<el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
<el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
<el-button
type="primary"
preIcon="ep:zoom-in"
:title="t('action.add')"
plain
@click="openForm('create')"
v-hasPermi="['system:notify-template:create']"
@click="handleCreate()"
/>
</template>
<template #actionbtns_default="{ row }">
<!-- 操作测试站内信 -->
<XTextButton
preIcon="ep:cpu"
:title="t('action.test')"
v-hasPermi="['system:notify-template:send-notify']"
@click="handleSendNotify(row)"
/>
<!-- 操作修改 -->
<XTextButton
preIcon="ep:edit"
:title="t('action.edit')"
v-hasPermi="['system:notify-template:update']"
@click="handleUpdate(row.id)"
/>
<!-- 操作详情 -->
<XTextButton
preIcon="ep:view"
:title="t('action.detail')"
v-hasPermi="['system:notify-template:query']"
@click="handleDetail(row.id)"
/>
<!-- 操作删除 -->
<XTextButton
preIcon="ep:delete"
:title="t('action.del')"
v-hasPermi="['system:notify-template:delete']"
@click="deleteData(row.id)"
/>
</template>
</XTable>
>
<Icon icon="ep:plus" class="mr-5px" />新增
</el-button>
</el-form-item>
</el-form>
</ContentWrap>
<!-- 添加/修改的弹窗 -->
<Dialog id="templateModel" :loading="modelLoading" v-model="dialogVisible" :title="dialogTitle">
<!-- 表单添加/修改 -->
<Form
ref="formRef"
v-if="['create', 'update'].includes(actionType)"
:schema="allSchemas.formSchema"
:rules="rules"
<!-- 列表 -->
<ContentWrap>
<el-table v-loading="loading" :data="list">
<el-table-column
label="模板编码"
align="center"
prop="code"
width="120"
:show-overflow-tooltip="true"
/>
<!-- 表单详情 -->
<Descriptions
v-if="actionType === 'detail'"
:schema="allSchemas.detailSchema"
:data="detailData"
<el-table-column
label="模板名称"
align="center"
prop="name"
width="120"
:show-overflow-tooltip="true"
/>
<template #footer>
<!-- 按钮保存 -->
<XButton
v-if="['create', 'update'].includes(actionType)"
type="primary"
:title="t('action.save')"
:loading="actionLoading"
@click="submitForm()"
/>
<!-- 按钮关闭 -->
<XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
<el-table-column label="类型" align="center" prop="type">
<template #default="scope">
<dict-tag :type="DICT_TYPE.SYSTEM_NOTIFY_TEMPLATE_TYPE" :value="scope.row.type" />
</template>
</Dialog>
</el-table-column>
<el-table-column label="发送人名称" align="center" prop="nickname" />
<el-table-column
label="模板内容"
align="center"
prop="content"
width="200"
:show-overflow-tooltip="true"
/>
<!-- 测试站内信的弹窗 -->
<Dialog id="sendTest" v-model="sendVisible" title="测试">
<el-form :model="sendForm" :rules="sendRules" label-width="200px" label-position="top">
<el-table-column label="开启状态" align="center" prop="status" width="80">
<template #default="scope">
<dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
</template>
</el-table-column>
<el-table-column label="备注" align="center" prop="remark" />
<el-table-column
label="创建时间"
align="center"
prop="createTime"
width="180"
:formatter="dateFormatter"
/>
<el-table-column label="操作" align="center" width="210" fixed="right">
<template #default="scope">
<el-button
link
type="primary"
@click="openForm('update', scope.row.id)"
v-hasPermi="['system:notify-template:update']"
>
修改
</el-button>
<el-button
link
type="primary"
@click="openSendForm(scope.row)"
v-hasPermi="['system:notify-template:send-notify']"
>
测试
</el-button>
<el-button
link
type="danger"
@click="handleDelete(scope.row.id)"
v-hasPermi="['system:notify-template:delete']"
>
删除
</el-button>
</template>
</el-table-column>
</el-table>
<!-- 分页 -->
<Pagination
:total="total"
v-model:page="queryParams.pageNo"
v-model:limit="queryParams.pageSize"
@pagination="getList"
/>
</ContentWrap>
<Dialog v-model="dialogFormVisible" title="测试发送" :max-height="500">
<el-form
ref="sendFormRef"
:model="sendFormData"
:rules="sendFormRules"
label-width="140px"
v-loading="formLoading"
>
<el-form-item label="模板内容" prop="content">
<el-input type="textarea" v-model="sendForm.content" readonly />
<el-input v-model="sendFormData.content" readonly />
</el-form-item>
<el-form-item label="接收人" prop="userId">
<el-select v-model="sendForm.userId" placeholder="请选择接收人">
<el-select v-model="sendFormData.userId" placeholder="请选择接收人">
<el-option
v-for="item in userOption"
:key="item.id"
@ -91,159 +173,155 @@
</el-select>
</el-form-item>
<el-form-item
v-for="param in sendForm.params"
v-for="param in sendFormData.params"
:key="param"
:label="'参数 {' + param + '}'"
:prop="'templateParams.' + param"
>
<el-input
v-model="sendForm.templateParams[param]"
v-model="sendFormData.templateParams[param]"
:placeholder="'请输入 ' + param + ' 参数'"
/>
</el-form-item>
</el-form>
<!-- 操作按钮 -->
<template #footer>
<XButton
type="primary"
:title="t('action.test')"
:loading="actionLoading"
@click="sendTest()"
/>
<XButton :title="t('dialog.close')" @click="sendVisible = false" />
<span class="dialog-footer">
<el-button @click="dialogFormVisible = false">取消</el-button>
<el-button type="primary" @click="submitForm"> 确定 </el-button>
</span>
</template>
</Dialog>
<!-- 表单弹窗添加/修改 -->
<NotifyTemplateForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts" name="SystemNotifyTemplate">
import { FormExpose } from '@/components/Form'
// import
import { rules, allSchemas } from './template.data'
<script setup lang="ts" name="NotifySmsTemplate">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import * as NotifyTemplateApi from '@/api/system/notify/template'
import { getSimpleUserList, UserVO } from '@/api/system/user'
import NotifyTemplateForm from './NotifyTemplateForm.vue'
const { t } = useI18n() //
const message = useMessage() //
//
const [registerTable, { reload, deleteData }] = useXTable({
allSchemas: allSchemas,
getListApi: NotifyTemplateApi.getNotifyTemplatePageApi,
deleteApi: NotifyTemplateApi.deleteNotifyTemplateApi
const loading = ref(false) //
const total = ref(0) //
const list = ref([]) //
const queryFormRef = ref() //
const formLoading = ref(false)
const dialogFormVisible = ref(false)
const sendFormRef = ref() // Ref
const queryParams = reactive({
pageNo: 1,
pageSize: 10,
name: undefined,
status: undefined,
code: undefined,
createTime: []
})
//
const dialogVisible = ref(false) //
const dialogTitle = ref('edit') //
const modelLoading = ref(false) // loading
const actionType = ref('') //
const actionLoading = ref(false) // Loading
const formRef = ref<FormExpose>() // Ref
const detailData = ref() // Ref
//
const setDialogTile = (type: string) => {
modelLoading.value = true
dialogTitle.value = t('action.' + type)
actionType.value = type
dialogVisible.value = true
}
//
const handleCreate = () => {
setDialogTile('create')
modelLoading.value = false
}
//
const handleUpdate = async (rowId: number) => {
setDialogTile('update')
//
const res = await NotifyTemplateApi.getNotifyTemplateApi(rowId)
unref(formRef)?.setValues(res)
modelLoading.value = false
}
//
const handleDetail = async (rowId: number) => {
setDialogTile('detail')
const res = await NotifyTemplateApi.getNotifyTemplateApi(rowId)
detailData.value = res
modelLoading.value = false
}
//
const submitForm = async () => {
const elForm = unref(formRef)?.getElFormRef()
if (!elForm) return
elForm.validate(async (valid) => {
if (valid) {
actionLoading.value = true
//
try {
const data = unref(formRef)?.formModel as NotifyTemplateApi.NotifyTemplateVO
if (actionType.value === 'create') {
await NotifyTemplateApi.createNotifyTemplateApi(data)
message.success(t('common.createSuccess'))
} else {
await NotifyTemplateApi.updateNotifyTemplateApi(data)
message.success(t('common.updateSuccess'))
}
dialogVisible.value = false
} finally {
actionLoading.value = false
//
await reload()
}
}
})
}
// ========== ==========
const sendForm = ref({
const sendFormData = ref({
content: '',
params: {},
userId: 0,
userId: null,
templateCode: '',
templateParams: {}
})
const sendRules = ref({
userId: [{ required: true, message: '用户编号不能为空', trigger: 'blur' }],
const sendFormRules = ref({
userId: [{ required: true, message: '用户编号不能为空', trigger: 'change' }],
templateCode: [{ required: true, message: '模版编号不能为空', trigger: 'blur' }],
templateParams: {}
})
const sendVisible = ref(false)
const userOption = ref<UserVO[]>([])
const handleSendNotify = (row: any) => {
sendForm.value.content = row.content
sendForm.value.params = row.params
sendForm.value.templateCode = row.code
sendForm.value.templateParams = row.params.reduce(function (obj, item) {
/** 查询列表 */
const getList = async () => {
loading.value = true
try {
const data = await NotifyTemplateApi.getNotifyTemplatePageApi(queryParams)
list.value = data.list
total.value = data.total
} finally {
loading.value = false
}
}
/** 搜索按钮操作 */
const handleQuery = () => {
queryParams.pageNo = 1
getList()
}
/** 重置按钮操作 */
const resetQuery = () => {
queryFormRef.value.resetFields()
handleQuery()
}
/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
formRef.value.open(type, id)
}
/** 删除按钮操作 */
const handleDelete = async (id: number) => {
try {
//
await message.delConfirm()
//
await NotifyTemplateApi.deleteNotifyTemplateApi(id)
message.success('删除成功')
//
await getList()
} catch {}
}
const openSendForm = (row: any) => {
sendFormData.value.content = row.content
sendFormData.value.params = row.params
sendFormData.value.templateCode = row.code
sendFormData.value.templateParams = row.params.reduce(function (obj, item) {
obj[item] = undefined
return obj
}, {})
sendRules.value.templateParams = row.params.reduce(function (obj, item) {
sendFormRules.value.templateParams = row.params.reduce(function (obj, item) {
obj[item] = { required: true, message: '参数 ' + item + ' 不能为空', trigger: 'change' }
return obj
}, {})
sendVisible.value = true
dialogFormVisible.value = true
}
const sendTest = async () => {
/** 提交表单 */
const submitForm = async () => {
//
if (!sendFormRef) return
const valid = await sendFormRef.value.validate()
if (!valid) return
//
formLoading.value = true
try {
const data: NotifyTemplateApi.NotifySendReqVO = {
userId: sendForm.value.userId,
templateCode: sendForm.value.templateCode,
templateParams: sendForm.value.templateParams as unknown as Map<string, Object>
userId: sendFormData.value.userId,
templateCode: sendFormData.value.templateCode,
templateParams: sendFormData.value.templateParams as unknown as Map<string, Object>
}
const res = await NotifyTemplateApi.sendNotifyApi(data)
if (res) {
message.success('提交发送成功!发送结果,见发送日志编号:' + res)
message.success('提交发送成功!发送结果,见消息记录编号:' + res)
}
dialogFormVisible.value = false
} finally {
formLoading.value = false
}
sendVisible.value = false
}
// ========== ==========
/** 初始化 **/
onMounted(() => {
getList()
getSimpleUserList().then((data) => {
userOption.value = data
})

View File

@ -1,85 +0,0 @@
import type { VxeCrudSchema } from '@/hooks/web/useVxeCrudSchemas'
// 表单校验
export const rules = reactive({
name: [required],
code: [required],
content: [required],
type: [required],
status: [required]
})
// CrudSchema
const crudSchemas = reactive<VxeCrudSchema>({
primaryKey: 'id',
primaryTitle: '编号',
primaryType: null,
action: true,
actionWidth: '260', // 3个按钮默认200如有删减对应增减即可
columns: [
{
title: '模版编码',
field: 'code',
isSearch: true
},
{
title: '模板名称',
field: 'name',
isSearch: true
},
{
title: '发件人名称',
field: 'nickname'
},
{
title: '类型',
field: 'type',
dictType: DICT_TYPE.SYSTEM_NOTIFY_TEMPLATE_TYPE,
dictClass: 'number'
},
{
title: '模版内容',
field: 'content',
table: {
width: 300
},
form: {
component: 'Input',
componentProps: {
type: 'textarea',
rows: 4
},
colProps: {
span: 24
}
}
},
{
title: '状态',
field: 'status',
dictType: DICT_TYPE.COMMON_STATUS,
dictClass: 'number',
isSearch: true
},
{
title: '备注',
field: 'remark'
},
{
title: '创建时间',
field: 'createTime',
isForm: false,
formatter: 'formatDate',
search: {
show: true,
itemRender: {
name: 'XDataTimePicker'
}
},
table: {
width: 180
}
}
]
})
export const { allSchemas } = useVxeCrudSchemas(crudSchemas)