detail.js 31.4 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 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 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 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 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 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 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 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
/**
 * 商品详情models
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/5/6
 */

'use strict';

const _ = require('lodash');
const comment = require('./consult-comment');

const api = global.yoho.API;
const helpers = global.yoho.helpers;

const SINGLE_TICKETS_SKN = 51335912; // 展览票

/**
 * 获取用户数据信息
 * @param  {[string]} uid
 * @return {[array]}
 */
const _getUserProfile = (uid) => {
    if (!uid) {
        return Promise.resolve({
            code: 200,
            data: {}
        });
    }
    return api.get('', {
        method: 'app.passport.profile',
        uid: uid
    }, {
        cache: true
    });
};

/**
 * 处理品牌关联店铺信息
 * @param  {array}
 * @return {array}
 */
const _processShopsInfo = (data) => {
    let enterStore = [];

    _.forEach(data, function(value) {
        let shopInfo = {
            img: value.brand_ico,
            storeName: value.brand_name
        };

        if (value.shop_id) {
            shopInfo.url = helpers.urlFormat('/product/index/brand', {
                shop_id: value.shop_id
            });
        } else {
            shopInfo.url = helpers.urlFormat('', null, value.brand_domain);
        }

        enterStore.push(shopInfo);
    });
    return enterStore;
};

/**
 * 处理限购商品的有关按钮状态(或取现购买以及底部商品购买按钮)
 * @param  {array} 需要处理的数据
 * @param  {int} 限购商品的关联状态
 * @param  {Boolean} 限购商品是否已开售
 * @return {array}
 */
const _procShowStatus = (data, showStatus, isBeginSale) => {
    switch (showStatus) {
        case 1: // 开售前/后,立即分享获得限购码(用户未领取限购码)
      // 显示获取限购码按钮
            data.canGetLimitCode = true;

            if (isBeginSale) { // 开售后
                data.cartInfo.noLimitCode = true;
            } else {
                data.cartInfo.limitNotForSale = true;
            }
            break;
        case 2: // 开售后,限购码已抢光(用户未领取限购码)
            data.codeEmpty = true;
            data.cartInfo.noLimitCode = true;
            break;
        case 3: // 开售后,商品已经售罄
            data.noLimitGoodsBtn = true;
            data.cartInfo.soldOut = true;
            break;
        case 4: // 开售后,立即购买(用户已领取限购码)
            data.gotCode = true;
            data.cartInfo.canBuyLimit = true;
            break;
        case 5: // 开售前,限购码已被抢光(用户未领取限购码)
            data.codeEmpty = true;
            data.cartInfo.limitNotForSale = true;
            break;
        case 6: // 开售前,即将开售(用户已领取限购码)
            data.gotCode = true;
            data.cartInfo.limitNotForSale = true;
            break;
        case 7: // 开售后,用户已经用获得的限购码购买过商品
            data.gotCode = true;
            data.cartInfo.noLimitCode = true;
            break;
        default:
            break;
    }

    return data;
};

/**
 * 根据设备类型获得限购商品跳转app的url
 * @param  {string} productCode 限购商品码
 * @param  {string} skn 限购商品skn
 * @return {string} 限购商品跳转url
 */
const _getLimitCodeUrl = (productCode, skn, ua) => {
    let url = 'yohoapp://yoho.app/openwith?limit_product_code=' + productCode +
    '&product_skn=' + skn;

    let isIphone = String(ua).indexOf('iPhone') >= 0;
    let isIpad = ua.indexOf('iPad') >= 0;

    if (isIphone || isIpad) {
        let params = {};

        params.lp = productCode;
        url = 'yohobuy://' + helpers.urlFormat('/limitpurchase', params);
    }
    return url;
};

/**
 * 详情页数据格式化
 * @param origin Object 原始数据
 * @return dest Object 格式化数据
 */
// 老接口h5的代码
// const _detailDataPkg = (origin, uid, vipLevel, ua) => {
//     let dest = {}, // 结果输出
//         thumbImageList = [],
//         colorGroup = {},
//         sizeGroup = [],
//         totalStorageNum = 0;

//     // 商品名称
//     if (origin.productName === null || typeof origin.productName === 'undefined') {
//         return dest;
//     }

//     dest.goodsName = origin.productName;

//     // 是否是虚拟商品
//     dest.virtualGoods = origin.attribute * 1 === 3 ? true : false;

