Merge branch 'release/4.6' of git.yoho.cn:fe/yohobuywap-node into release/4.6
Showing
1 changed file
with
41 additions
and
19 deletions
@@ -15,6 +15,30 @@ const Timer = require('./timer'); | @@ -15,6 +15,30 @@ const Timer = require('./timer'); | ||
15 | const config = require('../config/common'); | 15 | const config = require('../config/common'); |
16 | const api = config.domains.api; | 16 | const api = config.domains.api; |
17 | const serviceApi = config.domains.service; | 17 | const serviceApi = config.domains.service; |
18 | +const searchApi = config.domains.search; | ||
19 | + | ||
20 | +// 错误返回 | ||
21 | +const API_BAD_RETSULT = { | ||
22 | + code: 500, | ||
23 | + message: 'API result is not JSON string or null.' | ||
24 | +}; | ||
25 | + | ||
26 | +// 调用失败 | ||
27 | +const API_CALL_FAIL = { | ||
28 | + code: 500, | ||
29 | + message: 'Call API failed.' | ||
30 | +}; | ||
31 | + | ||
32 | +// all 方法错误的传参 | ||
33 | +const API_ALL_METHOD_ERROR = 'the parameters of api all method should be Array!'; | ||
34 | + | ||
35 | +// 获取缓存数据失败 | ||
36 | +const SLAVE_CACHE_FAIL = 'get slave cache fail'; | ||
37 | +const MASTER_CACHE_FAIL = 'get master cache fail'; | ||
38 | + | ||
39 | +// 获取缓存数据成功 | ||
40 | +const SLAVE_CACHE_SUCCESS = 'get slave cache success'; | ||
41 | +const MASTER_CACHE_SUCCESS = 'get master cache success'; | ||
18 | 42 | ||
19 | class Http { | 43 | class Http { |
20 | 44 | ||
@@ -36,18 +60,14 @@ class Http { | @@ -36,18 +60,14 @@ class Http { | ||
36 | const timer = new Timer(); | 60 | const timer = new Timer(); |
37 | const method = options.method || 'get'; | 61 | const method = options.method || 'get'; |
38 | 62 | ||
39 | - log.info(`${method} api: ${options.url}?${qs.stringify(options.qs)}`); | ||
40 | timer.put('getApi');// 统计时间开始 | 63 | timer.put('getApi');// 统计时间开始 |
41 | return rp(options).then((result) => { | 64 | return rp(options).then((result) => { |
42 | const duration = timer.put('getApi');// 统计时间结束 | 65 | const duration = timer.put('getApi');// 统计时间结束 |
43 | 66 | ||
44 | // 数据校验 | 67 | // 数据校验 |
45 | - if (!result || !result.code) { | ||
46 | - log.error('error: 接口返回的数据结构错误,非 JSON'); | ||
47 | - return Promise.reject({ | ||
48 | - statusCode: 500, | ||
49 | - message: '接口返回内容格式错误' | ||
50 | - }); | 68 | + if (!result) { |
69 | + log.error(`error: ${API_BAD_RETSULT.message}`); | ||
70 | + return Promise.reject(API_BAD_RETSULT); | ||
51 | } | 71 | } |
52 | 72 | ||
53 | // 写缓存, 否则返回 Slave 缓存服务器的数据 | 73 | // 写缓存, 否则返回 Slave 缓存服务器的数据 |
@@ -62,7 +82,7 @@ class Http { | @@ -62,7 +82,7 @@ class Http { | ||
62 | cache.setSlave(`apiCache:${reqId}`, result, 86400).catch(catchErr); // 二级缓存存储一天 | 82 | cache.setSlave(`apiCache:${reqId}`, result, 86400).catch(catchErr); // 二级缓存存储一天 |
63 | } | 83 | } |
64 | 84 | ||
65 | - log.info(`get api success: use: ${duration}ms`); | 85 | + log.info(`use: ${duration}ms for ${method} api: ${options.url}?${qs.stringify(options.qs)} `); |
66 | return result; | 86 | return result; |
67 | }).catch((err)=> { | 87 | }).catch((err)=> { |
68 | const duration = timer.put('getApi');// 统计时间结束 | 88 | const duration = timer.put('getApi');// 统计时间结束 |
@@ -74,10 +94,7 @@ class Http { | @@ -74,10 +94,7 @@ class Http { | ||
74 | if (config.useCache && cacheOption) { | 94 | if (config.useCache && cacheOption) { |
75 | return this._requestFromCache(options, true); | 95 | return this._requestFromCache(options, true); |
76 | } | 96 | } |
77 | - return Promise.resolve({ | ||
78 | - code: 500, | ||
79 | - message: '接口调用失败' | ||
80 | - }); | 97 | + return Promise.resolve(API_CALL_FAIL); |
81 | }); | 98 | }); |
82 | } | 99 | } |
83 | 100 | ||
@@ -97,7 +114,7 @@ class Http { | @@ -97,7 +114,7 @@ class Http { | ||
97 | try { | 114 | try { |
98 | result = JSON.parse(result); | 115 | result = JSON.parse(result); |
99 | } finally { | 116 | } finally { |
100 | - log.info(slave ? 'get slave cache success' : 'get master cache success'); | 117 | + log.info(slave ? SLAVE_CACHE_SUCCESS : MASTER_CACHE_SUCCESS); |
101 | return result; | 118 | return result; |
102 | } | 119 | } |
103 | } | 120 | } |
@@ -107,17 +124,14 @@ class Http { | @@ -107,17 +124,14 @@ class Http { | ||
107 | return this._requestFromAPI(options, true, reqId); | 124 | return this._requestFromAPI(options, true, reqId); |
108 | } | 125 | } |
109 | }).catch(() => { | 126 | }).catch(() => { |
110 | - log.error(slave ? 'get slave cache fail' : 'get master cache fail'); | 127 | + log.error(slave ? SLAVE_CACHE_FAIL : MASTER_CACHE_FAIL); |
111 | 128 | ||
112 | // 读取缓存失败,并且不是二级缓存的时候,调用 API | 129 | // 读取缓存失败,并且不是二级缓存的时候,调用 API |
113 | if (!slave) { | 130 | if (!slave) { |
114 | return this._requestFromAPI(options, true, reqId); | 131 | return this._requestFromAPI(options, true, reqId); |
115 | } | 132 | } |
116 | 133 | ||
117 | - return Promise.resolve({ | ||
118 | - code: 500, | ||
119 | - message: '接口调用失败' | ||
120 | - }); | 134 | + return Promise.resolve(API_CALL_FAIL); |
121 | }); | 135 | }); |
122 | } | 136 | } |
123 | 137 | ||
@@ -133,6 +147,7 @@ class Http { | @@ -133,6 +147,7 @@ class Http { | ||
133 | url: `${this.ApiUrl}${url}`, | 147 | url: `${this.ApiUrl}${url}`, |
134 | qs: data, | 148 | qs: data, |
135 | json: true, | 149 | json: true, |
150 | + gzip: true, | ||
136 | timeout: 3000 | 151 | timeout: 3000 |
137 | }; | 152 | }; |
138 | 153 | ||
@@ -165,7 +180,7 @@ class Http { | @@ -165,7 +180,7 @@ class Http { | ||
165 | if (_.isArray(list)) { | 180 | if (_.isArray(list)) { |
166 | return Promise.all(list); | 181 | return Promise.all(list); |
167 | } else { | 182 | } else { |
168 | - return Promise.reject(Error('the parameters of api all method should be Array!')); | 183 | + return Promise.reject(Error(API_ALL_METHOD_ERROR)); |
169 | } | 184 | } |
170 | } | 185 | } |
171 | } | 186 | } |
@@ -182,5 +197,12 @@ class ServiceAPI extends Http { | @@ -182,5 +197,12 @@ class ServiceAPI extends Http { | ||
182 | } | 197 | } |
183 | } | 198 | } |
184 | 199 | ||
200 | +class SearchAPI extends Http { | ||
201 | + constructor() { | ||
202 | + super(searchApi); | ||
203 | + } | ||
204 | +} | ||
205 | + | ||
185 | exports.API = API; | 206 | exports.API = API; |
186 | exports.ServiceAPI = ServiceAPI; | 207 | exports.ServiceAPI = ServiceAPI; |
208 | +exports.SearchAPI = SearchAPI; |
-
Please register or login to post a comment