...
|
...
|
@@ -55,9 +55,28 @@ class Http { |
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 处理接口返回状态码
|
|
|
*/
|
|
|
_handleDataCode(data, code, url) {
|
|
|
let result = {};
|
|
|
|
|
|
code = code || 200;
|
|
|
if (_.toNumber(data.code) === _.toNumber(code)) {
|
|
|
_.unset(data, 'code');
|
|
|
_.forEach(data, (value, key) => {
|
|
|
_.set(result, key, value);
|
|
|
});
|
|
|
} else {
|
|
|
log.error(`API: ${url} return code not ${code}`);
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 调用接口
|
|
|
*/
|
|
|
_requestFromAPI(options, cacheOption, reqId) {
|
|
|
_requestFromAPI(options, param, reqId) {
|
|
|
const timer = new Timer();
|
|
|
const method = options.method || 'get';
|
|
|
|
...
|
...
|
@@ -71,9 +90,15 @@ class Http { |
|
|
return Promise.reject(API_BAD_RETSULT);
|
|
|
}
|
|
|
|
|
|
// 处理返回数据状态码
|
|
|
if (param && param.code) {
|
|
|
result = this._handleDataCode(result, param.code, options.url);
|
|
|
}
|
|
|
|
|
|
// 写缓存, 否则返回 Slave 缓存服务器的数据
|
|
|
if (config.useCache && cacheOption) {
|
|
|
const cacheTime = _.isNumber(cacheOption) ? cacheOption : 60;
|
|
|
if (options.method === 'get' && config.useCache &&
|
|
|
param && param.cache) {
|
|
|
const cacheTime = _.isNumber(param.cache) ? param.cache : 60;
|
|
|
const catchErr = (err) => {
|
|
|
log.error(`cache: ${err.toString()}`);
|
|
|
};
|
...
|
...
|
@@ -92,7 +117,7 @@ class Http { |
|
|
log.error(`API: ${options.url}?${qs.stringify(options.qs)}`);
|
|
|
|
|
|
// 使用缓存的时候,读取二级缓存
|
|
|
if (config.useCache && cacheOption) {
|
|
|
if (config.useCache && param && param.cache) {
|
|
|
return this._requestFromCache(options, true);
|
|
|
}
|
|
|
return Promise.resolve(API_CALL_FAIL);
|
...
|
...
|
@@ -103,9 +128,10 @@ class Http { |
|
|
* 读取缓存
|
|
|
* @param {[object]} options
|
|
|
* @param {[boolean]} slave true: 读取二级缓存
|
|
|
* @param {[object]} param 请求API处理参数
|
|
|
* @return {[type]}
|
|
|
*/
|
|
|
_requestFromCache(options, slave) {
|
|
|
_requestFromCache(options, slave, param) {
|
|
|
const reqId = this._getReqId(options);
|
|
|
const getCache = slave ? cache.getFromSlave : cache.get;
|
|
|
|
...
|
...
|
@@ -122,14 +148,14 @@ class Http { |
|
|
|
|
|
// 读取缓存失败,并且不是二级缓存的时候,调用 API
|
|
|
if (!slave) {
|
|
|
return this._requestFromAPI(options, true, reqId);
|
|
|
return this._requestFromAPI(options, param, reqId);
|
|
|
}
|
|
|
}).catch(() => {
|
|
|
log.error(slave ? SLAVE_CACHE_FAIL : MASTER_CACHE_FAIL);
|
|
|
|
|
|
// 读取缓存失败,并且不是二级缓存的时候,调用 API
|
|
|
if (!slave) {
|
|
|
return this._requestFromAPI(options, true, reqId);
|
|
|
return this._requestFromAPI(options, param, reqId);
|
|
|
}
|
|
|
|
|
|
return Promise.resolve(API_CALL_FAIL);
|
...
|
...
|
@@ -140,10 +166,10 @@ class Http { |
|
|
* 使用 get 请求获取接口
|
|
|
* @param {[string]} url
|
|
|
* @param {[object]} data
|
|
|
* @param {[bool or number]} cacheOption 使用数字时,数字表示缓存时间
|
|
|
* @param {[object]} param 数据处理参数
|
|
|
* @return {[type]}
|
|
|
*/
|
|
|
get(url, data, cacheOption) {
|
|
|
get(url, data, param) {
|
|
|
const options = {
|
|
|
url: `${this.ApiUrl}${url}`,
|
|
|
qs: data.client_secret ? data : sign.apiSign(data),
|
...
|
...
|
@@ -153,19 +179,21 @@ class Http { |
|
|
};
|
|
|
|
|
|
// 从缓存获取数据
|
|
|
if (config.useCache && cacheOption) {
|
|
|
return this._requestFromCache(options);
|
|
|
if (config.useCache && param && param.catch) {
|
|
|
return this._requestFromCache(options, false, param);
|
|
|
}
|
|
|
|
|
|
return this._requestFromAPI(options, cacheOption);
|
|
|
return this._requestFromAPI(options, param);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* post
|
|
|
* @param url String
|
|
|
* @param data Obejct
|
|
|
* 使用 post 请求获取接口
|
|
|
* @param {[string]} url
|
|
|
* @param {[object]} data
|
|
|
* @param {[object]} param 数据处理参数
|
|
|
* @return {[type]}
|
|
|
*/
|
|
|
post(url, data) {
|
|
|
post(url, data, param) {
|
|
|
const options = {
|
|
|
url: `${this.ApiUrl}${url}`,
|
|
|
form: data.client_secret ? data : sign.apiSign(data),
|
...
|
...
|
@@ -174,7 +202,7 @@ class Http { |
|
|
timeout: 3000
|
|
|
};
|
|
|
|
|
|
return this._requestFromAPI(options);
|
|
|
return this._requestFromAPI(options, param);
|
|
|
}
|
|
|
|
|
|
all(list) {
|
...
|
...
|
|