Blame view

apps/cart/models/cart-service.js 31.8 KB
htoooth authored
1 2 3 4 5 6 7 8 9 10 11 12
/**
 * 商品详情models
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/5/6
 */

'use strict';

const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const helpers = global.yoho.helpers;
m  
刘传洋 authored
13
m  
刘传洋 authored
14
const ERROR_400_MESSAGE = '系统繁忙,请稍候再试!';
郝肖肖 authored
15 16
const ProductAPI = require('./product-api');
const CartApi = require('./cart-api');
m  
刘传洋 authored
17
const chelper = require('./cart-helper');
htoooth authored
18
郝肖肖 authored
19 20 21 22
module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }
OF1706 authored
23
郝肖肖 authored
24 25
    _getProductIntroAsync(productSkn) {
        let that = this;
htoooth authored
26
郝肖肖 authored
27 28 29 30
        return co(function * () {
            let result = yield Promise.props({
                sizeInfo: new ProductAPI(that.ctx).sizeInfoAsync(productSkn)
            });
htoooth authored
31
郝肖肖 authored
32 33
            return result;
        })();
htoooth authored
34 35
    }
郝肖肖 authored
36 37 38 39 40 41 42 43
    /**
     * 获得sku商品数据
     */
    _getSkuDataByProductBaseInfo(data) {
        let totalStorageNum = 0;
        let skuGoods = null;// sku商品
        let defaultImage = '';// 默认图
        let chooseSkuFlag = false; // 选中状态
htoooth authored
44
郝肖肖 authored
45 46 47 48 49 50
        if (_.isEmpty(_.get(data, 'goods_list', []))) {
            return {
                totalStorageNum,
                skuGoods,
                defaultImage
            };
htoooth authored
51 52
        }
郝肖肖 authored
53
        skuGoods = _.get(data, 'goods_list', []).reduce((acc, cur, pos)=> {
htoooth authored
54
郝肖肖 authored
55 56
            // 如果status为0,即skc下架时就跳过该商品$value['status'] === 0
            let goodsGroup = {};
htoooth authored
57
郝肖肖 authored
58 59
            if (_.isEmpty(cur.color_image)) {
                return acc;
htoooth authored
60 61
            }
郝肖肖 authored
62 63 64 65 66 67 68 69 70 71
            if (cur.images_list) {
                // 商品列表
                goodsGroup.productSkc = cur.product_skc;
                goodsGroup.src = helpers.image(cur.color_image, 25, 35);
                goodsGroup.title = `${_.trim(data.product_name)} ${cur.color_name}`;
                goodsGroup.name = cur.factory_goods_name;
                goodsGroup.focus = false;
                goodsGroup.total = 0;
                goodsGroup.thumbs = [];
                goodsGroup.size = [];
htoooth authored
72 73
            }
郝肖肖 authored
74 75 76 77 78 79 80 81
            _.get(cur, 'images_list', []).forEach((good) => {
                if (good.image_url) {
                    goodsGroup.thumbs.push({
                        url: '',
                        shower: helpers.image(good.image_url, 420, 560),
                        img: helpers.image(good.image_url, 75, 100)
                    });
                }
htoooth authored
82 83
            });
郝肖肖 authored
84 85 86 87
            // 缩略图空,不显示
            if (_.isEmpty(goodsGroup.thumbs)) {
                return acc;
            }
htoooth authored
88
郝肖肖 authored
89 90 91
            // 默认第一张图片
            if (pos === 0) {
                defaultImage = helpers.image(cur.color_image, 420, 560);
htoooth authored
92 93
            }
郝肖肖 authored
94 95 96 97 98 99
            // 商品的尺码列表
            _.get(cur, 'size_list', []).forEach((size) => {
                if (data.attribute === 3) {
                    // 虚拟商品,门票默认最大为4,
                    size.storage_number = size.storage_number > 4 ? 4 : size.storage_number;
                }
htoooth authored
100
郝肖肖 authored
101 102 103 104
                // 如果status为0,即skc下架时就跳过该商品
                if (cur.status === 0) {
                    size.storage_number = 0;
                }
htoooth authored
105
郝肖肖 authored
106 107 108 109 110 111
                goodsGroup.size.push({
                    name: size.size_name,
                    sku: size.product_sku,
                    num: _.parseInt(size.storage_number),
                    goodsId: size.size_id
                });
htoooth authored
112
郝肖肖 authored
113 114
                // 单个sku商品的总数
                goodsGroup.total += _.parseInt(size.storage_number);
htoooth authored
115
郝肖肖 authored
116 117 118 119
                if (goodsGroup.total > 0 && !chooseSkuFlag) { // 默认选中该sku商品
                    goodsGroup.focus = true;
                    chooseSkuFlag = true;// 选中sku商品
                }
htoooth authored
120
郝肖肖 authored
121
                totalStorageNum += _.parseInt(size.storage_number);
htoooth authored
122
郝肖肖 authored
123
            });
htoooth authored
124
郝肖肖 authored
125 126 127
            acc.push(goodsGroup);
            return acc;
        }, []);
htoooth authored
128
郝肖肖 authored
129 130 131
        if (!_.isEmpty(skuGoods) && !chooseSkuFlag) { // 没有选中一个sku商品,默认选中第一个sku商品
            _.head(skuGoods).focus = true;
        }
htoooth authored
132
郝肖肖 authored
133 134 135 136 137 138
        return {
            defaultImage: defaultImage,
            skuGoods: skuGoods,
            totalStorageNum: totalStorageNum
        };
    }
