Authored by ccbikai

Merge branch 'develop' into feature/sale

{
"extends": "yoho",
"rules": {
"no-unused-vars": [
"error",
{
"args": "after-used",
"vars": "all"
}
]
"parserOptions": {
"sourceType": "module"
}
}
... ...
... ... @@ -12,22 +12,26 @@ if (config.useOneapm) {
require('oneapm');
}
let express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
favicon = require('serve-favicon'),
pkg = require('./package.json');
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const favicon = require('serve-favicon');
const session = require('express-session');
const memcached = require('connect-memcached');
const pkg = require('./package.json');
require('express-handlebars');
const app = express();
const MemcachedStore = memcached(session);
let app = express();
// 向模板注入变量
app.locals.devEnv = app.get('env') === 'development';
app.locals.version = pkg.version;
// 指定libray目录
global.library = path.resolve('./library');
app.set('view engine', '.hbs');
app.use(favicon(path.join(__dirname, '/public/favicon.ico')));
... ... @@ -35,11 +39,22 @@ app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(session({
secret: '3e5fec7deca0b8305cefe2ad9d90ff5e',
name: 'PHPSESSID',
prefix: 'yohobuy',
proxy: true,
resave: true,
saveUninitialized: true,
store: new MemcachedStore({
hosts: config.memcache.session
})
}));
// dispatcher
require('./dispatch')(app);
// listener
app.listen(6001, function() {
app.listen(config.port, function() {
console.log('yohobuy start');
});
... ...
... ... @@ -6,9 +6,9 @@
'use strict';
const _ = require('lodash');
const channelModel = require('../models/channel');
const helpers = require('../../../library/helpers');
const log = require('../../../library/logger');
const cookie = require('../../../library/cookie');
const helpers = require(`${global.library}/helpers`);
const log = require(`${global.library}/logger`);
const cookie = require(`${global.library}/cookie`);
const renderData = {
module: 'channel',
... ...
... ... @@ -24,7 +24,7 @@ app.engine('.hbs', hbs({
defaultLayout: 'layout',
layoutsDir: doraemon,
partialsDir: [path.join(__dirname, 'views/partial'), `${doraemon}/partial`],
helpers: require('../../library/helpers')
helpers: require(`${global.library}/helpers`)
}));
// router
... ...
... ... @@ -5,11 +5,11 @@
*/
'use strict';
const _ = require('lodash');
const API = require('../../../library/api').ServiceAPI;
const sign = require('../../../library/sign');
const camelCase = require('../../../library/camel-case');
const ServiceAPI = require(`${global.library}/api`).ServiceAPI;
const sign = require(`${global.library}/sign`);
const camelCase = require(`${global.library}/camel-case`);
const api = new API();
const api = new ServiceAPI();
const genderData = {
boys: '1,3',
... ...
... ... @@ -19,7 +19,7 @@ exports.index = (req, res) => {
id: req.params.id,
uid: uid,
vipLevel: vipLevel,
ua: req.get('user-agent') ||  ''
ua: req.get('user-agent') || ''
}).then((result) => {
res.render('detail', {
resultShow: JSON.stringify(result, null, 4),
... ...
... ... @@ -10,12 +10,14 @@ const isProduction = process.env.NODE_ENV === 'production';
const isTest = process.env.NODE_ENV === 'test';
module.exports = {
port: 6001,
siteUrl: 'http://m.yohobuy.com',
domains: {
api: 'http://testapi.yoho.cn:28078/', // http://devapi.yoho.cn:58078/ http://testapi.yoho.cn:28078/
service: 'http://devapi.yoho.cn:58078/'
service: 'http://testservice.yoho.cn:28077/'
},
useCache: true,
useOneapm: false,
useCache: false,
memcache: {
master: ['192.168.102.168:12580'],
slave: ['192.168.102.168:12580'],
... ... @@ -26,12 +28,12 @@ module.exports = {
infoFile: {
name: 'info',
level: 'info',
filename: 'info.log'
filename: 'log/info.log'
},
errorFile: {
name: 'error',
level: 'error',
filename: 'error.log',
filename: 'log/error.log',
handleExceptions: true
},
udp: { // send by udp
... ...
... ... @@ -19,12 +19,10 @@ const serviceApi = config.domains.service;
const searchApi = config.domains.search;
let ApiUrl;
class Http {
class API {
constructor() {
ApiUrl = api;
constructor(baseUrl) {
this.ApiUrl = baseUrl;
}
/**
... ... @@ -39,10 +37,11 @@ class API {
*/
_requestFromAPI(options, cacheOption, reqId) {
let timer = new Timer();
let method = options.method || 'get';
timer.put('getApi');// 统计时间开始
log.info(`get api: ${options.url}?${qs.stringify(options.qs)}`);
log.info(`${method} api: ${options.url}?${qs.stringify(options.qs)}`);
return rp(options).then((result) => {
let duration = timer.put('getApi');// 统计时间结束
... ... @@ -61,10 +60,11 @@ class API {
}
}
return result;
}).catch((error)=>{
}).catch((error)=> {
let duration = timer.put('getApi');// 统计时间结束
log.error(`get api fail: use: ${duration}ms, statusCode: ${error.statusCode}, error: ${error.message}`);
log.error(`${method} api fail: use: ${duration}ms, statusCode:
${error.statusCode}, error: ${error.message}`);
// 使用缓存的时候,读取二级缓存
if (config.useCache) {
... ... @@ -117,12 +117,14 @@ class API {
*/
get(url, data, cacheOption) {
let options = {
url: `${ApiUrl}${url}`,
url: `${this.ApiUrl}${url}`,
qs: data,
json: true,
timeout: 3000
};
console.log('in api : ' + config.useCache);
// 从缓存获取数据
if (config.useCache && cacheOption) {
return this._requestFromCache(options);
... ... @@ -138,7 +140,7 @@ class API {
*/
post(url, data) {
let options = {
url: `${ApiUrl}${url}`,
url: `${this.ApiUrl}${url}`,
form: data,
method: 'post',
json: true,
... ... @@ -151,22 +153,27 @@ class API {
all(list) {
if (_.isArray(list)) {
return Promise.all(list);
} else {
return Promise.reject(Error('the parameters of api all method should be Array!'));
}
throw Error('the parameters of api all method should be Array!');
}
}
class ServiceAPI extends API {
class API extends Http {
constructor() {
super(api);
}
}
class ServiceAPI extends Http {
constructor() {
super();
ApiUrl = serviceApi;
super(serviceApi);
}
}
class SearchAPI extends API {
class SearchAPI extends Http {
constructor() {
super();
ApiUrl = searchApi;
super(searchApi);
}
}
... ...
... ... @@ -5,8 +5,7 @@
*/
'use strict';
const qs = require('querystring');
const _ = require('lodash');
const md5 = require('md5');
const privateKey = {
... ... @@ -39,15 +38,22 @@ const packageSort = argument => {
* @return {string} 生成的签名字符串
*/
const makeSign = argument => {
return md5(qs.stringify(argument)).toLowerCase();
let qs = [];
_.forEach(argument, function(value, key) {
qs.push(key + '=' + _.trim(value));
});
return md5(qs.join('&')).toLowerCase();
};
// 生成API签名,调用后端接口的时候有私钥校验
exports.apiSign = (params) => {
const clientType = params.client_type || 'h5';
/* eslint-disable */
var sign = packageSort(Object.assign({
client_type: 'h5',
private_key: privateKey.h5,
let sign = packageSort(Object.assign({
client_type: clientType,
private_key: privateKey[clientType],
app_version: '3.8.2',
os_version: 'yohobuy:h5',
screen_size: '720x1280',
... ...
... ... @@ -25,7 +25,7 @@ class Timer {
if (labelTime) {
let duration = process.hrtime(labelTime);
return this._round(duration[1]);
return this._round(duration[0], duration[1]);
} else {
this.timers[label] = process.hrtime();
}
... ... @@ -35,8 +35,8 @@ class Timer {
* 格式化成毫秒
* @param {Number} value 纳秒
*/
_round(value) {
return Math.round(value / 10000) / 100;
_round(seconds, nanoseconds) {
return Math.round((seconds * 1e9 + nanoseconds) / 10000) / 100;
}
}
... ...
# 日志
... ...
... ... @@ -13,9 +13,9 @@
"online": "NODE_ENV=\"production\" node app.js",
"debug": "DEBUG=\"express:*\" node app.js",
"lint-js": "./node_modules/.bin/eslint -c .eslintrc --cache --fix .",
"lint-css": "./node_modules/.bin/stylelint --config .stylelintrc public/**/*.css",
"lint-css": "./node_modules/.bin/stylelint --config .stylelintrc public/scss/**/*.css",
"precommit": "node lint.js",
"test": "ava"
"test": "./node_modules/.bin/nyc ./node_modules/.bin/ava"
},
"ava": {
"tap": true,
... ... @@ -32,9 +32,11 @@
"dependencies": {
"bluebird": "^3.3.5",
"body-parser": "^1.15.0",
"connect-memcached": "^0.2.0",
"cookie-parser": "^1.4.1",
"express": "^4.13.1",
"express-handlebars": "^3.0.0",
"express-session": "^1.13.0",
"influxdb-winston": "^1.0.1",
"lodash": "^4.12.0",
"md5": "^2.1.0",
... ... @@ -44,14 +46,15 @@
"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",
"ava": "^0.14.0",
"babel-cli": "^6.8.0",
"babel-preset-es2015": "^6.6.0",
"babel-register": "^6.8.0",
"eslint": "^2.9.0",
"eslint": "^2.10.2",
"eslint-config-yoho": "^1.0.1",
"gulp": "^3.9.1",
"gulp-cssnano": "^2.1.2",
... ... @@ -62,6 +65,7 @@
"husky": "^0.11.4",
"mocha": "^2.4.5",
"nodemon": "1.9.2",
"nyc": "^6.4.3",
"postcss-assets": "^4.0.1",
"postcss-cachebuster": "^0.1.2",
"postcss-calc": "^5.2.1",
... ... @@ -78,14 +82,15 @@
"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",
"yoho-fastclick": "0.0.1",
"yoho-hammer": "0.0.1",
"yoho-jquery": "0.0.3",
"yoho-jquery-lazyload": "0.0.2",
"yoho-swiper": "0.0.1"
"yoho-fastclick": "^1.0.6",
"yoho-hammer": "^2.0.7",
"yoho-handlebars": "^4.0.5",
"yoho-jquery": "^1.9.1",
"yoho-jquery-lazyload": "^1.9.7",
"yoho-swiper": "^3.3.1"
}
}
... ...
/**
* http api 测试
*
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 2016/05/17
*/
'use strict';
const test = require('ava');
// const rewire = require('rewire');
// const shelljs = require('shelljs');
const sign = require('../../library/sign');
// let config = rewire('../../config/common');
const API = require('../../library/api').API;
const ServiceAPI = require('../../library/api').ServiceAPI;
const SearchAPI = require('../../library/api').SearchAPI;
const getUrl = 'operations/api/v6/category/getCategory';
// test.before('create log folder', (t) => {
// shelljs.mkdir('log');
// t.pass();
// });
//
// test.after('delete log folder', (t) => {
// shelljs.rm('-rf', 'log');
// t.pass();
// });
test('api constructor test', (t) => {
let api = new ServiceAPI();
let api2 = new API();
let api3 = new SearchAPI();
t.true(api !== null);
t.true(api2 !== null);
t.true(api3 !== null);
});
test('api get test', t => {
let api = new ServiceAPI();
return api.get(getUrl, sign.apiSign({})).then(result => {
if (result && (result.code === 200 || result.code === 500)) {
t.pass();
} else {
t.fail();
}
});
});
test('api get use cache test', t => {
let api = new ServiceAPI();
return api.get(getUrl, sign.apiSign({}), true).then(result => {
if (result && (result.code === 200 || result.code === 500)) {
t.pass();
} else {
t.fail();
}
});
});
test('api post test', t => {
let api = new ServiceAPI();
return api.post(getUrl, sign.apiSign({})).then(result => {
if (result && (result.code === 200 || result.code === 500)) {
t.pass();
} else {
t.fail();
}
});
});
test('api multiple call test', (t) => {
let api = new ServiceAPI();
let multi = [api.get(getUrl, sign.apiSign({})), api.get(getUrl, sign.apiSign({}))];
return api.all(multi).then(result => {
if (result.length === 2) {
t.pass();
} else {
t.fail();
}
});
});
test('api multiple fail call test', (t) => {
let api = new ServiceAPI();
return api.all(1).catch((e) => {
if (e) {
t.pass();
} else {
t.fail();
}
});
});
... ...
/**
* cache 测试
*
* @author: jf<jeff.jiang@yoho.cn>
* @date: 2016/5/18
*/
'use strict';
import test from 'ava';
import cache from '../../library/cache';
let testKey = 'test_unit_key:' + (new Date()).getTime();
let testValue = 'anotherValue';
let anotherKey = 'test_unit_key2:' + (new Date()).getTime();
let anotherValue = {a: 1};
let slaveTestKey = 'test_unit_key3:' + (new Date()).getTime();
let slaveTestValue = 'anotherValue3';
test.before('set test key', (t) => {
cache.set(testKey, testValue);
cache.set(anotherKey, anotherValue);
t.pass();
});
test.after('del test key', (t) => {
cache.del(testKey);
cache.del(anotherKey);
t.pass();
});
test('cache get test', (t) => {
return cache.get(testKey).then((v) => {
t.is(v, testValue);
});
});
test('cache get multi test', (t) => {
cache.set(anotherKey, anotherValue);
return cache.getMulti([testKey, anotherKey]).then((values) => {
console.log(values);
t.is(values[testKey], testValue);
t.is(values[anotherKey], JSON.stringify(anotherValue));
});
});
test('cache get from slave test', (t) => {
return cache.getFromSlave(testKey).then((v) => {
t.is(v, testValue);
});
});
test('cache get multi from slave test', (t) => {
cache.set(anotherKey, anotherValue);
return cache.getMultiFromSlave([testKey, anotherKey]).then((values) => {
t.is(values[testKey], testValue);
t.is(values[anotherKey], JSON.stringify(anotherValue));
});
});
test('cache set to slave', (t) => {
return cache.setSlave(slaveTestKey, slaveTestValue).then(() => {
return cache.getFromSlave(slaveTestKey);
}).then((v) => {
t.is(v, slaveTestValue);
cache.del(slaveTestKey);
});
});
... ...
/**
* 对象键名驼峰测试
*
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 2016/05/17
*/
import {test} from 'ava';
const camelCase = require('../../library/camel-case');
test('camel case object', t => {
let o = {
A_B: 'ab_cd'
};
t.is(camelCase(o).aB, 'ab_cd');
});
test('camel case array', t => {
let arr = [{
A_B: 'ab_cd'
}];
t.is(camelCase(arr)[0].aB, 'ab_cd');
});
... ...
const headerModel = require('../../doraemon/models/header');
const test = require('ava').test;
test('test setNavHeader method', t => {
const headerData = headerModel.setNavHeader('逛');
t.is(headerData.navTitle, '逛');
t.true(headerData.backUrl);
t.true(headerData.navBtn);
});
/**
* library helpers 类单元测试
* @author jeff.jiang<jeff.jiang@yoho.cn>
* @date 2016/05/17
*/
'use strict';
const test = require('ava');
const helpers = require('../../library/helpers');
test('qiniu image url handle', t => {
let url = 'http://img11.static.yhbimg.com/yhb-img01/2016/04/18/03/016d50b20cfdec5a91c614b68546bc9d72.jpg?imageView2/{mode}/w/{width}/h/{height}';
let expected = 'http://img11.static.yhbimg.com/yhb-img01/2016/04/18/03/016d50b20cfdec5a91c614b68546bc9d72.jpg?imageView2/2/w/400/h/300';
t.is(helpers.image(url, 400, 300), expected);
});
test('uri format', t => {
let uri = '/test';
let qs = { name: 'yoho' };
let mod = 'list';
let expected = '//list.m.yohobuy.com/test?name=yoho';
t.is(helpers.url(uri, qs, mod), expected);
});
test('upper char to lowercase', t => {
let str = 'ABc';
let expected = 'abc';
t.is(helpers.lowerCase(str), expected);
});
test('lower char to uppercase', t => {
let str = 'abc!';
let expected = 'ABC!';
t.is(helpers.upperCase(str), expected);
});
... ...
/**
* logger 工具类测试
*/
const test = require('ava');
const shelljs = require('shelljs');
const logger = require('../../library/logger');
const today = () => {
let now = new Date();
let s = now.getFullYear();
if (now.getMonth() < 10) {
s += '-0' + now.getMonth();
} else {
s += now.getMonth();
}
if (now.getDay() < 10) {
s += '-0' + now.getDay();
} else {
s += now.getDay();
}
return s;
};
// test.before('create log folder', t => {
// shelljs.mkdir('log');
// t.pass();
// });
//
// test.after('clean test log file ', t => {
// shelljs.rm('-rf', 'log');
// t.pass();
// });
test.cb('logger test', t => {
shelljs.rm('-f', 'log/*.log.*');
logger.info('xxx', () => {
shelljs.ls('log/info.log.*').some(s => {
console.log('generate log file:' + s);
return s === 'info.log.' + today();
});
t.end();
});
});
... ...
/**
* 签名类测试
*
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 2016/05/17
*/
const test = require('ava');
const sign = require('../../library/sign');
test('app sign test', t => {
let params = {
client_type: 'h5', // eslint-disable-line
a: 1,
b: 'b'
};
let signedParams = sign.apiSign(params);
t.true(sign.checkSign(signedParams));
});
... ...
/**
* Timer 计时类测试
*
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 2016/05/17
*/
const test = require('ava');
const Timer = require('../../library/timer');
const sleep = (timeout) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, timeout);
});
};
test.cb('timer class ', t => {
let timer = new Timer();
timer.put('test');
sleep(300).then(() => {
let diff = timer.put('test');
t.true(diff >= 300);
t.end();
});
});
... ...