index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <view class="yd-page-container">
  3. <!-- 顶部导航栏 -->
  4. <wd-navbar
  5. :title="getTitle"
  6. left-arrow placeholder safe-area-inset-top fixed
  7. @click-left="handleBack"
  8. />
  9. <!-- 表单区域 -->
  10. <view>
  11. <wd-form ref="formRef" :model="formData" :rules="formRules">
  12. <wd-cell-group border>
  13. <wd-input
  14. v-model="formData.title"
  15. label="公告标题"
  16. label-width="180rpx"
  17. prop="title"
  18. clearable
  19. placeholder="请输入公告标题"
  20. />
  21. <wd-input
  22. v-model="formData.content"
  23. label="公告内容"
  24. label-width="180rpx"
  25. prop="content"
  26. clearable
  27. placeholder="请输入公告内容"
  28. />
  29. <wd-cell title="公告类型" title-width="180rpx" prop="type" center>
  30. <wd-radio-group v-model="formData.type" shape="button">
  31. <wd-radio
  32. v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_NOTICE_TYPE)"
  33. :key="dict.value"
  34. :value="dict.value"
  35. >
  36. {{ dict.label }}
  37. </wd-radio>
  38. </wd-radio-group>
  39. </wd-cell>
  40. <wd-cell title="公告状态" title-width="180rpx" prop="status" center>
  41. <wd-radio-group v-model="formData.status" shape="button">
  42. <wd-radio
  43. v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
  44. :key="dict.value"
  45. :value="dict.value"
  46. >
  47. {{ dict.label }}
  48. </wd-radio>
  49. </wd-radio-group>
  50. </wd-cell>
  51. </wd-cell-group>
  52. </wd-form>
  53. </view>
  54. <!-- 底部保存按钮 -->
  55. <view class="fixed bottom-0 left-0 right-0 bg-white p-24rpx">
  56. <wd-button
  57. type="primary"
  58. block
  59. :loading="formLoading"
  60. @click="handleSubmit"
  61. >
  62. 保存
  63. </wd-button>
  64. </view>
  65. </view>
  66. </template>
  67. <script lang="ts" setup>
  68. import type { Notice } from '@/api/system/notice'
  69. import { computed, onMounted, ref } from 'vue'
  70. import { useToast } from 'wot-design-uni'
  71. import { createNotice, getNotice, updateNotice } from '@/api/system/notice'
  72. import { getIntDictOptions } from '@/hooks/useDict'
  73. import { navigateBackPlus } from '@/utils'
  74. import { DICT_TYPE } from '@/utils/constants'
  75. const props = defineProps<{
  76. id?: number | any
  77. }>()
  78. definePage({
  79. style: {
  80. navigationBarTitleText: '',
  81. navigationStyle: 'custom',
  82. },
  83. })
  84. const toast = useToast()
  85. const getTitle = computed(() => props.id ? '编辑通知公告' : '新增通知公告')
  86. const formLoading = ref(false)
  87. const formData = ref<Notice>({
  88. id: undefined,
  89. title: '',
  90. content: '',
  91. type: 0,
  92. status: 0,
  93. })
  94. const formRules = {
  95. title: [{ required: true, message: '公告标题不能为空' }],
  96. content: [{ required: true, message: '公告内容不能为空' }],
  97. type: [{ required: true, message: '公告类型不能为空' }],
  98. status: [{ required: true, message: '公告状态不能为空' }],
  99. }
  100. const formRef = ref()
  101. /** 返回上一页 */
  102. function handleBack() {
  103. navigateBackPlus('/pages-system/notice/index')
  104. }
  105. /** 加载通知公告详情 */
  106. async function getDetail() {
  107. if (!props.id) {
  108. return
  109. }
  110. formData.value = await getNotice(props.id)
  111. }
  112. /** 提交表单 */
  113. async function handleSubmit() {
  114. const { valid } = await formRef.value.validate()
  115. if (!valid) {
  116. return
  117. }
  118. formLoading.value = true
  119. try {
  120. if (props.id) {
  121. await updateNotice(formData.value)
  122. toast.success('修改成功')
  123. } else {
  124. await createNotice(formData.value)
  125. toast.success('新增成功')
  126. }
  127. setTimeout(() => {
  128. handleBack()
  129. }, 500)
  130. } finally {
  131. formLoading.value = false
  132. }
  133. }
  134. /** 初始化 */
  135. onMounted(() => {
  136. getDetail()
  137. })
  138. </script>
  139. <style lang="scss" scoped>
  140. </style>