//     // 用户未登录时
//     if (!uid) {
//         let params = {};

//         params.refer = helpers.urlFormat('/product/show_' + origin.erpProductId + '.html');
//         dest.loginUrl = helpers.urlFormat('/signin.html', params);
//     }

//     // 商品促销短语
//     if (origin.salesPhrase) {
//         dest.goodsSubtitle = origin.salesPhrase;
//     }

//     // 促销信息 TODO: 换新接口
//     if (origin.promotionBoList) {
//         let discountList = [];

//         _.forEach(origin.promotionBoList, function(value) {
//             discountList.push({
//                 text: `【${value.promotionType}】${value.promotionTitle}`
//             });
//         });

//         if (discountList.length) {
//             dest.goodsDiscount = {
//                 list: discountList
//             };
//         }
//     }

//     // 商品标签
//     if (origin.productTagBoList) {
//         let productTags = {};

//         _.forEach(origin.productTagBoList, function(value) {
//             switch (value.tagLabel) {
//                 case 'is_soon_sold_out':
//                     productTags.is_soon_sold_out = true;
//                     break;
//                 case 'is_new':
//                     productTags.is_new = true;
//                     break;
//                 case 'is_discount':
//                     productTags.is_discount = true;
//                     break;
//                 case 'is_limited':
//                     productTags.is_limited = true;
//                     break;
//                 case 'is_yohood':
//                     productTags.is_yohood = true;
//                     break;
//                 case 'is_advance':
//                     productTags.is_advance = true;
//                     break;
//                 default:
//                     break;
//             }
//         });

//         dest.tags = productTags;
//     }

//     // 商品价格
//     if (origin.productPriceBo) {
//         let originPrice = origin.productPriceBo;
//         let goodsPrice = {
//             currentPrice: originPrice.formatSalesPrice
//         };

//         if (originPrice.formatMarketPrice !== originPrice.formatSalesPrice) {
//             goodsPrice.previousPrice = originPrice.formatMarketPrice;
//         }

//         dest.goodsPrice = goodsPrice;

//         // 商品返回 有货币
//         if (originPrice.yohoCoinNum) {
//             dest.commodityReturn = originPrice.yohoCoinNum;
//         }
//     }

//     // VIP 商品价格
//     if (origin.productPriceBo.vipPrices) {
//         let vipList = [];

//         _.forEach(origin.productPriceBo.vipPrices, function(value) {
//             vipList.push({
//                 level: value.vipLevel,
//                 text: value.vipPrice,
//                 currentLevel: (value.vipLevel === vipLevel)
//             });
//         });

//         if (vipList.length) {
//             dest.vipLevel = {
//                 list: vipList
//             };
//         }

//     }

//     // 上市期
//     if (origin.expectArrivalTime) {
//         dest.periodOfMarket = `${origin.expectArrivalTime}月`;
//     }

//     // 商品咨询
//     dest.feedbacks = {
//         consults: [],
//         consultsNum: 0
//     };

//     let consultParams = {
//         product_id: origin.id
//     };

//     if (_.has(dest, 'feedbacks.consultsNum')) {
//         consultParams.total = dest.feedbacks.consultsNum;
//         dest.feedbacks.consultsUrl = helpers.urlFormat('/product/detail/consults', consultParams);
//     } else {
//         dest.feedbacks.consultsUrl = helpers.urlFormat('/product/detail/consultform', consultParams);
//     }


//     // 商品评价
//     dest.feedbacks.commentsNum = 0;
//     if (origin.commentBoWrapper) {
//         dest.feedbacks.commentsNum = origin.commentBoWrapper.commentTotal;

//         let commentList = [];

//         _.forEach(origin.commentBoWrapper.commentBoList, function(value) {
//             commentList.push({
//                 userName: value.nickName,
//                 desc: `${value.colorName}/${value.sizeName}`,
//                 content: value.content ? value.content : '',
//                 time: value.createTime
//             });
//         });

//         dest.feedbacks.comments = commentList;

//         dest.feedbacks.commentsUrl = helpers.urlFormat('/product/detail/comments', {
//             product_id: origin.id,
//             total: dest.feedbacks.commentsNum
//         });
//     }

//     // 品牌信息
//     if (origin.brand) {
//         let extra = `?productSkn=${origin.erpProductId}&brandId=${origin.brand.id}`;

