index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <ContentWrap>
  3. <vxe-grid ref="xGrid" v-bind="gridOptions" class="xtable-scrollbar">
  4. <template #toolbar_buttons>
  5. <XButton
  6. type="primary"
  7. preIcon="ep:zoom-in"
  8. :title="t('action.add')"
  9. v-hasPermi="['system:error-code:create']"
  10. @click="handleCreate()"
  11. />
  12. </template>
  13. <template #actionbtns_default="{ row }">
  14. <XTextButton
  15. preIcon="ep:edit"
  16. :title="t('action.edit')"
  17. v-hasPermi="['system:error-code:update']"
  18. @click="handleUpdate(row.id)"
  19. />
  20. <XTextButton
  21. preIcon="ep:view"
  22. :title="t('action.detail')"
  23. v-hasPermi="['system:error-code:update']"
  24. @click="handleDetail(row)"
  25. />
  26. <XTextButton
  27. preIcon="ep:delete"
  28. :title="t('action.del')"
  29. v-hasPermi="['system:error-code:delete']"
  30. @click="handleDelete(row.id)"
  31. />
  32. </template>
  33. </vxe-grid>
  34. </ContentWrap>
  35. <XModal id="errorCodeModel" v-model="dialogVisible" :title="dialogTitle">
  36. <template #default>
  37. <!-- 对话框(添加 / 修改) -->
  38. <Form
  39. v-if="['create', 'update'].includes(actionType)"
  40. :schema="allSchemas.formSchema"
  41. :rules="rules"
  42. ref="formRef"
  43. />
  44. <!-- 对话框(详情) -->
  45. <Descriptions
  46. v-if="actionType === 'detail'"
  47. :schema="allSchemas.detailSchema"
  48. :data="detailRef"
  49. >
  50. <template #type="{ row }">
  51. <DictTag :type="DICT_TYPE.SYSTEM_ERROR_CODE_TYPE" :value="row.type" />
  52. </template>
  53. <template #createTime="{ row }">
  54. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  55. </template>
  56. </Descriptions>
  57. </template>
  58. <!-- 操作按钮 -->
  59. <template #footer>
  60. <XButton
  61. v-if="['create', 'update'].includes(actionType)"
  62. type="primary"
  63. :title="t('action.save')"
  64. :loading="actionLoading"
  65. @click="submitForm"
  66. />
  67. <XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
  68. </template>
  69. </XModal>
  70. </template>
  71. <script setup lang="ts">
  72. import { ref, unref } from 'vue'
  73. import dayjs from 'dayjs'
  74. import { DICT_TYPE } from '@/utils/dict'
  75. import type { ErrorCodeVO } from '@/api/system/errorCode/types'
  76. import { rules, allSchemas } from './errorCode.data'
  77. import * as ErrorCodeApi from '@/api/system/errorCode'
  78. import { useI18n } from '@/hooks/web/useI18n'
  79. import { useMessage } from '@/hooks/web/useMessage'
  80. import { useVxeGrid } from '@/hooks/web/useVxeGrid'
  81. import { VxeGridInstance } from 'vxe-table'
  82. import { FormExpose } from '@/components/Form'
  83. const { t } = useI18n() // 国际化
  84. const message = useMessage() // 消息弹窗
  85. const dialogVisible = ref(false) // 是否显示弹出层
  86. const dialogTitle = ref('edit') // 弹出层标题
  87. const actionType = ref('') // 操作按钮的类型
  88. const actionLoading = ref(false) // 按钮Loading
  89. const xGrid = ref<VxeGridInstance>() // grid Ref
  90. const formRef = ref<FormExpose>() // 表单 Ref
  91. const detailRef = ref() // 详情 Ref
  92. const { gridOptions } = useVxeGrid<ErrorCodeVO>({
  93. allSchemas: allSchemas,
  94. getListApi: ErrorCodeApi.getErrorCodePageApi
  95. })
  96. // 设置标题
  97. const setDialogTile = (type: string) => {
  98. dialogTitle.value = t('action.' + type)
  99. actionType.value = type
  100. dialogVisible.value = true
  101. }
  102. // 新增操作
  103. const handleCreate = () => {
  104. setDialogTile('create')
  105. // 重置表单
  106. unref(formRef)?.getElFormRef()?.resetFields()
  107. }
  108. // 详情操作
  109. const handleDetail = async (row: ErrorCodeVO) => {
  110. // 设置数据
  111. detailRef.value = row
  112. setDialogTile('detail')
  113. }
  114. // 修改操作
  115. const handleUpdate = async (rowId: number) => {
  116. setDialogTile('update')
  117. // 设置数据
  118. const res = await ErrorCodeApi.getErrorCodeApi(rowId)
  119. unref(formRef)?.setValues(res)
  120. }
  121. // 删除操作
  122. const handleDelete = async (rowId: number) => {
  123. message
  124. .delConfirm()
  125. .then(async () => {
  126. await ErrorCodeApi.deleteErrorCodeApi(rowId)
  127. message.success(t('common.delSuccess'))
  128. })
  129. .finally(() => {
  130. xGrid.value?.commitProxy('query')
  131. })
  132. }
  133. // 提交按钮
  134. const submitForm = async () => {
  135. const elForm = unref(formRef)?.getElFormRef()
  136. if (!elForm) return
  137. elForm.validate(async (valid) => {
  138. if (valid) {
  139. actionLoading.value = true
  140. // 提交请求
  141. try {
  142. const data = unref(formRef)?.formModel as ErrorCodeVO
  143. if (actionType.value === 'create') {
  144. await ErrorCodeApi.createErrorCodeApi(data)
  145. message.success(t('common.createSuccess'))
  146. } else {
  147. await ErrorCodeApi.updateErrorCodeApi(data)
  148. message.success(t('common.updateSuccess'))
  149. }
  150. dialogVisible.value = false
  151. } finally {
  152. actionLoading.value = false
  153. xGrid.value?.commitProxy('query')
  154. }
  155. }
  156. })
  157. }
  158. </script>