htoooth authored
139
郝肖肖 authored
140 141 142 143 144 145 146 147
    /**
     * 使sizeBoList id以 sizeAttributeBos id顺序一样
     * @param sizeInfoBo
     */
    _sizeInfoBoSort(sizeInfoBo) {
        if (!sizeInfoBo.sizeBoList || !sizeInfoBo.sizeAttributeBos) {
            return {};
        }
htoooth authored
148
郝肖肖 authored
149 150
        _.get(sizeInfoBo, 'sizeBoList', []).forEach((sizeBoList, sizek)=> {
            let sortAttr = {};
htoooth authored
151
郝肖肖 authored
152 153 154
            sizeBoList.sortAttributes.forEach(sortAttributes => {
                sortAttr[sortAttributes.id] = sortAttributes;
            });
htoooth authored
155
郝肖肖 authored
156 157
            sizeInfoBo.sizeBoList[sizek].sortAttributes = sortAttr;
        });
htoooth authored
158
郝肖肖 authored
159 160
        _.get(sizeInfoBo, 'sizeBoList', []).forEach((sizeBoList, sizek)=> {
            let sortAttr = [];
htoooth authored
161
郝肖肖 authored
162 163 164 165 166
            sizeInfoBo.sizeAttributeBos.forEach(val => {
                if (sizeBoList.sortAttributes[val.id]) {
                    sortAttr.push(sizeBoList.sortAttributes[val.id]);
                }
            });
htoooth authored
167
郝肖肖 authored
168
            sizeInfoBo.sizeBoList[sizek].sortAttributes = sortAttr;
htoooth authored
169
郝肖肖 authored
170
        });
htoooth authored
171
郝肖肖 authored
172
        return sizeInfoBo;
htoooth authored
173 174
    }
郝肖肖 authored
175 176 177 178 179 180
    /**
     * 获取尺寸信息
     * @param sizeInfo
     * @returns {{}}
     */
    _getSizeData(sizeInfo) {
htoooth authored
181
郝肖肖 authored
182 183 184 185
        // 尺码信息
        if (!_.has(sizeInfo, 'sizeInfoBo')) {
            return {};
        }
htoooth authored
186
郝肖肖 authored
187 188 189 190 191 192 193 194 195 196 197 198 199 200
        sizeInfo.sizeInfoBo = this._sizeInfoBoSort(sizeInfo.sizeInfoBo);

        let boyReference = _.get(sizeInfo, 'productExtra.boyReference', false);
        let girlReference = _.get(sizeInfo, 'productExtra.girlReference', false);
        let gender = _.get(sizeInfo, 'productDescBo.gender', 3);
        let referenceName = (function() {
            if (gender === 3 && boyReference) {
                return '参考尺码(男)';
            } else if (gender === 3 && girlReference) {
                return '参考尺码(女)';
            } else {
                return '参考尺码';
            }
        }());
htoooth authored
201
郝肖肖 authored
202 203 204
        // 判断是否显示参考尺码
        let showReference = (boyReference && _.get(sizeInfo, 'sizeInfoBo.sizeBoList[0].boyReferSize', false)) ||
            (girlReference && _.get(sizeInfo, 'sizeInfoBo.sizeBoList[0].girlReferSize', false));
htoooth authored
205
郝肖肖 authored
206 207 208
        if (!_.has(sizeInfo, 'sizeInfoBo.sizeAttributeBos')) {
            return {};
        }
htoooth authored
209
郝肖肖 authored
210 211 212 213 214 215 216 217 218
        // 尺码信息头部
        let size = {
            thead: [{name: '吊牌尺码', id: ''}],
            tbody: []
        };

        // 显示参考尺码
        if (showReference) {
            size.thead[1] = {name: referenceName, id: ''};
htoooth authored
219 220
        }
郝肖肖 authored
221 222 223 224 225
        _.get(sizeInfo, 'sizeInfoBo.sizeAttributeBos', []).forEach((value) => {
            size.thead.push({
                name: value.attributeName || ' ',
                id: value.id
            });
htoooth authored
226 227
        });
郝肖肖 authored
228 229
        _.get(sizeInfo, 'sizeInfoBo.sizeBoList', []).forEach((value) => {
            let sizes = [];
htoooth authored
230
郝肖肖 authored
231 232
            // 吊牌尺码
            sizes.push(value.sizeName);
htoooth authored
233
郝肖肖 authored
234 235 236 237 238 239 240 241 242 243
            // 判断是否显示参考尺码
            if (boyReference && (gender === 1 || gender === 3) && showReference) {
                sizes.push(_.get(value, 'boyReferSize.referenceName', ' '));
            } else if (girlReference && (gender === 2 || gender === 3) && showReference) {
                sizes.push(_.get(value, 'girlReferSize.referenceName', ' '));
            } else {
                if (size.thead[1] && showReference) {
                    size.thead[1] = {};
                }
            }
htoooth authored
244
郝肖肖 authored
245 246 247 248
            // 其他尺码信息
            _.get(value, 'sortAttributes', []).forEach(attr => {
                sizes.push(_.get(attr, 'sizeValue', ' '));
            });
htoooth authored
249
郝肖肖 authored
250 251 252
            // 尺码信息
            size.tbody.push(sizes);
        });
htoooth authored
253
郝肖肖 authored
254 255 256 257 258 259 260 261 262 263
        // 参考尺码为空
        if (_.isEmpty(size.thead[1]) && showReference) {
            // 移除这个值
            size.thead.splice(1, 1);
        }

        // 测量方式
        if (sizeInfo.sizeImage) {
            size.sizeImg = sizeInfo.sizeImage;
        }
htoooth authored
264
郝肖肖 authored
265
        return size;
htoooth authored
266 267
    }
