index.vue 4.4 KB

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