Authored by 毕凯

修改未ES6代码

... ... @@ -5,7 +5,6 @@
*/
'use strict';
const rp = require('request-promise');
const qs = require('querystring');
const md5 = require('md5');
... ... @@ -36,13 +35,13 @@ class Http {
* 调用接口
*/
_requestFromAPI(options, cacheOption, reqId) {
let timer = new Timer();
let method = options.method || 'get';
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) => {
let duration = timer.put('getApi');// 统计时间结束
const duration = timer.put('getApi');// 统计时间结束
// 数据校验
if (!result || !result.code) {
... ... @@ -55,7 +54,7 @@ class Http {
// 写缓存, 否则返回 Slave 缓存服务器的数据
if (config.useCache && cacheOption) {
let cacheTime = _.isNumber(cacheOption) ? cacheOption : 60;
const cacheTime = _.isNumber(cacheOption) ? cacheOption : 60;
reqId = reqId || this._getReqId(options);
cache.set(`apiCache:${reqId}`, result, cacheTime);
... ... @@ -65,7 +64,7 @@ class Http {
log.info(`get api success: use: ${duration}ms`);
return result;
}).catch((err)=> {
let duration = timer.put('getApi');// 统计时间结束
const duration = timer.put('getApi');// 统计时间结束
log.error(`${method} api fail: use: ${duration}ms, code:${err.statusCode}, error: ${err.message}`);
log.error(`API: ${options.url}?${qs.stringify(options.qs)}`);
... ... @@ -88,8 +87,8 @@ class Http {
* @return {[type]}
*/
_requestFromCache(options, slave) {
let reqId = this._getReqId(options);
let getCache = slave ? cache.getFromSlave : cache.get;
const reqId = this._getReqId(options);
const getCache = slave ? cache.getFromSlave : cache.get;
log.info(`get cache: ${reqId}, url: ${options.url}?${qs.stringify(options.qs)}`);
return getCache(`apiCache:${reqId}`).then((result) => {
... ... @@ -129,7 +128,7 @@ class Http {
* @return {[type]}
*/
get(url, data, cacheOption) {
let options = {
const options = {
url: `${this.ApiUrl}${url}`,
qs: data,
json: true,
... ... @@ -150,7 +149,7 @@ class Http {
* @param data Obejct
*/
post(url, data) {
let options = {
const options = {
url: `${this.ApiUrl}${url}`,
form: data,
method: 'post',
... ...
... ... @@ -4,7 +4,6 @@
* @author bikai kai.bi@yoho.cn
* @date 2016/05/16
*/
'use strict';
const Promise = require('bluebird');
const Memcached = require('memcached');
... ...
... ... @@ -4,9 +4,9 @@
* @return {[string]}
*/
exports.getUid = (req) => {
var _uid = 0,
cookie = req.cookies._UID,
cookieList;
const cookie = req.cookies._UID;
let _uid = 0;
let cookieList;
if (req.isApp) {
return req.query.uid || 0;
... ...
... ... @@ -49,10 +49,10 @@ exports.urlFormat = (uri, qs, module) => {
const subDomain = '.yohobuy.com';
const subName = {
default: config.siteUrl,
guang: '//guang' + subDomain,
list: '//list' + subDomain,
search: '//search' + subDomain,
huodong: '//huodong' + subDomain,
guang: `//guang${subDomain}`,
list: `//list${subDomain}`,
search: `//search${subDomain}`,
huodong: `//huodong${subDomain}`,
activity: '//activity.yohobuy.com',
index: config.siteUrl
};
... ... @@ -62,12 +62,12 @@ exports.urlFormat = (uri, qs, module) => {
if (subName[module]) {
url = subName[module];
} else {
url = '//' + module + subDomain; // 规则没匹配到就把模块当作子域名
url = `//${module}${subDomain}`; // 规则没匹配到就把模块当作子域名
}
url += uri;
if (qs) {
url += '?' + querystring.stringify(qs);
url += `?${querystring.stringify(qs)}`;
}
return url;
... ... @@ -106,7 +106,7 @@ exports.dateFormat = (format, date) => {
if (date instanceof Date) {
return moment(date).format(format);
} else {
let d = moment.unix(date);
const d = moment.unix(date);
return moment(d).utc().format(format);
}
... ... @@ -141,7 +141,7 @@ exports.dateDiffFormat = (format, diff, type) => {
return '';
} else {
type = type || 'ms';
let m = moment.duration(diff, type);
const m = moment.duration(diff, type);
format.match(/(\{.*?\})/g).forEach((s) => {
format = format.replace(s, m.get(s.substring(1, s.length - 1)));
... ...
/**
* 日志工具类
* @author: hbomb<qiqi.zhou@yoho.cn>
* @date: 2016/05/06
*/
'use strict';
'use strict';
let winston = require('winston'),
config = require('../config/common'),
FileTransport = require('winston-daily-rotate-file');
const winston = require('winston');
const config = require('../config/common');
const FileTransport = require('winston-daily-rotate-file');
require('influxdb-winston');
require('influxdb-winston');
let logger = new (winston.Logger)({
transports: [
new (FileTransport)(config.loggers.infoFile),
new (FileTransport)(config.loggers.errorFile),
new (winston.transports.UdpTransport)(config.loggers.udp),
new (winston.transports.Console)(config.loggers.console)
]
});
const logger = new (winston.Logger)({
transports: [
new (FileTransport)(config.loggers.infoFile),
new (FileTransport)(config.loggers.errorFile),
new (winston.transports.UdpTransport)(config.loggers.udp),
new (winston.transports.Console)(config.loggers.console)
]
});
module.exports = logger;
module.exports = logger;
... ...
... ... @@ -23,9 +23,9 @@ const privateKey = {
* @return {Object} 排序之后的参数对象
*/
const packageSort = argument => {
let newObj = {};
const newObj = {};
for (let k of Object.keys(argument).sort()) {
for (const k of Object.keys(argument).sort()) {
newObj[k] = argument[k];
}
... ... @@ -38,10 +38,10 @@ const packageSort = argument => {
* @return {string} 生成的签名字符串
*/
const makeSign = argument => {
let qs = [];
const qs = [];
_.forEach(argument, function(value, key) {
qs.push(key + '=' + _.trim(value));
_.forEach(argument, (value, key) => {
qs.push(`${key}=${_.trim(value)}`);
});
return md5(qs.join('&')).toLowerCase();
... ... @@ -71,8 +71,10 @@ exports.apiSign = (params) => {
// 检查签名,APP 访问 H5 页面的时候需要检查
exports.checkSign = (params) => {
let clientSecret = params.client_secret, // eslint-disable-line camelcase
sortedParams;
const // eslint-disable-line camelcase
clientSecret = params.client_secret;
let sortedParams;
// 忽略部分参数
delete params.client_secret;
... ...
'use strict';
/**
* 计时类
* @example
... ... @@ -10,7 +8,7 @@
* @author: hbomb<qiqi.zhou@yoho.cn>
* @date: 2016/05/07
*/
'use strict';
class Timer {
constructor() {
this.timers = {};
... ... @@ -20,10 +18,10 @@ class Timer {
* 打点计时
*/
put(label) {
let labelTime = this.timers[label];
const labelTime = this.timers[label];
if (labelTime) {
let duration = process.hrtime(labelTime);
const duration = process.hrtime(labelTime);
return this._round(duration[0], duration[1]);
} else {
... ...
... ... @@ -20,7 +20,7 @@ test('uri format', t => {
let uri = '/test';
let qs = { name: 'yoho' };
let mod = 'list';
let expected = '//list.m.yohobuy.com/test?name=yoho';
let expected = '//list.yohobuy.com/test?name=yoho';
t.is(helpers.urlFormat(uri, qs, mod), expected);
});
... ...