index.js 12.7 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
/**
 * 购物车
 * @author: feng.chen<feng.chen@yoho.cn>
 * @date: 2016/12/21
 */

'use strict';
const helpers = global.yoho.helpers;
const _ = require('lodash');
const headerModel = require('../../../doraemon/models/header'); // 头部model

const indexModel = require('../models/index');

/**
 * [购物车首页]
 */
const index = (req, res) => {
    let isLogin = req.user && req.user.uid,
        pageData = {
            isLogin,
            signurl: helpers.urlFormat('/signin.html')
        };

    res.render('index', Object.assign(pageData, {
        title: '购物车',
        module: 'cart',
        page: 'index',
        localCss: true,
        width750: true,
        pageHeader: headerModel.setNav({
            navTitle: '购物车',
            backUrl: 'javascript:;', // eslint-disable-line
            navBack: true,
            suggestSub: {
                text: ' '
            },
            navBtn: false
        })
    }));
};

/**
 * [购物车数据]
 */
const indexData = (req, res, next) => {
    if (!req.xhr) {
        return next();
    }
    let shoppingKey = req.cookies._SPK || '',
        channel = req.cookies._Channel,
        cartType = req.cookies.cartType || 'ordinary',
        uid = req.user && req.user.uid;

        // shoppingKey = 'dc9d09e2ffd8607f2cfd8b9c95962923';
        // uid = 20422448;

    return indexModel.indexData(uid, shoppingKey, channel, cartType).then(data => {
        data ? res.json({
            code: 200,
            data
        }) : res.status(400).json({
            message: '操作失败'
        });
    }).catch(next);
};

/**
 * [选择或者取消商品]
 */
const select = (req, res, next) => {
    if (!req.xhr) {
        return next();
    }
    let shoppingKey = req.cookies._SPK || '',
        uid = req.user && req.user.uid;
    let skuList = req.body.skuList,
        cartType = req.cookies.cartType || 'ordinary';

    if (!skuList || !skuList.length) {
        return res.json({
            code: 400,
            message: '参数错误'
        });
    }

        // shoppingKey = 'dc9d09e2ffd8607f2cfd8b9c95962923';
        // uid = 20422448;

    return indexModel.selectGood(uid, skuList, shoppingKey, cartType).then(data => {
        data ? res.json({
            code: 200,
            data
        }) : res.status(400).json({
            message: '操作失败'
        });
    }).catch(next);
};

/**
 * [移出购物车]
 */
const del = (req, res, next) => {
    if (!req.xhr) {
        return next();
    }
    let shoppingKey = req.cookies._SPK || '',
        uid = req.user && req.user.uid;
    let skuList = req.body.skuList,
        cartType = req.cookies.cartType || 'ordinary';

    if (!skuList || !skuList.length) {
        return res.json({
            code: 400,
            message: '参数错误'
        });
    }

        // shoppingKey = 'dc9d09e2ffd8607f2cfd8b9c95962923';
        // uid = 20422448;

    return indexModel.removeFromCart(uid, skuList, shoppingKey, cartType).then(data => {
        data ? res.json({
            code: 200,
            data
        }) : res.status(400).json({
            message: '操作失败'
        });
    }).catch(next);
};

/**
 * [加入购物车]
 */
const add = (req, res, next) => {
    if (!req.xhr) {
        return next();
    }
    let shoppingKey = req.cookies._SPK || '',
        uid = req.user && req.user.uid;
    let productSku = req.body.productSku,
        buyNumber = req.body.buyNumber || 1,
        goodsType = req.body.goodsType || 0,
        promotionId = req.body.promotionId || 0,
        isEdit = req.body.isEdit || 0;

    if (!productSku) {
        return res.json({
            code: 400,
            message: '参数错误'
        });
    }

        // shoppingKey = 'dc9d09e2ffd8607f2cfd8b9c95962923';
        // uid = 20422448;

    return indexModel.addToCart(productSku, buyNumber, goodsType, isEdit, promotionId, uid, shoppingKey).then(data => {
        if (!shoppingKey && _.has(data, 'data.shopping_key')) {
            res.cookie('_SPK', data.data.shopping_key, {
                expires: new Date(Date.now() + 86400 * 360),
                domain: '.m.yohobuy.com'
            });
        }
        data ? res.json(data) : res.status(400).json({
            message: '操作失败'
        });
    }).catch(next);
};

