|
|
import apiConfig from "./apiConfig";
|
|
|
|
|
|
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 default function ({ name, data:_data = {}, session, 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})
|
|
|
});
|
|
|
})
|
|
|
}
|