index.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <script setup lang="ts">
  2. import { onMounted, ref, unref } from 'vue'
  3. import dayjs from 'dayjs'
  4. import { ElMessage, ElTag, ElSelect, ElOption } 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 { SensitiveWordVO } from '@/api/system/sensitiveWord/types'
  10. import { rules, allSchemas } from './sensitiveWord.data'
  11. import * as SensitiveWordApi from '@/api/system/sensitiveWord'
  12. const { t } = useI18n() // 国际化
  13. // ========== 列表相关 ==========
  14. const { register, tableObject, methods } = useTable<SensitiveWordVO>({
  15. getListApi: SensitiveWordApi.getSensitiveWordPageApi,
  16. delListApi: SensitiveWordApi.deleteSensitiveWordApi,
  17. exportListApi: SensitiveWordApi.exportSensitiveWordApi
  18. })
  19. const { getList, setSearchParams, delList, exportList } = methods
  20. // 导出操作
  21. const handleExport = async () => {
  22. await exportList('敏感词数据.xls')
  23. }
  24. // 获取标签
  25. const tagsOptions = ref()
  26. const getTags = async () => {
  27. const res = await SensitiveWordApi.getSensitiveWordTagsApi()
  28. tagsOptions.value = res
  29. }
  30. // ========== CRUD 相关 ==========
  31. const actionLoading = ref(false) // 遮罩层
  32. const actionType = ref('') // 操作按钮的类型
  33. const dialogVisible = ref(false) // 是否显示弹出层
  34. const dialogTitle = ref('edit') // 弹出层标题
  35. const formRef = ref<FormExpose>() // 表单 Ref
  36. const tags = ref()
  37. // 设置标题
  38. const setDialogTile = (type: string) => {
  39. dialogTitle.value = t('action.' + type)
  40. actionType.value = type
  41. dialogVisible.value = true
  42. }
  43. // 新增操作
  44. const handleCreate = () => {
  45. setDialogTile('create')
  46. // 重置表单
  47. unref(formRef)?.getElFormRef()?.resetFields()
  48. }
  49. // 修改操作
  50. const handleUpdate = async (row: SensitiveWordVO) => {
  51. setDialogTile('update')
  52. // 设置数据
  53. const res = await SensitiveWordApi.getSensitiveWordApi(row.id)
  54. unref(formRef)?.setValues(res)
  55. }
  56. // 提交按钮
  57. const submitForm = async () => {
  58. actionLoading.value = true
  59. // 提交请求
  60. try {
  61. const data = unref(formRef)?.formModel as SensitiveWordVO
  62. if (actionType.value === 'create') {
  63. await SensitiveWordApi.createSensitiveWordApi(data)
  64. ElMessage.success(t('common.createSuccess'))
  65. } else {
  66. await SensitiveWordApi.updateSensitiveWordApi(data)
  67. ElMessage.success(t('common.updateSuccess'))
  68. }
  69. // 操作成功,重新加载列表
  70. dialogVisible.value = false
  71. await getList()
  72. } finally {
  73. actionLoading.value = false
  74. }
  75. }
  76. // 删除操作
  77. const handleDelete = (row: SensitiveWordVO) => {
  78. delList(row.id, false)
  79. }
  80. // ========== 详情相关 ==========
  81. const detailRef = ref() // 详情 Ref
  82. // 详情操作
  83. const handleDetail = async (row: SensitiveWordVO) => {
  84. // 设置数据
  85. detailRef.value = row
  86. setDialogTile('detail')
  87. }
  88. // ========== 初始化 ==========
  89. onMounted(async () => {
  90. await getTags()
  91. await getList()
  92. })
  93. </script>
  94. <template>
  95. <!-- 搜索工作区 -->
  96. <ContentWrap>
  97. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  98. </ContentWrap>
  99. <ContentWrap>
  100. <!-- 操作工具栏 -->
  101. <div class="mb-10px">
  102. <el-button type="primary" v-hasPermi="['system:post:create']" @click="handleCreate">
  103. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  104. </el-button>
  105. <el-button
  106. type="warning"
  107. v-hasPermi="['system:post:export']"
  108. :loading="tableObject.exportLoading"
  109. @click="handleExport"
  110. >
  111. <Icon icon="ep:download" class="mr-5px" /> {{ t('action.export') }}
  112. </el-button>
  113. </div>
  114. <!-- 列表 -->
  115. <Table
  116. :columns="allSchemas.tableColumns"
  117. :selection="false"
  118. :data="tableObject.tableList"
  119. :loading="tableObject.loading"
  120. :pagination="{
  121. total: tableObject.total
  122. }"
  123. v-model:pageSize="tableObject.pageSize"
  124. v-model:currentPage="tableObject.currentPage"
  125. @register="register"
  126. >
  127. <template #tags="{ row }">
  128. <el-tag
  129. :disable-transitions="true"
  130. :key="index"
  131. v-for="(tag, index) in row.tags"
  132. :index="index"
  133. >
  134. {{ tag }}
  135. </el-tag>
  136. </template>
  137. <template #status="{ row }">
  138. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  139. </template>
  140. <template #createTime="{ row }">
  141. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  142. </template>
  143. <template #action="{ row }">
  144. <el-button
  145. link
  146. type="primary"
  147. v-hasPermi="['system:post:update']"
  148. @click="handleUpdate(row)"
  149. >
  150. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  151. </el-button>
  152. <el-button
  153. link
  154. type="primary"
  155. v-hasPermi="['system:post:update']"
  156. @click="handleDetail(row)"
  157. >
  158. <Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
  159. </el-button>
  160. <el-button
  161. link
  162. type="primary"
  163. v-hasPermi="['system:post:delete']"
  164. @click="handleDelete(row)"
  165. >
  166. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  167. </el-button>
  168. </template>
  169. </Table>
  170. </ContentWrap>
  171. <Dialog v-model="dialogVisible" :title="dialogTitle">
  172. <!-- 对话框(添加 / 修改) -->
  173. <Form
  174. v-if="['create', 'update'].includes(actionType)"
  175. :schema="allSchemas.formSchema"
  176. :rules="rules"
  177. ref="formRef"
  178. >
  179. <template #tags>
  180. <el-select v-model="tags" multiple placeholder="请选择">
  181. <el-option v-for="item in tagsOptions" :key="item" :label="item" :value="item" />
  182. </el-select>
  183. </template>
  184. </Form>
  185. <!-- 对话框(详情) -->
  186. <Descriptions
  187. v-if="actionType === 'detail'"
  188. :schema="allSchemas.detailSchema"
  189. :data="detailRef"
  190. >
  191. <template #status="{ row }">
  192. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  193. </template>
  194. <template #createTime="{ row }">
  195. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  196. </template>
  197. </Descriptions>
  198. <!-- 操作按钮 -->
  199. <template #footer>
  200. <el-button
  201. v-if="['create', 'update'].includes(actionType)"
  202. type="primary"
  203. :loading="actionLoading"
  204. @click="submitForm"
  205. >
  206. {{ t('action.save') }}
  207. </el-button>
  208. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  209. </template>
  210. </Dialog>
  211. </template>