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

'use strict';

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

// sale 页 model
const sale = require(`${mRoot}/sale`);

// 开发用模拟数据
// const simulation = require(`${mRoot}/simulation`);

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

    headerModel.requestHeaderData()
        .then(headerData => {
            responseData = headerModel.setHeaderData(headerData.data, 'sale');

            responseData.module = 'product';
            responseData.page = 'sale';
            responseData.footerTop = true;
            responseData.devEnv = true;

            // 假数据输出
            // res.render('sale/index', Object.assign(responseData, simulation.saleIndex()));

            // 真实数据输出
            sale.getSaleIndexData().then(result => {

                responseData.result = result;
                responseData.resultShow = JSON.stringify(result, null, 4);

                res.render('sale/index', responseData);
            });
        })
        .catch(() => {
            res.render('error', { devEnv: true, pageErr: true });
        });

};

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

    let responseData = {};

    headerModel.requestHeaderData()
        .then(headerData => {
            responseData = headerModel.setHeaderData(headerData.data, 'sale-discount');
            responseData.module = 'product';
            responseData.page = 'sale';
            responseData.footerTop = true;
            responseData.devEnv = true;

            // 假数据输出
            // res.render('sale/discount', Object.assign(responseData, simulation.discount()));

            sale.getSaleDiscountData(params).then((result) => {
                responseData.result = result;
                responseData.resultShow = JSON.stringify(result, null, 4);

                // log.info(result);
                res.render('sale/discount', responseData);
            });


        })
        .catch(() => {
            res.render('error', { devEnv: true, pageErr: true });
        });
};

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

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

    let responseData = {};

    headerModel.requestHeaderData().then(headerData => {
        responseData = headerModel.setHeaderData(headerData.data, 'vip');
        responseData.module = 'product';
        responseData.page = 'sale';
        responseData.footerTop = true;
        responseData.devEnv = true;

        // 假数据输出
        // res.render('sale/other', Object.assign(responseData, simulation.other()));

        sale.getSaleOthersData(params).then((result) => {
            responseData.saleList = result;
            responseData.resultShow = JSON.stringify(result, null, 4);

            res.render('sale/other', responseData);
        });

    }).catch(() => {
        res.render('error', { devEnv: true, pageErr: true });
    });
};

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

    let responseData = {};

    headerModel.requestHeaderData().then(headerData => {
        responseData = headerModel.setHeaderData(headerData.data, 'newSale');
        responseData.module = 'product';
        responseData.page = 'sale';
        responseData.footerTop = true;
        responseData.devEnv = true;

            // 假数据输出
            // res.render('sale/other', Object.assign(responseData, simulation.other()));

        sale.getSaleOthersData(params).then((result) => {
            responseData.saleList = result;
            responseData.resultShow = JSON.stringify(result, null, 4);

            res.render('sale/other', responseData);
        });

    })
        .catch(() => {
            res.render('error', { devEnv: true, pageErr: true });
        });
};

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

    let responseData = {};

    headerModel.requestHeaderData().then(headerData => {
        responseData = headerModel.setHeaderData(headerData.data, 'breakingYards');
        responseData.module = 'product';
        responseData.page = 'sale';
        responseData.footerTop = true;
        responseData.devEnv = true;

        // 假数据输出
        // res.render('sale/other', Object.assign(responseData, simulation.other()));

        sale.getSalebreakingYardsData(params).then((result) => {
            responseData.saleList = result;
            responseData.resultShow = JSON.stringify(result, null, 4);

            res.render('sale/other', responseData);
        });

    }).catch(() => {
        res.render('error', { devEnv: true, pageErr: true });
    });
};

/**
 * Ajax 获取商品列表
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.getGoodsList = (req, res) => {
    let params = Object.assign({
        saleType: 1
    }, req.query);

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

    return sale.getSaleGoodsData(params).then(result => {
        let responseData = {};

        responseData.devEnv = true;
        responseData.footerTop = false;
        responseData.layout = false;

        responseData.result = result;
        responseData.resultShow = JSON.stringify(result, null, 4);

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