cart.js 8.42 KB
/**
 * Created by TaoHuang on 2016/10/19.
 */

'use strict';

const co = require('bluebird').coroutine;
const service = require('../models/cart-service');
const helper = require('../models/cart-helper');
const ghelper = require('../../guang/models/guang-helper');
const simpleHeaderModel = require('../../../doraemon/models/simple-header');

const getProductInfo = (req, res, next) => {
    let pid = req.query.productId || '';

    service.getProductInfoAsync(pid).then((result) => {
        return res.render('goods-detail', Object.assign({
            layout: false
        }, result));
    }).catch(next);
};

const getProductData = (req, res, next) => {
    let pid = req.query.productId || '';

    service.getProductInfoAsync(pid).then((result) => {
        return res.send(result);
    }).catch(next);
};

/**
 * 设置购物车COOKIE信息
 */
const setShoppingCookie = (req) => {

    let uid = req.user.uid || true;
    let shoppingKey = helper.getShoppingKeyByCookie(req);

    return service.getCartCount(uid, shoppingKey).then(ret => {
        if (ret && ret.data && ret.data.cart_goods_count) {
            req.cookies('_g', {
                _k: shoppingKey,
                _nac: ret.data.cart_goods_count,
                _ac: 0,
                _r: 1
            });
        }
    }).finally(() => {
        return;
    });
};

/**
 * 我的购物车
 */
const cart = (req, res, next) => {

    let uid = 8040155;  // req.user.uid;
    let shoppingKey = helper.getShoppingKeyByCookie(req);
    let cartDelList = req.cookies['cart-del-list'];

    if (cartDelList) {
        req.cookies['cart-del-list'] = '';
    }

    service.getCartData(uid, shoppingKey, cartDelList)
        .then(ret => {

            if (req.query.json === '1') {
                return res.send(ret);
            }

            if (req.query.page === '1') {
                return res.render('cart_bak', {
                    cart: {

                    }
                });
            }

            // res.send(ret);

            // console.log(JSON.stringify(ret));
            return res.render('cart', {
                module: 'cart',
                page: 'cart',
                simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
                uid: uid,
                cart: ret
            });
        })
        .catch(next);
};

/**
 * 加入购物车
 */
const cartAdd = (req, res) => {

    co(function * () {
        let uid = req.user.uid;
        let shoppingKey = helper.getShoppingKeyByCookie(req);
        let productSku = req.body.productSku;
        let buyNumber = req.body.buyNumber || 1;
        let goodsType = req.body.goodsType || 0;
        let promotionId = req.body.promotionId || 0;
        let isEdit = req.body.isEdit || 0;

        let result = yield service.addToCart(productSku, buyNumber, goodsType, isEdit, promotionId, uid, shoppingKey);

        // 设置加入购物车凭证到客户端浏览器
        if (!shoppingKey && result && result.data && result.data.shopping_key) {
            // req.cookies['_SPK'] = result.data.shopping_key
            // $this->setCookie('_SPK', $result['data']['shopping_key'], time() + 86400 * 360);
        }

        // 老站购物车需要的COOKIE
        if (result && result.data && result.data.shopping_key) {
            /* $this->setCookie('_g', json_encode(array(
             '_k' => $result['data']['shopping_key'],
             '_nac' => $result['data']['goods_count'],
             '_ac' => 0,
             '_r' => 1
             )));*/
        }

        res.send(result);
    })();
};

/**
 * 获取购物车商品总数
 */
const cartTotal = (req, res) => {

    co(function * () {

        let uid = req.user.uid;
        let shoppingKey = helper.getShoppingKeyByCookie(req);
        let callback = req.query.callback;
        let ret = yield service.getCartCount(uid, shoppingKey);

        return res.send(callback + '(' + JSON.stringify(ret) + ')');
    })();
};

/**
 * 购物车商品选择与取消
 */
const selectProduct = (req, res) => {

    let uid = req.user.uid;
    let productId = req.body.skuList;
    let hasPromotion = req.body.hasPromotion || false;
    let shoppingKey = helper.getShoppingKeyByCookie(req);

    service.selectGoods(uid, productId, shoppingKey, hasPromotion)
        .then(ret => {
            res.send(ret);
        }).catch(() => {
            res.send({
                code: 400
            });
        });
};