郝肖肖 authored
268 269 270 271 272 273 274 275 276 277 278
    /**
     * 商品尺码信息
     *
     * @param productSkn
     * @param maxSortId
     * @return object
     */
    _getIntroInfo(productSkn, additionalData) {
        if (!productSkn) {
            return {};
        }
htoooth authored
279
郝肖肖 authored
280
        let sizeInfo = additionalData.sizeInfo;
htoooth authored
281
郝肖肖 authored
282 283 284
        if (_.isEmpty(sizeInfo)) {
            return {};
        }
htoooth authored
285
郝肖肖 authored
286
        let result = {};
htoooth authored
287
郝肖肖 authored
288 289
        // 尺寸数据
        result.size = this._getSizeData(sizeInfo);
htoooth authored
290
郝肖肖 authored
291 292
        return result;
    }
htoooth authored
293
郝肖肖 authored
294 295 296 297 298 299 300 301 302
    /**
     * 详情页数据格式化
     * @param origin Object 原始数据
     * @return result Object 格式化数据
     */
    _detailDataPkg(origin) {
        if (_.isEmpty(origin) || _.isEmpty(origin)) {
            return {};
        }
htoooth authored
303
郝肖肖 authored
304 305
        let result = {};
        let propOrigin = _.partial(_.get, origin);
htoooth authored
306
郝肖肖 authored
307 308 309 310
        // 商品名称
        if (!propOrigin('product_name')) {
            return result;
        }
htoooth authored
311
郝肖肖 authored
312 313 314
        result.name = propOrigin('product_name');
        result.skn = propOrigin('product_skn');
        result.productId = propOrigin('product_id');
htoooth authored
315
郝肖肖 authored
316 317 318 319
        // 商品价格
        result.marketPrice = propOrigin('format_market_price');
        result.salePrice = propOrigin('format_sales_price');
        result.hasOtherPrice = true;
htoooth authored
320
郝肖肖 authored
321 322 323 324
        if (result.salePrice === '0') {
            delete result.salePrice;
            result.hasOtherPrice = false;
        }
htoooth authored
325
郝肖肖 authored
326 327 328 329 330 331 332
        // 上市期
        if (propOrigin('expect_arrival_time')) {
            result.arrivalDate = `${propOrigin('expect_arrival_time')}月`;
            result.presalePrice = propOrigin('format_sales_price');
            delete result.salePrice;
            result.hasOtherPrice = false;
        }
htoooth authored
333
郝肖肖 authored
334 335
        // sku商品信息
        let skuData = this._getSkuDataByProductBaseInfo(origin);
htoooth authored
336
郝肖肖 authored
337 338 339
        // 商品购买状态
        let soldOut = !!(propOrigin('status') === 0 || skuData.totalStorageNum === 0);
        let virtualGoods = propOrigin('attribute') === 3; // 虚拟商品
htoooth authored
340
郝肖肖 authored
341 342
        if (!soldOut && !virtualGoods) {
            result.addToCart = 1;
htoooth authored
343 344
        }
郝肖肖 authored
345 346
        result.isCollect = propOrigin('is_collect');
        result.colors = skuData.skuGoods;
htoooth authored
347
郝肖肖 authored
348 349
        return result;
    }
htoooth authored
350
郝肖肖 authored
351 352 353 354 355
    /**
     *  获取某一个商品详情主页面
     */
    getProductInfoAsync(pid) {
        let that = this;
htoooth authored
356
郝肖肖 authored
357 358 359 360
        return co(function * () {
            if (!pid) {
                return {};
            }
htoooth authored
361
郝肖肖 authored
362 363
            // 获取商品基本信息
            let productData = yield new ProductAPI(that.ctx).getProductAsync(pid);
htoooth authored
364
郝肖肖 authored
365 366 367 368 369 370
            if (_.isEmpty(productData.data)) {
                return Promise.reject({
                    code: 404,
                    message: 'app.product.data api wrong'
                });
            }
htoooth authored
371
郝肖肖 authored
372
            let productSkn = _.get(productData, 'data.product_skn');
yyq authored
373
郝肖肖 authored
374 375 376 377 378 379
            let productDescription = yield that._getProductIntroAsync(productSkn);
            let productInfo = that._detailDataPkg(productData.data);
            let intro = that._getIntroInfo(productSkn, productDescription);

            return Object.assign(productInfo, intro);
        })();
yyq authored
380 381
    }
