index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <ContentWrap>
  3. <vxe-grid ref="xGrid" v-bind="gridOptions" class="xtable-scrollbar">
  4. <template #toolbar_buttons>
  5. <XButton
  6. type="primary"
  7. preIcon="ep:zoom-in"
  8. :title="t('action.add')"
  9. v-hasPermi="['system:post:create']"
  10. @click="handleCreate()"
  11. />
  12. </template>
  13. <template #status_default="{ row }">
  14. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  15. </template>
  16. <template #action_default="{ row }">
  17. <XButton
  18. link
  19. type="primary"
  20. preIcon="ep:edit"
  21. :title="t('action.edit')"
  22. v-hasPermi="['system:post:update']"
  23. @click="handleUpdate(row.id)"
  24. />
  25. <XButton
  26. link
  27. type="primary"
  28. preIcon="ep:view"
  29. :title="t('action.detail')"
  30. v-hasPermi="['system:post:update']"
  31. @click="handleDetail(row)"
  32. />
  33. <XButton
  34. link
  35. type="primary"
  36. preIcon="ep:delete"
  37. :title="t('action.del')"
  38. v-hasPermi="['system:post:delete']"
  39. @click="handleDelete(row.id)"
  40. />
  41. </template>
  42. </vxe-grid>
  43. </ContentWrap>
  44. <XModal id="postModel" v-model="dialogVisible" :title="dialogTitle">
  45. <template #default>
  46. <!-- 对话框(添加 / 修改) -->
  47. <vxe-form
  48. ref="xForm"
  49. v-if="['create', 'update'].includes(actionType)"
  50. :data="formData"
  51. :items="formItems"
  52. :rules="rules"
  53. />
  54. <Descriptions
  55. v-if="actionType === 'detail'"
  56. :schema="allSchemas.detailSchema"
  57. :data="detailRef"
  58. >
  59. <template #status="{ row }">
  60. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  61. </template>
  62. <template #createTime="{ row }">
  63. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  64. </template>
  65. </Descriptions>
  66. </template>
  67. <template #footer>
  68. <XButton
  69. v-if="['create', 'update'].includes(actionType)"
  70. :loading="actionLoading"
  71. :title="t('action.save')"
  72. type="primary"
  73. @click="submitForm"
  74. />
  75. <XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
  76. </template>
  77. </XModal>
  78. </template>
  79. <script setup lang="ts">
  80. import { ref } from 'vue'
  81. import dayjs from 'dayjs'
  82. import { useI18n } from '@/hooks/web/useI18n'
  83. import { VxeFormEvents, VxeFormInstance, VxeFormItemProps, VxeGridInstance } from 'vxe-table'
  84. import * as PostApi from '@/api/system/post'
  85. import { DICT_TYPE } from '@/utils/dict'
  86. import { ContentWrap } from '@/components/ContentWrap'
  87. import { PostVO } from '@/api/system/post/types'
  88. import { rules, allSchemas } from './post.data'
  89. import { useMessage } from '@/hooks/web/useMessage'
  90. import { useVxeGrid } from '@/hooks/web/useVxeGrid'
  91. const { t } = useI18n() // 国际化
  92. const message = useMessage()
  93. const xGrid = ref<VxeGridInstance>()
  94. const xForm = ref<VxeFormInstance>()
  95. const dialogVisible = ref(false) // 是否显示弹出层
  96. const dialogTitle = ref('edit') // 弹出层标题
  97. const actionType = ref('') // 操作按钮的类型
  98. const actionLoading = ref(false) // 遮罩层
  99. const gridOptions = useVxeGrid(allSchemas, PostApi.getPostPageApi)
  100. const formData = ref<PostVO>()
  101. const formItems = ref<VxeFormItemProps[]>(allSchemas.formSchema)
  102. // 设置标题
  103. const setDialogTile = (type: string) => {
  104. dialogTitle.value = t('action.' + type)
  105. actionType.value = type
  106. dialogVisible.value = true
  107. }
  108. // ========== 详情相关 ==========
  109. const detailRef = ref() // 详情 Ref
  110. // 详情操作
  111. const handleDetail = (row: PostVO) => {
  112. setDialogTile('detail')
  113. detailRef.value = row
  114. }
  115. // 新增操作
  116. const handleCreate = () => {
  117. setDialogTile('create')
  118. formData.value = undefined
  119. }
  120. // 修改操作
  121. const handleUpdate = async (rowId: number) => {
  122. setDialogTile('update')
  123. // 设置数据
  124. const res = await PostApi.getPostApi(rowId)
  125. formData.value = res
  126. }
  127. // 删除操作
  128. const handleDelete = (rowId: number) => {
  129. message
  130. .confirm(t('common.delMessage'), t('common.confirmTitle'))
  131. .then(async () => {
  132. await PostApi.deletePostApi(rowId)
  133. message.success(t('common.delSuccess'))
  134. })
  135. .finally(() => {
  136. xGrid.value?.commitProxy('query')
  137. })
  138. }
  139. // 提交按钮
  140. const submitForm: VxeFormEvents.Submit = async () => {
  141. actionLoading.value = true
  142. // 提交请求
  143. try {
  144. const data = formData.value as 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. // 操作成功,重新加载列表
  153. dialogVisible.value = false
  154. } finally {
  155. actionLoading.value = false
  156. xGrid.value?.commitProxy('query')
  157. }
  158. }
  159. </script>