我使用的是Promise,是让两个请求都执行完再执行第三个请求
总结的注意事项
1.resolve方法要在success里面调用
代码:
uploadImage: function() {
return new Promise((resolve, reject) => {
// 执行请求1的代码
const self = this;
const imageSrc = self.data.imageSrc;
wx.uploadFile({
url: 'https://localhost:9776/lostAndFind/postFormAndImageOfItem',
filePath: imageSrc,
name: 'file',
success(res) {
self.setData({
imageUrl: JSON.parse(res.data).data // 将result.data赋值给poet
});
console.log(self.data.imageUrl)
resolve(self.data.imageUrl);
}
});
});
},
uploadImageOfContact: function() {
return new Promise((resolve, reject) => {
// 执行请求2的代码
const self = this;
const contactSrc = self.data.contactSrc;
wx.uploadFile({
url: 'https://localhost:9776/lostAndFind/postFormAndImageOfContact',
filePath: contactSrc,
name: 'file',
success(res) {
//new Promise模板
self.setData({
contactUrl: JSON.parse(res.data).data
});
resolve(self.data.contactUrl);
console.log(self.data.contactUrl)
}
});
});
},
powerDrawerFisrt:function(event){
const self=this
Promise.all([self.uploadImage(), self.uploadImageOfContact()])
.then((values) => {
// 所有请求都执行完成后的处理
console.log(self.data.imageUrl)
console.log(self.data.contactUrl)
console.log(values)
wx.request({
url: 'https://localhost:9776/lostAndFind/postFormAndImageOfForm',
method:'POST',
header:{
'token':wx.getStorageSync('token')
},
data:{
'imageSrc':self.data.imageUrl,
'contactSrc':self.data.contactUrl,
'note':self.data.note,
'addressName':self.data.addressName,
'thingName':self.data.thingName,
'processing':self.data.processing
}
})
})
.catch(error => {
// 处理请求错误
console.log(error)
});
}