1、调用微信文件上传方法时,需要将返回的数据转为json,以下为封装的公用请求
upload() {
if (this.isLoad) {
wx.showLoading({
title: "加载中",
mask: true
});
}
let self = this;
// //构建params,在params上面加上用户id,时间戳和签名
// try {
// let customerId = wx.getStorageSync('customerId');
// if (null != customerId && customerId != undefined && customerId.length > 0) {
// self.params.customerId = customerId;
// }
// } catch (e) {}
// self.params.timestamp = new Date().getTime();
// self.params.sign = self.generateSign();
console.log("文件上传请求接口:" + self.method)
console.log("请求报文:")
console.log(self.formData)
if (self.port.length > 0) {
self.url += ':' + self.port + '/';
}
// 文件上传
wx.uploadFile({
url: self.url + self.method,
filePath: self.filePath,
name: 'file',
header:{
'cookie': wx.getStorageSync("sessionid")
},
formData: self.formData,
success: function(res) {
let JsonSrc = JSON.parse(res.data)
console.log("json转换:",JsonSrc)
let response = JsonSrc;
if (null != response &&
response != undefined) {
let rspCode = response.code;
if ('0' == rspCode) {
self.success(response);
} else {
if (self.fail(response)) {
let rspMsg = response.msg;
if (null != rspMsg &&
rspMsg != undefined &&
rspMsg.length > 0) {
wx.showModal({
title: '提示',
showCancel: false,
content: rspMsg
});
}
}
}
}
},
fail: function(response) {
self.fail(response);
wx.showModal({
title: '提示',
showCancel: false,
content: "连接失败"
});
},
complete: function(response) {
wx.hideLoading();
self.complete(response);
console.log("应答报文:")
console.log(response.data)
let statusCode = response.statusCode;
if (404 == statusCode) {
self.fail(response);
wx.showModal({
title: '提示',
showCancel: false,
content: "服务器找不到请求的内容"
});
} else if (200 == statusCode) {
console.log("文件请求", response)
let JsonSrc = JSON.parse(response.data)
console.log("json转换:",JsonSrc)
let rspCode = JsonSrc.code;
if (null == rspCode ||
rspCode == undefined ||
rspCode == 40001) {
wx.reLaunch({
url: '../login/login'
})
}
}
}
})