Authored by biao

update for set channel middleware

@@ -27,6 +27,8 @@ const cookie = require('./library/cookie'); @@ -27,6 +27,8 @@ const cookie = require('./library/cookie');
27 const app = express(); 27 const app = express();
28 const MemcachedStore = memcached(session); 28 const MemcachedStore = memcached(session);
29 29
  30 +const setChannel = require('./doraemon/middleware/set-channel');
  31 +
30 32
31 // 向模板注入变量 33 // 向模板注入变量
32 app.locals.devEnv = app.get('env') === 'development'; 34 app.locals.devEnv = app.get('env') === 'development';
@@ -35,6 +37,7 @@ app.locals.version = pkg.version; @@ -35,6 +37,7 @@ app.locals.version = pkg.version;
35 // 指定libray目录 37 // 指定libray目录
36 global.library = path.resolve('./library/'); 38 global.library = path.resolve('./library/');
37 39
  40 +
38 app.set('view engine', '.hbs'); 41 app.set('view engine', '.hbs');
39 42
40 app.use(favicon(path.join(__dirname, '/public/favicon.ico'))); 43 app.use(favicon(path.join(__dirname, '/public/favicon.ico')));
@@ -62,6 +65,14 @@ app.use(session({ @@ -62,6 +65,14 @@ app.use(session({
62 }) 65 })
63 })); 66 }));
64 67
  68 +
  69 +// req和res绑定yoho对象,用于传递全局数据, 如req.yoho.channel等
  70 +app.use((req, res, next) => {
  71 + req.yoho = {};
  72 + res.yoho = {};
  73 + next();
  74 +});
  75 +
