index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. type="primary"
  64. :title="t('action.save')"
  65. :loading="actionLoading"
  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 { VxeGridInstance } from 'vxe-table'
  83. import { FormExpose } from '@/components/Form'
  84. const { t } = useI18n() // 国际化
  85. const message = useMessage() // 消息弹窗
  86. const dialogVisible = ref(false) // 是否显示弹出层
  87. const dialogTitle = ref('edit') // 弹出层标题
  88. const actionType = ref('') // 操作按钮的类型
  89. const actionLoading = ref(false) // 按钮Loading
  90. const xGrid = ref<VxeGridInstance>() // grid Ref
  91. const formRef = ref<FormExpose>() // 表单 Ref
  92. const detailRef = ref() // 详情 Ref
  93. const { gridOptions } = useVxeGrid<PostVO>({
  94. allSchemas: allSchemas,
  95. getListApi: PostApi.getPostPageApi
  96. })
  97. // 设置标题
  98. const setDialogTile = (type: string) => {
  99. dialogTitle.value = t('action.' + type)
  100. actionType.value = type
  101. dialogVisible.value = true
  102. }
  103. // 新增操作
  104. const handleCreate = () => {
  105. setDialogTile('create')
  106. // 重置表单
  107. unref(formRef)?.getElFormRef()?.resetFields()
  108. }
  109. // 详情操作
  110. const handleDetail = (row: PostVO) => {
  111. setDialogTile('detail')
  112. detailRef.value = row
  113. }
  114. // 修改操作
  115. const handleUpdate = async (rowId: number) => {
  116. setDialogTile('update')
  117. // 设置数据
  118. const res = await PostApi.getPostApi(rowId)
  119. unref(formRef)?.setValues(res)
  120. }
  121. // 删除操作
  122. const handleDelete = async (rowId: number) => {
  123. message
  124. .delConfirm()
  125. .then(async () => {
  126. await PostApi.deletePostApi(rowId)
  127. message.success(t('common.delSuccess'))
  128. })
  129. .finally(() => {
  130. xGrid.value?.commitProxy('query')
  131. })
  132. }
  133. // 提交按钮
  134. const submitForm = async () => {
  135. const elForm = unref(formRef)?.getElFormRef()
  136. if (!elForm) return
  137. elForm.validate(async (valid) => {
  138. if (valid) {
  139. actionLoading.value = true
  140. // 提交请求
  141. try {
  142. const data = unref(formRef)?.formModel as PostVO
  143. if (actionType.value === 'create') {
  144. await PostApi.createPostApi(data)
  145. message.success(t('common.createSuccess'))
  146. } else {
  147. await PostApi.updatePostApi(data)
  148. message.success(t('common.updateSuccess'))
  149. }
  150. dialogVisible.value = false
  151. } finally {
  152. actionLoading.value = false
  153. xGrid.value?.commitProxy('query')
  154. }
  155. }
  156. })
  157. }
  158. </script>