index.vue 5.2 KB

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