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