郝肖肖 authored
382 383 384 385 386 387
    // 获取优惠券列表
    getCoupons(uid) {
        let params = {
            method: 'app.Shopping.listCoupon',
            uid: uid
        };
yyq authored
388
郝肖肖 authored
389
        return this.get({data: params}).then(result => {
m  
刘传洋 authored
390
郝肖肖 authored
391 392 393
            if (result.code === 200) {
                let unuse = [];
                let use = [];
m  
刘传洋 authored
394
郝肖肖 authored
395 396 397 398 399 400 401 402
                _.get(result, 'data.unusable_coupons', []).forEach(i => {
                    unuse.push({
                        code: i.coupon_code,
                        name: i.coupon_name,
                        value: i.coupon_value,
                        canuse: false
                    });
                });
OF1706 authored
403
郝肖肖 authored
404 405 406 407 408 409 410 411 412 413
                _.get(result, 'data.usable_coupons', []).forEach(i => {
                    use.push({
                        code: i.coupon_code,
                        name: i.coupon_name,
                        value: i.coupon_value
                    });
                });

                result.data = _.concat(use, unuse);
            }
OF1706 authored
414 415

            return result;
郝肖肖 authored
416 417
        });
    }
OF1706 authored
418
郝肖肖 authored
419 420 421 422 423 424 425 426 427 428 429 430
    /**
     * 购物车数据
     * @param uid
     * @param shoppingKey
     * @param cartDelList
     * @returns {*}
     */
    getCartData(uid, shoppingKey, cartDelList) {
        return new CartApi(this.ctx).cartData(uid, shoppingKey).then(cartDataRet => {
            return chelper.formatCart(cartDataRet, uid, shoppingKey, cartDelList);
        });
    }
OF1706 authored
431
郝肖肖 authored
432 433 434 435 436 437 438 439 440 441
    /**
     * 获取购物车总数
     *
     * @param int $uid 用户ID
     * @param string $shoppingKey 客户端购物标识
     * @return array
     */
    getCartCount(uid, shoppingKey) {
        return new CartApi(this.ctx).cartCount(uid, shoppingKey);
    }
OF1706 authored
442
郝肖肖 authored
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    /**
     * 加入购物车 获取数量
     *
     * @param int $productSku 商品SKU
     * @param int $buyNumber 购买数量
     * @param int $goodsType 商品类型,0表示普通商品,1表示加价购商品
     * @param int $isEdit 是否是编辑商品SKU,0表示不是编辑
     * @param null|int $promotionId 促销id,默认null(加价购有关)
     * @param null|int $uid 用户UID,可以不传
     * @param string $shoppingKey 购物车在浏览器的唯一识别码,可以不传
     * @return array 加入购物车接口返回的数据
     */
    addCart(productSku, buyNumber, goodsType, isEdit, promotionId, uid, shoppingKey) {
        let that = this;

        return co(function * () {
            let result = {
                code: 400,
                message: ERROR_400_MESSAGE
            };
OF1706 authored
463
郝肖肖 authored
464 465 466
            if (!productSku) {
                return result;
            }
m  
刘传洋 authored
467
郝肖肖 authored
468 469 470
            let ret = yield new CartApi(that.ctx).addToCart(
                productSku, buyNumber, goodsType, isEdit, promotionId, uid, shoppingKey
            );
m  
刘传洋 authored
471
郝肖肖 authored
472 473 474
            if (ret && ret.code) {
                result = ret;
            }
m  
刘传洋 authored
475
郝肖肖 authored
476 477 478
            return result;
        })();
    }
m  
刘传洋 authored
479
郝肖肖 authored
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
    /**
     * 加入购物车
     *
     * @param int $productSku 商品SKU
     * @param int $buyNumber 购买数量
     * @param int $goodsType 商品类型,0表示普通商品,1表示加价购商品
     * @param int $isEdit 是否是编辑商品SKU,0表示不是编辑
     * @param null|int $promotionId 促销id,默认null(加价购有关)
     * @param null|int $uid 用户UID,可以不传
     * @param string $shoppingKey 购物车在浏览器的唯一识别码,可以不传
     * @return array 加入购物车接口返回的数据
     */
    addToCart(productSku, buyNumber, goodsType, isEdit, promotionId, uid, shoppingKey, cartDelList) {
        let that = this;

        return co(function * () {
            let result = {
                code: 400,
                message: ERROR_400_MESSAGE
m  
刘传洋 authored
499 500
            };
郝肖肖 authored
501 502 503
            if (!productSku) {
                return result;
            }
m  
刘传洋 authored
504
郝肖肖 authored
505 506 507
            let ret = yield new CartApi(that.ctx).addToCart(
                productSku, buyNumber, goodsType, isEdit, promotionId, uid, shoppingKey
            );
m  
刘传洋 authored
508
郝肖肖 authored
509 510 511 512 513 514
            if (ret && ret.code) {
                result = {
                    code: ret.code,
                    data: yield that.getCartData(uid, shoppingKey || _.get(ret, 'data.shopping_key'), cartDelList)
                };
            }
m  
刘传洋 authored
515
郝肖肖 authored
516 517 518
            if (!shoppingKey && result.data) {
                result.data.shopping_key = _.get(ret, 'data.shopping_key');
            }
m  
刘传洋 authored
519 520

            return result;
郝肖肖 authored
521 522
        })();
    }
