index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <ContentWrap>
  3. <!-- 列表 -->
  4. <XTable @register="registerTable">
  5. <template #toolbar_buttons>
  6. <!-- 操作:新增 -->
  7. <XButton
  8. type="primary"
  9. preIcon="ep:zoom-in"
  10. :title="t('action.add')"
  11. v-hasPermi="['system:post:create']"
  12. @click="openModel('create')"
  13. />
  14. <!-- 操作:导出 -->
  15. <XButton
  16. type="warning"
  17. preIcon="ep:download"
  18. :title="t('action.export')"
  19. v-hasPermi="['system:post:export']"
  20. @click="exportList('岗位列表.xls')"
  21. />
  22. </template>
  23. <template #actionbtns_default="{ row }">
  24. <!-- 操作:修改 -->
  25. <XTextButton
  26. preIcon="ep:edit"
  27. v-hasPermi="['system:post:update']"
  28. @click="openModel('update', row.id)"
  29. />
  30. <!-- 操作:详情 -->
  31. <XTextButton
  32. preIcon="ep:view"
  33. v-hasPermi="['system:post:query']"
  34. @click="openModel('detail', row.id)"
  35. />
  36. <!-- 操作:删除 -->
  37. <XTextButton
  38. preIcon="ep:delete"
  39. v-hasPermi="['system:post:delete']"
  40. @click="deleteData(row.id)"
  41. />
  42. </template>
  43. </XTable>
  44. </ContentWrap>
  45. <!-- 弹窗 -->
  46. <XModal id="postModel" :loading="modelLoading" v-model="modelVisible" :title="modelTitle">
  47. <!-- 表单:添加/修改 -->
  48. <Form
  49. ref="formRef"
  50. v-if="['create', 'update'].includes(actionType)"
  51. :schema="allSchemas.formSchema"
  52. :rules="rules"
  53. />
  54. <!-- 表单:详情 -->
  55. <Descriptions
  56. v-if="actionType === 'detail'"
  57. :schema="allSchemas.detailSchema"
  58. :data="detailData"
  59. />
  60. <template #footer>
  61. <!-- 按钮:保存 -->
  62. <XButton
  63. v-if="['create', 'update'].includes(actionType)"
  64. type="primary"
  65. :title="t('action.save')"
  66. :loading="actionLoading"
  67. @click="submitForm()"
  68. />
  69. <!-- 按钮:关闭 -->
  70. <XButton :loading="actionLoading" :title="t('dialog.close')" @click="modelVisible = false" />
  71. </template>
  72. </XModal>
  73. </template>
  74. <script setup lang="ts" name="Post">
  75. // 全局相关的 import
  76. import { ref, unref } from 'vue'
  77. import { useI18n } from '@/hooks/web/useI18n'
  78. import { useMessage } from '@/hooks/web/useMessage'
  79. import { useXTable } from '@/hooks/web/useXTable'
  80. import { FormExpose } from '@/components/Form'
  81. // 业务相关的 import
  82. import * as PostApi from '@/api/system/post'
  83. import { rules, allSchemas } from './post.data'
  84. const { t } = useI18n() // 国际化
  85. const message = useMessage() // 消息弹窗
  86. // 列表相关的变量
  87. const [registerTable, { reload, deleteData, exportList }] = useXTable({
  88. allSchemas: allSchemas,
  89. getListApi: PostApi.getPostPageApi,
  90. deleteApi: PostApi.deletePostApi,
  91. exportListApi: PostApi.exportPostApi
  92. })
  93. // 弹窗相关的变量
  94. const modelVisible = ref(false) // 是否显示弹出层
  95. const modelTitle = ref('edit') // 弹出层标题
  96. const modelLoading = ref(false) // 弹出层loading
  97. const actionType = ref('') // 操作按钮的类型
  98. const actionLoading = ref(false) // 按钮 Loading
  99. const formRef = ref<FormExpose>() // 表单 Ref
  100. const detailData = ref() // 详情 Ref
  101. const openModel = async (type: string, rowId?: number) => {
  102. modelLoading.value = true
  103. modelTitle.value = t('action.' + type)
  104. actionType.value = type
  105. modelVisible.value = true
  106. // 设置数据
  107. if (rowId) {
  108. const res = await PostApi.getPostApi(rowId)
  109. if (type === 'update') {
  110. unref(formRef)?.setValues(res)
  111. } else if (type === 'detail') {
  112. detailData.value = res
  113. }
  114. }
  115. modelLoading.value = false
  116. }
  117. // 提交新增/修改的表单
  118. const submitForm = async () => {
  119. const elForm = unref(formRef)?.getElFormRef()
  120. if (!elForm) return
  121. elForm.validate(async (valid) => {
  122. if (valid) {
  123. actionLoading.value = true
  124. // 提交请求
  125. try {
  126. const data = unref(formRef)?.formModel as PostApi.PostVO
  127. if (actionType.value === 'create') {
  128. await PostApi.createPostApi(data)
  129. message.success(t('common.createSuccess'))
  130. } else {
  131. await PostApi.updatePostApi(data)
  132. message.success(t('common.updateSuccess'))
  133. }
  134. modelVisible.value = false
  135. } finally {
  136. actionLoading.value = false
  137. // 刷新列表
  138. reload()
  139. }
  140. }
  141. })
  142. }
  143. </script>