index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // 导出操作
  21. const handleExport = async () => {
  22. await exportList('参数配置.xls')
  23. }
  24. // ========== CRUD 相关 ==========
  25. const actionLoading = ref(false) // 遮罩层
  26. const actionType = ref('') // 操作按钮的类型
  27. const dialogVisible = ref(false) // 是否显示弹出层
  28. const dialogTitle = ref('edit') // 弹出层标题
  29. const formRef = ref<FormExpose>() // 表单 Ref
  30. // 设置标题
  31. const setDialogTile = (type: string) => {
  32. dialogTitle.value = t('action.' + type)
  33. actionType.value = type
  34. dialogVisible.value = true
  35. }
  36. // 新增操作
  37. const handleCreate = () => {
  38. setDialogTile('create')
  39. // 重置表单
  40. unref(formRef)?.getElFormRef()?.resetFields()
  41. }
  42. // 修改操作
  43. const handleUpdate = async (row: ConfigVO) => {
  44. setDialogTile('update')
  45. // 设置数据
  46. const res = await ConfigApi.getConfigApi(row.id)
  47. unref(formRef)?.setValues(res)
  48. }
  49. // 提交按钮
  50. const submitForm = async () => {
  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. const handleDelete = (row: ConfigVO) => {
  71. delList(row.id, false)
  72. }
  73. // ========== 详情相关 ==========
  74. const detailRef = ref() // 详情 Ref
  75. // 详情操作
  76. const handleDetail = async (row: ConfigVO) => {
  77. // 设置数据
  78. detailRef.value = row
  79. setDialogTile('detail')
  80. }
  81. // ========== 初始化 ==========
  82. getList()
  83. </script>
  84. <template>
  85. <!-- 搜索工作区 -->
  86. <ContentWrap>
  87. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  88. </ContentWrap>
  89. <ContentWrap>
  90. <!-- 操作工具栏 -->
  91. <div class="mb-10px">
  92. <el-button type="primary" v-hasPermi="['infra:config:create']" @click="handleCreate">
  93. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  94. </el-button>
  95. <el-button
  96. type="warning"
  97. v-hasPermi="['infra:config:export']"
  98. :loading="tableObject.exportLoading"
  99. @click="handleExport"
  100. >
  101. <Icon icon="ep:download" class="mr-5px" /> {{ t('action.export') }}
  102. </el-button>
  103. </div>
  104. <!-- 列表 -->
  105. <Table
  106. :columns="allSchemas.tableColumns"
  107. :selection="false"
  108. :data="tableObject.tableList"
  109. :loading="tableObject.loading"
  110. :pagination="{
  111. total: tableObject.total
  112. }"
  113. v-model:pageSize="tableObject.pageSize"
  114. v-model:currentPage="tableObject.currentPage"
  115. @register="register"
  116. >
  117. <template #visible="{ row }">
  118. <span>{{ row.visible ? '是' : '否' }} </span>
  119. </template>
  120. <template #type="{ row }">
  121. <DictTag :type="DICT_TYPE.INFRA_CONFIG_TYPE" :value="row.type" />
  122. </template>
  123. <template #createTime="{ row }">
  124. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  125. </template>
  126. <template #action="{ row }">
  127. <el-button
  128. link
  129. type="primary"
  130. v-hasPermi="['infra:config:update']"
  131. @click="handleUpdate(row)"
  132. >
  133. <Icon icon="ep:edit" class="mr-5px" /> {{ t('action.edit') }}
  134. </el-button>
  135. <el-button
  136. link
  137. type="primary"
  138. v-hasPermi="['infra:config:update']"
  139. @click="handleDetail(row)"
  140. >
  141. <Icon icon="ep:view" class="mr-5px" /> {{ t('action.detail') }}
  142. </el-button>
  143. <el-button
  144. link
  145. type="primary"
  146. v-hasPermi="['infra:config:delete']"
  147. @click="handleDelete(row)"
  148. >
  149. <Icon icon="ep:delete" class="mr-5px" /> {{ t('action.del') }}
  150. </el-button>
  151. </template>
  152. </Table>
  153. </ContentWrap>
  154. <Dialog v-model="dialogVisible" :title="dialogTitle">
  155. <!-- 对话框(添加 / 修改) -->
  156. <Form
  157. v-if="['create', 'update'].includes(actionType)"
  158. :schema="allSchemas.formSchema"
  159. :rules="rules"
  160. ref="formRef"
  161. />
  162. <!-- 对话框(详情) -->
  163. <Descriptions
  164. v-if="actionType === 'detail'"
  165. :schema="allSchemas.detailSchema"
  166. :data="detailRef"
  167. >
  168. <template #visible="{ row }">
  169. <span>{{ row.visible ? '是' : '否' }} </span>
  170. </template>
  171. <template #type="{ row }">
  172. <DictTag :type="DICT_TYPE.INFRA_CONFIG_TYPE" :value="row.type" />
  173. </template>
  174. <template #createTime="{ row }">
  175. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  176. </template>
  177. </Descriptions>
  178. <!-- 操作按钮 -->
  179. <template #footer>
  180. <el-button
  181. v-if="['create', 'update'].includes(actionType)"
  182. type="primary"
  183. :loading="actionLoading"
  184. @click="submitForm"
  185. >
  186. {{ t('action.save') }}
  187. </el-button>
  188. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  189. </template>
  190. </Dialog>
  191. </template>