//         dest.preferenceUrl = `/product/detail/preference${extra}`;
//     }

//     dest.productSkn = origin.erpProductId;

//     // 商品信息
//     if (origin.goodsList) {
//         let goodsGroup = [],
//             sizeName = '',
//             colorList = [],
//             sizeList = {},
//             allSizeList = {},
//             colorStorageGroup = {},
//             colorStorageNum = 0;

//         _.forEach(origin.goodsList, function(value) {
//             if (value.status === 0) {
//                 return;
//             }

//             colorStorageNum = 0;

//             // 商品分组
//             if (value.goodsImagesList) {
//                 _.forEach(value.goodsImagesList, function(good) {
//                     goodsGroup.push({
//                         goodsId: good.goodsId,
//                         img: good.imageUrl
//                     });
//                 });
//             }

//             // 商品的尺码列表
//             colorStorageGroup[value.productSkc] = {};
//             if (value.goodsSizeBoList) {
//                 sizeList[value.productSkc] = [];
//                 _.forEach(value.goodsSizeBoList, function(size) {
//                     sizeList[value.productSkc].push({
//                         id: size.id,
//                         skuId: size.goodsSizeSkuId,
//                         goodsId: size.goodsId,
//                         colorId: size.colorId,
//                         name: size.sizeName,
//                         sizeNum: size.goodsSizeStorageNum
//                     });

//                     sizeName = size.sizeName;

//                     // 所有尺码列表,赋值用于前端展示默认尺码的时候
//                     // 判断出没有库存则显示灰色
//                     let build = {
//                         id: size.id,
//                         storage: size.goodsSizeStorageNum
//                     };

//                     allSizeList[sizeName] = (allSizeList[sizeName] === null ||
//                     typeof allSizeList[sizeName] === 'undefined') ? build :
//                         allSizeList[sizeName];

//                     colorStorageNum += parseInt(size.goodsSizeStorageNum, 10);
//                     colorStorageGroup[value.productSkc][sizeName] = parseInt(size.goodsSizeStorageNum, 10);
//                 });

//                 // 颜色分组
//                 colorList.push({
//                     id: value.colorId,
//                     skcId: value.productSkc,
//                     name: value.colorName,
//                     goodsName: value.goodsName,
//                     colorNum: colorStorageNum
//                 });
//             }

//             // 缩略图
//             thumbImageList.push({
//                 img: value.colorImage
//             });

//             // 商品库存总数
//             totalStorageNum += _.toNumber(colorStorageNum);
//         });

//         // 遍历所有尺码,构建颜色显示数据
//         let i = 1;

//         sizeGroup[0] = {
//             size: []
//         };

//         _.forEach(allSizeList, (value, key) => {

//             // 默认尺码
//             sizeGroup[0].size.push({
//                 name: key,
//                 sizeNum: _.toNumber(value.storage) > 0 ? true : false,
//                 id: value.id
//             });

//             colorGroup[i] = {
//                 color: []
//             };

//             // 各个颜色的尺码, 每行显示一个尺码对应的颜色
//             _.forEach(colorList, (colorArr) => {
//                 let tempColorArr = _.cloneDeep(colorArr);

//                 if (colorStorageGroup[tempColorArr.skcId] &&
//                     colorStorageGroup[tempColorArr.skcId][key]) {
//                     tempColorArr.colorNum = colorStorageGroup[tempColorArr.skcId][key];
//                 } else {
//                     tempColorArr.colorNum = 0;
//                 }
//                 colorGroup[i].color.push(Object.assign({}, tempColorArr));
//             });
//             colorGroup[i].id = value.id;

//             ++i;
//         });

//         colorGroup[0] = {
//             color: []
//         };

//         // 遍历所有颜色, 构建尺码显示数据
//         i = 1;
//         _.forEach(colorList, function(value) {
//             // 各个尺码的颜色, 每行显示一个颜色的对应尺码
//             sizeGroup[i] = {
//                 size: sizeList[value.skcId],
//                 colorId: value.skcId
//             };

//             // 默认颜色
//             colorGroup[0].color.push(value);
//             ++i;
//         });


//         // 商品图:多个
//         if (goodsGroup.length > 1) {
//             let bannerList = [];

//             _.forEach(goodsGroup, function(value) {
//                 bannerList.push({
//                     img: value.img
//                 });
//             });

