export default class{ cookies = "" constructor(){ } processCookies(cookies){ if(cookies.constructor==String) cookies = cookies.split(/,\s?(?=[^=,;]+=)/g); cookies = this.cookies = cookies.map(item => item.split(";")[0]).join(";"); 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) this.processCookies(res.cookies); else if (res.header["Set-Cookie"]) this.processCookies(res.header["Set-Cookie"]); else if(res.header["set-cookie"]) this.processCookies(res.header["set-Cookie"]); success(res); }, 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 }) } }