index.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 { NoticeVO } from '@/api/system/notice/types'
  10. import { rules, allSchemas } from './notice.data'
  11. import * as NoticeApi from '@/api/system/notice'
  12. const { t } = useI18n() // 国际化
  13. // ========== 列表相关 ==========
  14. const { register, tableObject, methods } = useTable<NoticeVO>({
  15. getListApi: NoticeApi.getNoticePageApi,
  16. delListApi: NoticeApi.deleteNoticeApi
  17. })
  18. const { getList, setSearchParams, delList } = methods
  19. // ========== CRUD 相关 ==========
  20. const actionLoading = ref(false) // 遮罩层
  21. const actionType = ref('') // 操作按钮的类型
  22. const dialogVisible = ref(false) // 是否显示弹出层
  23. const dialogTitle = ref('edit') // 弹出层标题
  24. const formRef = ref<FormExpose>() // 表单 Ref
  25. // 设置标题
  26. const setDialogTile = (type: string) => {
  27. dialogTitle.value = t('action.' + type)
  28. actionType.value = type
  29. dialogVisible.value = true
  30. }
  31. // 新增操作
  32. const handleCreate = () => {
  33. setDialogTile('create')
  34. // 重置表单
  35. unref(formRef)?.getElFormRef()?.resetFields()
  36. }
  37. // 修改操作
  38. const handleUpdate = async (row: NoticeVO) => {
  39. setDialogTile('update')
  40. // 设置数据
  41. const res = await NoticeApi.getNoticeApi(row.id)
  42. unref(formRef)?.setValues(res)
  43. }
  44. // 提交按钮
  45. const submitForm = async () => {
  46. const elForm = unref(formRef)?.getElFormRef()
  47. if (!elForm) return
  48. elForm.validate(async (valid) => {
  49. if (valid) {
  50. actionLoading.value = true
  51. // 提交请求
  52. try {
  53. const data = unref(formRef)?.formModel as NoticeVO
  54. if (actionType.value === 'create') {
  55. await NoticeApi.createNoticeApi(data)
  56. ElMessage.success(t('common.createSuccess'))
  57. } else {
  58. await NoticeApi.updateNoticeApi(data)
  59. ElMessage.success(t('common.updateSuccess'))
  60. }
  61. // 操作成功,重新加载列表
  62. dialogVisible.value = false
  63. await getList()
  64. } finally {
  65. actionLoading.value = false
  66. }
  67. }
  68. })
  69. }
  70. // ========== 详情相关 ==========
  71. const detailRef = ref() // 详情 Ref
  72. // 详情操作
  73. const handleDetail = async (row: NoticeVO) => {
  74. // 设置数据
  75. detailRef.value = row
  76. setDialogTile('detail')
  77. }
  78. // ========== 初始化 ==========
  79. getList()
  80. </script>
  81. <template>
  82. <!-- 搜索工作区 -->
  83. <ContentWrap>
  84. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  85. </ContentWrap>
  86. <ContentWrap>
  87. <!-- 操作工具栏 -->
  88. <div class="mb-10px">
  89. <el-button type="primary" v-hasPermi="['system:notice:create']" @click="handleCreate">
  90. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  91. </el-button>
  92. </div>
  93. <!-- 列表 -->
  94. <Table
  95. :columns="allSchemas.tableColumns"
  96. :selection="false"
  97. :data="tableObject.tableList"
  98. :loading="tableObject.loading"
  99. :pagination="{
  100. total: tableObject.total
  101. }"
  102. v-model:pageSize="tableObject.pageSize"
  103. v-model:currentPage="tableObject.currentPage"
  104. @register="register"
  105. >
  106. <template #type="{ row }">
  107. <DictTag :type="DICT_TYPE.SYSTEM_NOTICE_TYPE" :value="row.type" />
  108. </template>
  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:notice: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:notice: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:notice: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" maxHeight="500px" width="50%">
  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 #type="{ row }">
  158. <DictTag :type="DICT_TYPE.SYSTEM_NOTICE_TYPE" :value="row.type" />
  159. </template>
  160. <template #status="{ row }">
  161. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  162. </template>
  163. <template #createTime="{ row }">
  164. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  165. </template>
  166. </Descriptions>
  167. <!-- 操作按钮 -->
  168. <template #footer>
  169. <el-button
  170. v-if="['create', 'update'].includes(actionType)"
  171. type="primary"
  172. :loading="actionLoading"
  173. @click="submitForm"
  174. >
  175. {{ t('action.save') }}
  176. </el-button>
  177. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  178. </template>
  179. </Dialog>
  180. </template>