//             dest.bannerTop = {
//                 list: bannerList
//             };
//         } else if (goodsGroup[0] !== null && typeof goodsGroup[0] !== 'undefined') {
//             dest.bannerTop = {
//                 img: goodsGroup[0].img
//             };
//         }
//     }

//     // 悬浮的购物车信息
//     dest.cartInfo = {
//         cartUrl: helpers.urlFormat('/cart/index/index'),
//         numInCart: 0,
//         goodsInstore: origin.storage
//     };

//     let soldOut = (origin.storage === 0) || (origin.status === 0 || totalStorageNum === 0);
//     let notForSale = origin.attribute === 2;

//     // 显示加入购物车链接
//     if (!soldOut && !notForSale) {
//         _.orderBy(colorGroup);

//         Object.assign(dest.cartInfo, {
//             productId: origin.id,
//             thumbs: thumbImageList,
//             name: dest.goodsName ? dest.goodsName : '',
//             price: dest.goodsPrice.previousPrice ? dest.goodsPrice.previousPrice : '',
//             salePrice: dest.goodsPrice.currentPrice ? dest.goodsPrice.currentPrice : '',
//             totalNum: totalStorageNum,
//             colors: _.toArray(colorGroup),
//             sizes: sizeGroup
//         });

//         // 限购商品
//         if (origin.isLimitBuy === 'Y') {
//             // 是否开售
//             let isBeginSale = (origin.saleStatus !== null && typeof origin.saleStatus !== 'undefined' &&
//             origin.saleStatus === 1);

//             // 限购商品有关的展示状态
//             let showStatus = 1;

//             if (origin.showStatus !== null && typeof origin.showStatus !== 'undefined') {
//                 showStatus = parseInt(origin.showStatus, 10);
//             }

//             // 处理限购商品有关的按钮状态
//             dest = _procShowStatus(dest, showStatus, isBeginSale);

//             dest.cartInfo.limitProductCode = origin.limitProductCode;
//             dest.cartInfo.limitCodeUrl = _getLimitCodeUrl(origin.limitProductCode, origin.erpProductId, ua);
//             dest.cartInfo.limitProductPay = helpers.urlFormat('/cart/index/orderEnsure');
//         } else {
//             dest.cartInfo.addToCartUrl = helpers.urlFormat('/product/buy_' + origin.id + '_' +
//                 origin.goodsList.id + '.html');
//         }
//     } else if (notForSale) {
//         dest.cartInfo.notForSale = true;
//     } else if (soldOut) {
//         dest.cartInfo.soldOut = true;
//     }

//     // 是否收藏
//     dest.isCollect = false;
//     if (origin.isCollect !== null && typeof origin.isCollect !== 'undefined' && origin.isCollect === 'Y') {
//         dest.isCollect = true;
//         dest.cartInfo.isCollect = true;
//     }

//     // 底部简介URL链接
//     dest.introUrl = '/product/detail/intro/' + origin.erpProductId;
//     dest.id = origin.id;

//     // 虚拟商品(门票)
//     if (origin.attribute * 1 === 3) {
//         dest.tickets = true;
//         dest.ticketsConfirm = helpers.urlFormat('/cart/index/ticketsConfirm');

//         // 展览票
//         if (origin.erpProductId * 1 === SINGLE_TICKETS_SKN) {
//             dest.single = true;
//         } else {
//             // 套票
//             dest.package = true;
//         }

//         // 购票限制
//         dest.cartInfo.limit = 4;

//         // 清空活动
//         dest.goodsDiscount = [];

//         // 来自登录页,自动弹出选择框
//         // if (isset($_GET['product_type']) && $_GET['product_type'] == 'ticket') {
//         //     $result['showPannel'] = true;
//         // }
//     }

