index.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <ContentWrap>
  3. <!-- 列表 -->
  4. <XTable @register="registerTable">
  5. <template #toolbar_buttons>
  6. <!-- 操作:新增 -->
  7. <XButton
  8. type="primary"
  9. preIcon="ep:zoom-in"
  10. :title="t('action.add')"
  11. v-hasPermi="['system:notify-template:create']"
  12. @click="handleCreate()"
  13. />
  14. </template>
  15. <template #actionbtns_default="{ row }">
  16. <!-- 操作:测试站内信 -->
  17. <XTextButton
  18. preIcon="ep:cpu"
  19. :title="t('action.test')"
  20. v-hasPermi="['system:notify-template:send-notify']"
  21. @click="handleSendNotify(row)"
  22. />
  23. <!-- 操作:修改 -->
  24. <XTextButton
  25. preIcon="ep:edit"
  26. :title="t('action.edit')"
  27. v-hasPermi="['system:notify-template:update']"
  28. @click="handleUpdate(row.id)"
  29. />
  30. <!-- 操作:详情 -->
  31. <XTextButton
  32. preIcon="ep:view"
  33. :title="t('action.detail')"
  34. v-hasPermi="['system:notify-template:query']"
  35. @click="handleDetail(row.id)"
  36. />
  37. <!-- 操作:删除 -->
  38. <XTextButton
  39. preIcon="ep:delete"
  40. :title="t('action.del')"
  41. v-hasPermi="['system:notify-template:delete']"
  42. @click="deleteData(row.id)"
  43. />
  44. </template>
  45. </XTable>
  46. </ContentWrap>
  47. <!-- 添加/修改的弹窗 -->
  48. <XModal id="templateModel" :loading="modelLoading" v-model="dialogVisible" :title="dialogTitle">
  49. <!-- 表单:添加/修改 -->
  50. <Form
  51. ref="formRef"
  52. v-if="['create', 'update'].includes(actionType)"
  53. :schema="allSchemas.formSchema"
  54. :rules="rules"
  55. />
  56. <!-- 表单:详情 -->
  57. <Descriptions
  58. v-if="actionType === 'detail'"
  59. :schema="allSchemas.detailSchema"
  60. :data="detailData"
  61. />
  62. <template #footer>
  63. <!-- 按钮:保存 -->
  64. <XButton
  65. v-if="['create', 'update'].includes(actionType)"
  66. type="primary"
  67. :title="t('action.save')"
  68. :loading="actionLoading"
  69. @click="submitForm()"
  70. />
  71. <!-- 按钮:关闭 -->
  72. <XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
  73. </template>
  74. </XModal>
  75. <!-- 测试站内信的弹窗 -->
  76. <XModal id="sendTest" v-model="sendVisible" title="测试">
  77. <el-form :model="sendForm" :rules="sendRules" label-width="200px" label-position="top">
  78. <el-form-item label="模板内容" prop="content">
  79. <el-input type="textarea" v-model="sendForm.content" readonly />
  80. </el-form-item>
  81. <el-form-item label="接收人" prop="userId">
  82. <el-select v-model="sendForm.userId" placeholder="请选择接收人">
  83. <el-option
  84. v-for="item in userOption"
  85. :key="item.id"
  86. :label="item.nickname"
  87. :value="item.id"
  88. />
  89. </el-select>
  90. </el-form-item>
  91. <el-form-item
  92. v-for="param in sendForm.params"
  93. :key="param"
  94. :label="'参数 {' + param + '}'"
  95. :prop="'templateParams.' + param"
  96. >
  97. <el-input
  98. v-model="sendForm.templateParams[param]"
  99. :placeholder="'请输入 ' + param + ' 参数'"
  100. />
  101. </el-form-item>
  102. </el-form>
  103. <!-- 操作按钮 -->
  104. <template #footer>
  105. <XButton
  106. type="primary"
  107. :title="t('action.test')"
  108. :loading="actionLoading"
  109. @click="sendTest()"
  110. />
  111. <XButton :title="t('dialog.close')" @click="sendVisible = false" />
  112. </template>
  113. </XModal>
  114. </template>
  115. <script setup lang="ts" name="NotifyTemplate">
  116. import { FormExpose } from '@/components/Form'
  117. // 业务相关的 import
  118. import { rules, allSchemas } from './template.data'
  119. import * as NotifyTemplateApi from '@/api/system/notify/template'
  120. import { getSimpleUserList, UserVO } from '@/api/system/user'
  121. const { t } = useI18n() // 国际化
  122. const message = useMessage() // 消息弹窗
  123. // 列表相关的变量
  124. const [registerTable, { reload, deleteData }] = useXTable({
  125. allSchemas: allSchemas,
  126. getListApi: NotifyTemplateApi.getNotifyTemplatePageApi,
  127. deleteApi: NotifyTemplateApi.deleteNotifyTemplateApi
  128. })
  129. // 弹窗相关的变量
  130. const dialogVisible = ref(false) // 是否显示弹出层
  131. const dialogTitle = ref('edit') // 弹出层标题
  132. const modelLoading = ref(false) // 弹出层loading
  133. const actionType = ref('') // 操作按钮的类型
  134. const actionLoading = ref(false) // 按钮 Loading
  135. const formRef = ref<FormExpose>() // 表单 Ref
  136. const detailData = ref() // 详情 Ref
  137. // 设置标题
  138. const setDialogTile = (type: string) => {
  139. modelLoading.value = true
  140. dialogTitle.value = t('action.' + type)
  141. actionType.value = type
  142. dialogVisible.value = true
  143. }
  144. // 新增操作
  145. const handleCreate = () => {
  146. setDialogTile('create')
  147. modelLoading.value = false
  148. }
  149. // 修改操作
  150. const handleUpdate = async (rowId: number) => {
  151. setDialogTile('update')
  152. // 设置数据
  153. const res = await NotifyTemplateApi.getNotifyTemplateApi(rowId)
  154. unref(formRef)?.setValues(res)
  155. modelLoading.value = false
  156. }
  157. // 详情操作
  158. const handleDetail = async (rowId: number) => {
  159. setDialogTile('detail')
  160. const res = await NotifyTemplateApi.getNotifyTemplateApi(rowId)
  161. detailData.value = res
  162. modelLoading.value = false
  163. }
  164. // 提交按钮
  165. const submitForm = async () => {
  166. const elForm = unref(formRef)?.getElFormRef()
  167. if (!elForm) return
  168. elForm.validate(async (valid) => {
  169. if (valid) {
  170. actionLoading.value = true
  171. // 提交请求
  172. try {
  173. const data = unref(formRef)?.formModel as NotifyTemplateApi.NotifyTemplateVO
  174. if (actionType.value === 'create') {
  175. await NotifyTemplateApi.createNotifyTemplateApi(data)
  176. message.success(t('common.createSuccess'))
  177. } else {
  178. await NotifyTemplateApi.updateNotifyTemplateApi(data)
  179. message.success(t('common.updateSuccess'))
  180. }
  181. dialogVisible.value = false
  182. } finally {
  183. actionLoading.value = false
  184. // 刷新列表
  185. await reload()
  186. }
  187. }
  188. })
  189. }
  190. // ========== 测试相关 ==========
  191. const sendForm = ref({
  192. content: '',
  193. params: {},
  194. userId: 0,
  195. templateCode: '',
  196. templateParams: {}
  197. })
  198. const sendRules = ref({
  199. userId: [{ required: true, message: '用户编号不能为空', trigger: 'blur' }],
  200. templateCode: [{ required: true, message: '模版编号不能为空', trigger: 'blur' }],
  201. templateParams: {}
  202. })
  203. const sendVisible = ref(false)
  204. const userOption = ref<UserVO[]>([])
  205. const handleSendNotify = (row: any) => {
  206. sendForm.value.content = row.content
  207. sendForm.value.params = row.params
  208. sendForm.value.templateCode = row.code
  209. sendForm.value.templateParams = row.params.reduce(function (obj, item) {
  210. obj[item] = undefined
  211. return obj
  212. }, {})
  213. sendRules.value.templateParams = row.params.reduce(function (obj, item) {
  214. obj[item] = { required: true, message: '参数 ' + item + ' 不能为空', trigger: 'change' }
  215. return obj
  216. }, {})
  217. sendVisible.value = true
  218. }
  219. const sendTest = async () => {
  220. const data: NotifyTemplateApi.NotifySendReqVO = {
  221. userId: sendForm.value.userId,
  222. templateCode: sendForm.value.templateCode,
  223. templateParams: sendForm.value.templateParams as unknown as Map<string, Object>
  224. }
  225. const res = await NotifyTemplateApi.sendNotifyApi(data)
  226. if (res) {
  227. message.success('提交发送成功!发送结果,见发送日志编号:' + res)
  228. }
  229. sendVisible.value = false
  230. }
  231. // ========== 初始化 ==========
  232. onMounted(() => {
  233. getSimpleUserList().then((data) => {
  234. userOption.value = data
  235. })
  236. })
  237. </script>