/**
 * [获取购物车数据]
 */
const goodinfo = (req, res, next) => {
    if (!req.xhr) {
        return next();
    }
    let result = {};
    let uid = req.user && req.user.uid,
        buyNum = req.body.buy_num,
        mnum = req.body.mnum,
        skn = req.body.skn,
        promotionId = req.body.promotion_id;

    if (!buyNum || !skn) {
        return res.json({
            code: 400,
            message: '参数错误'
        });
    }

    return indexModel.cartProductData(uid, skn, buyNum).then(data => {
        return new Promise((resolve) => {
            if (mnum && data) {
                return indexModel.handleBundleInfo(skn).then(disRes => {
                    result.discountBuy = disRes;
                    resolve(data);
                });
            }
            resolve(data);
        }).then(cartInfo => {
            result.cartInfo = cartInfo;
            cartInfo ? res.json(Object.assign({
                code: 200,
                promotionId: promotionId
            }, result)) : res.status(400).json({
                message: '操作失败'
            });
        });

    }).catch(next);
};

/**
 * [加入收藏]
 */
const col = (req, res, next) => {
    if (!req.xhr) {
        return next();
    }
    let uid = req.user && req.user.uid;
    let skuList = req.body.skuList,
        cartType = req.cookies.cartType || 'ordinary';

    if (!skuList || !skuList.length) {
        return res.json({
            code: 400,
            message: '参数错误'
        });
    }

        // shoppingKey = 'dc9d09e2ffd8607f2cfd8b9c95962923';
        // uid = 20422448;
    if (!uid) {
        return res.json({
            code: 401,
            message: '请先登录',
            data: helpers.urlFormat('/signin.html')
        });
    }
    return indexModel.addToFav(uid, skuList, cartType).then(data => {
        data ? res.json({
            code: 200,
            data
        }) : res.status(400).json({
            message: '操作失败'
        });
    }).catch(next);

};

/**
 * [修改购物车数量]
 */
const modifyNum = (req, res, next) => {
    if (!req.xhr) {
        return next();
    }
    let uid = req.user && req.user.uid,
        shoppingKey = req.cookies._SPK || '',
        sku = req.body.sku,
        increaseNum = req.body.increaseNum,
        decreaseNum = req.body.decreaseNum;

    if (!sku || (!increaseNum && !decreaseNum)) {
        return res.json({
            code: 400,
            message: '参数错误'
        });
    }

        // shoppingKey = 'dc9d09e2ffd8607f2cfd8b9c95962923';
        // uid = 20422448;

    let promise;

    if (increaseNum > 0) {
        promise = indexModel.increaseProductNum(uid, sku, increaseNum, shoppingKey);
    } else if (decreaseNum > 0) {
        promise = indexModel.decreaseProductNum(uid, sku, decreaseNum, shoppingKey);
    }

    return promise.then(data => {
        if (data && data.code === 200 && data.data) {
            res.json({
                code: 200,
                goodsCount: data.data.goods_count
            });
        } else {
            res.json({
                code: 400,
                message: data.message
            });
        }

    }).catch(next);
};

/**
 * [修改购物车数据]
 */
const modify = (req, res, next) => {
    if (!req.xhr) {
        return next();
    }
    let uid = req.user && req.user.uid,
        shoppingKey = req.cookies._SPK || '';

    let oldProductSku = req.body.old_product_sku || 0,
        newProductSku = req.body.new_product_sku || 0,
        buyNumber = req.body.buy_number || 0,
        activity_id = req.body.activity_id,
        batch_no = req.body.batch_no,
        selected = req.body.selected;

    if (!oldProductSku || !newProductSku) {
        return res.json({
            code: 400,
            message: '参数错误'
        });
    }

        // shoppingKey = 'dc9d09e2ffd8607f2cfd8b9c95962923';
        // uid = 20422448;
    return indexModel.modifyCartProduct(uid, [{
        old_product_sku: oldProductSku,
        new_product_sku: newProductSku,
        buy_number: buyNumber,
        selected: selected,
        activity_id: activity_id,
        batch_no: batch_no
    }], shoppingKey).then(data => {
        data ? res.json(data) : res.status(400).json({
            message: '操作失败'
        });
    }).catch(next);
};

