index.vue 4.8 KB

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