index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <ContentWrap>
  3. <!-- 列表 -->
  4. <XTable @register="registerTable">
  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="deleteData(row.id)"
  36. />
  37. </template>
  38. </XTable>
  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 { useXTable } from '@/hooks/web/useXTable'
  75. import { FormExpose } from '@/components/Form'
  76. // 业务相关的 import
  77. import { rules, allSchemas } from './errorCode.data'
  78. import * as ErrorCodeApi from '@/api/system/errorCode'
  79. const { t } = useI18n() // 国际化
  80. const message = useMessage() // 消息弹窗
  81. // 列表相关的变量
  82. const [registerTable, { reload, deleteData }] = useXTable({
  83. allSchemas: allSchemas,
  84. getListApi: ErrorCodeApi.getErrorCodePageApi,
  85. deleteApi: ErrorCodeApi.deleteErrorCodeApi
  86. })
  87. // 弹窗相关的变量
  88. const dialogVisible = ref(false) // 是否显示弹出层
  89. const dialogTitle = ref('edit') // 弹出层标题
  90. const actionType = ref('') // 操作按钮的类型
  91. const actionLoading = ref(false) // 按钮 Loading
  92. const formRef = ref<FormExpose>() // 表单 Ref
  93. const detailData = ref() // 详情 Ref
  94. // 设置标题
  95. const setDialogTile = (type: string) => {
  96. dialogTitle.value = t('action.' + type)
  97. actionType.value = type
  98. dialogVisible.value = true
  99. }
  100. // 新增操作
  101. const handleCreate = () => {
  102. setDialogTile('create')
  103. }
  104. // 修改操作
  105. const handleUpdate = async (rowId: number) => {
  106. setDialogTile('update')
  107. // 设置数据
  108. const res = await ErrorCodeApi.getErrorCodeApi(rowId)
  109. unref(formRef)?.setValues(res)
  110. }
  111. // 详情操作
  112. const handleDetail = async (rowId: number) => {
  113. setDialogTile('detail')
  114. // 设置数据
  115. const res = await ErrorCodeApi.getErrorCodeApi(rowId)
  116. detailData.value = res
  117. }
  118. // 提交新增/修改的表单
  119. const submitForm = async () => {
  120. const elForm = unref(formRef)?.getElFormRef()
  121. if (!elForm) return
  122. elForm.validate(async (valid) => {
  123. if (valid) {
  124. actionLoading.value = true
  125. // 提交请求
  126. try {
  127. const data = unref(formRef)?.formModel as ErrorCodeApi.ErrorCodeVO
  128. if (actionType.value === 'create') {
  129. await ErrorCodeApi.createErrorCodeApi(data)
  130. message.success(t('common.createSuccess'))
  131. } else {
  132. await ErrorCodeApi.updateErrorCodeApi(data)
  133. message.success(t('common.updateSuccess'))
  134. }
  135. dialogVisible.value = false
  136. } finally {
  137. actionLoading.value = false
  138. // 刷新列表
  139. await reload()
  140. }
  141. }
  142. })
  143. }
  144. </script>