m  
刘传洋 authored
523
郝肖肖 authored
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
    /**
     * 购物车商品选择与取消接口返回的数据处理
     *
     * @param int $uid 用户ID
     * @param string $skuList 商品sku列表json格式, 如{"744403":1}
     * @param string $shoppingKey 未登录用户唯一识别码
     * @param bool hasPromotion 标识是不是有promotion_id参数, 后端会去调用不同的接口
     * @return array 处理之后的数据的数据
     */
    selectGoods(uid, skuList, shoppingKey, hasPromotion, cartDelList) {
        let that = this;

        return co(function * () {
            let result = {
                code: 400,
                message: ERROR_400_MESSAGE
m  
刘传洋 authored
540 541
            };
郝肖肖 authored
542 543 544
            if (!skuList) {
                return result;
            }
m  
刘传洋 authored
545
郝肖肖 authored
546
            let select = yield new CartApi(that.ctx).selectGoods(uid, skuList, shoppingKey, hasPromotion);
m  
刘传洋 authored
547
郝肖肖 authored
548 549 550 551 552 553
            if (select && select.code) {
                result = {
                    code: select.code,
                    data: chelper.formatCart(select, uid, shoppingKey, cartDelList)
                };
            }
m  
刘传洋 authored
554 555

            return result;
郝肖肖 authored
556 557
        })();
    }
m  
刘传洋 authored
558
郝肖肖 authored
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
    /**
     * 修改购物车商品数量
     *
     * @param int $uid 用户ID
     * @param string $sku 商品SKU
     * @param int $increaseNum 增加的数目
     * @param int $decreaseNum 减少的数目
     * @param string $shoppingKey 未登录用户唯一识别码
     * @return array 接口返回的数据
     */
    modifyProductNum(uid, sku, increaseNum, decreaseNum, shoppingKey, cartDelList) {
        let that = this;

        return co(function * () {
            let result = {
                code: 400,
                message: ERROR_400_MESSAGE
m  
刘传洋 authored
576
            };
m  
刘传洋 authored
577
郝肖肖 authored
578 579 580
            if (!sku) {
                return result;
            }
m  
刘传洋 authored
581
郝肖肖 authored
582
            let ret = yield new CartApi(that.ctx).modifyProductNum(uid, sku, increaseNum, decreaseNum, shoppingKey);
m  
刘传洋 authored
583
郝肖肖 authored
584 585 586 587 588 589
            if (ret && ret.code) {
                result = {
                    code: ret.code,
                    data: yield that.getCartData(uid, shoppingKey, cartDelList)
                };
            }
m  
刘传洋 authored
590 591

            return result;
郝肖肖 authored
592
        })();
m  
刘传洋 authored
593
郝肖肖 authored
594
    }
OF1706 authored
595
郝肖肖 authored
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
    /**
     * 移出购物车
     *
     * @param int $uid 用户ID
     * @param string $skuList 商品sku列表json格式, 如{"744403":1}
     * @param string $count 要删除的数目
     * @param string $shoppingKey 未登录用户唯一识别码
     * @param bool hasPromotion 标识是不是有promotion_id参数, 后端会去调用不同的接口
     * @return array 接口返回的数据
     */
    removeFromCart(uid, shoppingKey, skuList, hasPromotion, cartDelList) {
        let that = this;

        return co(function * () {
            let result = {
                code: 400,
                message: ERROR_400_MESSAGE
m  
刘传洋 authored
613
            };
m  
刘传洋 authored
614
郝肖肖 authored
615 616 617
            if (!skuList) {
                return result;
            }
m  
刘传洋 authored
618
郝肖肖 authored
619
            let ret = yield new CartApi(that.ctx).removeFromCart(uid, shoppingKey, skuList, hasPromotion);
m  
刘传洋 authored
620
郝肖肖 authored
621 622 623
            if (!hasPromotion) {
                return ret;
            }
m  
刘传洋 authored
624
郝肖肖 authored
625 626 627 628 629 630
            if (ret && ret.code) {
                result = {
                    code: ret.code,
                    data: chelper.formatCart(ret, uid, shoppingKey, cartDelList)
                };
            }
m  
刘传洋 authored
631 632

            return result;
郝肖肖 authored
633 634
        })();
    }
