Authored by ccbikai

修改未ES6代码

... ... @@ -5,7 +5,6 @@
*/
'use strict';
const rp = require('request-promise');
const qs = require('querystring');
const md5 = require('md5');
... ... @@ -36,16 +35,16 @@ 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) {
if (!result || !result.code) {
log.error('error: 接口返回的数据结构错误,非 JSON');
return Promise.reject({
statusCode: 500,
... ... @@ -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;
... ...
... ... @@ -11,11 +11,11 @@ const config = require('../config/common');
/**
* 七牛图片路径处理
* @param {[tring]} url
* @param {[tring]} width
* @param {[tring]} height
* @param {[tring]} mode
* @return {[tring]}
* @param {[string]} url
* @param {[string]} width
* @param {[string]} height
* @param {[string]} mode
* @return {[string]}
*/
exports.image = (url, width, height, mode) => {
mode = _.isNumber(mode) ? mode : 2;
... ... @@ -24,6 +24,21 @@ exports.image = (url, width, height, mode) => {
};
/**
* 条件判断
* @param {[string]} v1
* @param {[string]} v2
* @param {[object]} options 上下文环境,一般不手动传
* @return {[boolen]}
*/
exports.ifEqualTo = (v1, v2, _options) => {
if (v1 === v2) {
return _options.fn(this); // eslint-disable-line
}
return _options.inverse(this); // eslint-disable-line
};
/**
* 站内地址格式化
* @param {[string]} uri 路径
* @param {[object]} qs 查询字符串
... ... @@ -34,10 +49,10 @@ exports.urlFormat = (uri, qs, module) => {
const subDomain = '.m.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
};
... ... @@ -47,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;
... ... @@ -91,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);
}
... ... @@ -126,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();
... ... @@ -50,11 +50,12 @@ const makeSign = argument => {
// 生成API签名,调用后端接口的时候有私钥校验
exports.apiSign = (params) => {
const clientType = params.client_type || 'h5';
/* eslint-disable */
let sign = packageSort(Object.assign({
client_type: clientType,
private_key: privateKey[clientType],
app_version: '4.3.0',
app_version: '3.8.2',
os_version: 'yohobuy:h5',
screen_size: '720x1280',
v: '7'
... ... @@ -70,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 {
... ...