index.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 type { FormExpose } from '@/components/Form'
  86. // 业务相关的 import
  87. import * as ConfigApi from '@/api/infra/config'
  88. import { rules, allSchemas } from './config.data'
  89. const { t } = useI18n() // 国际化
  90. const message = useMessage() // 消息弹窗
  91. // 列表相关的变量
  92. const [registerTable, { reload, deleteData, exportList }] = useXTable({
  93. allSchemas: allSchemas,
  94. getListApi: ConfigApi.getConfigPageApi,
  95. deleteApi: ConfigApi.deleteConfigApi,
  96. exportListApi: ConfigApi.exportConfigApi
  97. })
  98. // ========== CRUD 相关 ==========
  99. const actionLoading = ref(false) // 遮罩层
  100. const actionType = ref('') // 操作按钮的类型
  101. const dialogVisible = ref(false) // 是否显示弹出层
  102. const dialogTitle = ref('edit') // 弹出层标题
  103. const formRef = ref<FormExpose>() // 表单 Ref
  104. const detailData = ref() // 详情 Ref
  105. // 设置标题
  106. const setDialogTile = (type: string) => {
  107. dialogTitle.value = t('action.' + type)
  108. actionType.value = type
  109. dialogVisible.value = true
  110. }
  111. // 新增操作
  112. const handleCreate = async () => {
  113. setDialogTile('create')
  114. await nextTick()
  115. console.log(allSchemas.formSchema, 'allSchemas.formSchema')
  116. if (allSchemas.formSchema[2].field !== 'key') {
  117. unref(formRef)?.addSchema(
  118. {
  119. field: 'key',
  120. label: '参数键名',
  121. component: 'Input'
  122. },
  123. 2
  124. )
  125. unref(formRef)?.addSchema(
  126. {
  127. field: 'value',
  128. label: '参数键值',
  129. component: 'Input'
  130. },
  131. 3
  132. )
  133. }
  134. }
  135. // 修改操作
  136. const handleUpdate = async (rowId: number) => {
  137. setDialogTile('update')
  138. // 设置数据
  139. const res = await ConfigApi.getConfigApi(rowId)
  140. unref(formRef)?.delSchema('key')
  141. unref(formRef)?.delSchema('value')
  142. unref(formRef)?.setValues(res)
  143. }
  144. // 详情操作
  145. const handleDetail = async (rowId: number) => {
  146. setDialogTile('detail')
  147. const res = await ConfigApi.getConfigApi(rowId)
  148. detailData.value = res
  149. }
  150. // 提交按钮
  151. const submitForm = async () => {
  152. const elForm = unref(formRef)?.getElFormRef()
  153. if (!elForm) return
  154. elForm.validate(async (valid) => {
  155. if (valid) {
  156. actionLoading.value = true
  157. // 提交请求
  158. try {
  159. const data = unref(formRef)?.formModel as ConfigApi.ConfigVO
  160. if (actionType.value === 'create') {
  161. await ConfigApi.createConfigApi(data)
  162. message.success(t('common.createSuccess'))
  163. } else {
  164. await ConfigApi.updateConfigApi(data)
  165. message.success(t('common.updateSuccess'))
  166. }
  167. dialogVisible.value = false
  168. } finally {
  169. actionLoading.value = false
  170. // 刷新列表
  171. await reload()
  172. }
  173. }
  174. })
  175. }
  176. </script>