index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <ContentWrap>
  3. <!-- 列表 -->
  4. <vxe-grid ref="xGrid" v-bind="gridOptions" class="xtable-scrollbar">
  5. <template #toolbar_buttons>
  6. <XButton
  7. type="primary"
  8. preIcon="ep:zoom-in"
  9. :title="t('action.add')"
  10. v-hasPermi="['system:sensitive-word:create']"
  11. @click="handleCreate()"
  12. />
  13. <XButton
  14. type="warning"
  15. preIcon="ep:download"
  16. :title="t('action.export')"
  17. v-hasPermi="['system:sensitive-word:export']"
  18. @click="handleExport()"
  19. />
  20. </template>
  21. <template #tags_default="{ row }">
  22. <el-tag
  23. :disable-transitions="true"
  24. :key="index"
  25. v-for="(tag, index) in row.tags"
  26. :index="index"
  27. >
  28. {{ tag }}
  29. </el-tag>
  30. </template>
  31. <template #actionbtns_default="{ row }">
  32. <!-- 操作:修改 -->
  33. <XTextButton
  34. preIcon="ep:edit"
  35. :title="t('action.edit')"
  36. v-hasPermi="['system:sensitive-word:update']"
  37. @click="handleUpdate(row.id)"
  38. />
  39. <!-- 操作:详情 -->
  40. <XTextButton
  41. preIcon="ep:view"
  42. :title="t('action.detail')"
  43. v-hasPermi="['system:sensitive-word:update']"
  44. @click="handleDetail(row.id)"
  45. />
  46. <!-- 操作:删除 -->
  47. <XTextButton
  48. preIcon="ep:delete"
  49. :title="t('action.del')"
  50. v-hasPermi="['system:sensitive-word:delete']"
  51. @click="handleDelete(row.id)"
  52. />
  53. </template>
  54. </vxe-grid>
  55. </ContentWrap>
  56. <XModal v-model="dialogVisible" :title="dialogTitle">
  57. <!-- 对话框(添加 / 修改) -->
  58. <Form
  59. v-if="['create', 'update'].includes(actionType)"
  60. :schema="allSchemas.formSchema"
  61. :rules="rules"
  62. ref="formRef"
  63. >
  64. <template #tags>
  65. <el-select v-model="tags" multiple placeholder="请选择">
  66. <el-option v-for="item in tagsOptions" :key="item" :label="item" :value="item" />
  67. </el-select>
  68. </template>
  69. </Form>
  70. <!-- 对话框(详情) -->
  71. <Descriptions
  72. v-if="actionType === 'detail'"
  73. :schema="allSchemas.detailSchema"
  74. :data="detailData"
  75. />
  76. <!-- 操作按钮 -->
  77. <template #footer>
  78. <!-- 按钮:保存 -->
  79. <XButton
  80. v-if="['create', 'update'].includes(actionType)"
  81. type="primary"
  82. :title="t('action.save')"
  83. :loading="actionLoading"
  84. @click="submitForm()"
  85. />
  86. <!-- 按钮:关闭 -->
  87. <XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
  88. </template>
  89. </XModal>
  90. </template>
  91. <script setup lang="ts">
  92. import { onMounted, ref, unref } from 'vue'
  93. import { useI18n } from '@/hooks/web/useI18n'
  94. import { useMessage } from '@/hooks/web/useMessage'
  95. import { useVxeGrid } from '@/hooks/web/useVxeGrid'
  96. import { VxeGridInstance } from 'vxe-table'
  97. import { FormExpose } from '@/components/Form'
  98. import { ElTag, ElSelect, ElOption } from 'element-plus'
  99. import * as SensitiveWordApi from '@/api/system/sensitiveWord'
  100. import { rules, allSchemas } from './sensitiveWord.data'
  101. const { t } = useI18n() // 国际化
  102. const message = useMessage() // 消息弹窗
  103. // 列表相关的变量
  104. const xGrid = ref<VxeGridInstance>() // 列表 Grid Ref
  105. const { gridOptions, getList, deleteData, exportList } =
  106. useVxeGrid<SensitiveWordApi.SensitiveWordVO>({
  107. allSchemas: allSchemas,
  108. getListApi: SensitiveWordApi.getSensitiveWordPageApi,
  109. deleteApi: SensitiveWordApi.deleteSensitiveWordApi,
  110. exportListApi: SensitiveWordApi.exportSensitiveWordApi
  111. })
  112. const actionLoading = ref(false) // 遮罩层
  113. const actionType = ref('') // 操作按钮的类型
  114. const dialogVisible = ref(false) // 是否显示弹出层
  115. const dialogTitle = ref('edit') // 弹出层标题
  116. const formRef = ref<FormExpose>() // 表单 Ref
  117. const detailData = ref() // 详情 Ref
  118. const tags = ref()
  119. // 获取标签
  120. const tagsOptions = ref()
  121. const getTags = async () => {
  122. const res = await SensitiveWordApi.getSensitiveWordTagsApi()
  123. tagsOptions.value = res
  124. }
  125. // 设置标题
  126. const setDialogTile = (type: string) => {
  127. dialogTitle.value = t('action.' + type)
  128. actionType.value = type
  129. dialogVisible.value = true
  130. }
  131. // 新增操作
  132. const handleCreate = () => {
  133. tags.value = null
  134. setDialogTile('create')
  135. }
  136. // 导出操作
  137. const handleExport = async () => {
  138. await exportList(xGrid, '敏感词数据.xls')
  139. }
  140. // 修改操作
  141. const handleUpdate = async (rowId: number) => {
  142. setDialogTile('update')
  143. // 设置数据
  144. const res = await SensitiveWordApi.getSensitiveWordApi(rowId)
  145. tags.value = res.tags
  146. unref(formRef)?.setValues(res)
  147. }
  148. // 详情操作
  149. const handleDetail = async (rowId: number) => {
  150. setDialogTile('detail')
  151. const res = await SensitiveWordApi.getSensitiveWordApi(rowId)
  152. detailData.value = res
  153. }
  154. // 删除操作
  155. const handleDelete = async (rowId: number) => {
  156. await deleteData(xGrid, rowId)
  157. }
  158. // 提交按钮
  159. const submitForm = async () => {
  160. const elForm = unref(formRef)?.getElFormRef()
  161. if (!elForm) return
  162. elForm.validate(async (valid) => {
  163. if (valid) {
  164. actionLoading.value = true
  165. // 提交请求
  166. try {
  167. const data = unref(formRef)?.formModel as SensitiveWordApi.SensitiveWordVO
  168. data.tags = tags.value
  169. if (actionType.value === 'create') {
  170. await SensitiveWordApi.createSensitiveWordApi(data)
  171. message.success(t('common.createSuccess'))
  172. } else {
  173. await SensitiveWordApi.updateSensitiveWordApi(data)
  174. message.success(t('common.updateSuccess'))
  175. }
  176. dialogVisible.value = false
  177. } finally {
  178. actionLoading.value = false
  179. // 刷新列表
  180. await getList(xGrid)
  181. }
  182. }
  183. })
  184. }
  185. // ========== 初始化 ==========
  186. onMounted(async () => {
  187. await getTags()
  188. })
  189. </script>