index.vue 4.8 KB

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