sale.js 4.75 KB
/**
 * 商品促销controller
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/5/18
 */

'use strict';

const mRoot = '../models';
const sale = require(`${mRoot}/sale`); // sale  model
const detailHelper = require(`${mRoot}/detail-helper`);

/**
 * sale 首页
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.index = (req, res, next) => {
    let channel = req.query.channel || req.yoho.channel;

    // 真实数据输出
    req.ctx(sale).getSaleIndexData(channel).then(result => {
        res.render('sale/index', Object.assign({
            module: 'product',
            page: 'sale'
        }, {
            result: result,
            headerData: result.headerData
        }));
    }).catch(next);
};

/**
 * 折扣专场
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.discount = (req, res, next) => {
    let params = Object.assign({
        saleType: '3'
    }, req.query);

    let channel = req.query.channel || req.yoho.channel;

    let responseData = {
        module: 'product',
        page: 'sale'
    };

    req.ctx(sale).getSaleDiscountData(params, channel).then((result) => {
        responseData.result = result;
        if (result) {
            responseData.headerData = result.headerData;

            Object.assign(responseData, result.seo || {});
        }

        res.render('sale/discount', responseData);
    }).catch(next);
};

/**
 * VIP会员专享
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.vip = (req, res, next) => {
    let params = Object.assign({
        saleType: '2'
    }, req.query);

    let channel = req.query.channel || req.yoho.channel;

    params.uid = req.user.uid || 0;

    req.ctx(sale).getSaleOthersData(params, channel).then((result) => {
        res.render('sale/other', Object.assign({
            module: 'product',
            page: 'sale',
            pageClass: 'static-list-page'
        }, {
            saleList: result,
            headerData: result.headerData
        }, result.seo));
    }).catch(next);

};

/**
 * 新降价专区
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.newSale = (req, res, next) => {
    let params = Object.assign({
        order: 's_t_desc'
    }, req.query);

    let channel = req.yoho.channel;

    req.ctx(sale).getSaleOthersData(params, channel).then((result) => {
        res.render('sale/other', Object.assign({
            module: 'product',
            page: 'sale',
            pageClass: 'static-list-page'
        }, {
            saleList: result,
            headerData: result.headerData
        }, result.seo));
    }).catch(next);
};

/**
 * 断码专区
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.breakingYards = (req, res, next) => {
    let params = Object.assign({
        saleType: '5'
    }, req.query);

    let channel = req.yoho.channel;

    let responseData = {
        module: 'product',
        page: 'sale',
        pageClass: 'static-list-page'
    };

    req.ctx(sale).getSalebreakingYardsData(params, channel).then((result) => {
        responseData.saleList = result;
        Object.assign(responseData, result.seo);
        if (result) {
            responseData.headerData = result.headerData;
        }
        res.render('sale/other', responseData);
    }).catch(next);
};

/**
 * 活动
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.special = (req, res, next) => {
    let params = req.query;
    let specialId = params.specialsale_id || params.specialsaleId, // 专区ID
        promotion = params.promotion; // 促销ID

    specialId = parseInt(`0${specialId}`, 10);

    if (!specialId && !promotion) {
        return next();
    }

    req.ctx(sale).getSaleSpecialData(specialId, params, req.yoho.channel).then(result => {
        res.render('sale/special', result);
    }).catch(next);
};

/**
 * Ajax 获取商品列表
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.getGoodsList = (req, res, next) => {
    let params = req.query;
    let vipLevel = detailHelper.vipLevel(req.user.vip);

    if (params.refresh && !vipLevel) {
        return res.send('');
    }

    req.ctx(sale).getSaleGoodsData(params, vipLevel).then(result => {
        let responseData = {};

        responseData.footerTop = false;
        responseData.layout = false;
        responseData.result = result;

        res.render('goods', responseData);
    }).catch(next);
};