Skip to content

Commit 3a6a811

Browse files
committed
✨ WangEditor 图片上传功能
1 parent c414e4d commit 3a6a811

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/api/notify/announcement.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,11 @@ export function close(id) {
4646
method: 'patch'
4747
})
4848
}
49+
50+
export function uploadImage (resultFiles) {
51+
const formData = new FormData()
52+
resultFiles.forEach(file => {
53+
formData.append('files', file);
54+
});
55+
return request.post('/notify/announcement/image', formData, { contentType: false, processData: false })
56+
}

src/components/Editor/WangEditor.vue

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export default {
1111
// eslint-disable-next-line
1212
value: {
1313
type: String
14+
},
15+
// 图片上传的服务端接口
16+
uploadImgReq: {
17+
type: Function
1418
}
1519
},
1620
data () {
@@ -46,7 +50,24 @@ export default {
4650
// v-model 方式的表单值联动
4751
this.$emit('input', this.editorData)
4852
}
49-
editor.config.zIndex = 500
53+
editor.config.zIndex = 100
54+
// 图片配置
55+
const uploadImgReq = this.uploadImgReq
56+
if(uploadImgReq){
57+
const that = this
58+
editor.config.customUploadImg = function (resultFiles, insertImgFn) {
59+
// resultFiles 是 input 中选中的文件列表
60+
// insertImgFn 是获取图片 url 后,插入到编辑器的方法
61+
uploadImgReq(resultFiles).then(res => {
62+
// 上传图片,返回结果,将图片插入到编辑器中
63+
// TODO 图片大小控制
64+
for (let objectName of res.data) {
65+
const imgUrl = that.fileAbsoluteUrl(objectName)
66+
insertImgFn(imgUrl)
67+
}
68+
})
69+
}
70+
}
5071
// 创建编辑器
5172
editor.create()
5273
this.editor = editor

src/views/notify/announcement/AnnouncementForm.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
</a-form-item>
1010

1111
<a-form-item label="内容">
12-
<wang-editor v-decorator="['content', decoratorOptions.content]"></wang-editor>
12+
<wang-editor
13+
v-decorator="['content', decoratorOptions.content]"
14+
:upload-img-req="reqFunctions.uploadImage"
15+
></wang-editor>
1316
</a-form-item>
1417

1518
<a-form-item label="选择接收人">
@@ -97,7 +100,7 @@
97100

98101
<script>
99102
import { FormPageMixin } from '@/mixins'
100-
import { addObj, putObj } from '@/api/notify/announcement'
103+
import { addObj, putObj, uploadImage } from '@/api/notify/announcement'
101104
import { getSelectData } from '@/api/sys/role'
102105
import { getTree } from '@/api/sys/organization'
103106
import WangEditor from '@/components/Editor/WangEditor'
@@ -111,7 +114,8 @@ export default {
111114
return {
112115
reqFunctions: {
113116
create: addObj,
114-
update: putObj
117+
update: putObj,
118+
uploadImage: uploadImage
115119
},
116120
117121
// 校验配置

0 commit comments

Comments
 (0)