Authored by biao

update for set channel middleware

... ... @@ -27,6 +27,8 @@ 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';
... ... @@ -35,6 +37,7 @@ 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')));
... ... @@ -62,6 +65,14 @@ app.use(session({
})
}));
// req和res绑定yoho对象,用于传递全局数据, 如req.yoho.channel等
app.use((req, res, next) => {
req.yoho = {};
res.yoho = {};
next();
});
app.use((req, res, next) => {
req.user = {};
... ... @@ -77,6 +88,8 @@ app.use((req, res, next) => {
next();
});
app.use(setChannel());
// dispatcher
require('./dispatch')(app);
... ...
... ... @@ -28,8 +28,11 @@ const headTab = [
];
const processPublicData = (req, title, navBtn) => {
let channel = req.query.channel || req.cookies._Channel || 'boys';
let headerData = headerModel.setNavHeader(title, channel, '', '', navBtn);
let headerData = headerModel.setNav({
navTitle: title,
navBtn: navBtn
});
let renderData = {
module: 'guang',
title: title,
... ...
/**
* 频道页面
* @author: Bi Kai<kai.bi@yoho.cn>
* 奥莱页面
* @author: 赵彪<bill.zhao@yoho.cn>
* @date: 2016/05/09
*/
'use strict';
... ... @@ -19,9 +19,13 @@ const yhChannelMap = {
lifestyle: 4
};
// 奥莱首页控制器
exports.index = (req, res) => {
let channel = req.query.channel || req.cookies._Channel || 'boys';
let headerData = headerModel.setNavHeader('OUTLET', channel, true, '', null);
let headerData = headerModel.setNav({
navTitle: 'OUTLET',
navBtn: false
});
let categoryId = req.query.category_id;
let yhChannel = req.query.yh_channel || 1;
let contentcode = req.query.content_code;
... ... @@ -29,15 +33,17 @@ exports.index = (req, res) => {
outletModel.getContent(categoryId, yhChannel, contentcode).then(result => {
res.render('outlet', Object.assign({
page: 'outlet',
pageHeader: headerData,
pageFooter: true
pageHeader: headerData
}, renderData, result));
});
};
exports.activity = (req, res) => {
let channel = req.query.channel || req.cookies._Channel || 'boys';
let headerData = headerModel.setNavHeader('OUTLET', channel, true, '', null);
// 奥莱活动详情页
exports.activityDetail = (req, res) => {
let headerData = headerModel.setNav({
navTitle: 'OUTLET',
navBtn: false
});
outletModel.getActivity(req.query.id).then(result => {
res.render('outlet/activity', Object.assign({
... ... @@ -48,14 +54,17 @@ exports.activity = (req, res) => {
});
};
exports.activityChannel = (req, res) => {
let channel = req.cookies._Channel || 'boys';
let headerData = headerModel.setNavHeader('OUTLET', channel, true, '', null);
// 奥莱活动频道列表页
exports.activityList = (req, res) => {
let headerData = headerModel.setNav({
navTitle: 'OUTLET',
navBtn: false
});
let categoryId = req.query.category_id;
let type = req.query.type || 2;
let template = type === 2 ? 'outlet/will-end' : 'outlet/will-start';
let page = type === 2 ? 'outlet-will-end' : 'outlet-will-start';
let yhChannel = yhChannelMap[channel];
let yhChannel = req.query.yh_channel || yhChannelMap[req.yoho.channel];
outletModel.getNearlyActivity(yhChannel, type, categoryId).then(result => {
res.render(template, Object.assign({
... ...
... ... @@ -29,11 +29,12 @@ const queryParam = {
*/
const processPublicData = (req, title, page) => {
let data = {};
let channel = req.query.channel || req.cookies._Channel || 'boys';
let headerData = headerModel.setNavHeader(title, channel);
let headerData = headerModel.setNav({
navTitle: title
});
data = {
channel: channel,
channel: req.yoho.channel,
renderData: {
module: 'product',
page: page,
... ...
/**
* 频道页面 model
* @author: Bi Kai<kai.bi@yoho.cn>
* @author: 赵彪<bill.zhao@yoho.cn>
* @date: 2016/05/09
*/
'use strict';
... ...
... ... @@ -35,8 +35,8 @@ router.get('/sale/search', sale.search);
router.get('/sale/filter', sale.filter);
router.get('/outlet', outlet.index);
router.get('/outlet/activity', outlet.activity);
router.get('/outlet/activity', outlet.activityDetail);
router.get('/outlet/activityinfo', outlet.activityChannel);
router.get('/outlet/activityinfo', outlet.activityList);
module.exports = router;
... ...
/**
* 设置频道
* @author: 赵彪<bill.zhao@yoho.cn>
* @date: 2016/6/16
*/
'use strict';
module.exports = () => {
return (req, res, next) => {
if (!req.xhr) {
const obj = {};
const channel = req.query.channel || req.cookies._Channel || 'boys';
obj[channel] = true;
Object.assign(res.locals, {
pageChannel: obj
});
req.yoho.channel = channel;
res.yoho.channel = channel;
}
next();
};
};
... ...
... ... @@ -8,36 +8,32 @@
const _ = require('lodash');
// const getChannle = () => ('kids');
/**
* 设置头部
* @param {String} title 标题
* @param {Boolen} channel 频道
* @param {Boolen} backUrl 返回的url
* @param {Boolen} navBtn 是否显示导航
* @param {Boolen} navBack 是否显示返回箭头
* 传配置对象设置头部
* @param {Object} opt 头部选项对象
* @return {Object} 头部数据对象
*/
exports.setNavHeader = (title, channel, navBack, backUrl, navBtn) => {
let header = {
navTitle: title,
backUrl: backUrl,
navBack: navBack,
navBtn: navBtn
exports.setNav = (opt) => {
let defaultOpt = {
navTitle: 'Yoho!Buy', // 标题
navBack: true, // 是否显示返回按钮
backUrl: '', // 返回按钮对应的url
navBtn: true // 顶部下拉菜单选项
};
header[channel] = true;
return _.merge({
navTitle: '',
backUrl: true,
navBack: '',
navBtn: {
indexUrl: '/?go=1',
categoryUrl: '/cate',
shoppingCartUrl: '/cart/index/index',
mineUrl: '/home'
},
boys: true
}, header);
Object.assign(defaultOpt, opt || {});
if (defaultOpt.navBtn || _.isNull(defaultOpt.navBtn)) {
Object.assign(defaultOpt, {
navBtn: { // 顶部下拉菜单选项
indexUrl: '/?go=1',
categoryUrl: '/cate',
shoppingCartUrl: '/cart/index/index',
mineUrl: '/home'
}
});
}
return defaultOpt;
};
... ...
{{#pageHeader}}
{{# ../pageChannel}}
<header id="yoho-header" class="yoho-header{{#if boys}} boys{{/if}}{{#if girls}} girls{{/if}}{{#if kids}} kids{{/if}}{{#if lifeStyle}} life-style{{/if}}">
{{/ ../pageChannel}}
{{#navBack}}
<a href="{{#if backUrl}}{{backUrl}}{{^}}javascript:history.go(-1);{{/if}}" class="iconfont nav-back">&#xe610;</a>
{{/navBack}}
... ... @@ -20,7 +22,9 @@
{{/navTitle}}
</header>
{{#navBtn}}
<div class="homebuttom hide {{#if ../boys}} boys{{/if}}{{#if ../girls}} girls{{/if}}{{#if ../kids}} kids{{/if}}{{#if ../lifeStyle}} life-style{{/if}}">
{{# ../../pageChannel}}
<div class="homebuttom hide {{#if boys}} boys{{/if}}{{#if girls}} girls{{/if}}{{#if kids}} kids{{/if}}{{#if lifeStyle}} life-style{{/if}}">
{{/ ../../pageChannel}}
<ul>
<li>
<a href="{{ indexUrl }}">
... ...