add.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <div class="newsSettingBox">
  3. <el-form ref="ruleFormRef" :model="formData" :rules="rules" label-width="120px">
  4. <el-form-item label="标题" prop="title">
  5. <el-input v-model="formData.title" placeholder="请输入标题" />
  6. </el-form-item>
  7. <el-form-item label="附件">
  8. <div class="uploadFileBox">
  9. <span>
  10. <el-button type="primary" @click="uploadHandle">上传</el-button>
  11. <input type="file" style="display: none" ref="fileRef" @change="fileChangeHandle" />
  12. </span>
  13. <ul class="prviewFile">
  14. <li v-for="(item, index) in fileUrls" :key="index">
  15. <span>
  16. <a href="javascript:void(0);" @click="downloadFile(item['url'])">{{
  17. item['name']
  18. }}</a>
  19. </span>
  20. <el-button
  21. type="text"
  22. @click="
  23. deleteFile(item['id'], () => {
  24. ElMessage.success('文件删除成功!')
  25. })
  26. "
  27. >删除</el-button
  28. >
  29. </li>
  30. </ul>
  31. </div>
  32. </el-form-item>
  33. <el-form-item label="内容">
  34. <Tinymce v-model="formData.content" width="100%" height="800px" />
  35. </el-form-item>
  36. </el-form>
  37. <div class="btnGroup" style="text-align: right">
  38. <span>
  39. <el-button type="primary" @click="onSubmit(ruleFormRef)">发布</el-button>
  40. </span>
  41. <el-button @click="router.back()">取消</el-button>
  42. </div>
  43. </div>
  44. </template>
  45. <script setup lang="ts">
  46. import { useRoute } from 'vue-router'
  47. import { ElMessage, FormRules, FormInstance } from 'element-plus'
  48. import request from '@/config/axios'
  49. import router from '@/router'
  50. import { useFiles } from './mixins'
  51. const { fileUrls, downloadFile, uploadFile, deleteFile } = useFiles(request)
  52. defineOptions({
  53. name: 'NoticeAdd'
  54. })
  55. const route: any = useRoute()
  56. const routeName: string = route.name ?? ''
  57. const initFormData: any = {
  58. id: null,
  59. title: '',
  60. type: routeName.startsWith('Notice') || routeName.startsWith('notice') ? 1 : 2,
  61. studyType: null,
  62. files: '',
  63. userId: '',
  64. nickname: '',
  65. content: ''
  66. }
  67. const formData = ref(initFormData)
  68. const ruleFormRef = ref<FormInstance>()
  69. const rules = reactive<
  70. FormRules<{
  71. title: string
  72. content: string
  73. }>
  74. >({
  75. title: {
  76. required: true,
  77. message: '标题不能为空! ',
  78. trigger: 'change'
  79. },
  80. content: {
  81. required: true,
  82. message: '内容不能为空! ',
  83. trigger: 'change'
  84. }
  85. })
  86. function onSubmit(formEl: FormInstance | undefined, num?: number): void {
  87. if (!formEl) return
  88. if (formData.value.content) {
  89. /**
  90. * 解决定义controlsList="nodownload"属性时tinymce组件会剔除
  91. * **/
  92. formData.value.content = formData.value.content.replace(
  93. 'title="nodownload"',
  94. 'controlsList="nodownload"'
  95. )
  96. }
  97. formEl.validate((valid, fields: any) => {
  98. if (valid) {
  99. let urlApi: string = '/adm/noticeAndLearn/add'
  100. const sendData = {
  101. ...formData.value
  102. }
  103. if (fileUrls.value.length > 0) {
  104. sendData.files = fileUrls.value.map((item) => item.id).join(',')
  105. }
  106. request.post({ url: urlApi, data: sendData }).then((result) => {
  107. if (result) {
  108. ElMessage({
  109. showClose: true,
  110. message: '保存成功.',
  111. type: 'success'
  112. })
  113. if (num !== 0) {
  114. const timer = setTimeout(() => {
  115. clearTimeout(timer)
  116. router.back()
  117. })
  118. }
  119. }
  120. })
  121. } else {
  122. const keys = Object.keys(fields)
  123. ElMessage.error(fields[keys[0]][0]['message'])
  124. }
  125. })
  126. }
  127. const fileRef = ref()
  128. function uploadHandle() {
  129. fileRef.value.click()
  130. }
  131. function fileChangeHandle(evt) {
  132. const target = evt.target
  133. uploadFile(target.files[0])
  134. }
  135. </script>
  136. <style lang="scss" scoped>
  137. .newsSettingBox {
  138. margin-top: 20px;
  139. height: calc(100% - 20px);
  140. background-color: #fff;
  141. border-radius: 20px;
  142. padding: 20px;
  143. overflow-y: auto;
  144. .prviewFile {
  145. padding-left: 20px;
  146. > li {
  147. margin-bottom: 10px;
  148. > span {
  149. display: inline-block;
  150. margin-right: 15px;
  151. }
  152. }
  153. }
  154. .switchGroup {
  155. display: flex;
  156. }
  157. .btnGroup {
  158. > span {
  159. margin-right: 20px;
  160. }
  161. }
  162. .uploadFileBox {
  163. position: relative;
  164. flex: 1;
  165. display: flex;
  166. }
  167. .preImg {
  168. margin-bottom: 20px;
  169. margin-left: 120px;
  170. > img {
  171. width: 200px;
  172. }
  173. }
  174. }
  175. </style>