65 app.use((req, res, next) => { 76 app.use((req, res, next) => {
66 req.user = {}; 77 req.user = {};
67 78
@@ -77,6 +88,8 @@ app.use((req, res, next) => { @@ -77,6 +88,8 @@ app.use((req, res, next) => {
77 next(); 88 next();
78 }); 89 });
79 90
  91 +app.use(setChannel());
  92 +
80 // dispatcher 93 // dispatcher
81 require('./dispatch')(app); 94 require('./dispatch')(app);
82 95
@@ -28,8 +28,11 @@ const headTab = [ @@ -28,8 +28,11 @@ const headTab = [
28 ]; 28 ];
29 29
30 const processPublicData = (req, title, navBtn) => { 30 const processPublicData = (req, title, navBtn) => {
31 - let channel = req.query.channel || req.cookies._Channel || 'boys';  
32 - let headerData = headerModel.setNavHeader(title, channel, '', '', navBtn); 31 + let headerData = headerModel.setNav({
  32 + navTitle: title,
  33 + navBtn: navBtn
  34 + });
  35 +
33 let renderData = { 36 let renderData = {
34 module: 'guang', 37 module: 'guang',
35 title: title, 38 title: title,
1 /** 1 /**
2 - * 频道页面  
3 - * @author: Bi Kai<kai.bi@yoho.cn> 2 + * 奥莱页面
  3 + * @author: 赵彪<bill.zhao@yoho.cn>
4 * @date: 2016/05/09 4 * @date: 2016/05/09
5 */ 5 */
6 'use strict'; 6 'use strict';
@@ -19,9 +19,13 @@ const yhChannelMap = { @@ -19,9 +19,13 @@ const yhChannelMap = {
19 lifestyle: 4 19 lifestyle: 4
20 }; 20 };
21 21
  22 +// 奥莱首页控制器
22 exports.index = (req, res) => { 23 exports.index = (req, res) => {
23 - let channel = req.query.channel || req.cookies._Channel || 'boys';  
24 - let headerData = headerModel.setNavHeader('OUTLET', channel, true, '', null); 24 + let headerData = headerModel.setNav({
  25 + navTitle: 'OUTLET',
  26 + navBtn: false
  27 + });
  28 +
25 let categoryId = req.query.category_id; 29 let categoryId = req.query.category_id;
26 let yhChannel = req.query.yh_channel || 1; 30 let yhChannel = req.query.yh_channel || 1;
27 let contentcode = req.query.content_code; 31 let contentcode = req.query.content_code;
@@ -29,15 +33,17 @@ exports.index = (req, res) => { @@ -29,15 +33,17 @@ exports.index = (req, res) => {
29 outletModel.getContent(categoryId, yhChannel, contentcode).then(result => { 33 outletModel.getContent(categoryId, yhChannel, contentcode).then(result => {
30 res.render('outlet', Object.assign({ 34 res.render('outlet', Object.assign({
31 page: 'outlet', 35 page: 'outlet',
32 - pageHeader: headerData,  
33 - pageFooter: true 36 + pageHeader: headerData
34 }, renderData, result)); 37 }, renderData, result));
35 }); 38 });
36 }; 39 };
37 40
38 -exports.activity = (req, res) => {  
39 - let channel = req.query.channel || req.cookies._Channel || 'boys';  
40 - let headerData = headerModel.setNavHeader('OUTLET', channel, true, '', null); 41 +// 奥莱活动详情页
  42 +exports.activityDetail = (req, res) => {
  43 + let headerData = headerModel.setNav({
  44 + navTitle: 'OUTLET',
  45 + navBtn: false
  46 + });
41 47
42 outletModel.getActivity(req.query.id).then(result => { 48 outletModel.getActivity(req.query.id).then(result => {
43 res.render('outlet/activity', Object.assign({ 49 res.render('outlet/activity', Object.assign({
@@ -48,14 +54,17 @@ exports.activity = (req, res) => { @@ -48,14 +54,17 @@ exports.activity = (req, res) => {
48 }); 54 });
49 }; 55 };
50 56
51 -exports.activityChannel = (req, res) => {  
52 - let channel = req.cookies._Channel || 'boys';  
53 - let headerData = headerModel.setNavHeader('OUTLET', channel, true, '', null); 57 +// 奥莱活动频道列表页
  58 +exports.activityList = (req, res) => {
  59 + let headerData = headerModel.setNav({
  60 + navTitle: 'OUTLET',
  61 + navBtn: false
  62 + });
54 let categoryId = req.query.category_id; 63 let categoryId = req.query.category_id;
55 let type = req.query.type || 2; 64 let type = req.query.type || 2;
56 let template = type === 2 ? 'outlet/will-end' : 'outlet/will-start'; 65 let template = type === 2 ? 'outlet/will-end' : 'outlet/will-start';
57 let page = type === 2 ? 'outlet-will-end' : 'outlet-will-start'; 66 let page = type === 2 ? 'outlet-will-end' : 'outlet-will-start';
58 - let yhChannel = yhChannelMap[channel]; 67 + let yhChannel = req.query.yh_channel || yhChannelMap[req.yoho.channel];
59 68
60 outletModel.getNearlyActivity(yhChannel, type, categoryId).then(result => { 69 outletModel.getNearlyActivity(yhChannel, type, categoryId).then(result => {
61 res.render(template, Object.assign({ 70 res.render(template, Object.assign({
@@ -29,11 +29,12 @@ const queryParam = { @@ -29,11 +29,12 @@ const queryParam = {
29 */ 29 */
30 const processPublicData = (req, title, page) => { 30 const processPublicData = (req, title, page) => {
31 let data = {}; 31 let data = {};
32 - let channel = req.query.channel || req.cookies._Channel || 'boys';  
33 - let headerData = headerModel.setNavHeader(title, channel); 32 + let headerData = headerModel.setNav({
  33 + navTitle: title
  34 + });
34 35
35 data = { 36 data = {
36 - channel: channel, 37 + channel: req.yoho.channel,
37 renderData: { 38 renderData: {
38 module: 'product', 39 module: 'product',
39 page: page, 40 page: page,
1 /** 1 /**
2 * 频道页面 model 2 * 频道页面 model
3 - * @author: Bi Kai<kai.bi@yoho.cn> 3 + * @author: 赵彪<bill.zhao@yoho.cn>
4 * @date: 2016/05/09 4 * @date: 2016/05/09
5 */ 5 */
6 'use strict'; 6 'use strict';
@@ -35,8 +35,8 @@ router.get('/sale/search', sale.search); @@ -35,8 +35,8 @@ router.get('/sale/search', sale.search);
35 router.get('/sale/filter', sale.filter); 35 router.get('/sale/filter', sale.filter);
36 36
37 router.get('/outlet', outlet.index); 37 router.get('/outlet', outlet.index);
38 -router.get('/outlet/activity', outlet.activity); 38 +router.get('/outlet/activity', outlet.activityDetail);
39 39
40 -router.get('/outlet/activityinfo', outlet.activityChannel); 40 +router.get('/outlet/activityinfo', outlet.activityList);
41 41
42 module.exports = router; 42 module.exports = router;
  1 +/**
  2 + * 设置频道
  3 + * @author: 赵彪<bill.zhao@yoho.cn>
  4 + * @date: 2016/6/16
  5 + */
  6 +
  7 +'use strict';
  8 +
  9 +module.exports = () => {
  10 + return (req, res, next) => {
  11 + if (!req.xhr) {
  12 + const obj = {};
  13 + const channel = req.query.channel || req.cookies._Channel || 'boys';
  14 +
  15 + obj[channel] = true;
  16 +
  17 + Object.assign(res.locals, {
  18 + pageChannel: obj
  19 + });
  20 +
  21 + req.yoho.channel = channel;
  22 + res.yoho.channel = channel;
  23 + }
  24 +
  25 + next();
  26 + };
  27 +};
@@ -8,36 +8,32 @@ @@ -8,36 +8,32 @@
8 8
9 const _ = require('lodash'); 9 const _ = require('lodash');
10 10
11 -// const getChannle = () => ('kids');  
12 -  
13 /** 11 /**
14 - * 设置头部  
15 - * @param {String} title 标题  
16 - * @param {Boolen} channel 频道  
17 - * @param {Boolen} backUrl 返回的url  
18 - * @param {Boolen} navBtn 是否显示导航  
19 - * @param {Boolen} navBack 是否显示返回箭头 12 + * 传配置对象设置头部
  13 + * @param {Object} opt 头部选项对象
  14 + * @return {Object} 头部数据对象
20 */ 15 */
21 -exports.setNavHeader = (title, channel, navBack, backUrl, navBtn) => {  
22 - let header = {  
23 - navTitle: title,  
24 - backUrl: backUrl,  
25 - navBack: navBack,  
26 - navBtn: navBtn 16 +exports.setNav = (opt) => {
  17 + let defaultOpt = {
  18 + navTitle: 'Yoho!Buy', // 标题
  19 + navBack: true, // 是否显示返回按钮
  20 + backUrl: '', // 返回按钮对应的url
  21 + navBtn: true // 顶部下拉菜单选项
27 }; 22 };
28 23
29 - header[channel] = true;  
30 24
31 - return _.merge({  
32 - navTitle: '',  
33 - backUrl: true,  
34 - navBack: '',  
35 - navBtn: {  
36 - indexUrl: '/?go=1',  
37 - categoryUrl: '/cate',  
38 - shoppingCartUrl: '/cart/index/index',  
39 - mineUrl: '/home'  
40 - },  
41 - boys: true  
42 - }, header); 25 + Object.assign(defaultOpt, opt || {});
  26 +
  27 + if (defaultOpt.navBtn || _.isNull(defaultOpt.navBtn)) {
  28 + Object.assign(defaultOpt, {
  29 + navBtn: { // 顶部下拉菜单选项
  30 + indexUrl: '/?go=1',
  31 + categoryUrl: '/cate',
  32 + shoppingCartUrl: '/cart/index/index',
  33 + mineUrl: '/home'
  34 + }
  35 + });
  36 + }
  37 +
  38 + return defaultOpt;
43 }; 39 };
1 {{#pageHeader}} 1 {{#pageHeader}}
  2 +{{# ../pageChannel}}
2 <header id="yoho-header" class="yoho-header{{#if boys}} boys{{/if}}{{#if girls}} girls{{/if}}{{#if kids}} kids{{/if}}{{#if lifeStyle}} life-style{{/if}}"> 3 <header id="yoho-header" class="yoho-header{{#if boys}} boys{{/if}}{{#if girls}} girls{{/if}}{{#if kids}} kids{{/if}}{{#if lifeStyle}} life-style{{/if}}">
  4 +{{/ ../pageChannel}}
3 {{#navBack}} 5 {{#navBack}}
4 <a href="{{#if backUrl}}{{backUrl}}{{^}}javascript:history.go(-1);{{/if}}" class="iconfont nav-back">&#xe610;</a> 6 <a href="{{#if backUrl}}{{backUrl}}{{^}}javascript:history.go(-1);{{/if}}" class="iconfont nav-back">&#xe610;</a>
5 {{/navBack}} 7 {{/navBack}}
@@ -20,7 +22,9 @@ @@ -20,7 +22,9 @@
20 {{/navTitle}} 22 {{/navTitle}}
21 </header> 23 </header>
22 {{#navBtn}} 24 {{#navBtn}}
23 -<div class="homebuttom hide {{#if ../boys}} boys{{/if}}{{#if ../girls}} girls{{/if}}{{#if ../kids}} kids{{/if}}{{#if ../lifeStyle}} life-style{{/if}}"> 25 +{{# ../../pageChannel}}
  26 +<div class="homebuttom hide {{#if boys}} boys{{/if}}{{#if girls}} girls{{/if}}{{#if kids}} kids{{/if}}{{#if lifeStyle}} life-style{{/if}}">
  27 +{{/ ../../pageChannel}}
24 <ul> 28 <ul>
25 <li> 29 <li>
26 <a href="{{ indexUrl }}"> 30 <a href="{{ indexUrl }}">