index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <script setup lang="ts">
  2. import { reactive, ref, unref } from 'vue'
  3. import dayjs from 'dayjs'
  4. import { ElMessage } from 'element-plus'
  5. import { DICT_TYPE } from '@/utils/dict'
  6. import { useTable } from '@/hooks/web/useTable'
  7. import { useI18n } from '@/hooks/web/useI18n'
  8. import { FormExpose } from '@/components/Form'
  9. import type { SmsTemplateVO } from '@/api/system/sms/smsTemplate/types'
  10. import { rules, allSchemas } from './sms.template.data'
  11. import * as SmsTemplateApi from '@/api/system/sms/smsTemplate'
  12. const { t } = useI18n() // 国际化
  13. // ========== 列表相关 ==========
  14. const { register, tableObject, methods } = useTable<SmsTemplateVO>({
  15. getListApi: SmsTemplateApi.getSmsTemplatePageApi,
  16. delListApi: SmsTemplateApi.deleteSmsTemplateApi
  17. })
  18. const { getList, setSearchParams, delList } = methods
  19. // ========== CRUD 相关 ==========
  20. const loading = ref(false) // 遮罩层
  21. const actionType = ref('') // 操作按钮的类型
  22. const dialogVisible = ref(false) // 是否显示弹出层
  23. const dialogTitle = ref('edit') // 弹出层标题
  24. const formRef = ref<FormExpose>() // 表单 Ref
  25. // 设置标题
  26. const setDialogTile = (type: string) => {
  27. dialogTitle.value = t('action.' + type)
  28. actionType.value = type
  29. dialogVisible.value = true
  30. }
  31. // 新增操作
  32. const handleCreate = () => {
  33. setDialogTile('create')
  34. // 重置表单
  35. unref(formRef)?.getElFormRef()?.resetFields()
  36. }
  37. // 修改操作
  38. const handleUpdate = async (row: SmsTemplateVO) => {
  39. setDialogTile('update')
  40. // 设置数据
  41. const res = await SmsTemplateApi.getSmsTemplateApi(row.id)
  42. unref(formRef)?.setValues(res)
  43. }
  44. // 提交按钮
  45. const submitForm = async () => {
  46. loading.value = true
  47. // 提交请求
  48. try {
  49. const data = unref(formRef)?.formModel as SmsTemplateVO
  50. if (actionType.value === 'create') {
  51. await SmsTemplateApi.createSmsTemplateApi(data)
  52. ElMessage.success(t('common.createSuccess'))
  53. } else {
  54. await SmsTemplateApi.updateSmsTemplateApi(data)
  55. ElMessage.success(t('common.updateSuccess'))
  56. }
  57. // 操作成功,重新加载列表
  58. dialogVisible.value = false
  59. await getList()
  60. } finally {
  61. loading.value = false
  62. }
  63. }
  64. // 删除操作
  65. const handleDelete = (row: SmsTemplateVO) => {
  66. delList(row.id, false)
  67. }
  68. // ========== 详情相关 ==========
  69. const detailRef = ref() // 详情 Ref
  70. // 详情操作
  71. const handleDetail = async (row: SmsTemplateVO) => {
  72. // 设置数据
  73. detailRef.value = row
  74. setDialogTile('detail')
  75. }
  76. const sendSmsForm = reactive({
  77. content: '',
  78. params: '',
  79. mobile: '',
  80. templateCode: '',
  81. templateParams: {}
  82. })
  83. // TODO 发送短信
  84. // ========== 测试相关 ==========
  85. const handleSendSms = (row: any) => {
  86. sendSmsForm.content = row.content
  87. sendSmsForm.params = row.params
  88. sendSmsForm.templateCode = row.code
  89. sendSmsForm.templateParams = row.params.reduce(function (obj, item) {
  90. obj[item] = undefined
  91. return obj
  92. }, {})
  93. }
  94. // ========== 初始化 ==========
  95. getList()
  96. </script>
  97. <template>
  98. <!-- 搜索工作区 -->
  99. <ContentWrap>
  100. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  101. </ContentWrap>
  102. <ContentWrap>
  103. <!-- 操作工具栏 -->
  104. <div class="mb-10px">
  105. <el-button type="primary" v-hasPermi="['system:sms-channel:create']" @click="handleCreate">
  106. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  107. </el-button>
  108. </div>
  109. <!-- 列表 -->
  110. <Table
  111. :columns="allSchemas.tableColumns"
  112. :selection="false"
  113. :data="tableObject.tableList"
  114. :loading="tableObject.loading"
  115. :pagination="{
  116. total: tableObject.total
  117. }"
  118. v-model:pageSize="tableObject.pageSize"
  119. v-model:currentPage="tableObject.currentPage"
  120. @register="register"
  121. >
  122. <template #type="{ row }">
  123. <DictTag :type="DICT_TYPE.SYSTEM_SMS_TEMPLATE_TYPE" :value="row.type" />
  124. </template>
  125. <template #status="{ row }">
  126. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  127. </template>
  128. <template #createTime="{ row }">
  129. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  130. </template>
  131. <template #action="{ row }">
  132. <el-button
  133. link
  134. type="primary"
  135. v-hasPermi="['system:sms-template:send-sms']"
  136. @click="handleSendSms(row)"
  137. >
  138. <Icon icon="ep:cpu" class="mr-1px" /> {{ t('action.test') }}
  139. </el-button>
  140. <el-button
  141. link
  142. type="primary"
  143. v-hasPermi="['system:sms-template:update']"
  144. @click="handleUpdate(row)"
  145. >
  146. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  147. </el-button>
  148. <el-button
  149. link
  150. type="primary"
  151. v-hasPermi="['system:sms-template:update']"
  152. @click="handleDetail(row)"
  153. >
  154. <Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
  155. </el-button>
  156. <el-button
  157. link
  158. type="primary"
  159. v-hasPermi="['system:sms-template:delete']"
  160. @click="handleDelete(row)"
  161. >
  162. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  163. </el-button>
  164. </template>
  165. </Table>
  166. </ContentWrap>
  167. <Dialog v-model="dialogVisible" :title="dialogTitle">
  168. <!-- 对话框(添加 / 修改) -->
  169. <Form
  170. v-if="['create', 'update'].includes(actionType)"
  171. :schema="allSchemas.formSchema"
  172. :rules="rules"
  173. ref="formRef"
  174. />
  175. <!-- 对话框(详情) -->
  176. <Descriptions
  177. v-if="actionType === 'detail'"
  178. :schema="allSchemas.detailSchema"
  179. :data="detailRef"
  180. >
  181. <template #type="{ row }">
  182. <DictTag :type="DICT_TYPE.SYSTEM_SMS_TEMPLATE_TYPE" :value="row.code" />
  183. </template>
  184. <template #status="{ row }">
  185. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  186. </template>
  187. <template #createTime="{ row }">
  188. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  189. </template>
  190. </Descriptions>
  191. <!-- 操作按钮 -->
  192. <template #footer>
  193. <el-button
  194. v-if="['create', 'update'].includes(actionType)"
  195. type="primary"
  196. :loading="loading"
  197. @click="submitForm"
  198. >
  199. {{ t('action.save') }}
  200. </el-button>
  201. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  202. </template>
  203. </Dialog>
  204. </template>