m  
刘传洋 authored
635
郝肖肖 authored
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
    /**
     * 移入收藏夹
     *
     * @param int $uid 用户ID
     * @param string $skuList 商品sku列表
     * @param bool hasPromotion 标识是不是有promotion_id参数, 后端会去调用不同的接口
     * @return array 接口返回的数据
     */
    addToFav(uid, skuList, hasPromotion, cartDelList) {
        let that = this;

        return co(function * () {
            let result = {
                code: 400,
                message: ERROR_400_MESSAGE
m  
刘传洋 authored
651
            };
m  
刘传洋 authored
652
郝肖肖 authored
653 654 655 656 657 658 659 660 661 662 663
            if (!uid) {
                return {
                    code: 403,
                    message: '请先登录!',
                    data: {
                        url: helpers.urlFormat('/signin.html', {
                            refer: helpers.urlFormat('/cart/cart')
                        })
                    }
                };
            }
m  
刘传洋 authored
664
郝肖肖 authored
665 666 667
            if (!skuList) {
                return result;
            }
m  
刘传洋 authored
668
郝肖肖 authored
669
            let ret = yield new CartApi(that.ctx).addToFav(uid, skuList, hasPromotion);
m  
刘传洋 authored
670
郝肖肖 authored
671 672 673 674 675 676
            if (ret && ret.code) {
                result = {
                    code: ret.code,
                    data: chelper.formatCart(ret, uid, null, cartDelList)
                };
            }
m  
刘传洋 authored
677 678

            return result;
郝肖肖 authored
679
        })();
m  
刘传洋 authored
680
郝肖肖 authored
681
    }
m  
刘传洋 authored
682
郝肖肖 authored
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
    /**
     * 检查用户是否收藏这一批商品
     *
     * @param int $uid 用户ID
     * @param array $skuList 商品SKU列表
     * @return array
     */
    checkUserIsFav(uid, skuList) {
        let that = this;

        return co(function * () {
            let result = {};

            if (!skuList) {
                return result;
m  
刘传洋 authored
698 699
            }
郝肖肖 authored
700
            if (!uid || !_.isNumber(uid)) {
m  
刘传洋 authored
701
郝肖肖 authored
702 703 704
                _.forEach(skuList, it => {
                    result[it] = false;
                });
m  
刘传洋 authored
705
郝肖肖 authored
706 707
                return result;
            }
m  
刘传洋 authored
708
郝肖肖 authored
709 710 711
            return yield new CartApi(that.ctx).checkUserIsFavProductList(uid, skuList);
        })();
    }
m  
刘传洋 authored
712
郝肖肖 authored
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
    /**
     * 获取为你优选商品 待处理
     *
     * @param $channel 频道
     * @param $uid 用户id
     * @param $udid 设备id
     * @param int $page 分页页码
     * @return array
     * @internal param $rec_pos
     * @internal param $limit
     */
    getRecommendProduct(channelNum, uid, udid, page) {
        let that = this;

        return co(function * () {

            let result = {
                code: 200,
                message: '',
                data: {
                    header: '为您优选',
                    hasPrev: false,
                    hasNext: false,
                    item: null
                }
m  
刘传洋 authored
738 739
            };
郝肖肖 authored
740 741
            if (!_.isNumber(page)) {
                return result;
m  
刘传洋 authored
742 743
            }
郝肖肖 authored
744 745
            let together = yield new CartApi(that.ctx).newPreference(channelNum, uid, udid, '100003', 30);
            let plist = _.get(together, 'data.product_list');
m  
刘传洋 authored
746
郝肖肖 authored
747 748 749
            if (!plist || !plist.length) {
                return result;
            }
m  
刘传洋 authored
750
郝肖肖 authored
751 752
            if (page > 5) {
                page = 1;
m  
刘传洋 authored
753 754
            }
郝肖肖 authored
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
            let begin = (page - 1) * 6;
            let end = begin + 7;
            let items = _.map(_.slice(plist, begin, end), it => {

                let item = {
                    id: it.product_id,
                    skn: it.product_skn,
                    title: it.product_name,
                    img: it.default_images ? helpers.image(it.default_images, 190, 250) : '',
                    alt: it.product_name,
                    price: '¥' + chelper.transPrice(it.sales_price),
                    marketPrice: (it.market_price && +it.market_price > +it.sales_price) ?
                                '¥' + chelper.transPrice(it.market_price) : ''
                };

                item.href = helpers.getUrlBySkc(it.product_skn);

                if (+it.sales_price !== +it.market_price) {
                    it.marketPrice = chelper.transPrice(it.market_price);
                }
                return item;
            });
m  
刘传洋 authored
777
郝肖肖 authored
778 779 780
            Object.assign(result.data, {
                item: items
            });
m  
刘传洋 authored
781 782

            return result;
郝肖肖 authored
783 784
        })();
    }
