index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.id)"
  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>
  51. <!-- 操作按钮 -->
  52. <template #footer>
  53. <XButton
  54. v-if="['create', 'update'].includes(actionType)"
  55. type="primary"
  56. :title="t('action.save')"
  57. :loading="actionLoading"
  58. @click="submitForm"
  59. />
  60. <XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
  61. </template>
  62. </XModal>
  63. </template>
  64. <script setup lang="ts">
  65. import { ref, unref } from 'vue'
  66. import { rules, allSchemas } from './errorCode.data'
  67. import * as ErrorCodeApi from '@/api/system/errorCode'
  68. import { useI18n } from '@/hooks/web/useI18n'
  69. import { useMessage } from '@/hooks/web/useMessage'
  70. import { useVxeGrid } from '@/hooks/web/useVxeGrid'
  71. import { VxeGridInstance } from 'vxe-table'
  72. import { FormExpose } from '@/components/Form'
  73. const { t } = useI18n() // 国际化
  74. const message = useMessage() // 消息弹窗
  75. const dialogVisible = ref(false) // 是否显示弹出层
  76. const dialogTitle = ref('edit') // 弹出层标题
  77. const actionType = ref('') // 操作按钮的类型
  78. const actionLoading = ref(false) // 按钮Loading
  79. const xGrid = ref<VxeGridInstance>() // grid Ref
  80. const formRef = ref<FormExpose>() // 表单 Ref
  81. const detailRef = ref() // 详情 Ref
  82. const { gridOptions } = useVxeGrid<ErrorCodeApi.ErrorCodeVO>({
  83. allSchemas: allSchemas,
  84. getListApi: ErrorCodeApi.getErrorCodePageApi
  85. })
  86. // 设置标题
  87. const setDialogTile = (type: string) => {
  88. dialogTitle.value = t('action.' + type)
  89. actionType.value = type
  90. dialogVisible.value = true
  91. }
  92. // 新增操作
  93. const handleCreate = () => {
  94. setDialogTile('create')
  95. // 重置表单
  96. unref(formRef)?.getElFormRef()?.resetFields()
  97. }
  98. // 修改操作
  99. const handleUpdate = async (rowId: number) => {
  100. setDialogTile('update')
  101. // 设置数据
  102. const res = await ErrorCodeApi.getErrorCodeApi(rowId)
  103. unref(formRef)?.setValues(res)
  104. }
  105. // 详情操作
  106. const handleDetail = async (rowId: number) => {
  107. setDialogTile('detail')
  108. // 设置数据
  109. const res = await ErrorCodeApi.getErrorCodeApi(rowId)
  110. detailRef.value = res
  111. }
  112. // 删除操作
  113. const handleDelete = async (rowId: number) => {
  114. message
  115. .delConfirm()
  116. .then(async () => {
  117. await ErrorCodeApi.deleteErrorCodeApi(rowId)
  118. message.success(t('common.delSuccess'))
  119. })
  120. .finally(() => {
  121. xGrid.value?.commitProxy('query')
  122. })
  123. }
  124. // 提交按钮
  125. const submitForm = async () => {
  126. const elForm = unref(formRef)?.getElFormRef()
  127. if (!elForm) return
  128. elForm.validate(async (valid) => {
  129. if (valid) {
  130. actionLoading.value = true
  131. // 提交请求
  132. try {
  133. const data = unref(formRef)?.formModel as ErrorCodeApi.ErrorCodeVO
  134. if (actionType.value === 'create') {
  135. await ErrorCodeApi.createErrorCodeApi(data)
  136. message.success(t('common.createSuccess'))
  137. } else {
  138. await ErrorCodeApi.updateErrorCodeApi(data)
  139. message.success(t('common.updateSuccess'))
  140. }
  141. dialogVisible.value = false
  142. } finally {
  143. actionLoading.value = false
  144. xGrid.value?.commitProxy('query')
  145. }
  146. }
  147. })
  148. }
  149. </script>