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

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

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

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

const saleLogger = (err, res) => {
    log.error('sale页面渲染错误:' + JSON.stringify(err));
    res.send('error');
};

/**
 * sale 首页 资源位获取
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
exports.index = (req, res) => {
    let headerData = headerModel.setNavHeader('SALE');
    let renderData = {
        module: 'product',
        page: 'sale',
        title: 'SALE',
        pageHeader: headerData,
        pageFooter: true
    };

    saleModel.getSaleData().then((result) => {
        res.render('sale/index', Object.assign(renderData, queryParam, {
            content: result,
            floorHeader: {
                title: {
                    title: '最新降价'
                }
            }
        }));
    }).catch((err) => {
        saleLogger(err, res);
    });
};

/**
 * 断码区
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
exports.breakingYards = (req, res) => {
    let headerData = headerModel.setNavHeader('断码区');
    let renderData = {
        module: 'product',
        page: 'break-code',
        title: '断码区',
        pageHeader: headerData,
        pageFooter: true
    };

    saleModel.getBreakCodeData({
        yhChannel: req.query.channel
    }).then((result) => {
        res.render('sale/break-code', Object.assign(renderData, result));
    }).catch((err) => {
        saleLogger(err, res);
    });
};

/**
 * 折扣专场列表
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
exports.discount = (req, res) => {
    let headerData = headerModel.setNavHeader('折扣专场');
    let renderData = {
        module: 'product',
        page: 'discount',
        pageHeader: headerData,
        pageFooter: true
    };

    saleModel.getDiscountData().then((result) => {
        res.render('sale/discount', Object.assign(renderData, result));
    }).catch((err) => {
        saleLogger(err, res);
    });
};


/**
 * 折扣专场专题详情
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
exports.discountDetail = (req, res) => {
    let headerData = headerModel.setNavHeader();
    let id = req.query.id;
    let renderData = {
        module: 'product',
        page: 'discount-detail',
        pageFooter: true
    };

    saleModel.getDiscountDetailData(id).then((result) => {
        headerData.navTitle = result.title;
        renderData.pageHeader = headerData;
        res.render('sale/discount-detail', Object.assign(renderData, result));
    }).catch((err) => {
        saleLogger(err, res);
    });
};

/**
 * vip 会员专享
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
exports.vip = (req, res) => {
    let headerData = headerModel.setNavHeader('会员专享');
    let renderData = {
        module: 'product',
        page: 'vip',
        title: '会员专享',
        pageHeader: headerData,
        pageFooter: true
    };

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

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

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


/**
 * 商品搜索
 * @param  {[object]} req
 * @param  {[object]} res
 * @return {[type]}
 */
exports.search = (req, res) => {
    let params = Object.assign({}, req.query);

    // uid = 9239279
    let uid = req.user.uid || 0;

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

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

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