plusstar.js 4.67 KB
/**
 * 潮流优选
 * @author: xiaoxiao<xiaoxiao.hao@yoho.cn>
 * @date: 2016/08/04
 */
'use strict';

const mRoot = '../models';
const typeLib = require('../../../config/type-lib');
const plusstarModel = require(`${mRoot}/plusstar`);
const headerModel = require('../../../doraemon/models/header'); // 头部model
const _ = require('lodash');
const qs = require('querystring');
let channels = {
    boys: 1,
    girl: 2,
    kids: 3,
    lifestyle: 4
};

/**
 * 潮流优选首页
 */
exports.index = (req, res, next) => {
    let isApp = req.query.app_version || req.query.appVersion || false;
    let parameter = {};
    let title = '潮流优选';
    let gender = req.query.gender ||
        req.query.channel && typeLib.channels[req.query.channel] ||
        req.cookies._Channel && channels[req.cookies._Channel] ||
        1;

    if (isApp === false) {
        parameter = {
            pageHeader: headerModel.setNav({
                navTitle: title
            }),
            pageFooter: true
        };
    }

    let result = {};

    switch (gender) {
        case typeLib.gender.boys:
            gender = 1;
            break;
        case typeLib.gender.girls:
            gender = 2;
            break;
        case typeLib.gender.kids:
            gender = 3;
            break;
        case typeLib.gender.lifestyle:
            gender = 4;
            break;
        default:
            break;
    }

    // 唤起 APP 的路径
    res.locals.appPath = `yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.h5","params":{"url":"http://guang.m.yohobuy.com${req.path}","param":${JSON.stringify(req.query)}}}`;

    req.ctx(plusstarModel).getAllChannels({
        gender: gender,
        app_type: 0
    }).then(data => {
        result = Object.assign(result, data);
        let focus = _.find(result.channel, d => d.focus) || {};
        let code = focus.code || '';

        return req.ctx(plusstarModel).getResourcesData({
            content_code: code,
            yh_channel: 1
        }, {
            isApp: isApp
        });
    }).then(data => {
        result = Object.assign(result, data);

        // 此处 channel 需要读取 cookies 的 channel
        if (req.cookies._Channel) {
            res.locals.pageChannel = {};
            res.locals.pageChannel[req.cookies._Channel] = true;
        }
        res.render('plusstar/index', Object.assign({
            page: 'plusstar-index',
            result: result,
            isApp: isApp,
            title: title,
            localCss: true
        }, parameter));
    }).catch(next);
};

// 301到新路由
exports.indexRedirect = (req, res) => {
    let param = qs.stringify(req.query);

    if (param) {
        param = '?' + param;
    }
    res.redirect(301, `/guang/${req.yoho.channel}-plusstar/${param}`);
};

exports.userSkn = (req, res, next) => {
    let isApp = req.body.app_version || req.body.appVersion || false;
    let uid = req.user.uid;
    let param = {};
    let skns = req.body.skns || '';

    skns = skns.split(',') || [];

    if (uid) {
        param.uid = uid;
        param.content_code = req.body.code || '';
        param.yh_channel = req.body.yh_channel || 1;

        req.ctx(plusstarModel).getFashionPrefer(param, {
            isApp: isApp
        }).then(result => {
            let preferSkns = result.data || [];

            skns = _.uniq(skns.concat(preferSkns));
            res.json(skns);
        }).catch(next);
    } else {
        res.json(skns);
    }
};

/**
 * 潮流优选首页-资源位
 */
exports.resourcesTemplate = (req, res, next) => {
    let isApp = req.query.app_version || req.query.appVersion || false;
    let uid = req.user.uid || req.query.uid;
    let param = {};

    if (uid) {
        param.uid = uid;
    }

    param.content_code = req.query.code || '';
    param.yh_channel = req.query.yh_channel || 1;


    req.ctx(plusstarModel).getResourcesData(param, {
        isApp: isApp
    }).then(result => {
        res.render('plusstar/resources-template', {
            layout: false,
            result: result,
            title: '潮流优选'
        });
    }).catch(next);
};

/**
 * 潮流优选首页-资源位-商品列表
 */
exports.resourcesGoodsList = (req, res, next) => {
    let productSkn = req.body.productSkn || '';
    let isApp = req.body.app_version || req.body.appVersion || false;
    let param = {};

    param = {
        productSkn: productSkn.split(',')
    };

    req.ctx(plusstarModel).getProductBatch(param, {
        isApp: isApp,
        showSimilar: true
    }).then(result => {
        res.render('plusstar/resources-goodsList', {
            layout: false,
            result: result,
            title: '潮流优选',
            _noLazy: true
        });
    }).catch(next);
};