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}) { console.debug("wx.requests arguments", arguments[0], new Date().getTime()); 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){ 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 } }) } } }); } 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 }) } }