Commit b6cb6469f1fefb083e3c0134c0217ea14dcd7495

Authored by YunaiV
1 parent 63e632ce

富文本编辑器的 Editor 的图片上传报错的问题

yudao-ui-admin/src/components/Editor/index.vue
1 <template> 1 <template>
2 <div> 2 <div>
3 <el-upload 3 <el-upload
4 - :action="uploadUrl" 4 + :action="uploadFileUrl"
5 :before-upload="handleBeforeUpload" 5 :before-upload="handleBeforeUpload"
6 :on-success="handleUploadSuccess" 6 :on-success="handleUploadSuccess"
7 :on-error="handleUploadError" 7 :on-error="handleUploadError"
@@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
10 :headers="headers" 10 :headers="headers"
11 style="display: none" 11 style="display: none"
12 ref="upload" 12 ref="upload"
13 - v-if="this.type == 'url'" 13 + v-if="this.type === 'url'"
14 > 14 >
15 </el-upload> 15 </el-upload>
16 <div class="editor" ref="editor" :style="styles"></div> 16 <div class="editor" ref="editor" :style="styles"></div>
@@ -60,10 +60,8 @@ export default { @@ -60,10 +60,8 @@ export default {
60 }, 60 },
61 data() { 61 data() {
62 return { 62 return {
63 - uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址  
64 - headers: {  
65 - Authorization: "Bearer " + getAccessToken()  
66 - }, 63 + uploadFileUrl: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload", // 请求地址
  64 + headers: { Authorization: "Bearer " + getAccessToken() }, // 设置上传的请求头部
67 Quill: null, 65 Quill: null,
68 currentValue: "", 66 currentValue: "",
69 options: { 67 options: {
@@ -126,7 +124,7 @@ export default { @@ -126,7 +124,7 @@ export default {
126 const editor = this.$refs.editor; 124 const editor = this.$refs.editor;
127 this.Quill = new Quill(editor, this.options); 125 this.Quill = new Quill(editor, this.options);
128 // 如果设置了上传地址则自定义图片上传事件 126 // 如果设置了上传地址则自定义图片上传事件
129 - if (this.type == 'url') { 127 + if (this.type === 'url') {
130 let toolbar = this.Quill.getModule("toolbar"); 128 let toolbar = this.Quill.getModule("toolbar");
131 toolbar.addHandler("image", (value) => { 129 toolbar.addHandler("image", (value) => {
132 this.uploadType = "image"; 130 this.uploadType = "image";
@@ -172,11 +170,13 @@ export default { @@ -172,11 +170,13 @@ export default {
172 // 获取富文本组件实例 170 // 获取富文本组件实例
173 let quill = this.Quill; 171 let quill = this.Quill;
174 // 如果上传成功 172 // 如果上传成功
175 - if (res.code == 200) { 173 + // edit by 芋道源码
  174 + if (res.code === 200 || res.code === 0) {
176 // 获取光标所在位置 175 // 获取光标所在位置
177 let length = quill.getSelection().index; 176 let length = quill.getSelection().index;
178 // 插入图片 res.url为服务器返回的图片地址 177 // 插入图片 res.url为服务器返回的图片地址
179 - quill.insertEmbed(length, "image", process.env.VUE_APP_BASE_API + res.fileName); 178 + // edit by 芋道源码
  179 + quill.insertEmbed(length, "image", res.data);
180 // 调整光标到最后 180 // 调整光标到最后
181 quill.setSelection(length + 1); 181 quill.setSelection(length + 1);
182 } else { 182 } else {
yudao-ui-admin/src/components/ImageUpload/index.vue
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 <div class="component-upload-image"> 2 <div class="component-upload-image">
3 <el-upload 3 <el-upload
4 multiple 4 multiple
5 - :action="url" 5 + :action="uploadFileUrl"
6 list-type="picture-card" 6 list-type="picture-card"
7 :on-success="handleUploadSuccess" 7 :on-success="handleUploadSuccess"
8 :before-upload="handleBeforeUpload" 8 :before-upload="handleBeforeUpload"
@@ -76,7 +76,7 @@ export default { @@ -76,7 +76,7 @@ export default {
76 dialogImageUrl: "", 76 dialogImageUrl: "",
77 dialogVisible: false, 77 dialogVisible: false,
78 hideUpload: false, 78 hideUpload: false,
79 - url: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload", // 请求地址 79 + uploadFileUrl: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload", // 请求地址
80 headers: { Authorization: "Bearer " + getAccessToken() }, // 设置上传的请求头部 80 headers: { Authorization: "Bearer " + getAccessToken() }, // 设置上传的请求头部
81 fileList: [] 81 fileList: []
82 }; 82 };
yudao-ui-admin/src/components/UploadImage/index.vue deleted
1 -<template>  
2 - <div class="component-upload-image">  
3 - <el-upload  
4 - :action="uploadImgUrl"  
5 - list-type="picture-card"  
6 - :on-success="handleUploadSuccess"  
7 - :before-upload="handleBeforeUpload"  
8 - :on-error="handleUploadError"  
9 - name="file"  
10 - :show-file-list="false"  
11 - :headers="headers"  
12 - style="display: inline-block; vertical-align: top"  
13 - >  
14 - <img v-if="value" :src="value" class="avatar" />  
15 - <i v-else class="el-icon-plus avatar-uploader-icon"></i>  
16 - </el-upload>  
17 - </div>  
18 -</template>  
19 -  
20 -<script>  
21 -import { getAccessToken } from "@/utils/auth";  
22 -  
23 -export default {  
24 - components: {},  
25 - data() {  
26 - return {  
27 - uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址  
28 - headers: {  
29 - Authorization: "Bearer " + getAccessToken(),  
30 - },  
31 - };  
32 - },  
33 - props: {  
34 - value: {  
35 - type: String,  
36 - default: "",  
37 - },  
38 - },  
39 - methods: {  
40 - handleUploadSuccess(res) {  
41 - this.$emit("input", res.url);  
42 - this.loading.close();  
43 - },  
44 - handleBeforeUpload() {  
45 - this.loading = this.$loading({  
46 - lock: true,  
47 - text: "上传中",  
48 - background: "rgba(0, 0, 0, 0.7)",  
49 - });  
50 - },  
51 - handleUploadError() {  
52 - this.$message({  
53 - type: "error",  
54 - message: "上传失败",  
55 - });  
56 - this.loading.close();  
57 - },  
58 - },  
59 - watch: {},  
60 -};  
61 -</script>  
62 -  
63 -<style scoped lang="scss">  
64 -.avatar {  
65 - width: 100%;  
66 - height: 100%;  
67 -}  
68 -</style>