/**
 * [修改购物车赠品、加价购数据]
 */
const modifyPriceGift = (req, res, next) => {
    if (!req.xhr) {
        return next();
    }
    let uid = req.user && req.user.uid,
        shoppingKey = req.cookies._SPK || '';

    let newProductSku = req.body.new_product_sku || 0,
        newProductSkn = req.body.new_product_skn || 0,
        promotionId = req.body.promotionId || 0;

    if (!promotionId || !newProductSku) {
        return res.json({
            code: 400,
            message: '参数错误'
        });
    }

        // shoppingKey = 'dc9d09e2ffd8607f2cfd8b9c95962923';
        // uid = 20422448;
    return indexModel.modifyCartPriceGiftProduct(uid, newProductSku,
        newProductSkn, promotionId, shoppingKey)
    .then(data => {
        data ? res.json(data) : res.status(400).json({
            message: '操作失败'
        });
    }).catch(next);
};

/**
 * [获取赠品商品列表]
 */
const gift = (req, res, next) => {
    let cartType = req.cookies.cartType || 'ordinary',
        promotionIds = req.query.promotion_ids;

    if (!promotionIds) {
        return next();
    }
    return indexModel.getPriceGiftList(promotionIds, 'Gift').then(data => {
        res.render('gift', Object.assign(data, {
            title: '赠品',
            module: 'cart',
            page: 'gift',
            localCss: true,
            pageHeader: headerModel.setNav({
                navTitle: '赠品',
                navBack: true,
                suggestSub: {
                    text: ' '
                },
                navBtn: false
            })
        }, {
            giftPage: true,
            cartType: cartType
        }));
    }).catch(next);
};

/**
 * [获取加价购商品列表]
 */
const advanceBuy = (req, res, next) => {
    let cartType = req.cookies.cartType || 'ordinary',
        promotionIds = req.query.promotion_ids;

    if (!promotionIds) {
        return next();
    }
    return indexModel.getPriceGiftList(promotionIds, 'Needpaygift').then(data => {
        res.render('gift', Object.assign(data, {
            title: '加价购',
            module: 'cart',
            page: 'gift',
            localCss: true,
            pageHeader: headerModel.setNav({
                navTitle: '加价购',
                navBack: true,
                suggestSub: {
                    text: ' '
                },
                navBtn: false
            })
        }, {
            advanceBuyPage: true,
            cartType: cartType
        }));
    }).catch(next);
};

/**
 * [获取购物车加价购商品数据]
 */
const giftinfo = (req, res, next) => {
    if (!req.xhr) {
        return next();
    }
    let skn = req.body.skn,
        promotionId = req.body.promotionId;

    if (!skn) {
        return res.json({
            code: 400,
            message: '参数错误'
        });
    }
    return indexModel.giftProductData(skn, promotionId).then(data => {
        data ? res.json({
            promotionId: promotionId,
            cartInfo: data
        }) : res.status(400).json({
            message: '操作失败'
        });
    }).catch(next);
};


/**
 * 增加套餐的数量
 * @param {*} req
 * @param {*} res
 * @param {*} next
 */
const incrBundle = (req, res, next) => {
    let uid = req.user.uid;
    let shoppingKey = req.cookies._SPK || '';

    return indexModel.incrBundle({
        uid: uid,
        shopping_key: shoppingKey,
        activity_id: req.body.activity_id,
        sku_ids: req.body.sku_ids,
        batch_no: req.body.batch_no,
    }).then(result => {
        res.json(result);
    }).catch(next);
};

/**
 * 增加套餐的数量
 * @param {*} req
 * @param {*} res
 * @param {*} next
 */
const decrBundle = (req, res, next) => {
    let uid = req.user.uid;
    let shoppingKey = req.cookies._SPK || '';

    return indexModel.decrBundle({
        uid: uid,
        shopping_key: shoppingKey,
        activity_id: req.body.activity_id,
        sku_ids: req.body.sku_ids,
        batch_no: req.body.batch_no,
    }).then(result => {
        res.json(result);
    }).catch(next);
};

module.exports = {
    index,
    indexData,
    select,
    del,
    goodinfo,
    col,
    modifyNum,
    add,
    modify,
    gift,
    giftinfo,
    advanceBuy,
    modifyPriceGift,
    incrBundle,
    decrBundle
};