index.vue 6.3 KB

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