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

Merge pull request !124 from ZanGe丶/dev
This commit is contained in:
芋道源码 2023-04-15 08:46:56 +00:00 committed by Gitee
commit 97f9560c4d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 407 additions and 273 deletions

View File

@ -1,13 +1,14 @@
import request from '@/config/axios' import request from '@/config/axios'
export interface NotifyTemplateVO { export interface NotifyTemplateVO {
id: number id: number | null
name: string name: string
nickname: string
code: string code: string
content: string content: string
type: number type: number | null
params: string params: string
status: number status: number | null
remark: string remark: string
} }
@ -19,7 +20,7 @@ export interface NotifyTemplatePageReqVO extends PageParam {
} }
export interface NotifySendReqVO { export interface NotifySendReqVO {
userId: number userId: number | null
templateCode: string templateCode: string
templateParams: Map<String, Object> 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> <template>
<doc-alert title="站内信配置" url="https://doc.iocoder.cn/notify/" />
<ContentWrap> <ContentWrap>
<!-- 列表 --> <!-- 搜索工作栏 -->
<XTable @register="registerTable"> <el-form
<template #toolbar_buttons> class="-mb-15px"
<!-- 操作新增 --> :model="queryParams"
<XButton 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" type="primary"
preIcon="ep:zoom-in" plain
:title="t('action.add')" @click="openForm('create')"
v-hasPermi="['system:notify-template:create']" v-hasPermi="['system:notify-template:create']"
@click="handleCreate()" >
/> <Icon icon="ep:plus" class="mr-5px" />新增
</template> </el-button>
<template #actionbtns_default="{ row }"> </el-form-item>
<!-- 操作测试站内信 --> </el-form>
<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>
</ContentWrap> </ContentWrap>
<!-- 添加/修改的弹窗 --> <!-- 列表 -->
<Dialog id="templateModel" :loading="modelLoading" v-model="dialogVisible" :title="dialogTitle"> <ContentWrap>
<!-- 表单添加/修改 --> <el-table v-loading="loading" :data="list">
<Form <el-table-column
ref="formRef" label="模板编码"
v-if="['create', 'update'].includes(actionType)" align="center"
:schema="allSchemas.formSchema" prop="code"
:rules="rules" width="120"
/> :show-overflow-tooltip="true"
<!-- 表单详情 --> />
<Descriptions <el-table-column
v-if="actionType === 'detail'" label="模板名称"
:schema="allSchemas.detailSchema" align="center"
:data="detailData" prop="name"
/> width="120"
<template #footer> :show-overflow-tooltip="true"
<!-- 按钮保存 --> />
<XButton <el-table-column label="类型" align="center" prop="type">
v-if="['create', 'update'].includes(actionType)" <template #default="scope">
type="primary" <dict-tag :type="DICT_TYPE.SYSTEM_NOTIFY_TEMPLATE_TYPE" :value="scope.row.type" />
:title="t('action.save')" </template>
:loading="actionLoading" </el-table-column>
@click="submitForm()" <el-table-column label="发送人名称" align="center" prop="nickname" />
<el-table-column
label="模板内容"
align="center"
prop="content"
width="200"
:show-overflow-tooltip="true"
/> />
<!-- 按钮关闭 -->
<XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
</template>
</Dialog>
<!-- 测试站内信的弹窗 --> <el-table-column label="开启状态" align="center" prop="status" width="80">
<Dialog id="sendTest" v-model="sendVisible" title="测试"> <template #default="scope">
<el-form :model="sendForm" :rules="sendRules" label-width="200px" label-position="top"> <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-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>
<el-form-item label="接收人" prop="userId"> <el-form-item label="接收人" prop="userId">
<el-select v-model="sendForm.userId" placeholder="请选择接收人"> <el-select v-model="sendFormData.userId" placeholder="请选择接收人">
<el-option <el-option
v-for="item in userOption" v-for="item in userOption"
:key="item.id" :key="item.id"
@ -91,159 +173,155 @@
</el-select> </el-select>
</el-form-item> </el-form-item>
<el-form-item <el-form-item
v-for="param in sendForm.params" v-for="param in sendFormData.params"
:key="param" :key="param"
:label="'参数 {' + param + '}'" :label="'参数 {' + param + '}'"
:prop="'templateParams.' + param" :prop="'templateParams.' + param"
> >
<el-input <el-input
v-model="sendForm.templateParams[param]" v-model="sendFormData.templateParams[param]"
:placeholder="'请输入 ' + param + ' 参数'" :placeholder="'请输入 ' + param + ' 参数'"
/> />
</el-form-item> </el-form-item>
</el-form> </el-form>
<!-- 操作按钮 -->
<template #footer> <template #footer>
<XButton <span class="dialog-footer">
type="primary" <el-button @click="dialogFormVisible = false">取消</el-button>
:title="t('action.test')" <el-button type="primary" @click="submitForm"> 确定 </el-button>
:loading="actionLoading" </span>
@click="sendTest()"
/>
<XButton :title="t('dialog.close')" @click="sendVisible = false" />
</template> </template>
</Dialog> </Dialog>
<!-- 表单弹窗添加/修改 -->
<NotifyTemplateForm ref="formRef" @success="getList" />
</template> </template>
<script setup lang="ts" name="SystemNotifyTemplate"> <script setup lang="ts" name="NotifySmsTemplate">
import { FormExpose } from '@/components/Form' import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
// import import { dateFormatter } from '@/utils/formatTime'
import { rules, allSchemas } from './template.data'
import * as NotifyTemplateApi from '@/api/system/notify/template' import * as NotifyTemplateApi from '@/api/system/notify/template'
import { getSimpleUserList, UserVO } from '@/api/system/user' import { getSimpleUserList, UserVO } from '@/api/system/user'
import NotifyTemplateForm from './NotifyTemplateForm.vue'
const { t } = useI18n() //
const message = useMessage() // const message = useMessage() //
// const loading = ref(false) //
const [registerTable, { reload, deleteData }] = useXTable({ const total = ref(0) //
allSchemas: allSchemas, const list = ref([]) //
getListApi: NotifyTemplateApi.getNotifyTemplatePageApi, const queryFormRef = ref() //
deleteApi: NotifyTemplateApi.deleteNotifyTemplateApi
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 sendFormData = ref({
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({
content: '', content: '',
params: {}, params: {},
userId: 0, userId: null,
templateCode: '', templateCode: '',
templateParams: {} 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' }], templateCode: [{ required: true, message: '模版编号不能为空', trigger: 'blur' }],
templateParams: {} templateParams: {}
}) })
const sendVisible = ref(false)
const userOption = ref<UserVO[]>([]) const userOption = ref<UserVO[]>([])
const handleSendNotify = (row: any) => { /** 查询列表 */
sendForm.value.content = row.content const getList = async () => {
sendForm.value.params = row.params loading.value = true
sendForm.value.templateCode = row.code try {
sendForm.value.templateParams = row.params.reduce(function (obj, item) { 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 obj[item] = undefined
return obj 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' } obj[item] = { required: true, message: '参数 ' + item + ' 不能为空', trigger: 'change' }
return obj return obj
}, {}) }, {})
sendVisible.value = true dialogFormVisible.value = true
} }
const sendTest = async () => { /** 提交表单 */
const data: NotifyTemplateApi.NotifySendReqVO = { const submitForm = async () => {
userId: sendForm.value.userId, //
templateCode: sendForm.value.templateCode, if (!sendFormRef) return
templateParams: sendForm.value.templateParams as unknown as Map<string, Object> const valid = await sendFormRef.value.validate()
if (!valid) return
//
formLoading.value = true
try {
const data: NotifyTemplateApi.NotifySendReqVO = {
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)
}
dialogFormVisible.value = false
} finally {
formLoading.value = false
} }
const res = await NotifyTemplateApi.sendNotifyApi(data)
if (res) {
message.success('提交发送成功!发送结果,见发送日志编号:' + res)
}
sendVisible.value = false
} }
// ========== ========== /** 初始化 **/
onMounted(() => { onMounted(() => {
getList()
getSimpleUserList().then((data) => { getSimpleUserList().then((data) => {
userOption.value = 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)