index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <script setup lang="ts">
  2. import { ref, unref } from 'vue'
  3. import dayjs from 'dayjs'
  4. import { ElMessage } from 'element-plus'
  5. import { DICT_TYPE } from '@/utils/dict'
  6. import { useTable } from '@/hooks/web/useTable'
  7. import { useI18n } from '@/hooks/web/useI18n'
  8. import { FormExpose } from '@/components/Form'
  9. import type { ConfigVO } from '@/api/infra/config/types'
  10. import { rules, allSchemas } from './config.data'
  11. import * as ConfigApi from '@/api/infra/config'
  12. const { t } = useI18n() // 国际化
  13. // ========== 列表相关 ==========
  14. const { register, tableObject, methods } = useTable<ConfigVO>({
  15. getListApi: ConfigApi.getConfigPageApi,
  16. delListApi: ConfigApi.deleteConfigApi,
  17. exportListApi: ConfigApi.exportConfigApi
  18. })
  19. const { getList, setSearchParams, delList, exportList } = methods
  20. // ========== CRUD 相关 ==========
  21. const actionLoading = ref(false) // 遮罩层
  22. const actionType = ref('') // 操作按钮的类型
  23. const dialogVisible = ref(false) // 是否显示弹出层
  24. const dialogTitle = ref('edit') // 弹出层标题
  25. const formRef = ref<FormExpose>() // 表单 Ref
  26. // 设置标题
  27. const setDialogTile = (type: string) => {
  28. dialogTitle.value = t('action.' + type)
  29. actionType.value = type
  30. dialogVisible.value = true
  31. }
  32. // 新增操作
  33. const handleCreate = () => {
  34. setDialogTile('create')
  35. }
  36. // 修改操作
  37. const handleUpdate = async (row: ConfigVO) => {
  38. setDialogTile('update')
  39. // 设置数据
  40. const res = await ConfigApi.getConfigApi(row.id)
  41. unref(formRef)?.setValues(res)
  42. }
  43. // 提交按钮
  44. const submitForm = async () => {
  45. const elForm = unref(formRef)?.getElFormRef()
  46. if (!elForm) return
  47. elForm.validate(async (valid) => {
  48. if (valid) {
  49. actionLoading.value = true
  50. // 提交请求
  51. try {
  52. const data = unref(formRef)?.formModel as ConfigVO
  53. if (actionType.value === 'create') {
  54. await ConfigApi.createConfigApi(data)
  55. ElMessage.success(t('common.createSuccess'))
  56. } else {
  57. await ConfigApi.updateConfigApi(data)
  58. ElMessage.success(t('common.updateSuccess'))
  59. }
  60. // 操作成功,重新加载列表
  61. dialogVisible.value = false
  62. await getList()
  63. } finally {
  64. actionLoading.value = false
  65. }
  66. }
  67. })
  68. }
  69. // ========== 详情相关 ==========
  70. const detailRef = ref() // 详情 Ref
  71. // 详情操作
  72. const handleDetail = async (row: ConfigVO) => {
  73. // 设置数据
  74. detailRef.value = row
  75. setDialogTile('detail')
  76. }
  77. // ========== 初始化 ==========
  78. getList()
  79. </script>
  80. <template>
  81. <!-- 搜索工作区 -->
  82. <ContentWrap>
  83. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  84. </ContentWrap>
  85. <ContentWrap>
  86. <!-- 操作工具栏 -->
  87. <div class="mb-10px">
  88. <el-button type="primary" v-hasPermi="['infra:config:create']" @click="handleCreate">
  89. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  90. </el-button>
  91. <el-button
  92. type="warning"
  93. v-hasPermi="['infra:config:export']"
  94. :loading="tableObject.exportLoading"
  95. @click="exportList('参数配置.xls')"
  96. >
  97. <Icon icon="ep:download" class="mr-5px" /> {{ t('action.export') }}
  98. </el-button>
  99. </div>
  100. <!-- 列表 -->
  101. <Table
  102. :columns="allSchemas.tableColumns"
  103. :selection="false"
  104. :data="tableObject.tableList"
  105. :loading="tableObject.loading"
  106. :pagination="{
  107. total: tableObject.total
  108. }"
  109. v-model:pageSize="tableObject.pageSize"
  110. v-model:currentPage="tableObject.currentPage"
  111. @register="register"
  112. >
  113. <template #visible="{ row }">
  114. <span>{{ row.visible ? '是' : '否' }} </span>
  115. </template>
  116. <template #type="{ row }">
  117. <DictTag :type="DICT_TYPE.INFRA_CONFIG_TYPE" :value="row.type" />
  118. </template>
  119. <template #createTime="{ row }">
  120. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  121. </template>
  122. <template #action="{ row }">
  123. <el-button
  124. link
  125. type="primary"
  126. v-hasPermi="['infra:config:update']"
  127. @click="handleUpdate(row)"
  128. >
  129. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  130. </el-button>
  131. <el-button
  132. link
  133. type="primary"
  134. v-hasPermi="['infra:config:update']"
  135. @click="handleDetail(row)"
  136. >
  137. <Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
  138. </el-button>
  139. <el-button
  140. link
  141. type="primary"
  142. v-hasPermi="['infra:config:delete']"
  143. @click="delList(row.id, false)"
  144. >
  145. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  146. </el-button>
  147. </template>
  148. </Table>
  149. </ContentWrap>
  150. <XModal v-model="dialogVisible" :title="dialogTitle">
  151. <!-- 对话框(添加 / 修改) -->
  152. <Form
  153. v-if="['create', 'update'].includes(actionType)"
  154. :schema="allSchemas.formSchema"
  155. :rules="rules"
  156. ref="formRef"
  157. />
  158. <!-- 对话框(详情) -->
  159. <Descriptions
  160. v-if="actionType === 'detail'"
  161. :schema="allSchemas.detailSchema"
  162. :data="detailRef"
  163. >
  164. <template #visible="{ row }">
  165. <span>{{ row.visible ? '是' : '否' }} </span>
  166. </template>
  167. </Descriptions>
  168. <!-- 操作按钮 -->
  169. <template #footer>
  170. <el-button
  171. v-if="['create', 'update'].includes(actionType)"
  172. type="primary"
  173. :loading="actionLoading"
  174. @click="submitForm"
  175. >
  176. {{ t('action.save') }}
  177. </el-button>
  178. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  179. </template>
  180. </XModal>
  181. </template>