index.js 2.73 KB
/**
 * girls controller
 * @author: 赵彪<bill.zhao@yoho.cn>
 * @date: 2016/05/16
 */

'use strict';

const _ = require('lodash');

const ChannelModel = require('../models/index');

exports.index = (req, res, next) => {
    let channelType = req.path.split('/')[1] || 'boys';
    let poolId = req.query.template_id;
    let isJKChannel = req.query.mChannel === 'japanKorean';

    req.ctx(ChannelModel).getContent(channelType, isJKChannel, poolId).then(data => {

        // channel为空不缓存
        if (_.isEmpty(data.channel)) {
            res.set('Cache-Control', 'no-cache');
        }
        res.render('channel', data);
    }).catch(next);
};

// 日韩馆频道页
exports.japanKorean = (req, res, next) => {

    req.ctx(ChannelModel).getJKContent(req).then(data => {

        // channel为空不缓存
        if (_.isEmpty(data.channel)) {
            res.set('Cache-Control', 'no-cache');
        }
        res.render('channel', data);
    }).catch(next);
};

exports.getNewArrival = (req, res, next) => {
    let reqBody = req.body,
        pageIndex = reqBody.pageIndex,
        pageCount = reqBody.pageCount,
        channel = reqBody.type || req.yoho.channel,
        goods = [],
        result = {},
        isJKChannel = reqBody.mChannel === 'japanKorean' ? true : false,
        poolId = reqBody.poolId;

    if (pageIndex < 0) {
        pageIndex = 0;
    }
    if (pageCount < 0 || pageCount > 50) {
        pageCount = 20;
    }

    req.ctx(ChannelModel).getNewArrival(channel, isJKChannel, poolId).then(data => {
        goods = _.slice(data, pageIndex, pageIndex + pageCount);

        if (goods.length !== 0) {
            result = {
                code: 200,
                goods: goods
            };
        }
        res.send(result);
    }).catch(next);
};

exports.getIndexGuide = (req, res, next) => {
    req.ctx(ChannelModel).getIndexGuideData().then(data => {
        if (data.code !== 200) {
            const err = new Error('异常');

            throw err;
        }
        return req.ctx(ChannelModel).formatIndexGuideData(data.data);
    }).then(data => {
        return req.ctx(ChannelModel).getResourceData(data);
    }).then(data => {
        let result = {list: data, layout: false};

        res.render('guide', result);
    }).catch(next);
};

exports.hasNewUserFloor = (req, res, next) => {
    let uid = req.user.uid;

    if (!uid) {
        return res.send({code: 200, isNewUser: false});
    }

    req.ctx(ChannelModel).hasNewUserFloor(req.yoho.channel, req.user.uid).then(data => {
        res.send(data);
    }).catch(next);
};


// 频道页301重定向
exports.redirect = (goUrl) => {
    return function(req, res, next) {
    if (req.path === goUrl) {
        return next();
    }

        res.redirect(301, goUrl);
    };
};