index.vue 7.0 KB

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