//     return dest;
// };
const _detailDataPkg = (origin, uid, vipLevel, ua) => {
    let dest = {}, // 结果输出
        thumbImageList = [],
        colorGroup = {},
        sizeGroup = [],
        totalStorageNum = 0;

    // 商品名称
    if (!origin.productName) {
        dest.feedbacks = {};
        return dest;
    }

    dest.goodsName = origin.productName;

    // 是否是虚拟商品
    dest.virtualGoods = (origin.attribute * 1 === 3);

  // 用户未登录时
    if (!uid) {
        dest.loginUrl = helpers.urlFormat('/signin.html', {
            refer: helpers.urlFormat('/product/show_' + origin.productSkn + '.html')
        });
    }

    // 商品促销短语
    origin.salesPhrase && (dest.goodsSubtitle = origin.salesPhrase);

  // 促销信息 TODO: 换新接口
    if (origin.promotionBoList) {
        let discountList = [];

        _.forEach(origin.promotionBoList, function(value) {
            discountList.push({
                text: `【${value.promotionType}${value.promotionTitle}`
            });
        });

        discountList.length && (dest.goodsDiscount = {
            list: discountList
        });
    }

    // 商品标签
    if (origin.tags) {
        let productTags = {};

        _.forEach(origin.tags, function(value) {
            productTags[value] = true;
        });

        dest.tags = productTags;
    }

    // 商品价格
    let goodsPrice = {
        currentPrice: origin.formatSalesPrice === '0' ? origin.formatMarketPrice : origin.formatSalesPrice
    };

    if (origin.formatSalesPrice !== '0' && origin.formatMarketPrice !== origin.formatSalesPrice) {
        goodsPrice.previousPrice = origin.formatMarketPrice;
    }

    dest.goodsPrice = goodsPrice;

    // 商品返回 YOHO 币
    origin.yohoCoinNum && (dest.commodityReturn = origin.yohoCoinNum);

    // VIP 商品价格
    if (origin.vip) {
        let vipList = [];
        let levelList = {
            银卡: 1,
            金卡: 2,
            白金: 3
        };

        _.forEach(origin.vip, function(value) {
            vipList.push({
                level: levelList[value.caption],
                text: value.price,
                currentLevel: (value.vipLevel === vipLevel)
            });
        });

        vipList.length && (dest.vipLevel = {
            list: vipList
        });

    }

    // 上市期
    origin.expectArrivalTime && (dest.periodOfMarket = `${origin.expectArrivalTime}月`);

  // 商品咨询
    dest.feedbacks = {
        consults: [],
        consultsNum: 0
    };

    let consultParams = {
        product_id: origin.productId
    };

    if (_.has(dest, 'feedbacks.consultsNum')) {
        consultParams.total = dest.feedbacks.consultsNum;
        dest.feedbacks.consultsUrl = helpers.urlFormat('/product/detail/consults', consultParams);
    } else {
        dest.feedbacks.consultsUrl = helpers.urlFormat('/product/detail/consultform', consultParams);
    }

    // 商品评价
    dest.feedbacks.commentsUrl = helpers.urlFormat('/product/detail/comments', {
        product_id: origin.productId,
    });

  // 品牌信息
    if (origin.brand) {
        let extra = `?productSkn=${origin.productSkn}&brandId=${origin.brandInfo.brandId}`;

        dest.preferenceUrl = `/product/detail/preference${extra}`;
    }

    dest.productSkn = origin.productSkn;

  // 商品信息
    if (origin.goodsList) {
        let goodsGroup = [],
            sizeName = '',
            colorList = [],
            sizeList = {},
            allSizeList = {},
            colorStorageGroup = {},
            colorStorageNum = 0;

        _.forEach(origin.goodsList, function(value) {
            if (value.status === 0) {
                return;
            }

            colorStorageNum = 0;

            // 商品分组
            if (value.imagesList) {
                _.forEach(value.imagesList, function(good) {
                    goodsGroup.push({
                        goodsId: value.goodsId,
                        img: good.imageUrl
                    });
                });
            }

      // 商品的尺码列表
            colorStorageGroup[value.productSkc] = {};
            if (value.sizeList) {
                sizeList[value.productSkc] = [];
                _.forEach(value.sizeList, function(size) {
                    sizeList[value.productSkc].push({
                        id: size.sizeId,
                        skuId: size.productSku,
                        goodsId: value.goodsId,
                        colorId: value.colorId,
                        name: size.sizeName,
                        sizeNum: size.storageNumber
                    });

                    sizeName = size.sizeName;

          // 所有尺码列表,赋值用于前端展示默认尺码的时候
          // 判断出没有库存则显示灰色
                    let build = {
                        id: size.sizeId,
                        storage: size.storageNumber
                    };

                    allSizeList[sizeName] = (allSizeList[sizeName] === null ||
                        typeof allSizeList[sizeName] === 'undefined') ? build : allSizeList[sizeName];
                    colorStorageNum += parseInt(size.storageNumber, 10);
                    colorStorageGroup[value.productSkc][sizeName] = parseInt(size.storageNumber, 10);
                });

        // 颜色分组
                colorList.push({
                    id: value.colorId,
                    skcId: value.productSkc,
                    name: value.colorName,
                    colorNum: colorStorageNum
                });
            }

      // 缩略图
            thumbImageList.push({
                img: value.colorImage
            });

      // 商品库存总数
            totalStorageNum += _.toNumber(colorStorageNum);
        });

    // 遍历所有尺码,构建颜色显示数据
        let i = 1;

        sizeGroup[0] = {
            size: []
        };
        _.forEach(allSizeList, (value, key) => {
            // 默认尺码
            sizeGroup[0].size.push({
                name: key,
                sizeNum: _.toNumber(value.storage) > 0 ? true : false,
                id: value.id
            });

            colorGroup[i] = {
                color: []
            };

      // 各个颜色的尺码, 每行显示一个尺码对应的颜色
            _.forEach(colorList, (colorArr) => {
                let tempColorArr = _.cloneDeep(colorArr);

                if (colorStorageGroup[tempColorArr.skcId] &&
          colorStorageGroup[tempColorArr.skcId][key]) {
                    tempColorArr.colorNum = colorStorageGroup[tempColorArr.skcId][key];
                } else {
                    tempColorArr.colorNum = 0;
                }
                colorGroup[i].color.push(Object.assign({}, tempColorArr));
            });
            colorGroup[i].id = value.id;

            ++i;
        });

        colorGroup[0] = {
            color: []
        };

    // 遍历所有颜色, 构建尺码显示数据
        i = 1;
        _.forEach(colorList, function(value) {
      // 各个尺码的颜色, 每行显示一个颜色的对应尺码
            sizeGroup[i] = {
                size: sizeList[value.skcId],
                colorId: value.skcId
            };

      // 默认颜色
            colorGroup[0].color.push(value);
            ++i;
        });


    // 商品图:多个
        if (goodsGroup.length > 1) {
            let bannerList = [];

            _.forEach(goodsGroup, function(value) {
                bannerList.push({
                    img: value.img
                });
            });

            dest.bannerTop = {
                list: bannerList
            };
        } else if (goodsGroup[0]) {
            dest.bannerTop = {
                img: goodsGroup[0].img
            };
        }
    }

  // 悬浮的购物车信息
    dest.cartInfo = {
        cartUrl: helpers.urlFormat('/cart/index/index'),
        numInCart: 0,
        goodsInstore: origin.storageSum
    };
    let soldOut = (origin.storageSum === 0) || (totalStorageNum === 0); // status
    let notForSale = origin.attribute === 2;

  // 显示加入购物车链接
    if (!soldOut && !notForSale) {
        _.orderBy(colorGroup);

        Object.assign(dest.cartInfo, {
            productId: origin.productId,
            thumbs: thumbImageList,
            name: dest.goodsName ? dest.goodsName : '',
            price: dest.goodsPrice.previousPrice ? dest.goodsPrice.previousPrice : '',
            salePrice: dest.goodsPrice.currentPrice ? dest.goodsPrice.currentPrice : '',
            totalNum: totalStorageNum,
            colors: _.toArray(colorGroup),
            sizes: sizeGroup
        });

        // 限购商品
        if (origin.isLimitBuy) {
            // 是否开售
            let isBeginSale = (origin.saleStatus === 1);

      // 限购商品有关的展示状态
            let showStatus = 1;

            origin.showStatus && (showStatus = parseInt(origin.showStatus, 10));

      // 处理限购商品有关的按钮状态
            dest = _procShowStatus(dest, showStatus, isBeginSale);

            dest.cartInfo.limitProductCode = origin.limitProductCode;
            dest.cartInfo.limitCodeUrl = _getLimitCodeUrl(origin.limitProductCode, origin.productSkn, ua);
            dest.cartInfo.limitProductPay = helpers.urlFormat('/cart/index/orderEnsure');
        } else {
            dest.cartInfo.addToCartUrl = helpers.urlFormat('/product/buy_' + origin.productId + '_' +
                origin.goodsList.goodsId + '.html');
        }
    } else if (notForSale) {
        dest.cartInfo.notForSale = true;
    } else if (soldOut) {
        dest.cartInfo.soldOut = true;
    }

  // 是否收藏
    dest.isCollect = false;
    if (origin.isCollect === 'Y') {
        dest.isCollect = true;
        dest.cartInfo.isCollect = true;
    }

    // 底部简介URL链接
    dest.introUrl = '/product/detail/intro/' + origin.productSkn;
    dest.id = origin.productId;

  // 虚拟商品(门票)
    if (origin.attribute * 1 === 3) {
        dest.tickets = true;
        dest.ticketsConfirm = helpers.urlFormat('/cart/index/ticketsConfirm');

        // 展览票
        if (origin.productSkn * 1 === SINGLE_TICKETS_SKN) {
            dest.single = true;
        } else {
      // 套票
            dest.package = true;
        }

    // 购票限制
        dest.cartInfo.limit = 4;

    // 清空活动
        dest.goodsDiscount = [];

  // 来自登录页,自动弹出选择框
  // if (isset($_GET['product_type']) && $_GET['product_type'] == 'ticket') {
  //     $result['showPannel'] = true;
  // }
    }

    return dest;
};

