add.vue 8.0 KB

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