index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <script setup lang="ts">
  2. import { ref, unref } from 'vue'
  3. import dayjs from 'dayjs'
  4. import { ElMessage } from 'element-plus'
  5. import { DICT_TYPE } from '@/utils/dict'
  6. import { useTable } from '@/hooks/web/useTable'
  7. import { useI18n } from '@/hooks/web/useI18n'
  8. import { FormExpose } from '@/components/Form'
  9. import type { PostVO } from '@/api/system/post/types'
  10. import { rules, allSchemas } from './post.data'
  11. import * as PostApi from '@/api/system/post'
  12. const { t } = useI18n() // 国际化
  13. // ========== 列表相关 ==========
  14. const { register, tableObject, methods } = useTable<PostVO>({
  15. getListApi: PostApi.getPostPageApi,
  16. delListApi: PostApi.deletePostApi,
  17. exportListApi: PostApi.exportPostApi
  18. })
  19. const { getList, setSearchParams, delList, exportList } = methods
  20. // 导出操作
  21. const handleExport = async () => {
  22. await exportList('用户数据.xls')
  23. }
  24. // ========== CRUD 相关 ==========
  25. const actionLoading = ref(false) // 遮罩层
  26. const actionType = ref('') // 操作按钮的类型
  27. const dialogVisible = ref(false) // 是否显示弹出层
  28. const dialogTitle = ref('edit') // 弹出层标题
  29. const formRef = ref<FormExpose>() // 表单 Ref
  30. // 设置标题
  31. const setDialogTile = (type: string) => {
  32. dialogTitle.value = t('action.' + type)
  33. actionType.value = type
  34. dialogVisible.value = true
  35. }
  36. // 新增操作
  37. const handleCreate = () => {
  38. setDialogTile('create')
  39. // 重置表单
  40. unref(formRef)?.getElFormRef()?.resetFields()
  41. }
  42. // 修改操作
  43. const handleUpdate = async (row: PostVO) => {
  44. setDialogTile('update')
  45. // 设置数据
  46. const res = await PostApi.getPostApi(row.id)
  47. unref(formRef)?.setValues(res)
  48. }
  49. // 提交按钮
  50. const submitForm = async () => {
  51. actionLoading.value = true
  52. // 提交请求
  53. try {
  54. const data = unref(formRef)?.formModel as PostVO
  55. if (actionType.value === 'create') {
  56. await PostApi.createPostApi(data)
  57. ElMessage.success(t('common.createSuccess'))
  58. } else {
  59. await PostApi.updatePostApi(data)
  60. ElMessage.success(t('common.updateSuccess'))
  61. }
  62. // 操作成功,重新加载列表
  63. dialogVisible.value = false
  64. await getList()
  65. } finally {
  66. actionLoading.value = false
  67. }
  68. }
  69. // 删除操作
  70. const handleDelete = (row: PostVO) => {
  71. delList(row.id, false)
  72. }
  73. // ========== 详情相关 ==========
  74. const detailRef = ref() // 详情 Ref
  75. // 详情操作
  76. const handleDetail = async (row: PostVO) => {
  77. // 设置数据
  78. detailRef.value = row
  79. setDialogTile('detail')
  80. }
  81. // ========== 初始化 ==========
  82. getList()
  83. </script>
  84. <template>
  85. <!-- 搜索工作区 -->
  86. <ContentWrap>
  87. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  88. </ContentWrap>
  89. <ContentWrap>
  90. <!-- 操作工具栏 -->
  91. <div class="mb-10px">
  92. <el-button type="primary" v-hasPermi="['system:post:create']" @click="handleCreate">
  93. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  94. </el-button>
  95. <el-button
  96. type="warning"
  97. v-hasPermi="['system:post:export']"
  98. :loading="tableObject.exportLoading"
  99. @click="handleExport"
  100. >
  101. <Icon icon="ep:download" class="mr-5px" /> {{ t('action.export') }}
  102. </el-button>
  103. </div>
  104. <!-- 列表 -->
  105. <Table
  106. :columns="allSchemas.tableColumns"
  107. :selection="false"
  108. :data="tableObject.tableList"
  109. :loading="tableObject.loading"
  110. :pagination="{
  111. total: tableObject.total
  112. }"
  113. v-model:pageSize="tableObject.pageSize"
  114. v-model:currentPage="tableObject.currentPage"
  115. @register="register"
  116. >
  117. <template #status="{ row }">
  118. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  119. </template>
  120. <template #createTime="{ row }">
  121. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  122. </template>
  123. <template #action="{ row }">
  124. <el-button
  125. link
  126. type="primary"
  127. v-hasPermi="['system:post:update']"
  128. @click="handleUpdate(row)"
  129. >
  130. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  131. </el-button>
  132. <el-button
  133. link
  134. type="primary"
  135. v-hasPermi="['system:post:update']"
  136. @click="handleDetail(row)"
  137. >
  138. <Icon icon="ep:view" class="mr-1px" /> {{ t('action.detail') }}
  139. </el-button>
  140. <el-button
  141. link
  142. type="primary"
  143. v-hasPermi="['system:post:delete']"
  144. @click="handleDelete(row)"
  145. >
  146. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  147. </el-button>
  148. </template>
  149. </Table>
  150. </ContentWrap>
  151. <Dialog v-model="dialogVisible" :title="dialogTitle">
  152. <!-- 对话框(添加 / 修改) -->
  153. <Form
  154. v-if="['create', 'update'].includes(actionType)"
  155. :schema="allSchemas.formSchema"
  156. :rules="rules"
  157. ref="formRef"
  158. />
  159. <!-- 对话框(详情) -->
  160. <Descriptions
  161. v-if="actionType === 'detail'"
  162. :schema="allSchemas.detailSchema"
  163. :data="detailRef"
  164. >
  165. <template #status="{ row }">
  166. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  167. </template>
  168. <template #createTime="{ row }">
  169. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  170. </template>
  171. </Descriptions>
  172. <!-- 操作按钮 -->
  173. <template #footer>
  174. <el-button
  175. v-if="['create', 'update'].includes(actionType)"
  176. type="primary"
  177. :loading="actionLoading"
  178. @click="submitForm"
  179. >
  180. {{ t('action.save') }}
  181. </el-button>
  182. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  183. </template>
  184. </Dialog>
  185. </template>