Authored by runner

dist update

Showing 35 changed files with 196 additions and 131 deletions
**/bundle/**/*.js
**/dist/**/*.js
coverage
... ...
... ... @@ -7,7 +7,6 @@
const config = require('./config/common');
// use one apm
if (config.useOneapm) {
require('oneapm');
... ... @@ -20,16 +19,12 @@ const cookieParser = require('cookie-parser');
const favicon = require('serve-favicon');
const session = require('express-session');
const memcached = require('connect-memcached');
const _ = require('lodash');
const hbs = require('express-handlebars');
const pkg = require('./package.json');
const cookie = require('./library/cookie');
const app = express();
const MemcachedStore = memcached(session);
const setChannel = require('./doraemon/middleware/set-channel');
// 向模板注入变量
app.locals.devEnv = app.get('env') === 'development';
app.locals.version = pkg.version;
... ... @@ -37,8 +32,15 @@ app.locals.version = pkg.version;
// 指定libray目录
global.library = path.resolve('./library/');
app.set('view engine', '.hbs');
app.set('views', './doraemon/views');
app.engine('.hbs', hbs({
extname: '.hbs',
defaultLayout: 'layout',
layoutsDir: './doraemon/views',
partialsDir: './doraemon/views/partial',
helpers: require(`${global.library}/helpers`)
}));
app.use(favicon(path.join(__dirname, '/public/favicon.ico')));
app.use(express.static(path.join(__dirname, 'public')));
... ... @@ -65,35 +67,35 @@ app.use(session({
})
}));
// req和res绑定yoho对象,用于传递全局数据, 如req.yoho.channel等
app.use((req, res, next) => {
req.yoho = {};
res.yoho = {};
next();
});
req.user = {}; // 全局的用户数据
req.yoho = {}; // req和res绑定yoho对象,用于传递全局数据, 如req.yoho.channel等
app.use((req, res, next) => {
req.user = {};
// 从 PHP 写的 SESSION 中获取到当前登录用户的 UID
if (req.session && _.isNumber(req.session._LOGIN_UID)) {
req.user.uid = req.session._LOGIN_UID;
}
// session 没有读取到的时候,从 cookie 读取 UID
if (!req.user.uid && req.cookies._UID) {
req.user.uid = cookie.getUid(req);
}
next();
});
const logger = require('./library/logger');
const user = require('./doraemon/middleware/user');
const setChannel = require('./doraemon/middleware/set-channel');
const errorHanlder = require('./doraemon/middleware/error-hanlder');
// YOHO 前置中间件
app.use(user());
app.use(setChannel());
// dispatcher
require('./dispatch')(app);
try {
require('./dispatch')(app);
app.all('*', errorHanlder.notFound()); // 404
// YOHO 后置中间件
app.use(errorHanlder.serverError());
} catch (err) {
logger.error(err);
}
// listener
app.listen(config.port, function() {
console.log('yohobuy start');
logger.info('yohobuy start');
});
... ...
... ... @@ -23,13 +23,12 @@ const contentCode = {
special: '89cc20483ee2cbc8a716dcfe2b6c7603'
};
class Star {
/**
/**
* 获取资源位数据
* @param {[string]} page
* @return {[array]}
*/
static getResources(page) {
const _getResources = (page) => {
return serviceAPI.get('operations/api/v5/resource/get', sign.apiSign({
content_code: contentCode[page]
})).then((result) => {
... ... @@ -40,14 +39,14 @@ class Star {
return [];
}
});
}
};
/**
/**
* 星潮教室首页数据处理
* @param {[array]} list
* @return {[array]}
*/
static processIndexData(list) {
const _processIndexData = (list) => {
const formatData = {
ads: [],
starAvatar: [],
... ... @@ -115,18 +114,18 @@ class Star {
}
return formatData;
}
};
static processShareUrl(post) {
const _processShareUrl = (post) => {
return `${post.share.url}&openby:yohobuy={"action":"go.share","params":{"pic":"${helpers.image(post.src, 640, 640)}","title":"${post.title}","url":"${post.share.url}","content":"潮流资讯,新鲜贩售,YOHO!Buy有货【逛】不停"}}`; // eslint-disable-line
}
};
/**
/**
* 明星专题列表数据处理
* @param {[array]} list
* @return {[array]}
*/
static processDetailData(list) {
const _processDetailData = (list) => {
const formatData = [];
list = list || [];
... ... @@ -135,19 +134,19 @@ class Star {
_.forEach(list, (data) => {
data.publishTime = helpers.dateFormat('MM月DD日 hh:mm', data.publishTime);
if (data.share && data.share.url) {
data.share.url = Star.processShareUrl(data);
data.share.url = _processShareUrl(data);
}
formatData.push(data);
});
return formatData;
}
};
/**
/**
* 星搭配文章列表数据处理
*/
static processCollocationData(list) {
const _processCollocationData = (list) => {
const formatData = [];
list = list || [];
... ... @@ -163,29 +162,29 @@ class Star {
});
return formatData;
}
};
/**
/**
* 星潮首页
*/
static getIndexData() {
const getIndexData = () => {
return api.get('', sign.apiSign({
method: 'app.starClass.index',
code: '8adc27fcf5676f356602889afcfd2a8e'
})).then((result) => {
if (result && result.code === 200) {
return Star.processIndexData(result.data);
return _processIndexData(result.data);
} else {
logger.error('星潮教室首页数据返回 code 不是 200');
return {};
}
});
}
};
/**
/**
* 明星专题
*/
static getDetailData(params) {
const getDetailData = (params) => {
return api.get('', sign.apiSign({
method: 'app.starClass.lastTagArticle',
tag: params.tag,
... ... @@ -196,21 +195,21 @@ class Star {
if (params.page > result.data.totalPage) {
return '';
} else {
return Star.processDetailData(result.data.list);
return _processDetailData(result.data.list);
}
} else {
logger.error('明星专题文章数据返回 code 不是 200');
return [];
}
});
}
};
/**
/**
* 星专题
*/
static getSpecialData() {
return Star.getResources('special').then((result) => {
const getSpecialData = () => {
return _getResources('special').then((result) => {
// 数据结构嵌套太深
_.forEach(result, (data) => {
... ... @@ -224,27 +223,27 @@ class Star {
});
return result;
});
}
};
/**
/**
* 星搭配
*/
static getCollocationListData(params, uid) {
const getCollocationListData = (params, uid) => {
return serviceAPI.get('guang/api/v5/article/getStarClassroomArticleList', sign.apiSign(Object.assign({
limit: '20',
uid: uid
}, params))).then((result) => {
if (result && result.code === 200) {
return Star.processCollocationData(result.data.list.artList);
return _processCollocationData(result.data.list.artList);
} else {
logger.error('获取星搭配文章列表返回 code 不是 200');
return [];
}
});
}
};
static setFavorite(params, uid) {
const setFavorite = (params, uid) => {
if (!uid) {
return Promise.resolve({
code: 401,
... ... @@ -258,7 +257,12 @@ class Star {
article_id: params.articleId,
uid: uid
}));
}
}
};
module.exports = Star;
module.exports = {
getIndexData: getIndexData,
getSpecialData: getSpecialData,
getCollocationListData: getCollocationListData,
setFavorite: setFavorite,
getDetailData: getDetailData
};
... ...
... ... @@ -69,14 +69,12 @@ const contentCode = {
}
};
class Sale {
/**
/**
* 折扣专场列表数据处理
* @param {[array]} list
* @return {[array]}
*/
static processDiscount(list, channel) {
const _processDiscount = (list, channel) => {
const formatData = [];
let flag = true;
... ... @@ -97,14 +95,14 @@ class Sale {
});
return formatData;
}
};
/**
/**
* 折扣专场接口调用
* @param {[object]} params
* @return {[array]}
*/
static discount(params) {
const _discount = (params) => {
params = params || {};
return api.get('', sign.apiSign(Object.assign({
... ... @@ -112,14 +110,14 @@ class Sale {
sort: 2,
plateform: 2
}, params)));
}
};
/**
/**
* 断码区分类数据处理
* @param {[array]} list
* @return {[array]}
*/
static processBreakingSort(list) {
const _processBreakingSort = (list) => {
const formatData = {};
const sort = [];
const sub = [];
... ... @@ -148,14 +146,14 @@ class Sale {
formatData.sub = sub;
return formatData;
}
};
/**
/**
* 商品搜索接口请求
* @param {[object]} params
* @return {[array]}
*/
static searchSales(params) {
const _searchSales = (params) => {
let method = 'app.search.sales';
... ... @@ -183,14 +181,14 @@ class Sale {
return api.get('', sign.apiSign(Object.assign({
method: method
}, params)), true);
}
};
/**
/**
* 获取用户数据信息
* @param {[string]} uid
* @return {[array]}
*/
static getUserProfile(uid) {
const _getUserProfile = (uid) => {
if (!uid) {
return Promise.resolve({
code: 200,
... ... @@ -201,14 +199,14 @@ class Sale {
method: 'app.passport.profile',
uid: uid
}), true);
}
};
/**
/**
* 获取资源位数据
* @param {[string]} page
* @return {[array]}
*/
static getResources(page, channel) {
const _getResources = (page, channel) => {
return serviceAPI.get('operations/api/v5/resource/get', sign.apiSign({
content_code: contentCode[channel][page]
})).then((result) => {
... ... @@ -219,33 +217,33 @@ class Sale {
return [];
}
});
}
};
/**
/**
* 获取断码区分类数据
* @param {[string]} yhChannel
* @return {[object]}
*/
static getBreakingSort(yhChannel) {
const _getBreakingSort = (yhChannel) => {
return api.get('', sign.apiSign({
method: 'app.sale.getBreakingSort',
yh_channel: channelType[yhChannel] || '1'
})).then((result) => {
if (result && result.code === 200) {
return Sale.processBreakingSort(result.data);
return _processBreakingSort(result.data);
} else {
logger.error('断码区分类接口返回 code 不是 200');
return {};
}
});
}
};
/**
/**
* 获取商品数据
*/
static getSearchData(params, uid) {
const getSearchData = (params, uid) => {
return Promise.all([
Sale.searchSales(params).then((result) => {
_searchSales(params).then((result) => {
if (result && result.code === 200) {
return productProcess.processProductList(result.data.product_list || [], {
yh_channel: params.yh_channel,
... ... @@ -256,7 +254,7 @@ class Sale {
return [];
}
}),
Sale.getUserProfile(uid).then((result) => {
_getUserProfile(uid).then((result) => {
if (result && result.code === 200) {
return result.data.vip_info ? camelCase(result.data.vip_info) : {};
} else {
... ... @@ -265,15 +263,15 @@ class Sale {
}
})
]);
}
};
/**
/**
* 获取筛选数据
* @param {[object]} params
* @return {[array]}
*/
static getFilterData(params) {
return Sale.searchSales(params).then((result) => {
const getFilterData = (params) => {
return _searchSales(params).then((result) => {
if (result && result.code === 200) {
return productProcess.processFilter(result.data.filter || [], {
hideSize: params.saleType === '1',
... ... @@ -284,78 +282,78 @@ class Sale {
return [];
}
});
}
};
/**
/**
* 获取sale首页数据
* @return {[array]}
*/
static getSaleData(channel) {
return Sale.getResources('sale', channel);
}
const getSaleData = (channel) => {
return _getResources('sale', channel);
};
/**
/**
* 获取会员享数据
* @return {[array]}
*/
static getVipData(channel) {
return Sale.getResources('vip', channel);
}
const getVipData = (channel) => {
return _getResources('vip', channel);
};
/**
/**
* 获取断码区数据
* @param {[object]} params
* @return {[object]}
*/
static getBreakCodeData(params) {
const getBreakCodeData = (params) => {
params = params || {};
return Promise.all([Sale.getResources('breakCode', params.yhChannel), Sale.getBreakingSort(params.yhChannel)])
return Promise.all([_getResources('breakCode', params.yhChannel), _getBreakingSort(params.yhChannel)])
.then((result) => {
return {
content: result[0],
nav: result[1]
};
});
}
};
/**
/**
* 获取折扣专场专题列表数据
* @param {[object]} params
* @return {[object]}
*/
static getDiscountData(yhChannel) {
const getDiscountData = (yhChannel) => {
const discountData = {};
return Sale.discount({
return _discount({
yh_channel: channelType[yhChannel] || '1'
}).then((result) => {
if (result && result.code === 200) {
discountData.data = Sale.processDiscount(result.data, yhChannel);
discountData.data = _processDiscount(result.data, yhChannel);
return discountData;
} else {
logger.error('折扣专场专题列表返回 code 不是 200');
return {};
}
});
}
};
/**
/**
* 获取折扣专场专题详情数据
* @param {[string]} id
* @return {[object]}
*/
static getDiscountDetailData(id, yhChannel) {
const getDiscountDetailData = (id, yhChannel) => {
let res = {};
let param = {
id: id,
yh_channel: channelType[yhChannel] || '1'
};
return Sale.discount(param).then((result) => {
return _discount(param).then((result) => {
if (result && result.code === 200) {
res = Sale.processDiscount(result.data);
res = _processDiscount(result.data);
return {
title: res[0].title,
... ... @@ -370,7 +368,14 @@ class Sale {
return {};
}
});
}
}
};
module.exports = Sale;
module.exports = {
getSaleData: getSaleData,
getBreakCodeData: getBreakCodeData,
getDiscountData: getDiscountData,
getDiscountDetailData: getDiscountDetailData,
getVipData: getVipData,
getFilterData: getFilterData,
getSearchData: getSearchData
};
... ...
/**
* 404 错误
* @return {[type]}
*/
exports.notFound = () => {
return (req, res) => {
if (req.xhr) {
return res.status(404).json({
code: 404,
message: '页面不存在'
});
}
return res.render('error/404');
};
};
/**
* 服务器错误
* @return {[type]}
*/
exports.serverError = () => {
return (err, req, res, next) => {
if (!res.headersSent) {
if (req.xhr) {
return res.status(500).json({
code: 500,
message: '服务器错误'
});
}
return res.render('error/500', err);
}
next(err);
};
};
... ...
... ... @@ -19,7 +19,6 @@ module.exports = () => {
});
req.yoho.channel = channel;
res.yoho.channel = channel;
}
next();
... ...
const _ = require('lodash');
const cookie = require('../../library/cookie');
module.exports = () => {
return (req, res, next) => {
// 从 SESSION 中获取到当前登录用户的 UID
if (req.session && _.isNumber(req.session._LOGIN_UID)) {
req.user.uid = req.session._LOGIN_UID;
}
// session 没有读取到的时候,从 cookie 读取 UID
if (!req.user.uid && req.cookies._UID) {
req.user.uid = cookie.getUid(req);
}
next();
};
};
... ...
... ... @@ -9,14 +9,14 @@
},
"scripts": {
"start": "node app.js",
"dev": "node_modules/.bin/nodemon -e js,hbs -i public/ app.js",
"dev": "nodemon -e js,hbs -i public/ app.js",
"online": "NODE_ENV=\"production\" node app.js",
"debug": "DEBUG=\"express:*\" node_modules/.bin/nodemon -e js,hbs -i public/ app.js",
"lint-js": "./node_modules/.bin/eslint -c .eslintrc --cache --fix .",
"lint-css": "./node_modules/.bin/stylelint --config .stylelintrc public/scss/**/*.css",
"debug": "DEBUG=\"express:*\" nodemon -e js,hbs -i public/ app.js",
"lint-js": "eslint -c .eslintrc --cache --fix .",
"lint-css": "stylelint --config .stylelintrc public/scss/**/*.css",
"precommit": "node lint.js",
"test": "NODE_ENV=test ./node_modules/.bin/nyc ./node_modules/.bin/ava",
"posttest": "./node_modules/.bin/nyc report --reporter=html"
"test": "NODE_ENV=test nyc ./node_modules/.bin/ava",
"posttest": "nyc report --reporter=html"
},
"ava": {
"tap": true,
... ... @@ -68,14 +68,13 @@
"gulp-util": "^3.0.7",
"husky": "^0.11.4",
"nodemon": "1.9.2",
"nyc": "^6.4.4",
"nyc": "^6.6.1",
"postcss-assets": "^4.0.1",
"postcss-cachebuster": "^0.1.3",
"postcss-calc": "^5.2.1",
"postcss-center": "^1.0.0",
"postcss-clearfix": "^1.0.0",
"postcss-crip": "^2.0.0",
"postcss-opacity": "^3.0.0",
"postcss-position": "^0.5.0",
"postcss-pxtorem": "^3.3.1",
"postcss-short": "^1.4.0",
... ...
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
... ... @@ -9,7 +9,7 @@
&:link,
&:visited,
&:hover,
&:actived {
&:active {
color: #000;
}
}
... ... @@ -259,7 +259,7 @@
position: relative;
}
li:first {
li:first-child {
margin-left: 0;
}
... ...