sale.js 5.95 KB
/**
 * sale controller
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/5/17
 */

'use strict';
const mRoot = '../models';

// const cookie = require(`${library}/cookie`);
const headerModel = require('../../../doraemon/models/header');
const saleModel = require(`${mRoot}/sale`);

// const queryParam = {
//     brand: '0',
//     gender: '1,2,3',
//     sort: '0',
//     size: '0',
//     price: '0',
//     pD: '0.1,0.9'
// };

/**
 * 公共数据处理
 * @param  {[object]} req
 * @param  {[string]} title 标题
 * @param  {[string]} page js文件page名称
 * @return {[type]}
 */
const _processPublicData = (req, title, page, backUrl) => {
    return {
        channel: req.yoho.channel,
        renderData: {
            module: 'product',
            page: page,

            // title: title,
            saleNav: true,
            pageHeader: headerModel.setNav({
                navTitle: title,
                navBtn: false,
                backUrl: backUrl || ''
            }),
            pageFooter: true
        }
    };
};

/**
 * sale 首页 资源位获取
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
let index = (req, res, next) => {
    let refer = req.get('Referer') || `${global.yoho.config.siteUrl}/${req.cookies._Channel || ''}`;
    let reqPath = req.baseUrl + req.path;
    let backUrl = refer.indexOf(reqPath) === -1 ? refer : '';

    if (['boys', 'girls', 'kids', 'lifestyle'].indexOf(req.yoho.channel) === -1) {
        return next();
    }

    backUrl && res.cookie('saleRefer', backUrl, {
        domain: 'm.yohobuy.com'
    });
    backUrl = backUrl || req.cookies.saleRefer;

    let params = _processPublicData(req, 'SALE', 'sale', backUrl);

    saleModel.getSaleData(params.channel).then((result) => {

        // 扩展头部频道选择数据
        Object.assign(params.renderData.pageHeader, {
            saleNav: saleModel.saleNav(req.yoho.channel)
        });

        // 此处 channel 需要读取 cookies 的 channel
        if (req.cookies._Channel) {
            res.locals.pageChannel = {};
            res.locals.pageChannel[req.cookies._Channel] = true;
        }

        if (!result.length) {
            res.set('Cache-Control', 'no-cache');
        }
        res.render('sale/index', Object.assign(params.renderData, {
            content: result,
            localCss: true,
            floorHeader: {
                title: {
                    title: '最新降价'
                }
            }
        }));
    }).catch(next);
};

/**
 * 断码区
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
let breakingYards = (req, res, next) => {
    let params = _processPublicData(req, '断码区', 'break-code');

    saleModel.getBreakCodeData({
        yhChannel: params.channel
    }).then((result) => {

        res.render('sale/break-code', Object.assign(params.renderData, result));
    }).catch(next);
};

/**
 * 折扣专场列表
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
let discount = (req, res, next) => {
    let params = _processPublicData(req, '折扣专场', 'discount');

    saleModel.getDiscountData(params.channel).then((result) => {
        res.render('sale/discount', Object.assign(params.renderData, {
            data: result
        }));
    }).catch(next);
};


/**
 * 折扣专场专题详情
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
let discountDetail = (req, res, next) => {
    let id = req.query.id;
    let params = _processPublicData(req, '', 'discount-detail');

    saleModel.getDiscountDetailData(id, req.yoho.channel).then((result) => {
        params.renderData.pageHeader.navTitle = result.title;

        res.render('sale/discount-detail', Object.assign(params.renderData, result));
    }).catch(next);
};

let getTime = (req, res, next) => {
    let id = req.query.id;

    saleModel.getDiscountTime(id, req.yoho.channel).then((time) => {
        res.json(time);
    }).catch(next);
};

/**
 * vip 会员专享
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
let vip = (req, res, next) => {
    let params = _processPublicData(req, '会员专享', 'vip');

    saleModel.getVipData(params.channel).then((result) => {
        res.render('sale/vip', Object.assign(params.renderData, {
            content: result
        }));
    }).catch(next);
};

/**
 * 筛选
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
let filter = (req, res, next) => {
    let params = Object.assign({}, req.query);

    saleModel.getFilterData(params).then((result) => {
        res.render('sale/filter', {
            layout: false,
            params: params,
            filter: result
        });
    }).catch(next);
};


/**
 * 商品搜索
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
let search = (req, res, next) => {
    let params = req.query; // tar modified 161121 safe
    let uid = req.user.uid || 0;

    // 断码区域必须传尺码
    if (params.saleType === '1' && (!params.breakSort || !params.breakSize)) {
        return res.render('sale/product', {
            layout: false,
            params: params
        });
    }

    saleModel.getSearchData(params, uid).then((result) => {
        let vipLevel = result[1].cur_level;
        let vipObj = {};

        if (req.query.saleType === '2') {
            vipObj = {
                saleVip: (req.query.saleType === '2' && (!uid || vipLevel === '0')),
                vipLevel: vipLevel,
                saleViplogin: vipLevel >= 1 ? true : false,
                vipPrice1: vipLevel === '1',
                vipPrice2: vipLevel === '2',
                vipPrice3: vipLevel === '3'
            };
        }

        res.render('sale/product', Object.assign({
            layout: false,
            params: params,
            goods: result[0]
        }, vipObj));
    }).catch(next);
};

module.exports = {
    index,
    breakingYards,
    discount,
    discountDetail,
    vip,
    filter,
    search,
    getTime
};