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

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

// const headerModel = require('../../../doraemon/models/header');

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

const processPublicData = (req, title) => {
    // let headerData = headerModel.setNav({
    //     navTitle: title,
    //     navBtn: navBtn
    // });

    let renderData = {
        module: 'guang',
        title: title

        // pageHeader: headerData
    };

    if (req.query.app_version || req.query.appVersion) {
        renderData.pageHeader = false;
    }

    return renderData;
};

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

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

exports.getIndexHtml = (req, res, next) => {
    starModel.getIndexData().then((result) => {
        res.render('star/index-html', _.assign({
            layout: false,
            content: {
                focus: true,
                data: result.ads
            },
            starAvatar: result.starAvatar.concat(result.starAvatar).concat(result.starAvatar), // 需要优化,数组重复三次
            articles: result.articles
        }));
    }).catch(next);
};

/**
 * 星专题
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
exports.special = (req, res, next) => {
    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,
            notIndex: true
        }));
    }).catch(next);
};


/**
 * 星搭配
 * @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: pageHeadTab,
        notIndex: true
    }));
};

/**
 * 星搭配文章请求
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
exports.collocationList = (req, res, next) => {
    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 || req.query.appVersion || false
        }));
    }).catch(next);
};

/**
 * 收藏文章
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}
 */
exports.setFavorite = (req, res, next) => {
    let params = req.body;
    let uid = req.body.uid || 0; // 客户端访问,不能使用 cookie
    let isApp = req.query.app_version || req.query.appVersion || false;
    let url = '/guang/star/collocation';
    let urlEncode = '';

    starModel.setFavorite(params, uid).then((result) => {
        if (result.code === 401) {
            if (!isApp) {
                result.data = helpers.urlFormat('/signin.html', {
                    refer: req.get('Referer') || url
                });
            } else {
                urlEncode = '\/guang\/star\/collocation';
                result.data = `${url}?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"http:\/\/m.yohobuy.com${urlEncode}","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"N"}}`; // eslint-disable-line
            }
        }
        res.json(result);
    }).catch(next);
};

/**
 * 明星文章专区
 * @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, false)));
};

/**
 * 明星专题文章请求
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
exports.detailList = (req, res, next) => {
    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 || req.query.appVersion || false
        }));
    }).catch(next);
};