详解mpvue开发小程序小总结(2)

//选择图片 chooseImage () { wx.chooseImage({ count: this.chooseImageNum, sizeType: ['original'], sourceType: ['album', 'camera'], success: (res) => { this.imageList = [...this.imageList, ...res.tempFilePaths] }, }) }, uploadImg (data, index) { return new Promise((resolve, reject) => { let filePath = data let fileName = path(this.id, 'test', fileType(filePath.substr(filePath.lastIndexOf('https://www.jb51.net/') + 1))) + index cos.postObject({ Bucket: Bucket, Region: Region, Key: fileName, FilePath: filePath, }, (err, res) => { if (res.statusCode === 200) { let item = { imageUrl: res.Location, } this.data.imageList.push(item) resolve(res) } else { reject(err) } }) }) }, //上传图片 upload () { return new Promise((resolve, reject) => { //没有图片 if (this.imageList.length === 0) { let data = { statusCode: 200, } resolve(data) return } //有图片 let all = [] for (let i = 0; i < this.imageList.length; i++) { all.push(this.uploadImg(this.imageList[i], i)) } Promise.all(all).then(res => { resolve(res) }).catch(err => { reject(err) }) }) }, handleSubmit(){ this.upload().then(res=>{ //执行剩余步骤 }).catch(err=>{ console.log(err) }) }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/c46f9688a7006081660dcea1f0f84e8f.html