Showing
7 changed files
with
69 additions
and
56 deletions
@@ -5,7 +5,6 @@ | @@ -5,7 +5,6 @@ | ||
5 | */ | 5 | */ |
6 | 6 | ||
7 | 'use strict'; | 7 | 'use strict'; |
8 | - | ||
9 | const rp = require('request-promise'); | 8 | const rp = require('request-promise'); |
10 | const qs = require('querystring'); | 9 | const qs = require('querystring'); |
11 | const md5 = require('md5'); | 10 | const md5 = require('md5'); |
@@ -36,16 +35,16 @@ class Http { | @@ -36,16 +35,16 @@ class Http { | ||
36 | * 调用接口 | 35 | * 调用接口 |
37 | */ | 36 | */ |
38 | _requestFromAPI(options, cacheOption, reqId) { | 37 | _requestFromAPI(options, cacheOption, reqId) { |
39 | - let timer = new Timer(); | ||
40 | - let method = options.method || 'get'; | 38 | + const timer = new Timer(); |
39 | + const method = options.method || 'get'; | ||
41 | 40 | ||
42 | log.info(`${method} api: ${options.url}?${qs.stringify(options.qs)}`); | 41 | log.info(`${method} api: ${options.url}?${qs.stringify(options.qs)}`); |
43 | timer.put('getApi');// 统计时间开始 | 42 | timer.put('getApi');// 统计时间开始 |
44 | return rp(options).then((result) => { | 43 | return rp(options).then((result) => { |
45 | - let duration = timer.put('getApi');// 统计时间结束 | 44 | + const duration = timer.put('getApi');// 统计时间结束 |
46 | 45 | ||
47 | // 数据校验 | 46 | // 数据校验 |
48 | - if (!result) { | 47 | + if (!result || !result.code) { |
49 | log.error('error: 接口返回的数据结构错误,非 JSON'); | 48 | log.error('error: 接口返回的数据结构错误,非 JSON'); |
50 | return Promise.reject({ | 49 | return Promise.reject({ |
51 | statusCode: 500, | 50 | statusCode: 500, |
@@ -55,7 +54,7 @@ class Http { | @@ -55,7 +54,7 @@ class Http { | ||
55 | 54 | ||
56 | // 写缓存, 否则返回 Slave 缓存服务器的数据 | 55 | // 写缓存, 否则返回 Slave 缓存服务器的数据 |
57 | if (config.useCache && cacheOption) { | 56 | if (config.useCache && cacheOption) { |
58 | - let cacheTime = _.isNumber(cacheOption) ? cacheOption : 60; | 57 | + const cacheTime = _.isNumber(cacheOption) ? cacheOption : 60; |
59 | 58 | ||
60 | reqId = reqId || this._getReqId(options); | 59 | reqId = reqId || this._getReqId(options); |
61 | cache.set(`apiCache:${reqId}`, result, cacheTime); | 60 | cache.set(`apiCache:${reqId}`, result, cacheTime); |
@@ -65,7 +64,7 @@ class Http { | @@ -65,7 +64,7 @@ class Http { | ||
65 | log.info(`get api success: use: ${duration}ms`); | 64 | log.info(`get api success: use: ${duration}ms`); |
66 | return result; | 65 | return result; |
67 | }).catch((err)=> { | 66 | }).catch((err)=> { |
68 | - let duration = timer.put('getApi');// 统计时间结束 | 67 | + const duration = timer.put('getApi');// 统计时间结束 |
69 | 68 | ||
70 | log.error(`${method} api fail: use: ${duration}ms, code:${err.statusCode}, error: ${err.message}`); | 69 | log.error(`${method} api fail: use: ${duration}ms, code:${err.statusCode}, error: ${err.message}`); |
71 | log.error(`API: ${options.url}?${qs.stringify(options.qs)}`); | 70 | log.error(`API: ${options.url}?${qs.stringify(options.qs)}`); |
@@ -88,8 +87,8 @@ class Http { | @@ -88,8 +87,8 @@ class Http { | ||
88 | * @return {[type]} | 87 | * @return {[type]} |
89 | */ | 88 | */ |
90 | _requestFromCache(options, slave) { | 89 | _requestFromCache(options, slave) { |
91 | - let reqId = this._getReqId(options); | ||
92 | - let getCache = slave ? cache.getFromSlave : cache.get; | 90 | + const reqId = this._getReqId(options); |
91 | + const getCache = slave ? cache.getFromSlave : cache.get; | ||
93 | 92 | ||
94 | log.info(`get cache: ${reqId}, url: ${options.url}?${qs.stringify(options.qs)}`); | 93 | log.info(`get cache: ${reqId}, url: ${options.url}?${qs.stringify(options.qs)}`); |
95 | return getCache(`apiCache:${reqId}`).then((result) => { | 94 | return getCache(`apiCache:${reqId}`).then((result) => { |
@@ -129,7 +128,7 @@ class Http { | @@ -129,7 +128,7 @@ class Http { | ||
129 | * @return {[type]} | 128 | * @return {[type]} |
130 | */ | 129 | */ |
131 | get(url, data, cacheOption) { | 130 | get(url, data, cacheOption) { |
132 | - let options = { | 131 | + const options = { |
133 | url: `${this.ApiUrl}${url}`, | 132 | url: `${this.ApiUrl}${url}`, |
134 | qs: data, | 133 | qs: data, |
135 | json: true, | 134 | json: true, |
@@ -150,7 +149,7 @@ class Http { | @@ -150,7 +149,7 @@ class Http { | ||
150 | * @param data Obejct | 149 | * @param data Obejct |
151 | */ | 150 | */ |
152 | post(url, data) { | 151 | post(url, data) { |
153 | - let options = { | 152 | + const options = { |
154 | url: `${this.ApiUrl}${url}`, | 153 | url: `${this.ApiUrl}${url}`, |
155 | form: data, | 154 | form: data, |
156 | method: 'post', | 155 | method: 'post', |
@@ -4,7 +4,6 @@ | @@ -4,7 +4,6 @@ | ||
4 | * @author bikai kai.bi@yoho.cn | 4 | * @author bikai kai.bi@yoho.cn |
5 | * @date 2016/05/16 | 5 | * @date 2016/05/16 |
6 | */ | 6 | */ |
7 | - | ||
8 | 'use strict'; | 7 | 'use strict'; |
9 | const Promise = require('bluebird'); | 8 | const Promise = require('bluebird'); |
10 | const Memcached = require('memcached'); | 9 | const Memcached = require('memcached'); |
@@ -4,9 +4,9 @@ | @@ -4,9 +4,9 @@ | ||
4 | * @return {[string]} | 4 | * @return {[string]} |
5 | */ | 5 | */ |
6 | exports.getUid = (req) => { | 6 | exports.getUid = (req) => { |
7 | - var _uid = 0, | ||
8 | - cookie = req.cookies._UID, | ||
9 | - cookieList; | 7 | + const cookie = req.cookies._UID; |
8 | + let _uid = 0; | ||
9 | + let cookieList; | ||
10 | 10 | ||
11 | if (req.isApp) { | 11 | if (req.isApp) { |
12 | return req.query.uid || 0; | 12 | return req.query.uid || 0; |
@@ -11,11 +11,11 @@ const config = require('../config/common'); | @@ -11,11 +11,11 @@ const config = require('../config/common'); | ||
11 | 11 | ||
12 | /** | 12 | /** |
13 | * 七牛图片路径处理 | 13 | * 七牛图片路径处理 |
14 | - * @param {[tring]} url | ||
15 | - * @param {[tring]} width | ||
16 | - * @param {[tring]} height | ||
17 | - * @param {[tring]} mode | ||
18 | - * @return {[tring]} | 14 | + * @param {[string]} url |
15 | + * @param {[string]} width | ||
16 | + * @param {[string]} height | ||
17 | + * @param {[string]} mode | ||
18 | + * @return {[string]} | ||
19 | */ | 19 | */ |
20 | exports.image = (url, width, height, mode) => { | 20 | exports.image = (url, width, height, mode) => { |
21 | mode = _.isNumber(mode) ? mode : 2; | 21 | mode = _.isNumber(mode) ? mode : 2; |
@@ -24,6 +24,21 @@ exports.image = (url, width, height, mode) => { | @@ -24,6 +24,21 @@ exports.image = (url, width, height, mode) => { | ||
24 | }; | 24 | }; |
25 | 25 | ||
26 | /** | 26 | /** |
27 | + * 条件判断 | ||
28 | + * @param {[string]} v1 | ||
29 | + * @param {[string]} v2 | ||
30 | + * @param {[object]} options 上下文环境,一般不手动传 | ||
31 | + * @return {[boolen]} | ||
32 | + */ | ||
33 | +exports.ifEqualTo = (v1, v2, _options) => { | ||
34 | + if (v1 === v2) { | ||
35 | + return _options.fn(this); // eslint-disable-line | ||
36 | + } | ||
37 | + | ||
38 | + return _options.inverse(this); // eslint-disable-line | ||
39 | +}; | ||
40 | + | ||
41 | +/** | ||
27 | * 站内地址格式化 | 42 | * 站内地址格式化 |
28 | * @param {[string]} uri 路径 | 43 | * @param {[string]} uri 路径 |
29 | * @param {[object]} qs 查询字符串 | 44 | * @param {[object]} qs 查询字符串 |
@@ -34,10 +49,10 @@ exports.urlFormat = (uri, qs, module) => { | @@ -34,10 +49,10 @@ exports.urlFormat = (uri, qs, module) => { | ||
34 | const subDomain = '.m.yohobuy.com'; | 49 | const subDomain = '.m.yohobuy.com'; |
35 | const subName = { | 50 | const subName = { |
36 | default: config.siteUrl, | 51 | default: config.siteUrl, |
37 | - guang: '//guang' + subDomain, | ||
38 | - list: '//list' + subDomain, | ||
39 | - search: '//search' + subDomain, | ||
40 | - huodong: '//huodong' + subDomain, | 52 | + guang: `//guang${subDomain}`, |
53 | + list: `//list${subDomain}`, | ||
54 | + search: `//search${subDomain}`, | ||
55 | + huodong: `//huodong${subDomain}`, | ||
41 | activity: '//activity.yohobuy.com', | 56 | activity: '//activity.yohobuy.com', |
42 | index: config.siteUrl | 57 | index: config.siteUrl |
43 | }; | 58 | }; |
@@ -47,12 +62,12 @@ exports.urlFormat = (uri, qs, module) => { | @@ -47,12 +62,12 @@ exports.urlFormat = (uri, qs, module) => { | ||
47 | if (subName[module]) { | 62 | if (subName[module]) { |
48 | url = subName[module]; | 63 | url = subName[module]; |
49 | } else { | 64 | } else { |
50 | - url = '//' + module + subDomain; // 规则没匹配到就把模块当作子域名 | 65 | + url = `//${module}${subDomain}`; // 规则没匹配到就把模块当作子域名 |
51 | } | 66 | } |
52 | 67 | ||
53 | url += uri; | 68 | url += uri; |
54 | if (qs) { | 69 | if (qs) { |
55 | - url += '?' + querystring.stringify(qs); | 70 | + url += `?${querystring.stringify(qs)}`; |
56 | } | 71 | } |
57 | 72 | ||
58 | return url; | 73 | return url; |
@@ -91,7 +106,7 @@ exports.dateFormat = (format, date) => { | @@ -91,7 +106,7 @@ exports.dateFormat = (format, date) => { | ||
91 | if (date instanceof Date) { | 106 | if (date instanceof Date) { |
92 | return moment(date).format(format); | 107 | return moment(date).format(format); |
93 | } else { | 108 | } else { |
94 | - let d = moment.unix(date); | 109 | + const d = moment.unix(date); |
95 | 110 | ||
96 | return moment(d).utc().format(format); | 111 | return moment(d).utc().format(format); |
97 | } | 112 | } |
@@ -126,7 +141,7 @@ exports.dateDiffFormat = (format, diff, type) => { | @@ -126,7 +141,7 @@ exports.dateDiffFormat = (format, diff, type) => { | ||
126 | return ''; | 141 | return ''; |
127 | } else { | 142 | } else { |
128 | type = type || 'ms'; | 143 | type = type || 'ms'; |
129 | - let m = moment.duration(diff, type); | 144 | + const m = moment.duration(diff, type); |
130 | 145 | ||
131 | format.match(/(\{.*?\})/g).forEach((s) => { | 146 | format.match(/(\{.*?\})/g).forEach((s) => { |
132 | format = format.replace(s, m.get(s.substring(1, s.length - 1))); | 147 | format = format.replace(s, m.get(s.substring(1, s.length - 1))); |
1 | - | ||
2 | /** | 1 | /** |
3 | * 日志工具类 | 2 | * 日志工具类 |
4 | * @author: hbomb<qiqi.zhou@yoho.cn> | 3 | * @author: hbomb<qiqi.zhou@yoho.cn> |
5 | * @date: 2016/05/06 | 4 | * @date: 2016/05/06 |
6 | */ | 5 | */ |
7 | - 'use strict'; | 6 | +'use strict'; |
8 | 7 | ||
9 | - let winston = require('winston'), | ||
10 | - config = require('../config/common'), | ||
11 | - FileTransport = require('winston-daily-rotate-file'); | 8 | +const winston = require('winston'); |
9 | +const config = require('../config/common'); | ||
10 | +const FileTransport = require('winston-daily-rotate-file'); | ||
12 | 11 | ||
13 | - require('influxdb-winston'); | 12 | +require('influxdb-winston'); |
14 | 13 | ||
15 | - let logger = new (winston.Logger)({ | ||
16 | - transports: [ | ||
17 | - new (FileTransport)(config.loggers.infoFile), | ||
18 | - new (FileTransport)(config.loggers.errorFile), | ||
19 | - new (winston.transports.UdpTransport)(config.loggers.udp), | ||
20 | - new (winston.transports.Console)(config.loggers.console) | ||
21 | - ] | ||
22 | - }); | 14 | +const logger = new (winston.Logger)({ |
15 | + transports: [ | ||
16 | + new (FileTransport)(config.loggers.infoFile), | ||
17 | + new (FileTransport)(config.loggers.errorFile), | ||
18 | + new (winston.transports.UdpTransport)(config.loggers.udp), | ||
19 | + new (winston.transports.Console)(config.loggers.console) | ||
20 | + ] | ||
21 | +}); | ||
23 | 22 | ||
24 | - module.exports = logger; | 23 | +module.exports = logger; |
@@ -23,9 +23,9 @@ const privateKey = { | @@ -23,9 +23,9 @@ const privateKey = { | ||
23 | * @return {Object} 排序之后的参数对象 | 23 | * @return {Object} 排序之后的参数对象 |
24 | */ | 24 | */ |
25 | const packageSort = argument => { | 25 | const packageSort = argument => { |
26 | - let newObj = {}; | 26 | + const newObj = {}; |
27 | 27 | ||
28 | - for (let k of Object.keys(argument).sort()) { | 28 | + for (const k of Object.keys(argument).sort()) { |
29 | newObj[k] = argument[k]; | 29 | newObj[k] = argument[k]; |
30 | } | 30 | } |
31 | 31 | ||
@@ -38,10 +38,10 @@ const packageSort = argument => { | @@ -38,10 +38,10 @@ const packageSort = argument => { | ||
38 | * @return {string} 生成的签名字符串 | 38 | * @return {string} 生成的签名字符串 |
39 | */ | 39 | */ |
40 | const makeSign = argument => { | 40 | const makeSign = argument => { |
41 | - let qs = []; | 41 | + const qs = []; |
42 | 42 | ||
43 | - _.forEach(argument, function(value, key) { | ||
44 | - qs.push(key + '=' + _.trim(value)); | 43 | + _.forEach(argument, (value, key) => { |
44 | + qs.push(`${key}=${_.trim(value)}`); | ||
45 | }); | 45 | }); |
46 | 46 | ||
47 | return md5(qs.join('&')).toLowerCase(); | 47 | return md5(qs.join('&')).toLowerCase(); |
@@ -50,11 +50,12 @@ const makeSign = argument => { | @@ -50,11 +50,12 @@ const makeSign = argument => { | ||
50 | // 生成API签名,调用后端接口的时候有私钥校验 | 50 | // 生成API签名,调用后端接口的时候有私钥校验 |
51 | exports.apiSign = (params) => { | 51 | exports.apiSign = (params) => { |
52 | const clientType = params.client_type || 'h5'; | 52 | const clientType = params.client_type || 'h5'; |
53 | + | ||
53 | /* eslint-disable */ | 54 | /* eslint-disable */ |
54 | let sign = packageSort(Object.assign({ | 55 | let sign = packageSort(Object.assign({ |
55 | client_type: clientType, | 56 | client_type: clientType, |
56 | private_key: privateKey[clientType], | 57 | private_key: privateKey[clientType], |
57 | - app_version: '4.3.0', | 58 | + app_version: '3.8.2', |
58 | os_version: 'yohobuy:h5', | 59 | os_version: 'yohobuy:h5', |
59 | screen_size: '720x1280', | 60 | screen_size: '720x1280', |
60 | v: '7' | 61 | v: '7' |
@@ -70,8 +71,10 @@ exports.apiSign = (params) => { | @@ -70,8 +71,10 @@ exports.apiSign = (params) => { | ||
70 | 71 | ||
71 | // 检查签名,APP 访问 H5 页面的时候需要检查 | 72 | // 检查签名,APP 访问 H5 页面的时候需要检查 |
72 | exports.checkSign = (params) => { | 73 | exports.checkSign = (params) => { |
73 | - let clientSecret = params.client_secret, // eslint-disable-line camelcase | ||
74 | - sortedParams; | 74 | + const // eslint-disable-line camelcase |
75 | + clientSecret = params.client_secret; | ||
76 | + | ||
77 | + let sortedParams; | ||
75 | 78 | ||
76 | // 忽略部分参数 | 79 | // 忽略部分参数 |
77 | delete params.client_secret; | 80 | delete params.client_secret; |
1 | -'use strict'; | ||
2 | - | ||
3 | /** | 1 | /** |
4 | * 计时类 | 2 | * 计时类 |
5 | * @example | 3 | * @example |
@@ -10,7 +8,7 @@ | @@ -10,7 +8,7 @@ | ||
10 | * @author: hbomb<qiqi.zhou@yoho.cn> | 8 | * @author: hbomb<qiqi.zhou@yoho.cn> |
11 | * @date: 2016/05/07 | 9 | * @date: 2016/05/07 |
12 | */ | 10 | */ |
13 | - | 11 | +'use strict'; |
14 | class Timer { | 12 | class Timer { |
15 | constructor() { | 13 | constructor() { |
16 | this.timers = {}; | 14 | this.timers = {}; |
@@ -20,10 +18,10 @@ class Timer { | @@ -20,10 +18,10 @@ class Timer { | ||
20 | * 打点计时 | 18 | * 打点计时 |
21 | */ | 19 | */ |
22 | put(label) { | 20 | put(label) { |
23 | - let labelTime = this.timers[label]; | 21 | + const labelTime = this.timers[label]; |
24 | 22 | ||
25 | if (labelTime) { | 23 | if (labelTime) { |
26 | - let duration = process.hrtime(labelTime); | 24 | + const duration = process.hrtime(labelTime); |
27 | 25 | ||
28 | return this._round(duration[0], duration[1]); | 26 | return this._round(duration[0], duration[1]); |
29 | } else { | 27 | } else { |
-
Please register or login to post a comment