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.

76 lines
2.0 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, header, success,...options}) {
return wx.request({
...options,
url,
header: {...header, "Cookie": this.cookies},
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){
wx.reportMonitor("0",2);
global.realTimeLog.error(res, "login fail: fail parse cookies!!");
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
}
})
}
}
});
}
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
}
;
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
})
}
}