You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.2 KiB

export default class{
cookies = ""
constructor(){
}
saveCookies(cookies){
this.cookies = cookies.map(item => item.split(";")[0]).join("; ");
}
splitCookies(string){
let cookies = string.split(/,\s?(?=[^=,\;]*?\=)/ig);
return cookies;
}
request({url, data, header, method, dataType, responseType, success, fail, complete}) {
console.debug("wx.requests arguments", arguments[0], new Date().getTime());
return wx.request({
url,
data,
header: {...header, "Cookie": this.cookies},
method,
dataType,
responseType,
success: (res) => {
if (res.cookies && res.cookies.length>0)
this.saveCookies(res.cookies);
else if (res.header["Set-Cookie"])
this.saveCookies(this.splitCookies(res.header["Set-Cookie"]));
else if(res.header["set-cookie"])
this.saveCookies(this.splitCookies(res.header["set-Cookie"]));
success(res);
if(url.indexOf("login.json")!=-1&&!res.data.status&&!this.cookies){
console.error("oh no!!!login fail!!!!");
wx.reportMonitor("0",2);
var db = wx.cloud.database();
db.collection("failCookies").add({
data:{
createdAt: db.serverDate(),
res,
header:res.header,
"Set-Cookies": res.header["Set-Cookie"],
"set-cookie":res.header["set-cookie"],
cookies:res.cookies
}
})
}
},
fail: fail,
complete: complete
});
}
uploadFile({url, filePath, name, header, formData, success, fail, complete}){
return wx.uploadFile({
url,
filePath,
name,
header: {"Cookie": this.cookies, ...header},
formData,
success:res=>{
try{
res.data = JSON.parse(res.data);
}catch(e){
//@todo
}
console.log("uploadFile",res);
if(success)
success(res)
},
fail,
complete
})
}
downloadFile({url, header, filePath, success, fail, complete}){
return wx.downloadFile({
url,
header: {...header, "Cookie": this.cookies},
filePath,
success,
fail,
complete
})
}
}