edit.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <template>
  2. <div class="app-container">
  3. <el-row style="padding:0 0 20px 0px;">
  4. <el-col :span="12"><div class="grid-content bg-purple"><div style="'line-height:35px;'">编辑文章</div></div></el-col>
  5. <el-col style="text-align:right;" :span="12">
  6. <div class="grid-content bg-purple-light">
  7. <el-button @click="edit" type="success" plain>保存</el-button>
  8. <!-- <el-button type="info" plain>复制</el-button> -->
  9. <!-- <el-button type="primary" plain>预览</el-button> -->
  10. </div>
  11. </el-col>
  12. </el-row>
  13. <el-row>
  14. <el-col :span="4">
  15. <el-upload
  16. ref="mYupload"
  17. class="avatar-uploader"
  18. :headers="headers"
  19. :action="uploadPath"
  20. :show-file-list="false"
  21. :on-success="uploadSuccessHandle"
  22. :before-upload="onBeforeUpload">
  23. <img v-if="form.coverImage" :src="form.coverImage" class="avatar">
  24. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  25. </el-upload>
  26. </el-col>
  27. <el-col :span="20">
  28. <el-select style="width:100%;margin-bottom:10px;" v-model="form.classification" placeholder="请选择分类">
  29. <el-option label="未分类" value="未分类"></el-option>
  30. <el-option
  31. v-for="item in options"
  32. :key="item.id"
  33. :label="item.classifyName"
  34. :value="item.classifyName">
  35. </el-option>
  36. </el-select>
  37. <el-input v-model="form.articleTitle" placeholder="请输入标题"></el-input>
  38. <el-select style="width:100%;margin:10px 0;" v-model="form.status" placeholder="请选择状态" clearable size="small">
  39. <el-option label="待发布" value="待发布" />
  40. <el-option label="已上线" value="已上线" />
  41. </el-select>
  42. <div style="margin-top:10px;">
  43. <el-input
  44. type="textarea"
  45. :rows="3"
  46. maxlength="500"
  47. placeholder="请输入文章简介(选填,最多支持500个字符)"
  48. v-model="form.articleIntroduction">
  49. </el-input>
  50. </div>
  51. </el-col>
  52. </el-row>
  53. <div class="rich-container">
  54. <textarea id="ck-editor" name="editor" rows="10" cols="80"></textarea>
  55. </div>
  56. <div class="searchTitle flex">
  57. <div>搜索关键词</div>
  58. <el-radio-group v-model="form.language">
  59. <el-radio-button label="中文"></el-radio-button>
  60. <el-radio-button label="English"></el-radio-button>
  61. </el-radio-group>
  62. </div>
  63. <el-input @keyup.enter.native="keyup" v-model="form.searchKeywords" placeholder="关键词将使用该网站的搜索结果更加准确,每篇文章最多填写5个,每个关键词之间请按回车键分隔"></el-input>
  64. </div>
  65. </template>
  66. <script>
  67. import { listDict, updateManagement, getManagement } from "@/api/cloud/articleManagement";
  68. import { uploadPath } from '@/api/storage'
  69. import { getToken } from '@/utils/auth'
  70. export default {
  71. data() {
  72. return {
  73. uploadPath,
  74. editor: null,
  75. // 表单参数
  76. form: {classification:'未分类',language:'中文'},
  77. input:'',
  78. // 表单校验
  79. rules: {
  80. gmtCreate: [
  81. { required: true, message: "不能为空", trigger: "blur" }
  82. ], gmtUpdate: [
  83. { required: true, message: "不能为空", trigger: "blur" }
  84. ], deleteFlag: [
  85. { required: true, message: "删除标识不能为空", trigger: "blur" }
  86. ]
  87. },
  88. options:[],
  89. editor: null,
  90. editorData: '<p>Content of the editor.</p>',
  91. editorConfig: {
  92. // The configuration of the editor.
  93. }
  94. }
  95. },
  96. computed: {
  97. headers() {
  98. return {
  99. accessToken: getToken()
  100. }
  101. }
  102. },
  103. created() {
  104. // this.getList();
  105. },
  106. mounted() {
  107. var that = this
  108. window.CKEDITOR.replace('ck-editor', {height: '400px', width: '100%', toolbar: 'toolbar_Full'});
  109. this.editor = window.CKEDITOR.instances['ck-editor']
  110. this.editor.on('fileUploadRequest', evt => {
  111. const requestData = evt.data.requestData
  112. const upload = requestData.upload
  113. delete requestData.upload
  114. requestData.file = upload
  115. })
  116. this.editor.on('fileUploadResponse', evt => {
  117. evt.stop();
  118. const data = evt.data
  119. const fileLoader = data.fileLoader
  120. const res = JSON.parse(fileLoader.xhr.responseText)
  121. console.log(res)
  122. if (res.errno !== 200) {
  123. data.message = '上传失败'
  124. evt.cancel();
  125. return
  126. }
  127. data.fileName = fileLoader.fileName
  128. data.url = res.url
  129. data.message = '上传成功'
  130. })
  131. listDict({ classifyType:'article_type',}).then(response => {
  132. this.options=response.data.data.items
  133. });
  134. getManagement(this.$route.query.Id).then(response => {
  135. this.form = response.data.data
  136. setTimeout(()=>{that.InsertHTML(that.form.content)},1000)
  137. });
  138. // this.initEditor()
  139. },
  140. methods: {
  141. InsertHTML(html) {
  142. // Get the editor instance that you want to interact with.
  143. // var value = document.getElementById('ck-editor').value;
  144. console.log(this.editor)
  145. // Check the active editing mode.
  146. if ( this.editor.mode == 'wysiwyg') {
  147. // Insert HTML code.
  148. // https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_editor.html#method-insertHtml
  149. this.editor.setData(html);
  150. } else
  151. alert('You must be in WYSIWYG mode!');
  152. },
  153. getContent() {
  154. console.log(this.editor.getData())
  155. },
  156. keyup(e){
  157. var content= this.form.searchKeywords.split(',')
  158. if(content.length>5){
  159. this.$message.error('关键词最多5个!')
  160. }else{
  161. if(this.form.searchKeywords[this.form.searchKeywords.length-1]==','){
  162. this.$message.error('请输入关键词内容!')
  163. }else{
  164. this.form.searchKeywords=this.form.searchKeywords+','
  165. }
  166. }
  167. },
  168. // 上传图片了处理图片
  169. uploadSuccessHandle(e, file) {
  170. const that = this
  171. this.form.coverImage = e.url
  172. console.log(this.form,this.editor)
  173. },
  174. onBeforeUpload(file) {
  175. const isIMAGE = file.type === 'image/jpeg' || 'image/gif' || 'image/png' || 'image/jpg'
  176. const isLt1M = file.size / 1024 / 1024 < 1
  177. if (!isIMAGE) {
  178. this.$message.error('上传文件只能是图片格式!')
  179. }
  180. if (!isLt1M) {
  181. this.$message.error('上传文件大小不能超过 1MB!')
  182. }
  183. return isIMAGE && isLt1M
  184. },
  185. edit(){
  186. if(this.form.searchKeywords){
  187. if(this.form.searchKeywords.split(',').length>5){
  188. this.$message.error('关键词最多5个!')
  189. return
  190. }
  191. }
  192. this.form.content=this.editor.getData()
  193. updateManagement(this.form).then(response => {
  194. if (response.data) {
  195. this.msgSuccess("修改成功");
  196. this.$refs['mYupload'].clearFiles();
  197. this.form={classification:'未分类'}
  198. this.$router.go(-1)
  199. } else {
  200. this.msgError(response.msg);
  201. }
  202. });
  203. }
  204. }
  205. };
  206. </script>
  207. <style lang="scss" scoped>
  208. .avatar-uploader{
  209. /deep/.el-upload {
  210. border: 1px dashed #ccc;
  211. border-radius: 6px;
  212. cursor: pointer;
  213. position: relative;
  214. overflow: hidden;
  215. }
  216. }
  217. .avatar-uploader .el-upload:hover {
  218. border-color: #409EFF;
  219. }
  220. .avatar-uploader-icon {
  221. font-size: 28px;
  222. color: #8c939d;
  223. width: 183px;
  224. height: 183px;
  225. line-height: 183px;
  226. text-align: center;
  227. }
  228. .avatar {
  229. width: 183px;
  230. height: 183px;
  231. display: block;
  232. }
  233. .searchTitle{
  234. margin:20px 0;
  235. }
  236. /*编辑框最低高度*/
  237. .ck-editor__editable{
  238. min-height: 400px;
  239. }
  240. .rich-container{
  241. margin-top:20px;
  242. }
  243. </style>