index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <script setup lang="ts">
  2. import { onMounted, ref, unref } from 'vue'
  3. import dayjs from 'dayjs'
  4. import { ElMessage } from 'element-plus'
  5. import { FormExpose } from '@/components/Form'
  6. import { rules, allSchemas } from './dataSourceConfig.data'
  7. import type { DataSourceConfigVO } from '@/api/infra/dataSourceConfig/types'
  8. import * as DataSourceConfiggApi from '@/api/infra/dataSourceConfig'
  9. import { useI18n } from '@/hooks/web/useI18n'
  10. const { t } = useI18n() // 国际化
  11. const tableData = ref()
  12. const getList = async () => {
  13. const res = await DataSourceConfiggApi.getDataSourceConfigListApi()
  14. tableData.value = res
  15. }
  16. // ========== CRUD 相关 ==========
  17. const loading = ref(false) // 遮罩层
  18. const actionType = ref('') // 操作按钮的类型
  19. const dialogVisible = ref(false) // 是否显示弹出层
  20. const dialogTitle = ref('edit') // 弹出层标题
  21. const formRef = ref<FormExpose>() // 表单 Ref
  22. // 设置标题
  23. const setDialogTile = (type: string) => {
  24. dialogTitle.value = t('action.' + type)
  25. actionType.value = type
  26. dialogVisible.value = true
  27. }
  28. // 新增操作
  29. const handleCreate = () => {
  30. setDialogTile('create')
  31. }
  32. // 修改操作
  33. const handleUpdate = async (row: DataSourceConfigVO) => {
  34. setDialogTile('update')
  35. // 设置数据
  36. const res = await DataSourceConfiggApi.getDataSourceConfigApi(row.id)
  37. unref(formRef)?.setValues(res)
  38. }
  39. // 提交按钮
  40. const submitForm = async () => {
  41. const elForm = unref(formRef)?.getElFormRef()
  42. if (!elForm) return
  43. elForm.validate(async (valid) => {
  44. if (valid) {
  45. loading.value = true
  46. // 提交请求
  47. try {
  48. const data = unref(formRef)?.formModel as DataSourceConfigVO
  49. if (actionType.value === 'create') {
  50. await DataSourceConfiggApi.createDataSourceConfigApi(data)
  51. ElMessage.success(t('common.createSuccess'))
  52. } else {
  53. await DataSourceConfiggApi.updateDataSourceConfigApi(data)
  54. ElMessage.success(t('common.updateSuccess'))
  55. }
  56. // 操作成功,重新加载列表
  57. dialogVisible.value = false
  58. await getList()
  59. } finally {
  60. loading.value = false
  61. }
  62. }
  63. })
  64. }
  65. // 删除操作
  66. const handleDelete = async (row: DataSourceConfigVO) => {
  67. await DataSourceConfiggApi.deleteDataSourceConfigApi(row.id)
  68. ElMessage.success(t('common.delSuccess'))
  69. }
  70. // ========== 详情相关 ==========
  71. const detailRef = ref() // 详情 Ref
  72. // 详情操作
  73. const handleDetail = async (row: DataSourceConfigVO) => {
  74. // 设置数据
  75. detailRef.value = row
  76. setDialogTile('detail')
  77. }
  78. onMounted(async () => {
  79. await getList()
  80. })
  81. </script>
  82. <template>
  83. <ContentWrap>
  84. <!-- 操作工具栏 -->
  85. <div class="mb-10px">
  86. <el-button
  87. v-hasPermi="['infra:data-source-config:create']"
  88. type="primary"
  89. @click="handleCreate"
  90. >
  91. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  92. </el-button>
  93. </div>
  94. <Table :columns="allSchemas.tableColumns" :data="tableData">
  95. <template #createTime="{ row }">
  96. <span>{{ row.createTime ? dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') : '' }}</span>
  97. </template>
  98. <template #action="{ row }">
  99. <el-button
  100. link
  101. type="primary"
  102. v-hasPermi="['infra:data-source-config:update']"
  103. @click="handleUpdate(row)"
  104. >
  105. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  106. </el-button>
  107. <el-button
  108. link
  109. type="primary"
  110. v-hasPermi="['infra:data-source-config:update']"
  111. @click="handleDetail(row)"
  112. >
  113. <Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
  114. </el-button>
  115. <el-button
  116. link
  117. type="primary"
  118. v-hasPermi="['infra:data-source-config:delete']"
  119. @click="handleDelete(row)"
  120. >
  121. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  122. </el-button>
  123. </template>
  124. </Table>
  125. </ContentWrap>
  126. <Dialog v-model="dialogVisible" :title="dialogTitle">
  127. <!-- 对话框(添加 / 修改) -->
  128. <Form
  129. v-if="['create', 'update'].includes(actionType)"
  130. :schema="allSchemas.formSchema"
  131. :rules="rules"
  132. ref="formRef"
  133. />
  134. <!-- 对话框(详情) -->
  135. <Descriptions
  136. v-if="actionType === 'detail'"
  137. :schema="allSchemas.detailSchema"
  138. :data="detailRef"
  139. >
  140. <template #createTime="{ row }">
  141. <span>{{ row.createTime ? dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') : '' }}</span>
  142. </template>
  143. </Descriptions>
  144. <!-- 操作按钮 -->
  145. <template #footer>
  146. <el-button
  147. v-if="['create', 'update'].includes(actionType)"
  148. type="primary"
  149. :loading="loading"
  150. @click="submitForm"
  151. >
  152. {{ t('action.save') }}
  153. </el-button>
  154. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  155. </template>
  156. </Dialog>
  157. </template>