edit.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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-select style="width:100%;margin-bottom:10px;" v-model="form.status" placeholder="请选择状态" clearable size="small">
  38. <el-option label="待发布" value="待发布" />
  39. <el-option label="上架" value="上架" />
  40. </el-select>
  41. <el-input v-model="form.productTitle" placeholder="请输入标题"></el-input>
  42. <div style="margin-top:10px;">
  43. <el-input
  44. type="textarea"
  45. :rows="4"
  46. maxlength="500"
  47. placeholder="请输入简介(选填,最多支持500个字符)"
  48. v-model="form.productIntroduction">
  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. <el-collapse v-model="activeNames" >
  57. <el-collapse-item title="搜索引擎优化" name="1">
  58. <div class="searchTitle flex">
  59. <div>搜索关键词</div>
  60. <el-radio-group v-model="form.language">
  61. <el-radio-button label="中文"></el-radio-button>
  62. <el-radio-button label="English"></el-radio-button>
  63. </el-radio-group>
  64. </div>
  65. <el-input @keyup.enter.native="keyup" v-model="form.searchKeywords" placeholder="每个关键词之间请按回车键分隔"></el-input>
  66. <div class="searchTitle">产品描述</div>
  67. <el-input
  68. type="textarea"
  69. :rows="4"
  70. maxlength="500"
  71. placeholder="请输入产品描述"
  72. v-model="form.productDescribe">
  73. </el-input>
  74. </el-collapse-item>
  75. </el-collapse>
  76. </div>
  77. </template>
  78. <script>
  79. import { listDict } from "@/api/cloud/articleManagement";
  80. import { updateProductManagement,getProductManagement } from "@/api/cloud/productManagement";
  81. import { uploadPath } from '@/api/storage'
  82. import { getToken } from '@/utils/auth'
  83. export default {
  84. data() {
  85. return {
  86. uploadPath,
  87. editor: null,
  88. activeNames: ['1'],
  89. // 表单参数
  90. form: {classification:'未分类'},
  91. input:'',
  92. // 表单校验
  93. rules: {
  94. gmtCreate: [
  95. { required: true, message: "不能为空", trigger: "blur" }
  96. ], gmtUpdate: [
  97. { required: true, message: "不能为空", trigger: "blur" }
  98. ], deleteFlag: [
  99. { required: true, message: "删除标识不能为空", trigger: "blur" }
  100. ]
  101. },
  102. options:[],
  103. editor: null,
  104. editorData: '<p>Content of the editor.</p>',
  105. editorConfig: {
  106. // The configuration of the editor.
  107. }
  108. }
  109. },
  110. computed: {
  111. headers() {
  112. return {
  113. accessToken: getToken()
  114. }
  115. }
  116. },
  117. created() {
  118. // this.getList();
  119. },
  120. mounted() {
  121. window.CKEDITOR.replace('ck-editor', {height: '400px', width: '100%', toolbar: 'toolbar_Full'});
  122. this.editor = window.CKEDITOR.instances['ck-editor']
  123. let that = this;
  124. this.editor.on('fileUploadRequest', evt => {
  125. const requestData = evt.data.requestData
  126. const upload = requestData.upload
  127. delete requestData.upload
  128. requestData.file = upload
  129. })
  130. this.editor.on('fileUploadResponse', evt => {
  131. evt.stop();
  132. const data = evt.data
  133. const fileLoader = data.fileLoader
  134. const res = JSON.parse(fileLoader.xhr.responseText)
  135. console.log(res)
  136. if (res.errno !== 200) {
  137. data.message = '上传失败'
  138. evt.cancel();
  139. return
  140. }
  141. data.fileName = fileLoader.fileName
  142. data.url = res.url
  143. data.message = '上传成功'
  144. })
  145. listDict({classifyType:'product_type',}).then(response => {
  146. this.options=response.data.data.items
  147. });
  148. getProductManagement(this.$route.query.Id).then(response => {
  149. this.form = response.data.data
  150. this.InsertHTML(this.form.content)
  151. });
  152. // this.initEditor()
  153. },
  154. methods: {
  155. InsertHTML(html) {
  156. // Get the editor instance that you want to interact with.
  157. // var value = document.getElementById('ck-editor').value;
  158. console.log(this.editor.getData())
  159. // Check the active editing mode.
  160. if ( this.editor.mode == 'wysiwyg') {
  161. // Insert HTML code.
  162. // https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_editor.html#method-insertHtml
  163. this.editor.setData(html);
  164. } else
  165. alert('You must be in WYSIWYG mode!');
  166. },
  167. getContent() {
  168. console.log(this.editor.getData())
  169. },
  170. keyup(e){
  171. var content= this.form.searchKeywords.split(',')
  172. if(content.length>5){
  173. this.$message.error('关键词最多5个!')
  174. }else{
  175. if(this.form.searchKeywords[this.form.searchKeywords.length-1]==','){
  176. this.$message.error('请输入关键词内容!')
  177. }else{
  178. this.form.searchKeywords=this.form.searchKeywords+','
  179. }
  180. }
  181. },
  182. // 上传图片了处理图片
  183. uploadSuccessHandle(e, file) {
  184. const that = this
  185. this.form.coverImage = e.url
  186. this.$forceUpdate()
  187. console.log(this.form,this.editor)
  188. },
  189. onBeforeUpload(file) {
  190. const isIMAGE = file.type === 'image/jpeg' || 'image/gif' || 'image/png' || 'image/jpg'
  191. const isLt1M = file.size / 1024 / 1024 < 1
  192. if (!isIMAGE) {
  193. this.$message.error('上传文件只能是图片格式!')
  194. }
  195. if (!isLt1M) {
  196. this.$message.error('上传文件大小不能超过 1MB!')
  197. }
  198. return isIMAGE && isLt1M
  199. },
  200. edit(){
  201. if(this.form.searchKeywords){
  202. if(this.form.searchKeywords.split(',').length>5){
  203. this.$message.error('关键词最多5个!')
  204. return
  205. }
  206. }
  207. this.form.content=this.editor.getData()
  208. updateProductManagement(this.form).then(response => {
  209. if (response.data) {
  210. this.msgSuccess("编辑成功");
  211. this.$refs['mYupload'].clearFiles();
  212. this.form={classification:'未分类'}
  213. this.$router.go(-1)
  214. } else {
  215. this.msgError(response.msg);
  216. }
  217. });
  218. }
  219. }
  220. };
  221. </script>
  222. <style lang="scss" scoped>
  223. .avatar-uploader{
  224. /deep/.el-upload {
  225. border: 1px dashed #ccc;
  226. border-radius: 6px;
  227. cursor: pointer;
  228. position: relative;
  229. overflow: hidden;
  230. }
  231. }
  232. .avatar-uploader .el-upload:hover {
  233. border-color: #409EFF;
  234. }
  235. .avatar-uploader-icon {
  236. font-size: 28px;
  237. color: #8c939d;
  238. width: 183px;
  239. height: 183px;
  240. line-height: 183px;
  241. text-align: center;
  242. }
  243. .avatar {
  244. width: 183px;
  245. height: 183px;
  246. display: block;
  247. }
  248. .searchTitle{
  249. margin:20px 0;
  250. }
  251. /*编辑框最低高度*/
  252. .ck-editor__editable{
  253. min-height: 400px;
  254. }
  255. .rich-container{
  256. margin-top:20px;
  257. }
  258. </style>