index.vue 5.5 KB

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