index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. unref(formRef)?.getElFormRef()?.resetFields()
  37. }
  38. // 修改操作
  39. const handleUpdate = async (row: ConfigVO) => {
  40. setDialogTile('update')
  41. // 设置数据
  42. const res = await ConfigApi.getConfigApi(row.id)
  43. unref(formRef)?.setValues(res)
  44. }
  45. // 提交按钮
  46. const submitForm = async () => {
  47. const elForm = unref(formRef)?.getElFormRef()
  48. if (!elForm) return
  49. elForm.validate(async (valid) => {
  50. if (valid) {
  51. actionLoading.value = true
  52. // 提交请求
  53. try {
  54. const data = unref(formRef)?.formModel as ConfigVO
  55. if (actionType.value === 'create') {
  56. await ConfigApi.createConfigApi(data)
  57. ElMessage.success(t('common.createSuccess'))
  58. } else {
  59. await ConfigApi.updateConfigApi(data)
  60. ElMessage.success(t('common.updateSuccess'))
  61. }
  62. // 操作成功,重新加载列表
  63. dialogVisible.value = false
  64. await getList()
  65. } finally {
  66. actionLoading.value = false
  67. }
  68. }
  69. })
  70. }
  71. // ========== 详情相关 ==========
  72. const detailRef = ref() // 详情 Ref
  73. // 详情操作
  74. const handleDetail = async (row: ConfigVO) => {
  75. // 设置数据
  76. detailRef.value = row
  77. setDialogTile('detail')
  78. }
  79. // ========== 初始化 ==========
  80. getList()
  81. </script>
  82. <template>
  83. <!-- 搜索工作区 -->
  84. <ContentWrap>
  85. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  86. </ContentWrap>
  87. <ContentWrap>
  88. <!-- 操作工具栏 -->
  89. <div class="mb-10px">
  90. <el-button type="primary" v-hasPermi="['infra:config:create']" @click="handleCreate">
  91. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  92. </el-button>
  93. <el-button
  94. type="warning"
  95. v-hasPermi="['infra:config:export']"
  96. :loading="tableObject.exportLoading"
  97. @click="exportList('参数配置.xls')"
  98. >
  99. <Icon icon="ep:download" class="mr-5px" /> {{ t('action.export') }}
  100. </el-button>
  101. </div>
  102. <!-- 列表 -->
  103. <Table
  104. :columns="allSchemas.tableColumns"
  105. :selection="false"
  106. :data="tableObject.tableList"
  107. :loading="tableObject.loading"
  108. :pagination="{
  109. total: tableObject.total
  110. }"
  111. v-model:pageSize="tableObject.pageSize"
  112. v-model:currentPage="tableObject.currentPage"
  113. @register="register"
  114. >
  115. <template #visible="{ row }">
  116. <span>{{ row.visible ? '是' : '否' }} </span>
  117. </template>
  118. <template #type="{ row }">
  119. <DictTag :type="DICT_TYPE.INFRA_CONFIG_TYPE" :value="row.type" />
  120. </template>
  121. <template #createTime="{ row }">
  122. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  123. </template>
  124. <template #action="{ row }">
  125. <el-button
  126. link
  127. type="primary"
  128. v-hasPermi="['infra:config:update']"
  129. @click="handleUpdate(row)"
  130. >
  131. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  132. </el-button>
  133. <el-button
  134. link
  135. type="primary"
  136. v-hasPermi="['infra:config:update']"
  137. @click="handleDetail(row)"
  138. >
  139. <Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
  140. </el-button>
  141. <el-button
  142. link
  143. type="primary"
  144. v-hasPermi="['infra:config:delete']"
  145. @click="delList(row.id, false)"
  146. >
  147. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  148. </el-button>
  149. </template>
  150. </Table>
  151. </ContentWrap>
  152. <Dialog v-model="dialogVisible" :title="dialogTitle">
  153. <!-- 对话框(添加 / 修改) -->
  154. <Form
  155. v-if="['create', 'update'].includes(actionType)"
  156. :schema="allSchemas.formSchema"
  157. :rules="rules"
  158. ref="formRef"
  159. />
  160. <!-- 对话框(详情) -->
  161. <Descriptions
  162. v-if="actionType === 'detail'"
  163. :schema="allSchemas.detailSchema"
  164. :data="detailRef"
  165. >
  166. <template #visible="{ row }">
  167. <span>{{ row.visible ? '是' : '否' }} </span>
  168. </template>
  169. <template #type="{ row }">
  170. <DictTag :type="DICT_TYPE.INFRA_CONFIG_TYPE" :value="row.type" />
  171. </template>
  172. <template #createTime="{ row }">
  173. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  174. </template>
  175. </Descriptions>
  176. <!-- 操作按钮 -->
  177. <template #footer>
  178. <el-button
  179. v-if="['create', 'update'].includes(actionType)"
  180. type="primary"
  181. :loading="actionLoading"
  182. @click="submitForm"
  183. >
  184. {{ t('action.save') }}
  185. </el-button>
  186. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  187. </template>
  188. </Dialog>
  189. </template>