index.vue 4.9 KB

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