博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于 muse-ui 封装一个微信公众号上传插件 实现多图上传
阅读量:4670 次
发布时间:2019-06-09

本文共 3266 字,大约阅读时间需要 10 分钟。

1 Vue.component('my-wx-upload', { 2     template: ` 3     
4
5
6
7
8
9
10
11
13
14
15 `,16 props: {17 imgList: {type: Array},18 chooseImage: {type: Number, default: 9},19 },20 data() {21 return {22 readyUploadImages: []23 }24 },25 mounted() {26 this.readyUploadImages = this.imgList27 },28 methods: {29 remove(index) {30 this.readyUploadImages.splice(index, 1)31 },32 add() {33 wx.chooseImage({34 count: this.oddchooseImageCount, // 最多可以选择的图片张数,默认935 sizeType: ['original'], // original 原图,compressed 压缩图,默认二者都有36 sourceType: ['album', 'camera'], // album 从相册选图,camera 使用相机,默认二者都有37 success: res => {38 this.readyUploadImages = this.readyUploadImages.concat(res.localIds);39 // myUpImageBlock40 },41 fail: function () {42 // fail43 },44 complete: function () {45 // complete46 }47 })48 },49 preview(img) {50 wx.previewImage({51 current: img, // 当前显示图片的http链接52 urls: this.readyUploadImages // 需要预览的图片http链接列表53 });54 }55 },56 computed: {57 oddchooseImageCount() {58 return this.chooseImage - this.readyUploadImages.length;59 },60 uploadIsFull() {61 return this.readyUploadImages.length !== this.chooseImage62 }63 },64 watch: {65 readyUploadImages(val) {66 this.$emit('update:imgList', val)67 }68 }69 });

调用方法

 <my-wx-upload :img-list.sync="传入一个数组"></my-wx-upload>

 

上传方法 

WxUploadImage(imgList) {            return new Promise((resolve, reject) => {                if (imgList && imgList instanceof Array && imgList.length > 0) {                    let promiseList = [];                    for (let i = 0; i < imgList.length; i++) {                        promiseList[i] = new Promise((resolve, reject) => {                            wx.uploadImage({                                localId: imgList[i],                                success: res => {                                    resolve(res.serverId);                                },                                fail: error => {                                    reject(error);                                }                            })                        });                    }                    Promise.all(promiseList)                        .then(result => {                            resolve(result);                        })                        .then(error => {                            reject(error);                        })                } else {                    reject('传参有误,请传数组格式');                }            })        }

  调用方法

this.WxUploadImage(this.imageList).then(res => {                });

 

转载于:https://www.cnblogs.com/machete/p/10560698.html

你可能感兴趣的文章
ACM 美素数 (没AC)
查看>>
Sqlserver学习研究
查看>>
VTK图形模型主要对象
查看>>
c# Linq实现 获得某一个路径下所有文件的名(不含扩展名)
查看>>
动静态广播的区别
查看>>
前缀式计算(前缀表达式)
查看>>
Linux常用命令大全
查看>>
添加删除tag
查看>>
ARM学习篇 中断定时理解
查看>>
卷积神经网络在tenserflow的实现
查看>>
[STL]用法
查看>>
PostgresException: 42883: function ifnull(integer, integer) does not exist
查看>>
python3 表情符号编码
查看>>
桥接模式
查看>>
跨server传输数据注意事项
查看>>
使用蒙版--渐变--制作瓶子倒影
查看>>
后代元素 span:first-child{...}
查看>>
LeetCode 540. 有序数组中的单一元素(Single Element in a Sorted Array) 42
查看>>
codevs 5958 无
查看>>
htaccess 实现网址缩短
查看>>