index.vue 5.5 KB

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