cart.js 18.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
/**
 * 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 stepper = [
    {name: '查看购物车', focus: true},
    {name: '填写订单'},
    {name: '付款,完成购买'}
];

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, res) => {

    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) {
            res.cookie('_g', {
                _k: shoppingKey,
                _nac: ret.data.cart_goods_count,
                _ac: 0,
                _r: 1
            });
        }
    }).finally(() => {
        return;
    });
};

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

    let uid = req.user.uid; // 8040155;  // req.user.uid; 20422448
// console.log('====================> ' + 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',
                stepper: stepper,
                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(req, res).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(req, res);
        }

        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(req, res);
        }

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

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

    co(function * () {

        let uid = req.user.uid;
        let pids = req.body.pidList.split(',');
        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);
        // test
        return res.send({
            'code': 200,
            'message': '',
            'data': {
                'header': '\u51d1\u5355\u5546\u54c1',
                'hasPrev': true,
                'hasNext': true,
                'item': [{
                    'id': 675944,
                    'skn': 51486050,
                    'href': '\/\/item.yohobuy.com\/product\/pro_675944_831288\/MYFUN yinliaopingxiuhuanvmianwa 1611DPA0195.html',
                    'title': 'MY FUN \u996e\u6599\u74f6\u7ee3\u82b1\u5973\u68c9\u889c',
                    'img': '\/\/img10.static.yhbimg.com\/goodsimg\/2017\/01\/12\/08\/01fcff8810cf74104dcb166d971a3fca91.jpg?imageMogr2\/thumbnail\/100x100\/extent\/100x100\/background\/d2hpdGU=\/position\/center\/quality\/80',
                    'alt': 'MY FUN \u996e\u6599\u74f6\u7ee3\u82b1\u5973\u68c9\u889c',
                    'price': '19.00',
                    'marketPrice': '49.00'
                }, {
                    'id': 675942,
                    'skn': 51486048,
                    'href': '\/\/item.yohobuy.com\/product\/pro_675942_831286\/MYFUN siyecaoxiuhuanvmianwa 1611DPA0194.html',
                    'title': 'MY FUN \u56db\u53f6\u8349\u7ee3\u82b1\u5973\u68c9\u889c',
                    'img': '\/\/img10.static.yhbimg.com\/goodsimg\/2017\/01\/12\/08\/01b5f0a983a76de8fe730e67247cdac0e6.jpg?imageMogr2\/thumbnail\/100x100\/extent\/100x100\/background\/d2hpdGU=\/position\/center\/quality\/80',
                    'alt': 'MY FUN \u56db\u53f6\u8349\u7ee3\u82b1\u5973\u68c9\u889c',
                    'price': '19.00',
                    'marketPrice': '49.00'
                }, {
                    'id': 675876,
                    'skn': 51485982,
                    'href': '\/\/item.yohobuy.com\/product\/pro_675876_831220\/MYFUN caitiaowennanshizhangtongwa 1611DPA0045.html',
                    'title': 'MYFUN \u5f69\u6761\u7eb9\u7537\u58eb\u957f\u7b52\u889c',
                    'img': '\/\/img12.static.yhbimg.com\/goodsimg\/2017\/01\/12\/10\/0290b5aafe4137bc7c5ec90f3963546cb9.jpg?imageMogr2\/thumbnail\/100x100\/extent\/100x100\/background\/d2hpdGU=\/position\/center\/quality\/80',
                    'alt': 'MYFUN \u5f69\u6761\u7eb9\u7537\u58eb\u957f\u7b52\u889c',
                    'price': '19.00',
                    'marketPrice': '39.00'
                }, {
                    'id': 675874,
                    'skn': 51485980,
                    'href': '\/\/item.yohobuy.com\/product\/pro_675874_831218\/MYFUN caitiaowennanshizhangtongwa 1611DPA0043.html',
                    'title': 'MY FUN \u5f69\u6761\u7eb9\u7537\u58eb\u957f\u7b52\u889c',
                    'img': '\/\/img10.static.yhbimg.com\/goodsimg\/2017\/01\/12\/10\/01604804248080b4c8cd60dd623e569d24.jpg?imageMogr2\/thumbnail\/100x100\/extent\/100x100\/background\/d2hpdGU=\/position\/center\/quality\/80',
                    'alt': 'MY FUN \u5f69\u6761\u7eb9\u7537\u58eb\u957f\u7b52\u889c',
                    'price': '19.00',
                    'marketPrice': '39.00'
                }, {
                    'id': 675872,
                    'skn': 51485978,
                    'href': '\/\/item.yohobuy.com\/product\/pro_675872_831216\/MYFUN ertiaogangnanshizhongtongwa 1611DPA0034.html',
                    'title': 'MY FUN \u7537\u58eb\u4e2d\u7b52\u889c',
                    'img': '\/\/img12.static.yhbimg.com\/goodsimg\/2017\/01\/12\/10\/02888bca5ef68cb669a0c47ae31232d87e.jpg?imageMogr2\/thumbnail\/100x100\/extent\/100x100\/background\/d2hpdGU=\/position\/center\/quality\/80',
                    'alt': 'MY FUN \u7537\u58eb\u4e2d\u7b52\u889c',
                    'price': '15.00',
                    'marketPrice': '29.00'
                }, {
                    'id': 675870,
                    'skn': 51485976,
                    'href': '\/\/item.yohobuy.com\/product\/pro_675870_831214\/MYFUN ertiaogangnanshizhongtongwa 1611DPA0033.html',
                    'title': 'MY FUN \u7537\u58eb\u4e2d\u7b52\u889c',
                    'img': '\/\/img10.static.yhbimg.com\/goodsimg\/2017\/01\/12\/10\/015ef2fb172c81a128b8c6603f89671f03.jpg?imageMogr2\/thumbnail\/100x100\/extent\/100x100\/background\/d2hpdGU=\/position\/center\/quality\/80',
                    'alt': 'MY FUN \u7537\u58eb\u4e2d\u7b52\u889c',
                    'price': '15.00',
                    'marketPrice': '29.00'
                }]
            }
        });
    })();
};

/**
 * 为你优选商品异步请求
 */
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);

        res.send({
            'code': 200,
            'message': '',
            'data': {
                'header': '\u4e3a\u60a8\u4f18\u9009',
                'hasPrev': false,
                'hasNext': true,
                'item': [{
                    'id': 493102,
                    'skn': 51295054,
                    'href': '\/\/item.yohobuy.com\/product\/pro_493102_450622\/LIFEAFTERLIFEDaYiLALA603WO97.html',
                    'title': 'Life\u00b7After Life \u6e14\u592b\u6263\u5927\u8863\u3010\u591a\u8272\u53ef\u9009\u3011',
                    'img': '\/\/img11.static.yhbimg.com\/goodsimg\/2016\/10\/08\/17\/01a6ac4cfb94fc6ca1bf4c8450ab7828c9.jpg?imageMogr2\/thumbnail\/100x100\/extent\/100x100\/background\/d2hpdGU=\/position\/center\/quality\/80',
                    'alt': 'Life\u00b7After Life \u6e14\u592b\u6263\u5927\u8863\u3010\u591a\u8272\u53ef\u9009\u3011',
                    'price': '589.00',
                    'marketPrice': '1389.00'
                }, {
                    'id': 599258,
                    'skn': 51404992,
                    'href': '\/\/item.yohobuy.com\/product\/pro_599258_575876\/VEIL KEIOS x GARFIELD Sports Cap: Monday landicixiudiandaojiafei heisewanyanmao  GFK-007.html',
                    'title': 'keios under the veil \u84dd\u5e95\u523a\u7ee3\u985b\u5012\u52a0\u83f2 \u9ed1\u8272\u5f2f\u6a90\u5e3d',
                    'img': '\/\/img10.static.yhbimg.com\/goodsimg\/2016\/10\/08\/14\/01816e75629f40c58a138a30aee2a9eca6.jpg?imageMogr2\/thumbnail\/100x100\/extent\/100x100\/background\/d2hpdGU=\/position\/center\/quality\/80',
                    'alt': 'keios under the veil \u84dd\u5e95\u523a\u7ee3\u985b\u5012\u52a0\u83f2 \u9ed1\u8272\u5f2f\u6a90\u5e3d',
                    'price': '248.00'
                }, {
                    'id': 494118,
                    'skn': 51296096,
                    'href': '\/\/item.yohobuy.com\/product\/pro_494118_451958\/PHANTACIPH16LTT02TXu.html',
                    'title': '\u3010JAY CHOU\u5730\u8868\u6700\u5f3a\u6f14\u5531\u4f1a\u7ebf\u4e0a\u72ec\u5bb6\u4e13\u5c5e\u3011PHANTACi X LOONEYTUNES X THEINVINCIBLE TEE-White',
                    'img': '\/\/img12.static.yhbimg.com\/goodsimg\/2016\/10\/12\/09\/022e6b74072cdce1c4df430707905fc4c6.jpg?imageMogr2\/thumbnail\/100x100\/extent\/100x100\/background\/d2hpdGU=\/position\/center\/quality\/80',
                    'alt': '\u3010JAY CHOU\u5730\u8868\u6700\u5f3a\u6f14\u5531\u4f1a\u7ebf\u4e0a\u72ec\u5bb6\u4e13\u5c5e\u3011PHANTACi X LOONEYTUNES X THEINVINCIBLE TEE-White',
                    'price': '119.00',
                    'marketPrice': '300.00'
                }, {
                    'id': 657894,
                    'skn': 51466614,
                    'href': '\/\/item.yohobuy.com\/product\/pro_657894_644268\/converse JACK PURCELL MODERN 155020Cnan.html',
                    'title': 'converse JACK PURCELL MODERN Man',
                    'img': '\/\/img10.static.yhbimg.com\/goodsimg\/2016\/12\/18\/14\/01cb0aef0bd168ebcb1de93cefc0d2e376.jpg?imageMogr2\/thumbnail\/100x100\/extent\/100x100\/background\/d2hpdGU=\/position\/center\/quality\/80',
                    'alt': 'converse JACK PURCELL MODERN Man',
                    'price': '989.00',
                    'marketPrice': '1099.00'
                }, {
                    'id': 646540,
                    'skn': 51454606,
                    'href': '\/\/item.yohobuy.com\/product\/pro_646540_631202\/5cm MEN SWEAT SHIRT 5CXSWX3503W67BKX.html',
                    'title': '5cm X KYE \u540e\u80cc\u5370\u82b1\u8fde\u5e3d\u536b\u8863',
                    'img': '\/\/img11.static.yhbimg.com\/goodsimg\/2016\/12\/08\/10\/0116d5eb256f45a66f082ab54d9ed7926f.jpg?imageMogr2\/thumbnail\/100x100\/extent\/100x100\/background\/d2hpdGU=\/position\/center\/quality\/80',
                    'alt': '5cm X KYE \u540e\u80cc\u5370\u82b1\u8fde\u5e3d\u536b\u8863',
                    'price': '1099.00'
                }, {
                    'id': 496860,
                    'skn': 51298904,
                    'href': '\/\/item.yohobuy.com\/product\/pro_496860_454976\/INTOTHERAINBOWDuanXiuTXuITRMA603TE103.html',
                    'title': 'Into The Rainbow \u7f8e\u56fd\u961f\u957f75\u5468\u5e74 \u77ed\u8896T\u6064',
                    'img': '\/\/img10.static.yhbimg.com\/goodsimg\/2016\/07\/18\/16\/01497d761773446050fcc57c414bee4653.jpg?imageMogr2\/thumbnail\/100x100\/extent\/100x100\/background\/d2hpdGU=\/position\/center\/quality\/80',
                    'alt': 'Into The Rainbow \u7f8e\u56fd\u961f\u957f75\u5468\u5e74 \u77ed\u8896T\u6064',
                    'price': '89.00',
                    'marketPrice': '299.00'
                }]
            }
        });
    })();
};

/**
 * 凑单 加价购异步请求
 */
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);
};

const swapGift = (req, res, next) => {

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

    let promotionId = req.body.promotionId;
    let newSkn = req.body.newSkn;
    let newSku = req.body.newSku;

    service.swapGift(uid, shoppingKey, promotionId, newSkn, newSku)
        .then(ret => {
            res.send(ret);
        }).catch(next);
};

/**
 * 查询优惠可选择的商品,
 * 根据用户购物车数据标记是否选中
 * @param req
 * @param res
 * @param next
 */
const queryUserPromotionGift = (req, res, next) => {

    let uid = req.user && req.user.uid;
    let shoppingKey = helper.getShoppingKeyByCookie(req);
    let promotionId = req.query.promotionId;

    service.queryUserPromotionGift(promotionId, uid, shoppingKey)
        .then(ret => {
            res.send(ret);
        }).catch(next);
};

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