index.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. unref(formRef)?.getElFormRef()?.resetFields()
  33. }
  34. // 修改操作
  35. const handleUpdate = async (row: DataSourceConfigVO) => {
  36. setDialogTile('update')
  37. // 设置数据
  38. const res = await DataSourceConfiggApi.getDataSourceConfigApi(row.id)
  39. unref(formRef)?.setValues(res)
  40. }
  41. // 提交按钮
  42. const submitForm = async () => {
  43. loading.value = true
  44. // 提交请求
  45. try {
  46. const data = unref(formRef)?.formModel as DataSourceConfigVO
  47. if (actionType.value === 'create') {
  48. await DataSourceConfiggApi.createDataSourceConfigApi(data)
  49. ElMessage.success(t('common.createSuccess'))
  50. } else {
  51. await DataSourceConfiggApi.updateDataSourceConfigApi(data)
  52. ElMessage.success(t('common.updateSuccess'))
  53. }
  54. // 操作成功,重新加载列表
  55. dialogVisible.value = false
  56. await getList()
  57. } finally {
  58. loading.value = false
  59. }
  60. }
  61. // 删除操作
  62. const handleDelete = async (row: DataSourceConfigVO) => {
  63. await DataSourceConfiggApi.deleteDataSourceConfigApi(row.id)
  64. ElMessage.success(t('common.delSuccess'))
  65. }
  66. // ========== 详情相关 ==========
  67. const detailRef = ref() // 详情 Ref
  68. // 详情操作
  69. const handleDetail = async (row: DataSourceConfigVO) => {
  70. // 设置数据
  71. detailRef.value = row
  72. setDialogTile('detail')
  73. }
  74. onMounted(async () => {
  75. await getList()
  76. })
  77. </script>
  78. <template>
  79. <ContentWrap>
  80. <!-- 操作工具栏 -->
  81. <div class="mb-10px">
  82. <el-button
  83. v-hasPermi="['infra:data-source-config:create']"
  84. type="primary"
  85. @click="handleCreate"
  86. >
  87. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  88. </el-button>
  89. </div>
  90. <Table :columns="allSchemas.tableColumns" :data="tableData">
  91. <template #createTime="{ row }">
  92. <span>{{ row.createTime ? dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') : '' }}</span>
  93. </template>
  94. <template #action="{ row }">
  95. <el-button
  96. link
  97. type="primary"
  98. v-hasPermi="['infra:data-source-config:update']"
  99. @click="handleUpdate(row)"
  100. >
  101. <Icon icon="ep:edit" class="mr-5px" /> {{ t('action.edit') }}
  102. </el-button>
  103. <el-button
  104. link
  105. type="primary"
  106. v-hasPermi="['infra:data-source-config:update']"
  107. @click="handleDetail(row)"
  108. >
  109. <Icon icon="ep:view" class="mr-5px" /> {{ t('action.detail') }}
  110. </el-button>
  111. <el-button
  112. link
  113. type="primary"
  114. v-hasPermi="['infra:data-source-config:delete']"
  115. @click="handleDelete(row)"
  116. >
  117. <Icon icon="ep:delete" class="mr-5px" /> {{ t('action.del') }}
  118. </el-button>
  119. </template>
  120. </Table>
  121. </ContentWrap>
  122. <Dialog v-model="dialogVisible" :title="dialogTitle">
  123. <!-- 对话框(添加 / 修改) -->
  124. <Form
  125. v-if="['create', 'update'].includes(actionType)"
  126. :schema="allSchemas.formSchema"
  127. :rules="rules"
  128. ref="formRef"
  129. />
  130. <!-- 对话框(详情) -->
  131. <Descriptions
  132. v-if="actionType === 'detail'"
  133. :schema="allSchemas.detailSchema"
  134. :data="detailRef"
  135. >
  136. <template #createTime="{ row }">
  137. <span>{{ row.createTime ? dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') : '' }}</span>
  138. </template>
  139. </Descriptions>
  140. <!-- 操作按钮 -->
  141. <template #footer>
  142. <el-button
  143. v-if="['create', 'update'].includes(actionType)"
  144. type="primary"
  145. :loading="loading"
  146. @click="submitForm"
  147. >
  148. {{ t('action.save') }}
  149. </el-button>
  150. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  151. </template>
  152. </Dialog>
  153. </template>