detail.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <div>
  3. <el-form label-width="150px">
  4. <!-- <div class="container">
  5. <h3 class="margin_b_20">主图</h3>
  6. <el-form-item><uni-file-picker v-model="imgval" fileMediatype="image" returnType="array"
  7. :image-styles="imageStyle" @select="imgDelete" /></el-form-item>
  8. </div> -->
  9. <div class=" container">
  10. <h3 class="margin_b_20">商品详情</h3>
  11. <el-form-item>
  12. <div id="wangeditor" class="editor">
  13. <div ref="editorElem"></div>
  14. </div>
  15. </el-form-item>
  16. </div>
  17. </el-form>
  18. </div>
  19. </template>
  20. <script>
  21. import E from 'wangeditor';
  22. export default {
  23. data() {
  24. return {
  25. imageValue: [],
  26. imgval:null,
  27. imageStyle: {
  28. height: '150px',
  29. width: '150px'
  30. },
  31. editor: null,
  32. editorContent: ''
  33. };
  34. },
  35. props: ['catchData'],
  36. methods: {
  37. submitData(callback) {
  38. // if (this.imageValue.length < 1) {
  39. // this.$message({
  40. // message: '请选择主图',
  41. // type: 'warning'
  42. // });
  43. // this.$emit('detail');
  44. // return;
  45. // }
  46. if (!this.editorContent) {
  47. this.$message({
  48. message: '请输入商品详情',
  49. type: 'warning'
  50. });
  51. this.$emit('detail');
  52. return;
  53. }
  54. if (typeof callback === 'function') {
  55. callback({
  56. content: this.editorContent,
  57. imgs: this.imageValue
  58. });
  59. }
  60. },
  61. getData(callback) {
  62. this.submitData(callback);
  63. },
  64. setData(data) {
  65. this.imageValue = data.imgs;
  66. if (data.desc) {
  67. this.editor.txt.html(data.desc);
  68. this.editorContent = data.desc;
  69. }
  70. },
  71. imgDelete(e) {
  72. var img = e.tempFiles[0].file;
  73. const file = img;
  74. const formData = new FormData();
  75. formData.append('file', file);
  76. // 文件上传
  77. this.$axios.post("/file/open/",formData,
  78. {
  79. headers:{
  80. 'Content-type' : 'multipart/form-data'
  81. }
  82. }).then(response => {
  83. let res = response
  84. if (res.success) {
  85. img =res.data
  86. this.imgval=img
  87. this.imageValue.push(img)
  88. } else {
  89. uni.showToast({
  90. icon:'error',
  91. title: '请重新上传!'
  92. });
  93. img =''
  94. this.imageValue = null;
  95. }
  96. }).catch(res =>{
  97. uni.showToast({
  98. icon:'error',
  99. title: '请重新上传!'
  100. });
  101. img =''
  102. this.imageValue = null;
  103. });
  104. this.imageValue.splice(this.imageValue, 1);
  105. // this.imageValue.splice(this.imageValue.findIndex(x => x.url == e.url), 1);
  106. }
  107. },
  108. mounted() {
  109. let $this = this;
  110. this.editor = new E(this.$refs.editorElem);
  111. // 编辑器的事件,每次改变会获取其html内容
  112. this.editor.customConfig.onchange = html => {
  113. this.editorContent = html;
  114. };
  115. // 将图片大小限制为 2M
  116. this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024;
  117. // 限制一次最多上传 10张图片
  118. this.editor.customConfig.uploadImgMaxLength = 10;
  119. this.editor.customConfig.customAlert = function(info) {
  120. // info 是需要提示的内容
  121. alert('自定义提示:' + info);
  122. };
  123. // 插入网络图片的校验
  124. this.editor.customConfig.linkImgCheck = function (src) {
  125. console.log(src) // 图片的链接
  126. return true // 返回 true 表示校验成功
  127. // return ‘验证失败’ // 返回字符串,即校验失败的提示信息
  128. };
  129. this.editor.customConfig.customUploadImg = function(files, insert) {
  130. // files 是 input 中选中的文件列表
  131. // insert 是获取图片 url 后,插入到编辑器的方法
  132. for (var i = 0; i < files.length; i++) {
  133. const formData = new FormData();
  134. formData.append('file', files[i]);
  135. // 文件上传
  136. $this.$axios.post("/file/open/",formData,
  137. {
  138. headers:{
  139. 'Content-type' : 'multipart/form-data'
  140. }
  141. }).then(response => {
  142. let res = response
  143. if (res.success) {
  144. insert(res.data);
  145. } else {
  146. uni.showToast({
  147. icon:'error',
  148. title: '请重新上传!'
  149. });
  150. img =''
  151. }
  152. }).catch(res =>{
  153. uni.showToast({
  154. icon:'error',
  155. title: '请重新上传!'
  156. });
  157. img =''
  158. });
  159. }
  160. };
  161. this.editor.customConfig.menus = [
  162. // 菜单配置
  163. 'head', // 标题
  164. 'bold', // 粗体
  165. 'fontSize', // 字号
  166. 'fontName', // 字体
  167. 'italic', // 斜体
  168. 'underline', // 下划线
  169. 'strikeThrough', // 删除线
  170. 'foreColor', // 文字颜色
  171. 'backColor', // 背景颜色
  172. 'link', // 插入链接
  173. 'list', // 列表
  174. 'justify', // 对齐方式
  175. 'quote', // 引用
  176. 'emoticon', // 表情
  177. 'image', // 插入图片
  178. 'table', // 表格
  179. 'code', // 插入代码
  180. 'undo', // 撤销
  181. 'redo' // 重复
  182. ];
  183. this.editor.create(); // 创建富文本实例
  184. }
  185. };
  186. </script>
  187. <style></style>