index.vue 4.8 KB

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