add.vue 7.5 KB

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