index.js 3.06 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`);
const log = require(`${library}/logger`);

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

const channelLogger = (err, res) => {
    log.error('频道页面渲染错误:' + JSON.stringify(err));
    res.send('error');
};

/**
 * 频道页生成函数
 * @param  {[object]} req
 * @param  {[object]} res
 * @param  {[object]} data 自定义数据
 * @return {[type]}
 */
const channelPage = (req, res, data) => {
    channelModel.getChannelDate({
        gender: data.gender,
        uid: _.toString(req.user.uid)
    }).then(result => {
        res.render('channel', Object.assign(renderData, data, result));
    }).catch((err) => {
        channelLogger(err, res);
    });
};

/**
 * 频道选择页
 */
// exports.index = (req, res) => {
//
// };

/**
 * 频道页,根据查询字符串跳转频道中间件
 * @param  {object} req
 * @param  {object} res
 * @param  {Function} next
 * @return {Function}
 */
exports.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();
    }
};

/**
 * 男生首页
 */
exports.boys = (req, res) => {
    channelPage(req, res, {
        gender: 'boys',
        title: '男生首页',
        boysHomePage: true
    });
};

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

/**
 * 潮童首页
 */

exports.kids = (req, res) => {
    channelPage(req, res, {
        gender: 'kids',
        title: '潮童首页',
        boysHomePage: true
    });
};

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

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

    channelModel.getBottomBannerDate(gender).then(result => {
        res.send(result);
    }).catch((err) => {
        channelLogger(err, res);
    });
};