/**
 * 修改购物车商品数量
 */
const modifyProductNum = (req, res, next) => {

    let uid = req.user.uid;
    let shoppingKey = helper.getShoppingKeyByCookie(req);
    let sku = req.body.sku;
    let increaseNum = req.body.increaseNum || null;
    let decreaseNum = req.body.decreaseNum || null;

    return service.modifyProductNum(uid, sku, increaseNum, decreaseNum, shoppingKey)
        .then(ret => {
            if (ret && ret.code === 200) {
                return setShoppingCookie().then(() => {
                    return res.send(ret);
                });
            } else {
                return res.send({
                    code: 400,
                    message: '修改购物车商品数量失败!'
                });
            }
        })
        .catch(next);
};

/**
 * 移出购物车
 */
const removeProduct = (req, res) => {

    co(function * () {
        let uid = req.user.uid;
        let shoppingKey = helper.getShoppingKeyByCookie(req);
        let skuList = req.body.skuList;
        let hasPromotion = true;

        let ret = yield service.removeFromCart(uid, shoppingKey, skuList, hasPromotion);

        if (ret && ret.code === 200) {
            yield setShoppingCookie();
        }

        return res.send(ret);
    })();
};

/**
 * 移入收藏夹
 * 支持批量移入收藏夹
 */
const moveToFav = (req, res) => {

    co(function * () {
        let uid = req.user.uid;

        // let shoppingKey = helper.getShoppingKeyByCookie(req);
        let skuList = req.body.skuList;
        let hasPromotion = req.body.hasPromotion || false;

        let ret = yield service.addToFav(uid, skuList, hasPromotion);

        if (ret && ret.code === 200) {
            yield setShoppingCookie();
        }

        return res.send(ret);
    })();
};

/**
 * 检查是否收藏
 */
const checkFav = (req, res) => {

    co(function * () {

        let uid = req.user.uid;
        let pids = req.body.pidList;
        let ret = {
            code: 200,
            message: '是否收藏',
            data: {}
        };

        ret.data = yield service.checkUserIsFav(uid, pids);

        return res.send(ret);
    })();
};

/**
 * 凑单商品异步请求
 */
const getTogetherProduct = (req, res) => {

    co(function * () {

        let page = req.query.page;

        /* let ret = {
            code: 200,
            message: '凑单商品'
        };*/

        let ret = yield service.getTogetherProduct(page);

        return res.send(ret);
    })();
};

/**
 * 为你优选商品异步请求
 */
const getRecommendProductAction = (req, res) => {

    co(function * () {

        let channel = req.yoho.channel;
        let uid = req.user.uid;
        let udid = ghelper.getUdid(req, res);
        let page = req.query.page;

        if (page === '6') {
            page = 1;
        }

        let ret = yield service.getRecommendProduct(channel, uid, udid, page);

        res.send(ret);
    })();
};

/**
 * 凑单 加价购异步请求
 */
const getIncreasePurchase = (req, res) => {

    co(function * () {

        let page = req.query.page;

        /* let ret = {
             code: 200,
             message: '凑单商品'
         };*/

        let ret = yield service.getTogetherProduct(page);

        return res.send(ret);
    });
};

// 修改购物车商品颜色和尺寸
const modifyProduct = (req, res, next) => {
    const uid = req.user && req.user.uid;
    const shoppingKey = helper.getShoppingKeyByCookie(req);

    // swapData => [{"buy_number":"1","selected":"Y","new_product_sku":"735172","old_product_sku":"735171"}]
    const swapData = req.body.swapData;

    service.modifyProduct({swapData, shoppingKey, uid}).then((result) => {
        /* if (result.code === 200) {

        } else {
            res.json(result);
        }*/
        res.send(result);
    }).catch(next);
};

module.exports = {
    getProductInfo,
    getProductData,
    cart,
    cartAdd,
    cartTotal,
    setShoppingCookie,
    selectProduct,
    modifyProductNum,
    removeProduct,
    moveToFav,
    checkFav,
    getTogetherProduct,
    getRecommendProductAction,
    getIncreasePurchase,
    modifyProduct
};