index.vue 4.3 KB

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