index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. <Dialog 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="detailRef"
  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. </Dialog>
  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. import download from '@/utils/download'
  88. const { t } = useI18n() // 国际化
  89. const message = useMessage() // 消息弹窗
  90. // 列表相关的变量
  91. const xGrid = ref<VxeGridInstance>() // 列表 Grid Ref
  92. const { gridOptions } = useVxeGrid<PostApi.PostVO>({
  93. allSchemas: allSchemas,
  94. getListApi: PostApi.getPostPageApi
  95. })
  96. // 弹窗相关的变量
  97. const dialogVisible = ref(false) // 是否显示弹出层
  98. const dialogTitle = ref('edit') // 弹出层标题
  99. const actionType = ref('') // 操作按钮的类型
  100. const actionLoading = ref(false) // 按钮 Loading
  101. const formRef = ref<FormExpose>() // 表单 Ref
  102. const detailRef = ref() // 详情 Ref
  103. // 设置标题
  104. const setDialogTile = (type: string) => {
  105. dialogTitle.value = t('action.' + type)
  106. actionType.value = type
  107. dialogVisible.value = true
  108. }
  109. // 新增操作
  110. const handleCreate = () => {
  111. setDialogTile('create')
  112. // 重置表单
  113. unref(formRef)?.getElFormRef()?.resetFields()
  114. }
  115. // 导出操作
  116. const handleExport = async () => {
  117. const queryParams = Object.assign(
  118. {},
  119. JSON.parse(JSON.stringify(xGrid.value?.getRefMaps().refForm.value.data))
  120. )
  121. const res = await PostApi.exportPostApi(queryParams)
  122. download.excel(res, '岗位列表.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. }
  131. // 详情操作
  132. const handleDetail = async (rowId: number) => {
  133. setDialogTile('detail')
  134. const res = await PostApi.getPostApi(rowId)
  135. detailRef.value = res
  136. }
  137. // 删除操作
  138. const handleDelete = async (rowId: number) => {
  139. message
  140. .delConfirm()
  141. .then(async () => {
  142. await PostApi.deletePostApi(rowId)
  143. message.success(t('common.delSuccess'))
  144. })
  145. .finally(() => {
  146. // 刷新列表
  147. xGrid.value?.commitProxy('query')
  148. })
  149. }
  150. // 提交新增/修改的表单
  151. const submitForm = async () => {
  152. const elForm = unref(formRef)?.getElFormRef()
  153. if (!elForm) return
  154. elForm.validate(async (valid) => {
  155. if (valid) {
  156. actionLoading.value = true
  157. // 提交请求
  158. try {
  159. const data = unref(formRef)?.formModel as PostApi.PostVO
  160. if (actionType.value === 'create') {
  161. await PostApi.createPostApi(data)
  162. message.success(t('common.createSuccess'))
  163. } else {
  164. await PostApi.updatePostApi(data)
  165. message.success(t('common.updateSuccess'))
  166. }
  167. dialogVisible.value = false
  168. } finally {
  169. actionLoading.value = false
  170. // 刷新列表
  171. xGrid.value?.commitProxy('query')
  172. }
  173. }
  174. })
  175. }
  176. </script>