index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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:query']"
  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. <XModal 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="detailData"
  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. </XModal>
  68. </template>
  69. <script setup lang="ts" name="ErrorCode">
  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, getList, deleteData } = useVxeGrid<ErrorCodeApi.ErrorCodeVO>({
  85. allSchemas: allSchemas,
  86. getListApi: ErrorCodeApi.getErrorCodePageApi,
  87. deleteApi: ErrorCodeApi.deleteErrorCodeApi
  88. })
  89. // 弹窗相关的变量
  90. const dialogVisible = ref(false) // 是否显示弹出层
  91. const dialogTitle = ref('edit') // 弹出层标题
  92. const actionType = ref('') // 操作按钮的类型
  93. const actionLoading = ref(false) // 按钮 Loading
  94. const formRef = ref<FormExpose>() // 表单 Ref
  95. const detailData = ref() // 详情 Ref
  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. // 修改操作
  107. const handleUpdate = async (rowId: number) => {
  108. setDialogTile('update')
  109. // 设置数据
  110. const res = await ErrorCodeApi.getErrorCodeApi(rowId)
  111. unref(formRef)?.setValues(res)
  112. }
  113. // 详情操作
  114. const handleDetail = async (rowId: number) => {
  115. setDialogTile('detail')
  116. // 设置数据
  117. const res = await ErrorCodeApi.getErrorCodeApi(rowId)
  118. detailData.value = res
  119. }
  120. // 删除操作
  121. const handleDelete = async (rowId: number) => {
  122. await deleteData(xGrid, rowId)
  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. // 刷新列表
  145. await getList(xGrid)
  146. }
  147. }
  148. })
  149. }
  150. </script>