index.vue 5.3 KB

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