plustar.js 4.5 KB
/**
 * 国际优选
 * @author: zxr<xiaoru.zhang@yoho.cn>
 * @date: 2016/09/12
 */
'use strict';

const mRoot = '../models';
const plustarModel = require(`${mRoot}/plustar`);
const htmlProcess = require(`${global.utils}/html-process`);
const headerModel = require('../../../doraemon/models/header'); // 头部model
const _ = require('lodash');

let channels = {
    boys: '1,3',
    girl: '2,3',
    kids: '3,3',
    lifestyle: '4'
};

let yhChannel = {
    boys: 1,
    girl: 2
};

exports.getListData = (req, res, next) => {
    let gender = req.query.gender || req.cookies._Channel && channels[req.cookies._Channel] || '1,3';
    let recom = '4';
    let all = '1';
    let type = req.query.type;
    let channel = req.cookies._Channel && yhChannel[req.cookies._Channel] || 1;
    let isRecommend = '0';
    let starBrand = '2';
    let originalBrand = '3';

    if (type === '2') {
        req.ctx(plustarModel).getBrandsData(gender, starBrand, originalBrand, channel, isRecommend).then((result) => {

            _.forEach(result, r => {
                let list = r.list || [];
                let head3 = _.take(list, 3);

                _.forEach(head3, d => {
                    d._noLazy = true;
                });
            });

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

            res.render('plustar/list', {
                module: 'guang',
                page: 'plustar-list',
                title: '明星原创',
                pageHeader: headerModel.setNav({
                    navTitle: '明星原创'
                }),
                pageFooter: true,
                ps: result,
                localCss: true
            });
        }).catch(next);
    } else {
        req.ctx(plustarModel).getListData(gender, recom, all).then((result) => {
            _.forEach(result, r => {
                let list = r.list || [];
                let head3 = _.take(list, 3);

                _.forEach(head3, d => {
                    d._noLazy = true;
                });
            });
            res.render('plustar/list', {
                module: 'guang',
                page: 'plustar-list',
                title: '国际优选',
                pageHeader: headerModel.setNav({
                    navTitle: '国际优选'
                }),
                pageFooter: true,
                ps: result,
                localCss: true
            });
        }).catch(next);
    }
};

exports.getDetailData = (req, res, next) => {
    let uid = req.user.uid || req.query.uid;
    let id = req.query.id;
    let udid = req.sessionID || 'yoho';
    let gender = req.query.gender || req.cookies._Channel && channels[req.cookies._Channel] || '1,3';
    let isApp = req.query.app_version || req.query.appVersion || false;
    let isWeixin = req.yoho.isWechat; // 标识是否是微信访问

    if (isApp) {
        if (req.query.uid) {
            uid = req.query.uid;
        }
    } else {
        uid = req.user.uid;
    }

    req.ctx(plustarModel).getDetailData(id, uid, udid, gender, isApp).then((result) => {
        result.brand_intro = htmlProcess.removeHtml(result.brand_intro);

        if (!isApp & !isWeixin) {
            res.render('plustar/detail', {
                module: 'guang',
                page: 'plustar-detail',
                title: result.brand_name,
                pageHeader: headerModel.setNav({
                    navTitle: result.brand_name
                }),
                ps: result,
                localCss: true
            });
        } else {
            res.render('plustar/detail', {
                module: 'guang',
                page: 'plustar-detail',
                title: result.brand_name,
                ps: result,
                uid: uid,
                isApp: isApp ? true : false,
                localCss: true
            });
        }

    }).catch(next);
};

/**
 * [品牌详情异步数据]
 */
exports.getDetailDataAsync = (req, res, next) => {
    let uid = req.user.uid;
    let udid = req.sessionID || 'yoho';
    let brandId = req.body.brand_id || 0;
    let isApp = req.body.isApp || false;

    if (!brandId) {
        return res.json({code: 400, message: '参数错误'});
    }
    req.ctx(plustarModel).getDetailDataAsync(brandId, uid, udid, isApp).then((result) => {
        res.json({
            isLike: result.isLike,
            infos: result.infos
        });
    }).catch(next);
};