index.vue 4.2 KB

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