m  
刘传洋 authored
785
郝肖肖 authored
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
    /**
     * 获取凑单商品
     *
     * @param int $page 分页页码
     * @return array
     */
    getTogetherProduct(page) {
        let that = this;

        return co(function * () {
            let result = {
                code: 200,
                message: '',
                data: {
                    header: '凑单商品',
                    hasPrev: false,
                    hasNext: false,
                    item: null
                }
m  
刘传洋 authored
805 806
            };
郝肖肖 authored
807 808
            if (!_.isNumber(page)) {
                return result;
m  
刘传洋 authored
809 810
            }
郝肖肖 authored
811
            let together = yield new CartApi(that.ctx).togetherProduct(page);
m  
刘传洋 authored
812
郝肖肖 authored
813 814 815
            if (!_.get(together, 'data.goods')) {
                return result;
            }
m  
刘传洋 authored
816
郝肖肖 authored
817 818
            result.data.hasPrev = true;
            result.data.hasNext = true;
m  
刘传洋 authored
819
郝肖肖 authored
820
            result.data.item = _.map(together.data.goods, it => {
m  
刘传洋 authored
821
郝肖肖 authored
822 823 824 825 826 827 828 829 830
                let item = {
                    id: it.id,
                    skn: it.product_skn,
                    href: it.url,
                    title: it.product_name,
                    img: it.default_pic ? helpers.image(it.default_pic, 100, 100) : '',
                    alt: it.product_name,
                    price: Number(it.price.sales_price)
                };
m  
刘传洋 authored
831
郝肖肖 authored
832 833 834
                if (item.price !== Number(it.price.market_price)) {
                    it.marketPrice = Number(it.price.market_price);
                }
m  
刘传洋 authored
835
郝肖肖 authored
836 837
                return item;
            });
m  
刘传洋 authored
838
郝肖肖 authored
839 840 841 842
            // 当数据量不足6个时,判定为没有下一页
            if (page === 1) {
                result.data.hasPrev = false;
            }
m  
刘传洋 authored
843
郝肖肖 authored
844 845 846
            if (!result.data.item || result.data.item.length < 6) {
                result.data.hasNext = false;
            }
m  
刘传洋 authored
847
郝肖肖 authored
848 849 850
            return result;
        })();
    }
m  
刘传洋 authored
851
郝肖肖 authored
852 853 854 855 856 857 858 859 860 861 862 863 864 865
    /**
     * 修改购物车商品
     * @param param
     * @param uid
     * @param shoppingKey
     * @param cartDelList
     */
    modifyProduct(param, uid, shoppingKey, cartDelList) {
        let that = this;

        return co(function * () {
            let result = {
                code: 400,
                message: ERROR_400_MESSAGE
m  
刘传洋 authored
866 867
            };
郝肖肖 authored
868
            let ret = yield new CartApi(that.ctx).modifyProduct(param);
m  
刘传洋 authored
869
郝肖肖 authored
870 871 872 873 874 875
            if (ret && ret.code) {
                result = {
                    code: ret.code,
                    data: yield that.getCartData(uid, shoppingKey, cartDelList)
                };
            }
m  
刘传洋 authored
876
郝肖肖 authored
877 878 879
            return result;
        })();
    }
m  
刘传洋 authored
880
郝肖肖 authored
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
    /**
     * 重选赠品或加价购商品
     * @param uid
     * @param shoppingKey
     * @param promotionId
     * @param newSkn
     * @param newSku
     * @param cartDelList
     */
    swapGift(uid, shoppingKey, promotionId, newSkn, newSku, cartDelList) {
        let that = this;

        return co(function * () {

            let result = {
                code: 400,
                message: ERROR_400_MESSAGE
            };
m  
刘传洋 authored
899
郝肖肖 authored
900
            let ret = yield new CartApi(that.ctx).swapGift(uid, shoppingKey, promotionId, newSkn, newSku);
m  
刘传洋 authored
901
郝肖肖 authored
902 903 904 905 906 907
            if (ret && ret.code) {
                result = {
                    code: ret.code,
                    data: yield that.getCartData(uid, shoppingKey, cartDelList)
                };
            }
m  
刘传洋 authored
908
郝肖肖 authored
909 910
            return result;
        })();
m  
刘传洋 authored
911
郝肖肖 authored
912
    }
m  
刘传洋 authored
913
郝肖肖 authored
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
    /**
     * 查询指定优惠下的可选赠品或加价购
     * @param promotionId 优惠ID
     * @param uid
     * @param shoppingKey
     */
    queryUserPromotionGift(promotionId, uid, shoppingKey) {
        let that = this;
        let cartApiModel = new CartApi(that.ctx);

        return co(function * () {
            let promotionGifts = yield cartApiModel.queryPromotionGift(promotionId);
            let cartDataRet = yield cartApiModel.cartData(uid, shoppingKey);
            let selectedGift = chelper.formatCartGoods(_.get(cartDataRet, 'data.ordinary_cart_data.goods_list'));
            let currentProduct = null;  // 默认展示第一个商品

            if (promotionGifts && promotionGifts.code === 200) {
                let promotionList = chelper.formatPriceGifts(_.get(promotionGifts, 'data.arrays', []));

                promotionGifts.data = _.get(promotionList, '[0]', {});

                let goodsList = [];
                let checked;

                _.forEach(promotionList, value => {
                    if (!value.goodsList || !value.goodsList.length) {
                        return;
yyq authored
941 942
                    }
郝肖肖 authored
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
                    _.forEach(value.goodsList, it => {
                        if (!checked) {
                            checked = true;
                            it.active = true;
                            currentProduct = it;
                            promotionGifts.data.promotionDesc = value.promotionTitle;
                        }

                        Object.assign(it, {
                            isSelected: !!_.find(selectedGift, ['skn', it.skn]),
                            promotionId: value.promotionId,
                            promotionTitle: value.promotionTitle,
                            maxSelectNumber: value.maxSelectNumber
                        });

                        goodsList.push(it);
m  
刘传洋 authored
959 960
                    });
                });
yyq authored
961
郝肖肖 authored
962
                promotionGifts.data.goodsList = goodsList;
m  
刘传洋 authored
963
郝肖肖 authored
964 965 966
                // 获取选中商品的数据详情
                if (currentProduct && currentProduct.id) {
                    let prodInfo = yield that.getProductInfoAsync(currentProduct.id);
m  
刘传洋 authored
967
郝肖肖 authored
968 969 970 971
                    // 价格为活动价
                    if (prodInfo) {
                        prodInfo.salePrice = '¥' + currentProduct.subjoinPrice;
                    }
yyq authored
972
郝肖肖 authored
973 974
                    promotionGifts.data.productInfo = prodInfo;
                }
m  
刘传洋 authored
975
            }
yyq authored
976
m  
刘传洋 authored
977
郝肖肖 authored
978 979
            return promotionGifts;
        })();
m  
刘传洋 authored
980
郝肖肖 authored
981
    }
