Authored by biao

Merge branch 'develop' into feature/index-girls

/**
* 对象键名驼峰化
* @author: Bi Kai<kai.bi@yoho.cn>
* @date: 2016/05/09
*/
'use strict';
const _ = require('lodash');
let camelCase,
camelCaseObject,
camelCaseArray;
camelCaseObject = (obj) => {
_.forEach(Object.keys(obj), (k) => {
obj[k] = camelCase(obj[k]);
obj[_.camelCase(k)] = obj[k];
});
return obj;
};
camelCaseArray = (list) => {
_.forEach(list, (k) => {
k = camelCase(k);
});
return list;
};
camelCase = (data) => {
if (_.isObject(data)) {
data = camelCaseObject(data);
}
if (_.isArray(data)) {
data = camelCaseArray(data);
}
return data;
};
module.exports = camelCase;
... ...
/*
* @Author: Targaryen
* @Date: 2016-05-10 10:11:34
* @Last Modified by: Targaryen
* @Last Modified time: 2016-05-12 11:25:53
/**
* Handlebars helpers
* bikai kai.bi@yoho.cn
* 2016-05-10
*/
/* -----------------------------------------
* 实现 PHP 的 Helpers 函数
* -----------------------------------------
*/
'use strict';
const SUB_DOMAIN = '.yohobuy.com';
const SITE_MAIN = 'http://www.yohobuy.com';
const querystring = require('querystring');
const _ = require('lodash');
const config = require('../config/common');
class HELPERS {
/**
* 构建网站的URL
* @param uri String
* @param param Array
* @param module String
*/
url(uri, param, module) {
let url = '';
if (module === null) {
module = 'index';
}
switch (module) {
case 'default':
url = 'http://m.yohobuy.com';
break;
case 'guang':
url = 'http://guang' + SUB_DOMAIN;
break;
case 'list':
url = 'http://list' + SUB_DOMAIN;
break;
case 'search':
url = 'http://search' + SUB_DOMAIN;
break;
case 'index':
url = SITE_MAIN;
break;
case '':
break;
case undefined:
break;
default:
url = 'http://' + module + SUB_DOMAIN;
break;
}
/**
* 七牛图片路径处理
* @param {[tring]} url
* @param {[tring]} width
* @param {[tring]} height
* @param {[tring]} mode
* @return {[tring]}
*/
exports.image = (url, width, height, mode) => {
mode = _.isNumber(mode) ? mode : 2;
url = url || '';
return url.replace(/{width}/g, width).replace(/{height}/g, height).replace(/{mode}/g, mode);
};
url += uri;
if (param !== null && param !== undefined) {
url += '?';
_.forEach(param, function(value, key) {
url += (key + '=' + value + '&');
});
}
if (url.substr(-1, 1) === '&') {
return url.substr(0, url.length - 1);
} else {
return url;
}
/**
* 站内地址格式化
* @param {[string]} uri 路径
* @param {[object]} qs 查询字符串
* @param {[string]} module 模块
* @return {[string]}
*/
exports.url = (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,
activity: '//activity.yohobuy.com',
index: config.siteUrl
};
let url;
module = module || 'default';
if (subName[module]) {
url = subName[module];
} else {
url = '//' + module + subDomain; // 规则没匹配到就把模块当作子域名
}
/**
* 根据尺寸获得图片url
* @param uri String
* @param param Array
* @param module String
*/
getImageUrl(url, width, height, mode) {
if (mode === null) {
mode = 2;
}
return url.replace(/{width}/g, width)
.replace(/{height}/g, height)
.replace(/{mode}/g, mode);
url += uri;
if (qs) {
url += '?' + querystring.stringify(qs);
}
}
module.exports = HELPERS;
return url;
};
/**
* 大写转小写处理
* @param {[string]} str 转换字符
*/
exports.lowerCase = (str) => {
str = str || '';
return str.toLowerCase();
};
/**
* 小写转大写处理
* @param {[string]} str 转换字符
*/
exports.upperCase = (str) => {
str = str || '';
return str.toUpperCase();
};
... ...
... ... @@ -6,7 +6,7 @@
'use strict';
const _ = require('lodash');
const qs = require('querystring');
const md5 = require('md5');
const privateKey = {
... ... @@ -24,7 +24,7 @@ const privateKey = {
* @return {Object} 排序之后的参数对象
*/
const packageSort = argument => {
var newObj = {};
let newObj = {};
for (let k of Object.keys(argument).sort()) {
newObj[k] = argument[k];
... ... @@ -39,41 +39,32 @@ const packageSort = argument => {
* @return {string} 生成的签名字符串
*/
const makeSign = argument => {
var qs = [];
_.forEach(argument, function(value, key) {
qs.push(key + '=' + _.trim(value));
});
return md5(qs.join('&')).toLowerCase();
return md5(qs.stringify(argument)).toLowerCase();
};
// 生成API签名,调用后端接口的时候有私钥校验
exports.apiSign = (params) => {
var clientType = params.client_type || 'h5';
/* eslint-disable */
var sign = packageSort(Object.assign({
client_type: clientType,
private_key: privateKey[clientType],
client_type: 'h5',
private_key: privateKey.h5,
app_version: '3.8.2',
os_version: 'yohobuy:h5',
screen_size: '720x1280',
v: '7'
}, params));
/* eslint-enable */
return Object.assign(sign, {
sign = Object.assign(sign, {
client_secret: makeSign(sign) // eslint-disable-line camelcase
});
delete sign.private_key;
return sign;
};
// 检查签名,APP 访问 H5 页面的时候需要检查
exports.checkSign = (params) => {
var clientSecret = params.client_secret, // eslint-disable-line camelcase
let clientSecret = params.client_secret, // eslint-disable-line camelcase
sortedParams;
// 忽略部分参数
... ... @@ -90,7 +81,7 @@ exports.checkSign = (params) => {
// 检查签名,APP 访问 H5 页面的时候需要检查, 有可能不同于上边的签名方式
exports.webSign = (params) => {
var webPrivateKey = 'yohobuyapp';
const webPrivateKey = 'yohobuyapp';
return params.key === md5(md5(webPrivateKey) + params.uid);
};
... ...
... ... @@ -34,12 +34,11 @@
"request-promise": "^3.0.0",
"serve-favicon": "^2.3.0",
"winston": "^2.2.0",
"winston-daily-rotate-file": "^1.0.1",
"yoho-handlebars": "0.0.1"
"winston-daily-rotate-file": "^1.0.1"
},
"devDependencies": {
"autoprefixer": "^6.3.6",
"eslint": "^2.9.0",
"eslint": "^2.10.2",
"eslint-config-yoho": "^1.0.1",
"gulp": "^3.9.1",
"gulp-cssnano": "^2.1.2",
... ... @@ -60,14 +59,17 @@
"postcss-position": "^0.4.0",
"postcss-short": "^1.4.0",
"postcss-sprites": "^3.1.2",
"postcss-use": "^2.0.2",
"postcss-use": "^2.1.0",
"precss": "^1.4.0",
"rewire": "^2.5.1",
"shelljs": "^0.7.0",
"stylelint": "^6.3.3",
"stylelint-config-yoho": "^1.2.2",
"stylelint-config-yoho": "^1.2.3",
"webpack": "^1.13.0",
"webpack-dev-server": "^1.14.1",
"webpack-stream": "^3.1.0"
"webpack-stream": "^3.1.0",
"yoho-handlebars": "^4.0.5",
"yoho-jquery": "^1.9.1",
"yoho-jquery-lazyload": "^1.9.7"
}
}
... ...