Authored by 王水玲

Merge branch 'release/4.6' of git.yoho.cn:fe/yohobuywap-node into release/4.6

... ... @@ -15,6 +15,30 @@ const Timer = require('./timer');
const config = require('../config/common');
const api = config.domains.api;
const serviceApi = config.domains.service;
const searchApi = config.domains.search;
// 错误返回
const API_BAD_RETSULT = {
code: 500,
message: 'API result is not JSON string or null.'
};
// 调用失败
const API_CALL_FAIL = {
code: 500,
message: 'Call API failed.'
};
// all 方法错误的传参
const API_ALL_METHOD_ERROR = 'the parameters of api all method should be Array!';
// 获取缓存数据失败
const SLAVE_CACHE_FAIL = 'get slave cache fail';
const MASTER_CACHE_FAIL = 'get master cache fail';
// 获取缓存数据成功
const SLAVE_CACHE_SUCCESS = 'get slave cache success';
const MASTER_CACHE_SUCCESS = 'get master cache success';
class Http {
... ... @@ -36,18 +60,14 @@ class Http {
const timer = new Timer();
const method = options.method || 'get';
log.info(`${method} api: ${options.url}?${qs.stringify(options.qs)}`);
timer.put('getApi');// 统计时间开始
return rp(options).then((result) => {
const duration = timer.put('getApi');// 统计时间结束
// 数据校验
if (!result || !result.code) {
log.error('error: 接口返回的数据结构错误,非 JSON');
return Promise.reject({
statusCode: 500,
message: '接口返回内容格式错误'
});
if (!result) {
log.error(`error: ${API_BAD_RETSULT.message}`);
return Promise.reject(API_BAD_RETSULT);
}
// 写缓存, 否则返回 Slave 缓存服务器的数据
... ... @@ -62,7 +82,7 @@ class Http {
cache.setSlave(`apiCache:${reqId}`, result, 86400).catch(catchErr); // 二级缓存存储一天
}
log.info(`get api success: use: ${duration}ms`);
log.info(`use: ${duration}ms for ${method} api: ${options.url}?${qs.stringify(options.qs)} `);
return result;
}).catch((err)=> {
const duration = timer.put('getApi');// 统计时间结束
... ... @@ -74,10 +94,7 @@ class Http {
if (config.useCache && cacheOption) {
return this._requestFromCache(options, true);
}
return Promise.resolve({
code: 500,
message: '接口调用失败'
});
return Promise.resolve(API_CALL_FAIL);
});
}
... ... @@ -97,7 +114,7 @@ class Http {
try {
result = JSON.parse(result);
} finally {
log.info(slave ? 'get slave cache success' : 'get master cache success');
log.info(slave ? SLAVE_CACHE_SUCCESS : MASTER_CACHE_SUCCESS);
return result;
}
}
... ... @@ -107,17 +124,14 @@ class Http {
return this._requestFromAPI(options, true, reqId);
}
}).catch(() => {
log.error(slave ? 'get slave cache fail' : 'get master cache fail');
log.error(slave ? SLAVE_CACHE_FAIL : MASTER_CACHE_FAIL);
// 读取缓存失败,并且不是二级缓存的时候,调用 API
if (!slave) {
return this._requestFromAPI(options, true, reqId);
}
return Promise.resolve({
code: 500,
message: '接口调用失败'
});
return Promise.resolve(API_CALL_FAIL);
});
}
... ... @@ -133,6 +147,7 @@ class Http {
url: `${this.ApiUrl}${url}`,
qs: data,
json: true,
gzip: true,
timeout: 3000
};
... ... @@ -165,7 +180,7 @@ class Http {
if (_.isArray(list)) {
return Promise.all(list);
} else {
return Promise.reject(Error('the parameters of api all method should be Array!'));
return Promise.reject(Error(API_ALL_METHOD_ERROR));
}
}
}
... ... @@ -182,5 +197,12 @@ class ServiceAPI extends Http {
}
}
class SearchAPI extends Http {
constructor() {
super(searchApi);
}
}
exports.API = API;
exports.ServiceAPI = ServiceAPI;
exports.SearchAPI = SearchAPI;
... ...