1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<template>
<div class="goods-img">
<div id="wangeditor" ref="editorElem"></div>
</div>
</template>
<script>
import E from "wangeditor";
import { batchUploadImage } from "@/api/module/goods";
export default {
props: {
description: {
type: String,
required: true,
},
},
data() {
return {
allImg: [],
editor: null,
};
},
mounted() {
let that = this;
// 富文本编辑器
const editor = new E("#wangeditor");
editor.config.height = 500;
editor.config.uploadImgShowBase64 = true;
editor.config.showLinkImg = false;
// editor.config.uploadImgMaxLength = 1
editor.config.customUploadImg = function (files, insertImgFn) {
// resultFiles 是 input 中选中的文件列表
// insertImgFn 是获取图片 url 后,插入到编辑器的方法
// 上传图片,返回结果,将图片插入到编辑器中
that.getAllImg(files).then((res) => {
let data = { data: [] };
let imgBase = "";
if (res.length > 0) {
for (let i = 0; i < res.length; i++) {
imgBase = "";
imgBase = res[i].split(",")[1];
if (imgBase) {
data.data.push({ img_data: imgBase });
}
}
batchUploadImage(data).then((res) => {
if (res.code == 1) {
if (
res.code == 1 &&
Array.isArray(res.data) &&
res.data.length > 0
) {
for (let i = 0; i < res.data.length; i++) {
if (res.data[i].image_url) {
insertImgFn(res.data[i].image_url);
}
}
}
}else {
this.$message({type: 'error',message: res.message ? res.message : '上传图片失败'});
}
});
} // res.length > 0 end
});
// });
};
editor.create();
this.editor = editor;
if (this.description) {
this.editor.txt.html(this.description);
}
},
methods: {
initInfo() {
this.editor = null;
},
getBase64(file) {
return new Promise(function (resolve, reject) {
let reader = new FileReader();
let imgResult = "";
reader.readAsDataURL(file);
reader.onload = function () {
imgResult = reader.result;
};
reader.onerror = function (error) {
reject(error);
};
reader.onloadend = function () {
resolve(imgResult);
};
});
},
async getAllImg(files) {
let imgObj = { img_data: "" };
let res = [];
let orgRes = "";
files.forEach((item) => {
res.push(
(async (item) => {
return await this.getBase64(item);
})(item)
);
});
return await Promise.all(res);
},
},
};
</script>
<style scoped>
.goods-img {
padding: 20px;
}
/deep/ .w-e-text-container {
z-index: 1000 !important;
}
/deep/ .w-e-toolbar {
z-index: 1001 !important;
}
</style>