Files
ipms-sjy/yudao-ui-admin-vue3/src/views/system/sms/smsTemplate/index.vue

245 lines
7.2 KiB
Vue
Raw Normal View History

2022-11-15 17:42:04 +08:00
<template>
<ContentWrap>
<!-- 列表 -->
2022-11-15 20:43:02 +08:00
<vxe-grid ref="xGrid" v-bind="gridOptions" class="xtable-scrollbar">
<!-- 操作新增 -->
<template #toolbar_buttons>
<XButton
type="primary"
preIcon="ep:zoom-in"
:title="t('action.add')"
v-hasPermi="['system:sms-channel:create']"
@click="handleCreate()"
/>
2022-11-15 17:42:04 +08:00
</template>
2022-11-15 20:43:02 +08:00
<template #actionbtns_default="{ row }">
2022-11-15 17:42:04 +08:00
<XTextButton
preIcon="ep:cpu"
:title="t('action.test')"
v-hasPermi="['system:sms-template:send-sms']"
@click="handleSendSms(row)"
/>
<!-- 操作修改 -->
<XTextButton
preIcon="ep:edit"
:title="t('action.edit')"
v-hasPermi="['system:sms-template:update']"
@click="handleUpdate(row.id)"
/>
<!-- 操作详情 -->
<XTextButton
preIcon="ep:view"
:title="t('action.detail')"
2022-11-16 10:07:24 +08:00
v-hasPermi="['system:sms-template:query']"
2022-11-15 20:43:02 +08:00
@click="handleDetail(row.id)"
2022-11-15 17:42:04 +08:00
/>
<!-- 操作删除 -->
<XTextButton
preIcon="ep:delete"
:title="t('action.del')"
v-hasPermi="['system:sms-template:delete']"
2022-11-15 20:43:02 +08:00
@click="handleDelete(row.id)"
2022-11-15 17:42:04 +08:00
/>
</template>
2022-11-15 20:43:02 +08:00
</vxe-grid>
2022-11-15 17:42:04 +08:00
</ContentWrap>
2022-11-15 20:43:02 +08:00
<XModal id="smsTemplate" v-model="dialogVisible" :title="dialogTitle">
2022-11-15 17:42:04 +08:00
<!-- 对话框(添加 / 修改) -->
<Form
v-if="['create', 'update'].includes(actionType)"
:schema="allSchemas.formSchema"
:rules="rules"
ref="formRef"
/>
<!-- 对话框(详情) -->
<Descriptions
v-if="actionType === 'detail'"
:schema="allSchemas.detailSchema"
2022-11-15 20:43:02 +08:00
:data="detailData"
2022-11-15 17:42:04 +08:00
/>
<!-- 操作按钮 -->
<template #footer>
<!-- 按钮保存 -->
<XButton
v-if="['create', 'update'].includes(actionType)"
type="primary"
:title="t('action.save')"
2022-11-15 20:43:02 +08:00
:loading="actionLoading"
2022-11-15 17:42:04 +08:00
@click="submitForm()"
/>
<!-- 按钮关闭 -->
2022-11-15 20:43:02 +08:00
<XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
2022-11-15 17:42:04 +08:00
</template>
</XModal>
2022-11-15 20:43:02 +08:00
<XModal id="sendTest" v-model="sendVisible" title="测试">
2022-11-15 17:42:04 +08:00
<el-form :model="sendSmsForm" :rules="sendSmsRules" label-width="140px">
<el-form-item label="模板内容" prop="content">
<el-input
v-model="sendSmsForm.content"
type="textarea"
placeholder="请输入模板内容"
readonly
/>
</el-form-item>
<el-form-item label="手机号" prop="mobile">
<el-input v-model="sendSmsForm.mobile" placeholder="请输入手机号" />
</el-form-item>
<el-form-item
v-for="param in sendSmsForm.params"
:key="param"
:label="'参数 {' + param + '}'"
:prop="'templateParams.' + param"
>
<el-input
v-model="sendSmsForm.templateParams[param]"
:placeholder="'请输入 ' + param + ' 参数'"
/>
</el-form-item>
</el-form>
<!-- 操作按钮 -->
<template #footer>
2022-11-15 20:43:02 +08:00
<XButton
type="primary"
:title="t('action.test')"
:loading="actionLoading"
@click="sendSmsTest()"
/>
2022-11-15 17:42:04 +08:00
<XButton :title="t('dialog.close')" @click="dialogVisible = false" />
</template>
</XModal>
</template>
2022-07-18 19:06:37 +08:00
<script setup lang="ts">
2022-11-15 20:43:02 +08:00
// 全局相关的 import
2022-07-25 12:55:02 +08:00
import { ref, unref } from 'vue'
2022-07-18 19:06:37 +08:00
import { useI18n } from '@/hooks/web/useI18n'
2022-11-15 20:43:02 +08:00
import { useMessage } from '@/hooks/web/useMessage'
import { useVxeGrid } from '@/hooks/web/useVxeGrid'
import { VxeGridInstance } from 'vxe-table'
2022-07-18 19:06:37 +08:00
import { FormExpose } from '@/components/Form'
2022-11-15 20:43:02 +08:00
// 业务相关的 import
2022-07-18 19:06:37 +08:00
import * as SmsTemplateApi from '@/api/system/sms/smsTemplate'
2022-11-15 20:43:02 +08:00
import { rules, allSchemas } from './sms.template.data'
2022-07-18 19:06:37 +08:00
const { t } = useI18n() // 国际化
2022-11-15 20:43:02 +08:00
const message = useMessage() // 消息弹窗
2022-07-18 19:06:37 +08:00
2022-11-15 20:43:02 +08:00
// 列表相关的变量
const xGrid = ref<VxeGridInstance>() // 列表 Grid Ref
2022-11-16 10:28:28 +08:00
const { gridOptions, reloadList, deleteData } = useVxeGrid<SmsTemplateApi.SmsTemplateVO>({
2022-11-15 20:43:02 +08:00
allSchemas: allSchemas,
2022-07-18 19:06:37 +08:00
getListApi: SmsTemplateApi.getSmsTemplatePageApi,
2022-11-16 10:28:28 +08:00
deleteApi: SmsTemplateApi.deleteSmsTemplateApi
2022-07-18 19:06:37 +08:00
})
2022-11-15 20:43:02 +08:00
// 弹窗相关的变量
2022-07-18 19:06:37 +08:00
const dialogVisible = ref(false) // 是否显示弹出层
const dialogTitle = ref('edit') // 弹出层标题
2022-11-15 20:43:02 +08:00
const actionType = ref('') // 操作按钮的类型
const actionLoading = ref(false) // 按钮 Loading
2022-07-18 19:06:37 +08:00
const formRef = ref<FormExpose>() // 表单 Ref
2022-11-15 20:43:02 +08:00
const detailData = ref() // 详情 Ref
2022-07-18 19:06:37 +08:00
// 设置标题
const setDialogTile = (type: string) => {
dialogTitle.value = t('action.' + type)
actionType.value = type
dialogVisible.value = true
}
// 新增操作
const handleCreate = () => {
setDialogTile('create')
}
// 修改操作
2022-11-15 17:42:04 +08:00
const handleUpdate = async (rowId: number) => {
2022-07-18 19:06:37 +08:00
// 设置数据
2022-11-15 17:42:04 +08:00
const res = await SmsTemplateApi.getSmsTemplateApi(rowId)
2022-07-18 19:06:37 +08:00
unref(formRef)?.setValues(res)
2022-11-16 10:28:28 +08:00
setDialogTile('update')
2022-07-18 19:06:37 +08:00
}
2022-11-15 20:43:02 +08:00
// 详情操作
const handleDetail = async (rowId: number) => {
// 设置数据
const res = await SmsTemplateApi.getSmsTemplateApi(rowId)
detailData.value = res
2022-11-16 10:28:28 +08:00
setDialogTile('detail')
2022-11-15 20:43:02 +08:00
}
// 删除操作
const handleDelete = async (rowId: number) => {
2022-11-16 10:28:28 +08:00
await deleteData(xGrid, rowId)
2022-11-15 20:43:02 +08:00
}
2022-07-18 19:06:37 +08:00
// 提交按钮
const submitForm = async () => {
2022-10-17 11:24:22 +08:00
const elForm = unref(formRef)?.getElFormRef()
if (!elForm) return
elForm.validate(async (valid) => {
if (valid) {
2022-11-15 20:43:02 +08:00
actionLoading.value = true
2022-10-17 11:24:22 +08:00
// 提交请求
try {
2022-11-15 20:43:02 +08:00
const data = unref(formRef)?.formModel as SmsTemplateApi.SmsTemplateVO
2022-10-17 11:24:22 +08:00
if (actionType.value === 'create') {
await SmsTemplateApi.createSmsTemplateApi(data)
2022-11-15 20:43:02 +08:00
message.success(t('common.createSuccess'))
2022-10-17 11:24:22 +08:00
} else {
await SmsTemplateApi.updateSmsTemplateApi(data)
2022-11-15 20:43:02 +08:00
message.success(t('common.updateSuccess'))
2022-10-17 11:24:22 +08:00
}
dialogVisible.value = false
} finally {
2022-11-15 20:43:02 +08:00
actionLoading.value = false
// 刷新列表
2022-11-16 10:28:28 +08:00
await reloadList(xGrid)
2022-10-17 11:24:22 +08:00
}
2022-07-18 19:06:37 +08:00
}
2022-10-17 11:24:22 +08:00
})
2022-07-18 19:06:37 +08:00
}
2022-07-25 12:55:02 +08:00
// ========== 测试相关 ==========
const sendSmsForm = ref({
2022-07-18 19:06:37 +08:00
content: '',
2022-07-25 12:55:02 +08:00
params: {},
mobile: '141',
2022-07-18 19:06:37 +08:00
templateCode: '',
templateParams: {}
})
2022-07-25 12:55:02 +08:00
const sendSmsRules = ref({
mobile: [{ required: true, message: '手机不能为空', trigger: 'blur' }],
templateCode: [{ required: true, message: '手机不能为空', trigger: 'blur' }],
templateParams: {}
})
const sendVisible = ref(false)
2022-07-18 19:06:37 +08:00
const handleSendSms = (row: any) => {
2022-07-25 12:55:02 +08:00
sendSmsForm.value.content = row.content
sendSmsForm.value.params = row.params
sendSmsForm.value.templateCode = row.code
sendSmsForm.value.templateParams = row.params.reduce(function (obj, item) {
2022-07-18 19:06:37 +08:00
obj[item] = undefined
return obj
}, {})
2022-07-25 12:55:02 +08:00
sendSmsRules.value.templateParams = row.params.reduce(function (obj, item) {
obj[item] = { required: true, message: '参数 ' + item + ' 不能为空', trigger: 'change' }
return obj
}, {})
sendVisible.value = true
}
const sendSmsTest = () => {
const data = {
mobile: sendSmsForm.value.mobile,
templateCode: sendSmsForm.value.templateCode,
templateParams: sendSmsForm.value.templateParams
}
SmsTemplateApi.sendSmsApi(data)
2022-11-15 20:43:02 +08:00
message.info('发送成功')
2022-07-25 12:55:02 +08:00
sendVisible.value = false
2022-07-18 19:06:37 +08:00
}
</script>