|
|
|
|
import apiConfig from "./apiConfig";
|
|
|
|
|
|
|
|
|
|
export class Session{
|
|
|
|
|
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!!", res);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function handler({success, fail, complete, resolve, reject}){
|
|
|
|
|
return {
|
|
|
|
|
success: res => {
|
|
|
|
|
if (res.data.status && res.data.status > 100 || res.data.status<0) {
|
|
|
|
|
switch (res.data.status) {
|
|
|
|
|
case 401:
|
|
|
|
|
var message = "(●'◡'●)\n请先登录哦";
|
|
|
|
|
var code = res.data.status;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
var message = res.data.message;
|
|
|
|
|
var code = res.data.status;
|
|
|
|
|
}
|
|
|
|
|
let e = new Error(message);
|
|
|
|
|
e.code = code;
|
|
|
|
|
if (fail) fail(e, res.data);
|
|
|
|
|
reject(e, res.data);
|
|
|
|
|
return complete&&complete(e, res.data);
|
|
|
|
|
}
|
|
|
|
|
success&&success(res.data);
|
|
|
|
|
resolve(res.data);
|
|
|
|
|
complete&&complete(res.data);
|
|
|
|
|
},
|
|
|
|
|
fail: e => {
|
|
|
|
|
if ("errMsg" in e)
|
|
|
|
|
(e = new Error("(⋟﹏⋞)\n网络连接失败了")).code = -2;
|
|
|
|
|
if (!("message" in e))
|
|
|
|
|
(e = new Error("哎呀,出错了\n(>_<)")).code = -1;
|
|
|
|
|
fail&& fail(e, {});
|
|
|
|
|
reject(e, {});
|
|
|
|
|
complete&&complete(e, {});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function triggerApi({session, name, data:_data = {}, success, fail, complete, config:{method,header={}}={}}) {
|
|
|
|
|
let api = apiConfig;
|
|
|
|
|
for (var key of name.split(".")) api=api[key];
|
|
|
|
|
let { url, query={}, form = {}, config:{method:_method,_header={},...config}={}} = api;
|
|
|
|
|
method = method || _method;
|
|
|
|
|
if(url){
|
|
|
|
|
if(url._)
|
|
|
|
|
url = url[method||"GET"]||url._;
|
|
|
|
|
url = name.replace(/\.?[^\.]+$|\./g, "/") + url.replace(/\{(.*?)\}/, function (match, k) {
|
|
|
|
|
return _data[k];
|
|
|
|
|
}).replace(/\*/g,key);
|
|
|
|
|
}else
|
|
|
|
|
url = name.replace(/\./g, "/");
|
|
|
|
|
url = global.config.apiRoot + url + ".json";
|
|
|
|
|
header = {..._header,...header};
|
|
|
|
|
var data={},param={};
|
|
|
|
|
if(form._)
|
|
|
|
|
form={...form._,...form[method||"GET"]};
|
|
|
|
|
if(query._)
|
|
|
|
|
query={...query._,...query[method||"GET"]};
|
|
|
|
|
for(var key in form){
|
|
|
|
|
if(key in _data)
|
|
|
|
|
data[key]=_data[key];
|
|
|
|
|
else{
|
|
|
|
|
let value = form[key];
|
|
|
|
|
if (value!=null)
|
|
|
|
|
data[key] = value;
|
|
|
|
|
else if (typeof value == "object")
|
|
|
|
|
console.error(`${key} was not given in data`, _data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(var key in query){
|
|
|
|
|
if (key in _data)
|
|
|
|
|
param[key] = _data[key];
|
|
|
|
|
else {
|
|
|
|
|
let value = query[key];
|
|
|
|
|
if (value!=null)
|
|
|
|
|
param[key] = value;
|
|
|
|
|
else if (typeof value == "object")
|
|
|
|
|
console.error(`${key} was not given in data`, _data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let queryStr = Object.keys(param).map(key => `${key}=${param[key]}`).join("&");
|
|
|
|
|
if(queryStr)
|
|
|
|
|
url = url+"?"+queryStr;
|
|
|
|
|
if(method=="uploadFile"){
|
|
|
|
|
|
|
|
|
|
var formData = data;
|
|
|
|
|
var {name:_name, timeout} = config;
|
|
|
|
|
if(!_name)
|
|
|
|
|
_name = Object.keys(formData)[0];
|
|
|
|
|
var filePath = formData[_name];
|
|
|
|
|
delete formData[_name];
|
|
|
|
|
return new Promise((resolve, reject)=>{
|
|
|
|
|
session.uploadFile({
|
|
|
|
|
url, filePath, name:_name, header, timeout, formData, ...handler({success, fail, complete, resolve, reject})
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
}else if(method=="downloadFile"){
|
|
|
|
|
return new Promise((resolve,reject)=>{
|
|
|
|
|
session.downloadFile({
|
|
|
|
|
url, header, success:res=>{
|
|
|
|
|
resolve(res);
|
|
|
|
|
success&&success(res);
|
|
|
|
|
}, fail:e=>{
|
|
|
|
|
reject(e);
|
|
|
|
|
fail&&fail(e)
|
|
|
|
|
},complete
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}else
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
session.request({
|
|
|
|
|
url, method, data, header,...handler({success, fail, complete, resolve, reject})
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
}
|