|
|
@ -3,7 +3,6 @@ import apiConfig from "./apiConfig";
|
|
|
|
function handler({success, fail, resolve, reject}){
|
|
|
|
function handler({success, fail, resolve, reject}){
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
success: res => {
|
|
|
|
success: res => {
|
|
|
|
if (res.data.status <=100 ) console.error("!!!api response!!! status==="+res.data.status);
|
|
|
|
|
|
|
|
if (res.data.status && res.data.status > 100 || res.data.status<0) {
|
|
|
|
if (res.data.status && res.data.status > 100 || res.data.status<0) {
|
|
|
|
switch (res.data.status) {
|
|
|
|
switch (res.data.status) {
|
|
|
|
case 401:
|
|
|
|
case 401:
|
|
|
@ -23,12 +22,11 @@ function handler({success, fail, resolve, reject}){
|
|
|
|
resolve(res.data);
|
|
|
|
resolve(res.data);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fail: e => {
|
|
|
|
fail: e => {
|
|
|
|
console.error(e);
|
|
|
|
global.realTimeLog.warn(e);
|
|
|
|
if ("errMsg" in e)
|
|
|
|
if ("errMsg" in e)
|
|
|
|
(e = new Error("(⋟﹏⋞)\n网络连接出错了呢")).code = -2;
|
|
|
|
(e = new Error("(⋟﹏⋞)\n网络连接出错了呢")).code = -2;
|
|
|
|
if (!("message" in e))
|
|
|
|
if (!("message" in e))
|
|
|
|
(e = new Error("(>_<)\n出现了未知错误")).code = -1;
|
|
|
|
(e = new Error("(>_<)\n出现了未知错误")).code = -1;
|
|
|
|
console.debug(e);
|
|
|
|
|
|
|
|
if (fail) { fail(e); }
|
|
|
|
if (fail) { fail(e); }
|
|
|
|
reject(e);
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -38,15 +36,14 @@ function handler({success, fail, resolve, reject}){
|
|
|
|
|
|
|
|
|
|
|
|
export default function ({ name, data:_data = {}, session, success, fail, complete, config:{method,header={}}={}}) {
|
|
|
|
export default function ({ name, data:_data = {}, session, success, fail, complete, config:{method,header={}}={}}) {
|
|
|
|
let api = apiConfig;
|
|
|
|
let api = apiConfig;
|
|
|
|
for (var key of name.split(".")) api=api[key]||console.error(`no api named ${key} in apiList`, api);
|
|
|
|
for (var key of name.split(".")) api=api[key];
|
|
|
|
let { url, query={}, form = {}, config:{method:_method,_header={},...config}={}} = api;
|
|
|
|
let { url, query={}, form = {}, config:{method:_method,_header={},...config}={}} = api;
|
|
|
|
method = method || _method;
|
|
|
|
method = method || _method;
|
|
|
|
console.debug(`call EduCoder api ${name}`, api, arguments[0], "require arguments:",form,query);
|
|
|
|
|
|
|
|
if(url){
|
|
|
|
if(url){
|
|
|
|
if(url._)
|
|
|
|
if(url._)
|
|
|
|
url = url[method||"GET"]||url._;
|
|
|
|
url = url[method||"GET"]||url._;
|
|
|
|
url = name.replace(/\.?[^\.]+$|\./g, "/") + url.replace(/\{(.*?)\}/, function (match, k) {
|
|
|
|
url = name.replace(/\.?[^\.]+$|\./g, "/") + url.replace(/\{(.*?)\}/, function (match, k) {
|
|
|
|
return _data[k]||console.error(`${k} was not given in data`, _data)
|
|
|
|
return _data[k];
|
|
|
|
}).replace(/\*/g,key);
|
|
|
|
}).replace(/\*/g,key);
|
|
|
|
}else
|
|
|
|
}else
|
|
|
|
url = name.replace(/\./g, "/");
|
|
|
|
url = name.replace(/\./g, "/");
|
|
|
@ -65,7 +62,7 @@ export default function ({ name, data:_data = {}, session, success, fail, comple
|
|
|
|
if (value!=null)
|
|
|
|
if (value!=null)
|
|
|
|
data[key] = value;
|
|
|
|
data[key] = value;
|
|
|
|
else if (typeof value == "object")
|
|
|
|
else if (typeof value == "object")
|
|
|
|
console.error(`${key} was not given in data`, _data);
|
|
|
|
console.error(`${key} was not given in data`, _data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(var key in query){
|
|
|
|
for(var key in query){
|
|
|
@ -83,14 +80,14 @@ export default function ({ name, data:_data = {}, session, success, fail, comple
|
|
|
|
if(queryStr)
|
|
|
|
if(queryStr)
|
|
|
|
url = url+"?"+queryStr;
|
|
|
|
url = url+"?"+queryStr;
|
|
|
|
if(method=="uploadFile"){
|
|
|
|
if(method=="uploadFile"){
|
|
|
|
console.log("uploadFile")
|
|
|
|
|
|
|
|
var formData = data;
|
|
|
|
var formData = data;
|
|
|
|
var {name:_name, timeout} = config;
|
|
|
|
var {name:_name, timeout} = config;
|
|
|
|
if(!_name)
|
|
|
|
if(!_name)
|
|
|
|
_name = Object.keys(formData)[0];
|
|
|
|
_name = Object.keys(formData)[0];
|
|
|
|
var filePath = formData[_name];
|
|
|
|
var filePath = formData[_name];
|
|
|
|
delete formData[_name];
|
|
|
|
delete formData[_name];
|
|
|
|
console.log({ url, filePath, name: _name, header, timeout, formData});
|
|
|
|
;
|
|
|
|
return new Promise((resolve, reject)=>{
|
|
|
|
return new Promise((resolve, reject)=>{
|
|
|
|
session.uploadFile({
|
|
|
|
session.uploadFile({
|
|
|
|
url, filePath, name:_name, header, timeout, formData,complete, ...handler({success, fail, resolve, reject})
|
|
|
|
url, filePath, name:_name, header, timeout, formData,complete, ...handler({success, fail, resolve, reject})
|
|
|
|