index.vue 5.1 KB

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