index.vue 4.2 KB

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