index.js 6.31 KB
/**
 * 频道页面
 * @author: Bi Kai<kai.bi@yoho.cn>
 * @date: 2016/05/09
 */
'use strict';
const channelModel = require('../models/channel');

// const homeModel = require('../../home/models/index');
const footerModel = require('../../../doraemon/models/footer_tab'); // 底部tab

const helpers = global.yoho.helpers;

let _renderData = {
    module: 'channel',
    page: 'home',
    homeHeader: {
        searchUrl: helpers.urlFormat('/search', null, 'search')
    },
    maybeLike: true,
    pageFooter: true
};

/**
 * 频道页生成函数
 * @param  {[object]} req
 * @param  {[object]} res
 * @param  {[object]} data 自定义数据
 * @return {[type]}
 */
let _channelPage = (req, res, data) => {
    return req.ctx(channelModel).getChannelData({
        gender: data.gender,
        uid: req.user.uid
    }).then(result => {
        _renderData.homeHeader.defaultTerms = result.defaultTerms;

        if (!result.content.length || !result.sideNav.length) {
            res.set('Cache-Control', 'no-cache');
        }

        if (result && result.content && result.content.length) {
            result.content.length = 8; // 首屏先获取前8个楼层,其余用ajax获取
        }

        // result.content = [{
        //     seckill: true,
        //     data: {
        //         title: {
        //             name: '限时秒抢',
        //             title: '限时秒抢',
        //             moreUrl: 'http://m.yohobuy.com'
        //         }
        //     }
        // }].concat(result.content);
        // console.log(result.content[9]);
        let gender;

        if (data.gender === 'boys') {
            gender = '1,3';
        } else if (data.gender === 'girls') {
            gender = '2,3';
        } else {
            gender = '1,2,3';
        }

        res.render('channel', Object.assign({}, _renderData, data, result, {
            localCss: true,
            showFooterTab: footerModel.getUrlData('home', gender),
        }));
    });
};

/**
 * 获取首页频道其余楼层
 */
exports.getResourceContent = (req, res, next) => {

    return req.ctx(channelModel).getChannelResource({
        gender: req.query.gender,
        uid: req.user.uid,
    }).then(result => {

        if (result.length) {
            result = result.slice(8);
        }

        res.render('channel-resource', {
            layout: false,
            content: result
        });
    }).catch(next);
};

/**
 * 频道选择页
 */
exports.index = (req, res, next) => {
    req.ctx(channelModel).getChannelSwitchData().then((result) => {
        res.render('channel-index', {
            module: 'channel',
            page: 'index',
            title: 'Yoho!Buy 有货',
            searchUrl: helpers.urlFormat('/', null, 'search'),
            pageFooter: true,
            channelList: result[0].channelList,
            yohood: result[0].yohood,
            double11: result[0].double11,
            background: result[1],
            appPath: 'yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.home","params":{"channel":"1"}}'
        });
    }).catch(next);
};

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

    // 如果查询字符串设置了 go 参数,跳转到 cookie 中设置的频道页
    if (req.query.go && channel) {
        return res.redirect('/' + channel);
    }

    // 设置浏览器缓存5分钟 300000ms
    // res.set('Expires', (new Date(_.now() + 300000)).toGMTString());
    next();
};

/**
 * 男生首页
 */
exports.boys = (req, res, next) => {
    _channelPage(req, res, {
        gender: 'boys',

        // title: '男生 | Yoho!Buy有货 | 潮流购物逛不停',
        boysHomePage: true,
        appPath: 'yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.home","params":{"channel":"1"}}'
    }).catch(next); // TODO 我们在路由处理的最上层的方法处理catch
};

/**
 * 女生首页
 */
exports.girls = (req, res, next) => {
    _channelPage(req, res, {
        gender: 'girls',

        // title: '女生 | Yoho!Buy有货 | 潮流购物逛不停',
        girlsHomePage: true,
        appPath: 'yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.home","params":{"channel":"2"}}'
    }).catch(next);
};

/**
 * 潮童首页
 */

exports.kids = (req, res, next) => {
    _channelPage(req, res, {
        gender: 'kids',

        // title: '潮童 | Yoho!Buy有货 | 潮流购物逛不停',
        kidsHomePage: true,
        appPath: 'yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.home","params":{"channel":"3"}}'
    }).catch(next);
};

/**
 * 创意生活首页
 */
exports.lifestyle = (req, res, next) => {
    _channelPage(req, res, {
        gender: 'lifestyle',

        // title: '创意生活 | Yoho!Buy有货 | 潮流购物逛不停',
        lifestyleHomePage: true,
        appPath: 'yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.home","params":{"channel":"4"}}'
    }).catch(next);
};

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

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

/**
 * 店铺推荐收藏状态人数
 */
exports.shopRecom = (req, res, next) => {
    req.ctx(channelModel).shopRecom({
        shopIds: req.body.shopIds || '',
        uid: req.user.uid || 0,
    }).then(result => {
        res.send(result);
    }).catch(next);
};

/**
 * 获取用户vip信息
 */
// exports.userVip = (req, res, next) => {
//     let uid = req.user.uid;

//     if (!uid) {
//         res.json({
//             code: 555,
//             msg: '未登录'
//         });
//     } else {
//         req.ctx(homeModel).getGradeGrade(uid, req.query.channel || 1).then(result => {
//             if (result.code === 200) {
//                 res.json({
//                     code: 200,
//                     current_vip_level: result.data.current_vip_level,
//                 });
//             } else {
//                 res.json({
//                     code: 500,
//                     msg: '出错了',
//                 });
//             }
//         }).catch(next);
//     }
// };