index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 type { FormExpose } from '@/components/Form'
  71. // 业务相关的 import
  72. import * as DataSourceConfiggApi from '@/api/infra/dataSourceConfig'
  73. import { rules, allSchemas } from './dataSourceConfig.data'
  74. const { t } = useI18n() // 国际化
  75. const message = useMessage() // 消息弹窗
  76. // 列表相关的变量
  77. const [registerTable, { reload, deleteData }] = useXTable({
  78. allSchemas: allSchemas,
  79. isList: true,
  80. getListApi: DataSourceConfiggApi.getDataSourceConfigListApi,
  81. deleteApi: DataSourceConfiggApi.deleteDataSourceConfigApi
  82. })
  83. // ========== CRUD 相关 ==========
  84. const loading = ref(false) // 遮罩层
  85. const actionType = ref('') // 操作按钮的类型
  86. const dialogVisible = ref(false) // 是否显示弹出层
  87. const dialogTitle = ref('edit') // 弹出层标题
  88. const formRef = ref<FormExpose>() // 表单 Ref
  89. const detailData = ref() // 详情 Ref
  90. // 设置标题
  91. const setDialogTile = (type: string) => {
  92. dialogTitle.value = t('action.' + type)
  93. actionType.value = type
  94. dialogVisible.value = true
  95. }
  96. // 新增操作
  97. const handleCreate = () => {
  98. setDialogTile('create')
  99. }
  100. // 修改操作
  101. const handleUpdate = async (rowId: number) => {
  102. setDialogTile('update')
  103. // 设置数据
  104. const res = await DataSourceConfiggApi.getDataSourceConfigApi(rowId)
  105. unref(formRef)?.setValues(res)
  106. }
  107. // 详情操作
  108. const handleDetail = async (rowId: number) => {
  109. // 设置数据
  110. const res = await DataSourceConfiggApi.getDataSourceConfigApi(rowId)
  111. detailData.value = res
  112. setDialogTile('detail')
  113. }
  114. // 提交按钮
  115. const submitForm = async () => {
  116. const elForm = unref(formRef)?.getElFormRef()
  117. if (!elForm) return
  118. elForm.validate(async (valid) => {
  119. if (valid) {
  120. loading.value = true
  121. // 提交请求
  122. try {
  123. const data = unref(formRef)?.formModel as DataSourceConfiggApi.DataSourceConfigVO
  124. if (actionType.value === 'create') {
  125. await DataSourceConfiggApi.createDataSourceConfigApi(data)
  126. message.success(t('common.createSuccess'))
  127. } else {
  128. await DataSourceConfiggApi.updateDataSourceConfigApi(data)
  129. message.success(t('common.updateSuccess'))
  130. }
  131. dialogVisible.value = false
  132. } finally {
  133. loading.value = false
  134. // 刷新列表
  135. await reload()
  136. }
  137. }
  138. })
  139. }
  140. </script>