index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <template>
  2. <ContentWrap>
  3. <!-- 列表 -->
  4. <XTable @register="registerTable">
  5. <template #toolbar_buttons>
  6. <!-- 操作:新增 -->
  7. <XButton
  8. type="primary"
  9. preIcon="ep:zoom-in"
  10. :title="t('action.add')"
  11. v-hasPermi="['infra:config:create']"
  12. @click="handleCreate()"
  13. />
  14. <!-- 操作:导出 -->
  15. <XButton
  16. type="warning"
  17. preIcon="ep:download"
  18. :title="t('action.export')"
  19. v-hasPermi="['infra:config:export']"
  20. @click="exportList('配置.xls')"
  21. />
  22. </template>
  23. <template #visible_default="{ row }">
  24. <span>{{ row.visible ? '是' : '否' }} </span>
  25. </template>
  26. <template #actionbtns_default="{ row }">
  27. <!-- 操作:修改 -->
  28. <XTextButton
  29. preIcon="ep:edit"
  30. :title="t('action.edit')"
  31. v-hasPermi="['infra:config:update']"
  32. @click="handleUpdate(row.id)"
  33. />
  34. <!-- 操作:详情 -->
  35. <XTextButton
  36. preIcon="ep:view"
  37. :title="t('action.detail')"
  38. v-hasPermi="['infra:config:query']"
  39. @click="handleDetail(row.id)"
  40. />
  41. <!-- 操作:删除 -->
  42. <XTextButton
  43. preIcon="ep:delete"
  44. :title="t('action.del')"
  45. v-hasPermi="['infra:config:delete']"
  46. @click="deleteData(row.id)"
  47. />
  48. </template>
  49. </XTable>
  50. </ContentWrap>
  51. <XModal v-model="dialogVisible" :title="dialogTitle">
  52. <!-- 对话框(添加 / 修改) -->
  53. <Form
  54. v-if="['create', 'update'].includes(actionType)"
  55. :schema="allSchemas.formSchema"
  56. :rules="rules"
  57. ref="formRef"
  58. />
  59. <!-- 对话框(详情) -->
  60. <Descriptions
  61. v-if="actionType === 'detail'"
  62. :schema="allSchemas.detailSchema"
  63. :data="detailData"
  64. >
  65. <template #visible="{ row }">
  66. <span>{{ row.visible ? '是' : '否' }} </span>
  67. </template>
  68. </Descriptions>
  69. <!-- 操作按钮 -->
  70. <template #footer>
  71. <!-- 按钮:保存 -->
  72. <XButton
  73. v-if="['create', 'update'].includes(actionType)"
  74. type="primary"
  75. :title="t('action.save')"
  76. :loading="actionLoading"
  77. @click="submitForm()"
  78. />
  79. <!-- 按钮:关闭 -->
  80. <XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
  81. </template>
  82. </XModal>
  83. </template>
  84. <script setup lang="ts" name="Config">
  85. // 全局相关的 import
  86. import { ref, unref } from 'vue'
  87. import { useI18n } from '@/hooks/web/useI18n'
  88. import { useMessage } from '@/hooks/web/useMessage'
  89. import { useXTable } from '@/hooks/web/useXTable'
  90. import { FormExpose } from '@/components/Form'
  91. // 业务相关的 import
  92. import * as ConfigApi from '@/api/infra/config'
  93. import { rules, allSchemas } from './config.data'
  94. const { t } = useI18n() // 国际化
  95. const message = useMessage() // 消息弹窗
  96. // 列表相关的变量
  97. const [registerTable, { reload, deleteData, exportList }] = useXTable({
  98. allSchemas: allSchemas,
  99. getListApi: ConfigApi.getConfigPageApi,
  100. deleteApi: ConfigApi.deleteConfigApi,
  101. exportListApi: ConfigApi.exportConfigApi
  102. })
  103. // ========== CRUD 相关 ==========
  104. const actionLoading = ref(false) // 遮罩层
  105. const actionType = ref('') // 操作按钮的类型
  106. const dialogVisible = ref(false) // 是否显示弹出层
  107. const dialogTitle = ref('edit') // 弹出层标题
  108. const formRef = ref<FormExpose>() // 表单 Ref
  109. const detailData = ref() // 详情 Ref
  110. // 设置标题
  111. const setDialogTile = (type: string) => {
  112. dialogTitle.value = t('action.' + type)
  113. actionType.value = type
  114. dialogVisible.value = true
  115. }
  116. // 新增操作
  117. const handleCreate = () => {
  118. setDialogTile('create')
  119. }
  120. // 修改操作
  121. const handleUpdate = async (rowId: number) => {
  122. setDialogTile('update')
  123. // 设置数据
  124. const res = await ConfigApi.getConfigApi(rowId)
  125. unref(formRef)?.setValues(res)
  126. }
  127. // 详情操作
  128. const handleDetail = async (rowId: number) => {
  129. setDialogTile('detail')
  130. const res = await ConfigApi.getConfigApi(rowId)
  131. detailData.value = res
  132. }
  133. // 提交按钮
  134. const submitForm = async () => {
  135. const elForm = unref(formRef)?.getElFormRef()
  136. if (!elForm) return
  137. elForm.validate(async (valid) => {
  138. if (valid) {
  139. actionLoading.value = true
  140. // 提交请求
  141. try {
  142. const data = unref(formRef)?.formModel as ConfigApi.ConfigVO
  143. if (actionType.value === 'create') {
  144. await ConfigApi.createConfigApi(data)
  145. message.success(t('common.createSuccess'))
  146. } else {
  147. await ConfigApi.updateConfigApi(data)
  148. message.success(t('common.updateSuccess'))
  149. }
  150. dialogVisible.value = false
  151. } finally {
  152. actionLoading.value = false
  153. // 刷新列表
  154. await reload()
  155. }
  156. }
  157. })
  158. }
  159. </script>