index.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <template>
  2. <ContentWrap>
  3. <!-- 列表 -->
  4. <vxe-grid ref="xGrid" v-bind="gridOptions" class="xtable-scrollbar">
  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="handleDelete(row.id)"
  42. />
  43. </template>
  44. </vxe-grid>
  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="dialogVisible = false" />
  108. </template>
  109. </XModal>
  110. </template>
  111. <script setup lang="ts">
  112. // 全局相关的 import
  113. import { ref, unref } from 'vue'
  114. import { useI18n } from '@/hooks/web/useI18n'
  115. import { useMessage } from '@/hooks/web/useMessage'
  116. import { useVxeGrid } from '@/hooks/web/useVxeGrid'
  117. import { VxeGridInstance } from 'vxe-table'
  118. import { FormExpose } from '@/components/Form'
  119. // 业务相关的 import
  120. import * as SmsTemplateApi from '@/api/system/sms/smsTemplate'
  121. import { rules, allSchemas } from './sms.template.data'
  122. const { t } = useI18n() // 国际化
  123. const message = useMessage() // 消息弹窗
  124. // 列表相关的变量
  125. const xGrid = ref<VxeGridInstance>() // 列表 Grid Ref
  126. const { gridOptions, getList, deleteData } = useVxeGrid<SmsTemplateApi.SmsTemplateVO>({
  127. allSchemas: allSchemas,
  128. getListApi: SmsTemplateApi.getSmsTemplatePageApi,
  129. deleteApi: SmsTemplateApi.deleteSmsTemplateApi
  130. })
  131. // 弹窗相关的变量
  132. const dialogVisible = ref(false) // 是否显示弹出层
  133. const dialogTitle = ref('edit') // 弹出层标题
  134. const actionType = ref('') // 操作按钮的类型
  135. const actionLoading = ref(false) // 按钮 Loading
  136. const formRef = ref<FormExpose>() // 表单 Ref
  137. const detailData = ref() // 详情 Ref
  138. // 设置标题
  139. const setDialogTile = (type: string) => {
  140. dialogTitle.value = t('action.' + type)
  141. actionType.value = type
  142. dialogVisible.value = true
  143. }
  144. // 新增操作
  145. const handleCreate = () => {
  146. setDialogTile('create')
  147. }
  148. // 修改操作
  149. const handleUpdate = async (rowId: number) => {
  150. setDialogTile('update')
  151. // 设置数据
  152. const res = await SmsTemplateApi.getSmsTemplateApi(rowId)
  153. unref(formRef)?.setValues(res)
  154. }
  155. // 详情操作
  156. const handleDetail = async (rowId: number) => {
  157. setDialogTile('detail')
  158. // 设置数据
  159. const res = await SmsTemplateApi.getSmsTemplateApi(rowId)
  160. detailData.value = res
  161. }
  162. // 删除操作
  163. const handleDelete = async (rowId: number) => {
  164. await deleteData(xGrid, rowId)
  165. }
  166. // 提交按钮
  167. const submitForm = async () => {
  168. const elForm = unref(formRef)?.getElFormRef()
  169. if (!elForm) return
  170. elForm.validate(async (valid) => {
  171. if (valid) {
  172. actionLoading.value = true
  173. // 提交请求
  174. try {
  175. const data = unref(formRef)?.formModel as SmsTemplateApi.SmsTemplateVO
  176. if (actionType.value === 'create') {
  177. await SmsTemplateApi.createSmsTemplateApi(data)
  178. message.success(t('common.createSuccess'))
  179. } else {
  180. await SmsTemplateApi.updateSmsTemplateApi(data)
  181. message.success(t('common.updateSuccess'))
  182. }
  183. dialogVisible.value = false
  184. } finally {
  185. actionLoading.value = false
  186. // 刷新列表
  187. await getList(xGrid)
  188. }
  189. }
  190. })
  191. }
  192. // ========== 测试相关 ==========
  193. const sendSmsForm = ref({
  194. content: '',
  195. params: {},
  196. mobile: '141',
  197. templateCode: '',
  198. templateParams: {}
  199. })
  200. const sendSmsRules = ref({
  201. mobile: [{ required: true, message: '手机不能为空', trigger: 'blur' }],
  202. templateCode: [{ required: true, message: '手机不能为空', trigger: 'blur' }],
  203. templateParams: {}
  204. })
  205. const sendVisible = ref(false)
  206. const handleSendSms = (row: any) => {
  207. sendSmsForm.value.content = row.content
  208. sendSmsForm.value.params = row.params
  209. sendSmsForm.value.templateCode = row.code
  210. sendSmsForm.value.templateParams = row.params.reduce(function (obj, item) {
  211. obj[item] = undefined
  212. return obj
  213. }, {})
  214. sendSmsRules.value.templateParams = row.params.reduce(function (obj, item) {
  215. obj[item] = { required: true, message: '参数 ' + item + ' 不能为空', trigger: 'change' }
  216. return obj
  217. }, {})
  218. sendVisible.value = true
  219. }
  220. const sendSmsTest = () => {
  221. const data = {
  222. mobile: sendSmsForm.value.mobile,
  223. templateCode: sendSmsForm.value.templateCode,
  224. templateParams: sendSmsForm.value.templateParams
  225. }
  226. SmsTemplateApi.sendSmsApi(data)
  227. message.info('发送成功')
  228. sendVisible.value = false
  229. }
  230. </script>