m  
刘传洋 authored
982
郝肖肖 authored
983 984 985 986 987 988 989
    /**
     * 首页mini购物车数据
     * @param uid
     * @param shoppingKey
     */
    getMiniCartData(uid, shoppingKey) {
        let that = this;
m  
刘传洋 authored
990
郝肖肖 authored
991 992
        return co(function * () {
            let cartDataRet = yield new CartApi(that.ctx).cartData(uid, shoppingKey);
m  
刘传洋 authored
993
郝肖肖 authored
994 995 996
            if (!cartDataRet || cartDataRet.code !== 200) {
                return null;
            }
m  
刘传洋 authored
997
郝肖肖 authored
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
            let ret = cartDataRet && cartDataRet.data;
            let advCart = _.get(ret, 'advance_cart_data');
            let comCart = _.get(ret, 'ordinary_cart_data');
            let advGoodsList = _.flatten(_.map(_.get(advCart, 'goods_pool_list'), 'goods_list'));
            let comGoodsList = _.flatten(_.map(_.get(comCart, 'goods_pool_list'), 'goods_list'));
            let comSubs = _.flatten(_.map(_.get(comCart, 'goods_pool_list'), 'sub_pool'));
            let subsGoodsList = _.flatten(_.map(comSubs, 'goods_list'));

            let advOffShe = _.get(advCart, 'off_shelves_goods_list');
            let comOffShe = _.get(comCart, 'off_shelves_goods_list');

            let advSoldOuts = _.get(advCart, 'sold_out_goods_list');
            let comSoldOuts = _.get(comCart, 'sold_out_goods_list');

            let goodsList = _(advGoodsList).concat(comGoodsList, subsGoodsList,
                                                    advOffShe, comOffShe,
                                                    advSoldOuts, comSoldOuts);

            goodsList = _.filter(goodsList.value(), it => !!it);

            let glist = _.map(goodsList, g => {
                let goods = {
                    is_advance: g.is_advance,
                    default_img: g.goods_images ? helpers.image(g.goods_images, 46, 62) : '',
                    product_name: g.product_name,
                    factory_goods_name: g.factory_goods_name,
                    size_name: g.size_name,
                    show_price: chelper.transPrice(g.real_price),
                    buy_number: g.buy_number,
                    goods_incart_id: g.shopping_cart_id,
                    product_sku: g.product_sku,
                    promotion_id: g.promotion_id
                };

                goods.product_url = helpers.getUrlBySkc(g.product_skn);
                return goods;
            });
m  
刘传洋 authored
1035
郝肖肖 authored
1036 1037
            let advCount = _.get(ret, 'advance_cart_data.shopping_cart_data.goods_count') || 0;
            let comCount = _.get(ret, 'ordinary_cart_data.shopping_cart_data.goods_count') || 0;
m  
刘传洋 authored
1038
郝肖肖 authored
1039 1040 1041 1042 1043 1044
            return {
                main_goods: glist,
                total: Number(advCount) + Number(comCount)
            };
        })();
    }
1045
郝肖肖 authored
1046 1047 1048 1049
    // 套餐添加购物车
    addBundleData(params) {
        return new CartApi(this.ctx).addBundle(params);
    }
郝肖肖 authored
1050
郝肖肖 authored
1051 1052 1053
    // 套餐增减购物车
    bundleNumData(params, cartDelList) {
        let that = this;
郝肖肖 authored
1054
郝肖肖 authored
1055 1056
        return co(function * () {
            let result = {
郝肖肖 authored
1057
                code: 400,
郝肖肖 authored
1058
                message: ERROR_400_MESSAGE
郝肖肖 authored
1059 1060
            };
郝肖肖 authored
1061 1062 1063 1064 1065 1066
            if (!params.activity_id) {
                return {
                    code: 400,
                    message: '无效的活动'
                };
            }
郝肖肖 authored
1067
郝肖肖 authored
1068
            let ret = yield new CartApi(that.ctx).bundleNum(params);
郝肖肖 authored
1069
郝肖肖 authored
1070 1071 1072 1073 1074 1075
            if (ret && ret.code) {
                result = {
                    code: ret.code,
                    data: yield that.getCartData(params.uid, params.shopping_key, cartDelList)
                };
            }
郝肖肖 authored
1076
郝肖肖 authored
1077 1078 1079
            return result;
        })();
    }
htoooth authored
1080
};