channel.js 1.85 KB
/**
 * 主页
 * @author: Bi Kai<kai.bi@yoho.cn>
 * @date: 2016/05/09
 */
'use strict';
const channelModel = require('../models/channel');
const channelMap = {
    men: '9ee58aadd9559d07207fe4a98843eaac',
    women: '3ad8826fc89fb0d023a4cd06a6991219',
    lifestyle: 'aa8d34c85934c2ccc16e2babd3eb5e47'
};
const _ = require('lodash');

/**
 * 频道选择页
 */
module.exports = {
    index(req, res, next) {
        let channel = req.path.split('/')[1] || req.yoho.channel;

        channelModel.getResourcesData({
            contentCode: channelMap[channel]
        }).then(result => {
            const resources = result.slice(0, 3);

            _.each(resources, (resource) => {
                // 只拿第一个数据
                if (_.isArray(resource.data)) {
                    resource.data = [resource.data[0]];
                }
            });

            res.locals.resources = resources;

            res.render('cindex', {
                module: 'channel',
                page: 'home',
                channel: channel
            });
        }).catch(next);
    },
    channel(req, res, next) {
        channelModel.getChannelData().then(result => {
            return res.json(result);
        }).catch(next);
    },
    resources(req, res, next) {
        channelModel.getResourcesData(req.query).then(result => {
            return res.json(result);
        }).catch(next);
    },
    goods(req, res, next) {
        channelModel.getGoodsData(req.query).then(result => {
            return res.json(result);
        }).catch(next);
    },
    sidebar(req, res, next) {
        if (!req.xhr) {
            return res.render('sidebar', {
                module: 'channel',
                page: 'sidebar'
            });
        }
        channelModel.getSidebarData(req.query).then(result => {
            return res.json(result);
        }).catch(next);
    }
};