index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <template>
  2. <ContentWrap>
  3. <!-- 列表 -->
  4. <XTable @register="registerTable">
  5. <!-- 操作:新增 -->
  6. <template #toolbar_buttons>
  7. <XButton
  8. type="primary"
  9. preIcon="ep:zoom-in"
  10. :title="t('action.add')"
  11. v-hasPermi="['system:notice: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:notice:update']"
  21. @click="handleUpdate(row.id)"
  22. />
  23. <!-- 操作:详情 -->
  24. <XTextButton
  25. preIcon="ep:view"
  26. :title="t('action.detail')"
  27. v-hasPermi="['system:notice:query']"
  28. @click="handleDetail(row.id)"
  29. />
  30. <!-- 操作:删除 -->
  31. <XTextButton
  32. preIcon="ep:delete"
  33. :title="t('action.del')"
  34. v-hasPermi="['system:notice:delete']"
  35. @click="handleDelete(row.id)"
  36. />
  37. </template>
  38. </XTable>
  39. </ContentWrap>
  40. <!-- 弹窗 -->
  41. <XModal id="noticeModel" v-model="dialogVisible" :title="dialogTitle">
  42. <!-- 对话框(添加 / 修改) -->
  43. <Form
  44. ref="formRef"
  45. v-if="['create', 'update'].includes(actionType)"
  46. :schema="allSchemas.formSchema"
  47. :rules="rules"
  48. />
  49. <!-- 对话框(详情) -->
  50. <Descriptions
  51. v-if="actionType === 'detail'"
  52. :schema="allSchemas.detailSchema"
  53. :data="detailData"
  54. >
  55. <template #content="{ row }">
  56. <Editor :model-value="row.content" :readonly="true" />
  57. </template>
  58. </Descriptions>
  59. <template #footer>
  60. <!-- 按钮:保存 -->
  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. <!-- 按钮:关闭 -->
  69. <XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
  70. </template>
  71. </XModal>
  72. </template>
  73. <script setup lang="ts" name="Notice">
  74. // 全局相关的 import
  75. import { ref, unref } from 'vue'
  76. import { useI18n } from '@/hooks/web/useI18n'
  77. import { useMessage } from '@/hooks/web/useMessage'
  78. import { useXTable } from '@/hooks/web/useXTable'
  79. import { FormExpose } from '@/components/Form'
  80. // 业务相关的 import
  81. import * as NoticeApi from '@/api/system/notice'
  82. import { rules, allSchemas } from './notice.data'
  83. import { Editor } from '@/components/Editor'
  84. const { t } = useI18n() // 国际化
  85. const message = useMessage() // 消息弹窗
  86. // 列表相关的变量
  87. const [registerTable, { reload, deleteData }] = useXTable({
  88. allSchemas: allSchemas,
  89. getListApi: NoticeApi.getNoticePageApi,
  90. deleteApi: NoticeApi.deleteNoticeApi
  91. })
  92. // 弹窗相关的变量
  93. const dialogVisible = ref(false) // 是否显示弹出层
  94. const dialogTitle = ref('edit') // 弹出层标题
  95. const actionType = ref('') // 操作按钮的类型
  96. const actionLoading = ref(false) // 按钮 Loading
  97. const formRef = ref<FormExpose>() // 表单 Ref
  98. const detailData = ref() // 详情 Ref
  99. // 设置标题
  100. const setDialogTile = (type: string) => {
  101. dialogTitle.value = t('action.' + type)
  102. actionType.value = type
  103. dialogVisible.value = true
  104. }
  105. // 新增操作
  106. const handleCreate = () => {
  107. setDialogTile('create')
  108. }
  109. // 修改操作
  110. const handleUpdate = async (rowId: number) => {
  111. setDialogTile('update')
  112. // 设置数据
  113. const res = await NoticeApi.getNoticeApi(rowId)
  114. unref(formRef)?.setValues(res)
  115. }
  116. // 详情操作
  117. const handleDetail = async (rowId: number) => {
  118. setDialogTile('detail')
  119. // 设置数据
  120. const res = await NoticeApi.getNoticeApi(rowId)
  121. detailData.value = res
  122. }
  123. // 删除操作
  124. const handleDelete = async (rowId: number) => {
  125. await deleteData(rowId)
  126. }
  127. // 提交新增/修改的表单
  128. const submitForm = async () => {
  129. const elForm = unref(formRef)?.getElFormRef()
  130. if (!elForm) return
  131. elForm.validate(async (valid) => {
  132. if (valid) {
  133. actionLoading.value = true
  134. // 提交请求
  135. try {
  136. const data = unref(formRef)?.formModel as NoticeApi.NoticeVO
  137. if (actionType.value === 'create') {
  138. await NoticeApi.createNoticeApi(data)
  139. message.success(t('common.createSuccess'))
  140. } else {
  141. await NoticeApi.updateNoticeApi(data)
  142. message.success(t('common.updateSuccess'))
  143. }
  144. dialogVisible.value = false
  145. } finally {
  146. actionLoading.value = false
  147. await reload()
  148. }
  149. }
  150. })
  151. }
  152. </script>