let _getShopsInfo = (brandId) => {
    return api.get('', {
        method: 'app.shop.queryShopsByBrandId',
        brand_id: _.toString(brandId)
    }, {
        cache: true
    }).then(shops => {
        if (shops.code === 200) {
            return _processShopsInfo(shops.data);
        }

        return [];
    });
};

let _getPromotionInfo = (skn) => {
    return api.get('', {
        method: 'app.product.promotion',
        product_skn: _.toString(skn)
    }, {
        cache: true
    }).then((result) => {
        if (result.code === 200) {
            return result.data;
        }

        return {};
    });
};


/**
 * 获取默认咨询列表
 */
const _getCommonConsult = () => {
    let params = {
        method: 'app.consult.common'
    };

    return api.get('', params, {
        code: 200
    }).then(result => {
        let data = {};

        if (result.data) {
            data = result.data;
        }

        return data;
    });
};

let getProductData = (data) => {
    let finalResult;
    let params = {
        method: 'app.product.data'
    };

    if (data.id) { // 通过 productId 获取商品详情
        Object.assign(params, {
            product_id: _.toString(data.id)
        });
    } else if (data.productSkn) { // 通过 productSkn 获取商品详情
        Object.assign(params, {
            product_skn: _.toString(data.productSkn)
        });
    }

    if (data.uid) {
        params.uid = data.uid;
    }

    return _getUserProfile(params.uid).then((user) => {
        data.vipLevel = (user.data && user.data.vip_info && user.data.vip_info.cur_level) || '0';
        data.isStudent = (user.data && user.data.vip_info && user.data.vip_info.is_student) ? true : false;
        params.is_student = data.isStudent ? 1 : 0;
        params.current_vip_level = data.vipLevel;

        return api.get('', params, {
            code: 200
        }).then(global.yoho.camelCase).then(result => {
            if (result.code === 500 || !result.data) {
                return {};
            }
            result = result.data;
            return Promise.all([
                _getShopsInfo(result.brandInfo.brandId),
                _getPromotionInfo(result.productSkn),
                comment.getCommentInfo({
                    productId: result.productId
                }),
                _getCommonConsult(),
                comment.getConsults(result.productId, 1, 2)
            ]).then((info) => {
                result.promotionBoList = info[1];
                finalResult = _detailDataPkg(result, data.uid, data.vipLevel, data.ua);
                finalResult.enterStore = info[0];
                finalResult.isStudent = data.isStudent;

                Object.assign(finalResult.feedbacks, info[2]);

        /* 如果有咨询,显示咨询,否则显示常见问题 */
                if (info[4].total) {
                    finalResult.feedbacks.consultsNum = parseInt(info[4].total, 10);

                    Object.assign(finalResult.feedbacks, {
                        commonConsults: false,
                        consultsNum: parseInt(info[4].total, 10),
                        consults: _.take(info[4].list, 2)
                    });
                } else if (!_.isEmpty(info[3]) && !_.get(info[2], 'feedbacks.consultsNum', 0)) {
                    Object.assign(finalResult.feedbacks, {
                        commonConsults: true,
                        consultsNum: true,
                        consults: _.take(info[3], 2)
                    });
                }
                return finalResult;
            });

        });
    });
};

module.exports = {
    getProductData,
    getUserProfile: _getUserProfile
};