article-edit.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <div v-loading="isLoading" class="page-form-container">
  3. <el-form ref="formRef" label-width="120px" :model="formData" :rules="formRules">
  4. <el-form-item label="文章类型:" prop="articleType">
  5. <el-radio-group v-model="formData.articleType">
  6. <el-radio :label="1">自主</el-radio>
  7. <el-radio :label="2">第三方</el-radio>
  8. </el-radio-group>
  9. </el-form-item>
  10. <el-form-item label="标题:" prop="articleTitle">
  11. <el-input v-model="formData.articleTitle" placeholder="请输入文章标题" maxlength="50" show-word-limit />
  12. </el-form-item>
  13. <el-form-item label="头图:" prop="articleImage">
  14. <upload-image
  15. tip-title="128px * 128px"
  16. :image-list="imageList"
  17. @success="handleUploadImageSussces"
  18. @remove="handleRemoveImage"
  19. />
  20. <el-input v-show="false" v-model="formData.articleImage" />
  21. </el-form-item>
  22. <template v-if="formData.articleType === 1">
  23. <el-form-item label="文章内容:" prop="articleContent">
  24. <tinymce v-model="formData.articleContent" :token="token" :action="action" :height="300" />
  25. <el-input v-show="false" v-model="formData.articleContent" />
  26. </el-form-item>
  27. </template>
  28. <template v-else>
  29. <el-form-item label="文章链接:" prop="articleLink">
  30. <el-input v-model="formData.articleLink" placeholder="请输入链接" />
  31. </el-form-item>
  32. </template>
  33. </el-form>
  34. <div class="control-box center">
  35. <el-button type="primary" @click="submit">保存</el-button>
  36. <el-button type="warning" @click="navigateBack()">返回</el-button>
  37. </div>
  38. </div>
  39. </template>
  40. <script>
  41. import { mapGetters } from 'vuex'
  42. import UploadImage from '@/components/UploadImage'
  43. import Tinymce from '@/components/Tinymce'
  44. import { getToken } from '@/utils/auth'
  45. import { saveArticle, getArticleDeatil } from '@/api/doc'
  46. export default {
  47. components: { UploadImage, Tinymce },
  48. data() {
  49. const fileNameValidate = (rule, value, callback) => {
  50. if (/[\\\/\[\]\|^%&',;=?$\x22]+/g.test(value)) {
  51. callback(new Error("文章标题不能包含|\/\\^%&',;=?$等特殊字符"))
  52. } else {
  53. callback()
  54. }
  55. }
  56. return {
  57. editType: 0, // 编辑模式 1:添加文章 2:修改文章
  58. formData: {
  59. articleId: '', // 文章id
  60. articleType: 1, // 文章类型
  61. articleTitle: '', // 文章标题
  62. articleImage: '', // 文章封面
  63. articleContent: '', // 文章内容
  64. articleLink: '',
  65. parentId: ''
  66. },
  67. // 表单验证规则
  68. formRules: {
  69. articleTitle: [
  70. { required: true, message: '文章标题不能为空', trigger: 'blur' },
  71. { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' },
  72. { validator: fileNameValidate, trigger: 'blur' }
  73. ],
  74. articleImage: [{ required: true, message: '文章必须上传封面', trigger: 'change' }],
  75. articleContent: [{ required: true, message: '文章内容不能为空', trigger: 'change' }],
  76. articleLink: [{ required: true, message: '文章链接不能为空', trigger: 'blur' }]
  77. },
  78. // 表单是否处于加载状态
  79. isLoading: true,
  80. // 当前用户登录的token
  81. token: '',
  82. // 富文本框上传图片的接口
  83. action: process.env.VUE_APP_UPLOAD_API + '/upload/image',
  84. // 封面图片列表
  85. imageList: []
  86. }
  87. },
  88. computed: {
  89. ...mapGetters(['authUserId'])
  90. },
  91. created() {
  92. this.token = getToken()
  93. this.formData.articleId = this.$route.query.articleId || ''
  94. this.formData.parentId = this.$route.query.parentId || 0
  95. this.editType = this.formData.articleId ? 2 : 1
  96. this.initFormData()
  97. },
  98. mounted() {
  99. setTimeout(() => {
  100. this.isLoading = false
  101. }, 500)
  102. },
  103. methods: {
  104. // 保存文章
  105. submit() {
  106. this.formData.authUserId = this.authUserId
  107. this.$refs.formRef.validate((valid) => {
  108. if (!valid) return
  109. this.isLoading = true
  110. saveArticle(this.formData)
  111. .then((res) => {
  112. console.log(res)
  113. if (res.code !== 0) return
  114. this.$message.success(res.data)
  115. // 如果保存文章成功就要关闭当前页面
  116. this.$store.dispatch('tagsView/delView', this.$route)
  117. this.$router.back()
  118. })
  119. .catch((error) => {
  120. console.log(error)
  121. })
  122. .finally(() => {
  123. this.isLoading = false
  124. })
  125. })
  126. },
  127. // 初始化表单回显数据
  128. initFormData() {
  129. if (!this.formData.articleId) return
  130. getArticleDeatil({ articleId: this.formData.articleId })
  131. .then((res) => {
  132. for (const key in this.formData) {
  133. if (Object.hasOwnProperty.call(this.formData, key)) {
  134. if (res.data[key]) {
  135. this.formData[key] = res.data[key]
  136. }
  137. }
  138. }
  139. this.imageList = [{ name: '文章封面', url: this.formData.articleImage }]
  140. console.log(res)
  141. })
  142. .catch((error) => {
  143. console.log(error)
  144. })
  145. },
  146. // 封面图片上传成功
  147. handleUploadImageSussces({ response, file, fileList }) {
  148. this.imageList = fileList
  149. this.formData.articleImage = response.data
  150. },
  151. handleRemoveImage({ response, file, fileList }) {
  152. this.imageList = fileList
  153. this.formData.articleImage = ''
  154. }
  155. }
  156. }
  157. </script>
  158. <style lang="scss" scoped>
  159. .page-form-container {
  160. width: 900px;
  161. }
  162. </style>