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

const mRoot = '../models';
const express = require('express');
const app = express();
const plustarModel = require(`${mRoot}/plustar`);
const htmlProcess = require(`${global.utils}/html-process`);
const headerModel = require('../../../doraemon/models/header'); // 头部model
const crypto = global.yoho.crypto;
let channels = {
    boys: '1,3',
    girl: '2,3',
    kids: '3,3',
    lifestyle: '4'
};

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

const 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') {
        plustarModel.getBrandsData(gender, starBrand, originalBrand, channel, isRecommend).then((result) => {
            res.render('plustar/list', {
                module: 'guang',
                page: 'plustar-list',
                title: '明星原创',
                pageHeader: headerModel.setNav({
                    navTitle: '明星原创'
                }),
                pageFooter: true,
                ps: result
            });
        }).catch(next);
    } else {
        plustarModel.getListData(gender, recom, all).then((result) => {

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

const getDetailData = (req, res, next) => {
    let id = req.query.id;
    let uid = '';
    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 clientType = req.body.client_type || '';
    let version = req.body.app_version || '';
    let userAgent = req.get('User-Agent');
    let isWeixin = userAgent.includes('MicroMessenger'); // 标识是否是微信访问

    if (req.yoho.isApp) {
       uid = crypto.encryption('', req.query.uid + '');
    } else {
        uid = req.user.uid;
    }

    if (clientType.toLowerCase() === 'ios' && version) {
        clientType = 'iphone';
    } else if (clientType.toLowerCase() === 'android' && version) {
        clientType = 'android';
    } else {
        clientType = 'h5';
    }
    plustarModel.getDetailData(id, uid, udid, gender, isApp, clientType).then((result) => {
        result.brand_intro = htmlProcess.removeHtml(result.brand_intro);

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

    }).catch(next);
};

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

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

module.exports = {
    getListData,
    getDetailData,
    getDetailDataAsync
};