star.js 4.05 KB
/**
 * 新潮教室
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/05/30
 */
'use strict';

const mRoot = '../models';
const library = '../../../library';
const _ = require('lodash');
const headerModel = require('../../../doraemon/models/header');
const starModel = require(`${mRoot}/star`);
const helpers = require(`${library}/helpers`);

const headTab = [
    {
        url: '/guang/star',
        name: '全部'
    },
    {
        url: '/guang/star/special',
        name: '星专题'
    },
    {
        url: '/guang/star/collocation',
        name: '星搭配'
    }
];

const processPublicData = (req, title) => {
    let channel = req.query.channel || req.cookies._Channel || 'boys';
    let headerData = headerModel.setNavHeader(title, channel);
    let renderData = {
        module: 'guang',
        title: title,
        pageHeader: headerData
    };

    return renderData;
};

/**
 * 星潮教室首页
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
exports.index = (req, res) => {
    starModel.getIndexData().then((result) => {
        const pageHeadTab = _.cloneDeep(headTab);

        pageHeadTab[0].cur = true;
        res.render('star/index', _.assign({
            page: 'star'
        }, processPublicData(req, '星潮教室'), {
            content: {
                focus: true,
                data: result.ads
            },
            isStarIndexPage: true,
            headTab: pageHeadTab,
            starAvatar: result.starAvatar,
            articles: result.articles
        }));
    });
};

/**
 * 星专题
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
exports.special = (req, res) => {
    starModel.getSpecialData().then((result) => {
        const pageHeadTab = _.cloneDeep(headTab);

        pageHeadTab[1].cur = true;
        res.render('star/special', _.assign({
            page: 'special'
        }, processPublicData(req, '星潮教室'), {
            resources: result,
            headTab: pageHeadTab
        }));
    });
};


/**
 * 星搭配
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
exports.collocation = (req, res) => {
    const pageHeadTab = _.cloneDeep(headTab);

    pageHeadTab[2].cur = true;
    res.render('star/collocation', _.assign({
        isStarDetailPage: true,
        page: 'collocation'
    }, processPublicData(req, '星潮教室'), {
        headTab: headTab
    }));
};

/**
 * 星搭配文章请求
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
exports.collocationList = (req, res) => {
    let params = req.query;
    let uid = req.user.uid || 0;

    starModel.getCollocationListData(params, uid).then((result) => {
        res.render('star/list', _.assign({
            layout: false,
            params: params,
            list: result,
            isApp: req.query.app_version || false
        }));
    });
};

/**
 * 收藏文章
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}
 */
exports.setFavorite = (req, res) => {
    let params = req.body;
    let uid = req.user.uid || 0;

    starModel.setFavorite(params, uid).then((result) => {
        if (result.code === 401) {
            result.data = helpers.urlFormat('/signin.html', {
                refer: req.get('Referer') || '/guang/star/collocation'
            });
        }
        res.json(result);
    });
};

/**
 * 明星文章专区
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
exports.detail = (req, res) => {
    res.render('star/detail', _.assign({
        isStarDetailPage: true,
        page: 'detail-list'
    }, processPublicData(req, req.query.tag)));
};

/**
 * 明星专题文章请求
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
exports.detailList = (req, res) => {
    let params = req.query;

    starModel.getDetailData(params).then((result) => {
        res.render('star/list', _.assign({
            layout: false,
            params: params,
            list: result,
            isApp: req.query.app_version || false
        }));
    });
};