edit.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. listDict({ classifyType:'article_type',}).then(response => {
  111. this.options=response.data.data.items
  112. });
  113. getManagement(this.$route.query.Id).then(response => {
  114. this.form = response.data.data
  115. setTimeout(()=>{that.InsertHTML(that.form.content)},1000)
  116. });
  117. // this.initEditor()
  118. },
  119. methods: {
  120. InsertHTML(html) {
  121. // Get the editor instance that you want to interact with.
  122. // var value = document.getElementById('ck-editor').value;
  123. console.log(this.editor)
  124. // Check the active editing mode.
  125. if ( this.editor.mode == 'wysiwyg') {
  126. // Insert HTML code.
  127. // https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_editor.html#method-insertHtml
  128. this.editor.setData(html);
  129. } else
  130. alert('You must be in WYSIWYG mode!');
  131. },
  132. getContent() {
  133. console.log(this.editor.getData())
  134. },
  135. keyup(e){
  136. var content= this.form.searchKeywords.split(',')
  137. if(content.length>5){
  138. this.$message.error('关键词最多5个!')
  139. }else{
  140. if(this.form.searchKeywords[this.form.searchKeywords.length-1]==','){
  141. this.$message.error('请输入关键词内容!')
  142. }else{
  143. this.form.searchKeywords=this.form.searchKeywords+','
  144. }
  145. }
  146. },
  147. // 上传图片了处理图片
  148. uploadSuccessHandle(e, file) {
  149. const that = this
  150. this.form.coverImage = e.url
  151. console.log(this.form,this.editor)
  152. },
  153. onBeforeUpload(file) {
  154. const isIMAGE = file.type === 'image/jpeg' || 'image/gif' || 'image/png' || 'image/jpg'
  155. const isLt1M = file.size / 1024 / 1024 < 1
  156. if (!isIMAGE) {
  157. this.$message.error('上传文件只能是图片格式!')
  158. }
  159. if (!isLt1M) {
  160. this.$message.error('上传文件大小不能超过 1MB!')
  161. }
  162. return isIMAGE && isLt1M
  163. },
  164. edit(){
  165. if(this.form.searchKeywords){
  166. if(this.form.searchKeywords.split(',').length>5){
  167. this.$message.error('关键词最多5个!')
  168. return
  169. }
  170. }
  171. this.form.content=this.editor.getData()
  172. updateManagement(this.form).then(response => {
  173. if (response.data) {
  174. this.msgSuccess("修改成功");
  175. this.$refs['mYupload'].clearFiles();
  176. this.form={classification:'未分类'}
  177. this.$router.go(-1)
  178. } else {
  179. this.msgError(response.msg);
  180. }
  181. });
  182. }
  183. }
  184. };
  185. </script>
  186. <style lang="scss" scoped>
  187. .avatar-uploader{
  188. /deep/.el-upload {
  189. border: 1px dashed #ccc;
  190. border-radius: 6px;
  191. cursor: pointer;
  192. position: relative;
  193. overflow: hidden;
  194. }
  195. }
  196. .avatar-uploader .el-upload:hover {
  197. border-color: #409EFF;
  198. }
  199. .avatar-uploader-icon {
  200. font-size: 28px;
  201. color: #8c939d;
  202. width: 183px;
  203. height: 183px;
  204. line-height: 183px;
  205. text-align: center;
  206. }
  207. .avatar {
  208. width: 183px;
  209. height: 183px;
  210. display: block;
  211. }
  212. .searchTitle{
  213. margin:20px 0;
  214. }
  215. /*编辑框最低高度*/
  216. .ck-editor__editable{
  217. min-height: 400px;
  218. }
  219. .rich-container{
  220. margin-top:20px;
  221. }
  222. </style>