index.js 3.53 KB
/**
 * 频道页面
 * @author: Bi Kai<kai.bi@yoho.cn>
 * @date: 2016/05/09
 */
'use strict';
const library = '../../../library';
const _ = require('lodash');
const channelModel = require('../models/channel');
const helpers = require(`${library}/helpers`);

let _renderData = {
    module: 'channel',
    page: 'home',
    homeHeader: {
        searchUrl: helpers.urlFormat('/search', null, 'search')
    },
    maybeLike: true,
    showFooterTab: {
        indexUrl: helpers.urlFormat('/?go=1'), // 首页
        categoryUrl: helpers.urlFormat('/cate'), // 分类
        guangUrl: helpers.urlFormat('', null, 'guang'), // 逛首页
        shoppingCartUrl: helpers.urlFormat('/cart/index/index'), // 购物车
        mineUrl: helpers.urlFormat('/home') // 个人中心
    },
    pageFooter: true
};

/**
 * 频道页生成函数
 * @param  {[object]} req
 * @param  {[object]} res
 * @param  {[object]} data 自定义数据
 * @return {[type]}
 */
let _channelPage = (req, res, data) => {
    return channelModel.getChannelData({ // TODO 内部的Promise方法必须 return 出来
        gender: data.gender,
        uid: _.toString(req.user.uid)
    }).then(result => {
        res.render('channel', Object.assign({}, _renderData, data, result));
    });
};

/**
 * 频道选择页
 */
let index = (req, res, next) => {
    channelModel.getChannelSwitchData().then((result) => {
        res.render('index', {
            module: 'channel',
            page: 'index',
            title: 'Yoho!Buy 有货',
            searchUrl: helpers.urlFormat('/', null, 'search'),
            pageFooter: true,
            channelList: result[0].channelList,
            yohood: result[0].yohood,
            background: result[1]
        });
    }).catch(next);
};

/**
 * 频道页,根据查询字符串跳转频道中间件
 * @param  {object} req
 * @param  {object} res
 * @param  {Function} next
 * @return {Function}
 */
let switchChannel = (req, res, next) => {
    let channel = req.cookies._Channel;

    // 如果查询字符串设置了 go 参数,跳转到 cookie 中设置的频道页
    if (req.query.go && channel) {
        res.redirect('/' + channel);
    } else {
        // 设置浏览器缓存5分钟 300000ms
        res.set('Expires', (new Date(_.now() + 300000)).toGMTString());
        return next();
    }
};

/**
 * 男生首页
 */
let boys = (req, res, next) => {
    _channelPage(req, res, {
        gender: 'boys',
        title: '男生首页',
        boysHomePage: true
    }).catch(next); // TODO 我们在路由处理的最上层的方法处理catch
};

/**
 * 女生首页
 */
let girls = (req, res, next) => {
    _channelPage(req, res, {
        gender: 'girls',
        title: '女生首页',
        girlsHomePage: true
    }).catch(next);
};

/**
 * 潮童首页
 */

let kids = (req, res, next) => {
    _channelPage(req, res, {
        gender: 'kids',
        title: '潮童首页',
        kidsHomePage: true
    }).catch(next);
};

/**
 * 创意生活首页
 */
let lifestyle = (req, res, next) => {
    _channelPage(req, res, {
        gender: 'lifestyle',
        title: '创意生活首页',
        lifestyleHomePage: true
    }).catch(next);
};

/**
 * 频道页底部 bannel
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
let bottomBanner = (req, res, next) => {
    let gender = req.query.gender || 'boys';

    channelModel.getBottomBannerData(gender).then(result => {
        res.send(result);
    }).catch(next);
};

module.exports = {
    switchChannel,
    index,
    boys,
    girls,
    kids,
    lifestyle,
    bottomBanner
};