item-handler.js 22.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 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
/**
 * 商品详情页model
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/7/11
 */
'use strict';

const _ = require('lodash');
const helpers = global.yoho.helpers;

/**
 * 使sizeBoList id以 sizeAttributeBos id顺序一样
 * @function _sizeInfoBoSort
 * @param { Object } sizeInfoBo 尺码数据对象
 * @return { Object } 返回正确排序的尺码信息
 */
const _sizeInfoBoSort = sizeInfoBo => {
    if (!sizeInfoBo.sizeBoList || !sizeInfoBo.sizeAttributeBos) {
        return {};
    }

    // TODO: 这里的排序代码很乱

    _.forEach(sizeInfoBo.sizeBoList, (sizeBoList, sizek)=> {
        let sortAttr = {};

        _.forEach(sizeBoList.sortAttributes, sortAttributes => {
            sortAttr[sortAttributes.id] = sortAttributes;
        });

        sizeInfoBo.sizeBoList[sizek].sortAttributes = sortAttr;
    });


    _.forEach(sizeInfoBo.sizeBoList, (sizeBoList, sizek)=> {
        let sortAttr = [];

        _.forEach(sizeInfoBo.sizeAttributeBos, val => {
            sortAttr.push(sizeBoList.sortAttributes[val.id]);
        });

        sizeInfoBo.sizeBoList[sizek].sortAttributes = sortAttr;

    });

    return sizeInfoBo;
};

/**
 * 处理限购商品的有关按钮状态(或取现购买以及底部商品购买按钮)
 * @function _FashionTopGoodsStatus
 * @param { string } uid 用户uid
 * @param { number } showStatus 限购商品的关联状态
 * @param { boolean } isBeginSale 限购商品是否已开售
 * @return { Object } 潮流尖货状态
 */
const _FashionTopGoodsStatus = (uid, showStatus, isBeginSale) => {
    // 潮流尖货状态
    let resData = {
        getLimitedCode: true // 限购码状态
        // hasLimitedCode: false, //是否已经获取限购码
        // limitedCodeSoldOut: false, //限购码是否已经抢光
        // openSoon: false,//即将开售
        // dis: false, //失效
        // buyNow: false,  //是否立即购买
        // soldOut: false,  //是否已经售罄
        // getLimitedCodeDis: false // 限购码是否失效
    };

    switch (showStatus) {
        case 1: // 开售前/后,立即分享获得限购码(用户未领取限购码)
            if (isBeginSale) { // 开售后
                Object.assign(resData, {
                    buyNow: true,
                    dis: true
                });
            } else {
                Object.assign(resData, {
                    openSoon: true,
                    hasLimitedCode: false
                });
            }
            break;
        case 2: // 开售后,限购码已抢光(用户未领取限购码)
            Object.assign(resData, {
                buyNow: true,
                dis: true,
                limitedCodeSoldOut: true,
                getLimitedCode: false,
                hasLimitedCode: false
            });
            break;
        case 3: // 开售后,商品已经售罄
            Object.assign(resData, {
                soldOut: true,
                getLimitedCode: false
            });
            break;
        case 4: // 开售后,立即购买(用户已领取限购码)
            Object.assign(resData, {
                buyNow: true,
                dis: false,
                hasLimitedCode: true
            });
            if (!uid) { // 限购码失效
                resData.getLimitedCodeDis = true;
            }
            break;
        case 5: // 开售前,限购码已被抢光(用户未领取限购码)
            Object.assign(resData, {
                openSoon: true,
                hasLimitedCode: true,
                limitedCodeSoldOut: true,
                getLimitedCode: false
            });
            break;
        case 6: // 开售前,即将开售(用户已领取限购码)
            Object.assign(resData, {
                openSoon: true,
                hasLimitedCode: true
            });
            if (!uid) { // 限购码失效
                resData.getLimitedCodeDis = true;
            }
            break;
        case 7: // 开售后,用户已经用获得的限购码购买过商品
            Object.assign(resData, {
                buyNow: true,
                dis: true,
                hasLimitedCode: true
            });
            if (!uid) { // 限购码失效
                resData.getLimitedCodeDis = true;
            }
            break;
        default:
            break;
    }

    return resData;
};

/**
 * 设置SEO信息
 * @function setSeoInfo
 * @param { Object } goodInfo 商品信息
 * @param { Object } nav 导航菜单信息
 * @return { Object } 商品SEO信息
 */
const setSeoInfo = (goodInfo, nav) => {
    let brandName = _.get(goodInfo, 'brandName', ''),
        sortName = _.get(nav, '[1].name', '');

    return {
        title: `${brandName} ${sortName}|${goodInfo.name}正品`,
        keywords: `${brandName} ${sortName},${brandName}正品官网专卖店,${brandName}官方授权店,${brandName}正品,` +
            `${brandName}打折,${brandName}折扣店,${brandName}真品,${brandName}代购`,
        description: _.get(goodInfo, 'share.shareDesc', goodInfo.name)
    };
};

/**
 * 处理商品详情面表屑导航
 * @function setPathNav
 * @param { Object } data 商品分类信息
 * @param { string } name 商品名称
 * @return { Object } 面包屑导航数据
 */
const setPathNav = (data, name) => {
    let resData = {};

    if (!_.isEmpty(data)) {
        let navs = [{
            link: helpers.urlFormat(''),
            name: 'MEN首页' // 待处理
        }];
        let sort = data.sort[0];

        navs.push({
            link: helpers.urlFormat('/list', {
                msort: sort.sort_id

                // gender: '' // 待处理
            }),
            name: sort.sort_name,
            pathTitle: sort.sort_name
        });

        // 二级分类
        if (!_.isEmpty(sort.sub)) {
            navs.push({
                link: helpers.urlFormat('/list', {
                    msort: sort.sort_id,
                    misort: sort.sub.sort_id

                    // gender: '' // 待处理
                }),
                name: sort.sub[0].sort_name,
                pathTitle: sort.sub[0].sort_name
            });
        }

        navs.push({
            name: name,
            pathTitle: name
        });

        resData.nav = navs;
    }

    return resData;
};

/**
 * 设置品牌banner数据
 * @function setBrandBanner
 * @param { Object } brand 品牌相关数据
 * @return { Object } 品牌banner
 */
const setBrandBanner = brand => {
    let data = {
        brandId: brand.id,
        bgColor: '#000',
        brandLogo: {
            link: `/product/shop/${brand.brandDomain}`, // 品牌跳转链接 -- 待处理
            img: brand.brandIco
        },
        brandHome: {
            link: `/product/shop/${brand.brandDomain}` // 品牌跳转链接 -- 待处理
        }
    };

    return {
        brandBanner: data
    };
};

/**
 * 设置商品基本信息
 * @function setProductData
 * @param { Object } base 接口返货商品数据
 * @return { Object } 商品基本信息
 */
const setProductData = base => {
    let resData = {
        id: base.id,
        name: base.productName,
        brandName: _.has(base, 'brand.brandName') ? base.brand.brandName : '',
        intro: base.salesPhrase,
        sellPrice: base.productPriceBo.salesPrice,
        marketPrice: base.productPriceBo.marketPrice,
        total: 0
    };

    let chooseSkuFlag;

    if (_.toNumber(resData.sellPrice) >= _.toNumber(resData.marketPrice)) {
        // 售价与吊牌价相同,只显示售价
        _.unset(resData, 'marketPrice');
    }

    // 遍历颜色尺寸
    if (base.goodsList) {
        let goods = [];

        // 处理商品数据
        _.forEach(base.goodsList, function(value) {
            let group = {},
                thumbs = [],
                sizes = [];

            // 如果status为0,即skc下架时就跳过该商品
            if (!value.status) {
                return;
            }

            if (value.goodsImagesList) {
                group = {
                    name: value.colorName,
                    title: `${resData.name} ${value.colorName}`,
                    color: value.colorName,
                    total: 0
                };

                // 有RGB值限时rgb值没有则显示背景图
                if (value.colorCode) {
                    group.rgb = `#${value.colorCode}`;
                } else {
                    group.rgb = `url('${helpers.image(value.colorImage, 30, 30)}')`;
                }

                // 商品颜色列表
                _.forEach(value.goodsImagesList, function(subValue) {
                    thumbs.push(subValue.imageUrl);
                });
                group.thumbs = thumbs;

                // 缩略图空,不显示
                if (!thumbs.length) {
                    return;
                }

                // 默认第一张图片
                if (!_.has(resData, 'img')) {
                    resData.img = value.colorImage;
                }

                // 商品尺码列表
                _.forEach(value.goodsSizeBoList, function(subValue) {
                    let size = {
                        name: subValue.sizeName,
                        title: subValue.sizeName,
                        sku: subValue.goodsSizeSkuId,
                        num: _.toInteger(subValue.goodsSizeStorageNum),
                        goodsId: subValue.goodsId
                    };

                    // 虚拟商品,增加为一件
                    if (subValue.attribute === 3) {
                        size.num = size.num > 1 ? 1 : 0;
                    }

                    // 如果status为0,即skc下架时,则库存设为0
                    if (subValue.status === 0) {
                        size.num = 0;
                    }

                    // 单个sku商品的总数累加
                    group.total += size.num;

                    // 默认选中该sku商品
                    if (group.total > 0 && !chooseSkuFlag) {
                        resData.colorName = group.name;
                        group.cur = true; // 选中sku商品
                        chooseSkuFlag = true;
                    }

                    // 商品的总数累加
                    resData.total += size.num;

                    sizes.push(size);
                });
                group.sizes = sizes;
            }

            goods.push(group);
        });

        // 默认设置第一个选中
        if (goods.length && !chooseSkuFlag) {
            goods[0].cur = true;
        }

        resData.colors = goods;
    }

    // 限购商品
    if (base.isLimitBuy === 'Y') {
        let isBeginSale = (base.saleStatus && +base.saleStatus) === 1; // 是否开售
        let showStatus = 1; // 限购商品有关的展示状态

        if (base.showStatus) {
            showStatus = _.toInteger(base.showStatus);
        }

        let fashTopGoods = _FashionTopGoodsStatus(base.uid, showStatus, isBeginSale);

        // 潮流尖货状态
        resData.fashionTopGoods = {
            getLimitedCode: fashTopGoods.getLimitedCode, // 限购码状态
            hasLimitedCode: fashTopGoods.hasLimitedCode, // 是否已经获取限购码
            limitedCodeSoldOut: fashTopGoods.limitedCodeSoldOut, // 限购码是否已经抢光
            getLimitedCodeDis: fashTopGoods.getLimitedCodeDis // 限购码是否失效
        };

        if (fashTopGoods.soldOut) {
            resData.soldOut = fashTopGoods.soldOut;

            // totalStorageNum = 0;//改总数为已售磬
        } else {
            resData.openSoon = fashTopGoods.openSoon;// 即将开售
            resData.dis = fashTopGoods.dis;// 是否失效
            resData.buyNow = fashTopGoods.buyNow;// 是否立即购买
        }
    }

    let soldOut = +base.status === 0 || resData.total === 0;

    if (+base.attribute === 2) { // 非卖品
        resData.notForSale = true;
    } else if (soldOut) { // 已售磬
        resData.soldOut = true;
        _.unset(resData, 'fashionTopGoods');
    } else if (+base.attribute === 3) { // 虚拟商品
        // 虚拟商品老代码功能有问题,暂不处理
    } else {
        // 立即购买及即将开售不存在显示加入购物袋
        if (!resData.buyNow && !resData.openSoon) {
            resData.addToBag = true;
        }
    }

    // 分享相关,产品的链接
    resData.share = {
        weixinUrl: helpers.urlFormat(base.mainPath),
        shareTitle: resData.name,
        shareImg: resData.img,
        shareDesc: base.phrase
    };

    return resData;
};

/**
 * 设置品牌介绍
 * @function setBrandIntro
 * @param { Object } brand 品牌相关数据
 * @return { Object } 品牌介绍
 */
const setBrandIntro = brand => {
    let barndIntro = {};

    if (brand) {
        let text = _.replace(brand.brandIntro, /<\/?[^>]*>|\s*|(\n)|(\t)|(\r)/g, ''),
            more = `... <a href="/product/shop/${brand.brandDomain}?brandIntro=true" class="blue">了解更多>></a>`;

        barndIntro = {
            brand: {
                titleEn: 'BRAND',
                titleCn: '品牌介绍',
                logo: brand.brandIco,
                intro: _.truncate(text, {
                    length: 300,
                    omission: more
                })
            }
        };
    }
    return barndIntro;
};

/**
 * 设置商品描述
 * @function setDescriptionData
 * @param { Object } sizeInfo 尺寸信息
 * @param { Object } comfortInfo 试穿信息
 * @return { Object } 商品描述
 */
const setDescriptionData = (sizeInfo, comfortInfo) => {
    let resData = {};

    if (_.has(sizeInfo, 'productDescBo.erpProductId')) {
        let description = {
            titleEn: 'DESCRIPTION',
            titleCn: '商品信息'
        };
        let sex;

        switch (sizeInfo.productDescBo.gender) {
            case 1:
                sex = '男款';
                break;
            case 2:
                sex = '女款';
                break;
            default:
                sex = '通用';
                break;
        }

        let basic = [
            { key: '编号', value: sizeInfo.productDescBo.erpProductId },
            { key: '颜色', value: sizeInfo.productDescBo.colorName },
            { key: '性别', value: sex }
        ];

        if (_.isArray(sizeInfo.productDescBo.standardBos)) {
            _.forEach(sizeInfo.productDescBo.standardBos, value => {
                basic.push({
                    key: value.standardName,
                    value: value.standardVal
                });
            });
        }

        description.basic = basic;

        if (comfortInfo) {
            let comfort = [];

            _.forEach(comfortInfo, value => {
                let i = 1;
                let blocks = [];
                let flag = false;

                do {
                    if (i === +value.wearSense.value) {
                        flag = true;
                        blocks.push({
                            cur: true
                        });
                    } else {
                        blocks.push({});
                    }
                    i++;
                } while (i <= 5);

                if (flag) {
                    comfort.push({
                        name: value.caption.caption,
                        minDes: value.caption.low,
                        blocks: blocks,
                        maxDes: value.caption.high
                    });
                }
            });

            description.comfort = comfort;
        }

        resData = {
            description: description
        };
    }

    return resData;
};

/**
 * 设置洗涤材质信息
 * @function setMaterialData
 * @param { Object } sizeInfo 尺寸信息
 * @return { Object } 洗涤材质信息
 */
const setMaterialData = sizeInfo => {
    let resData = {};
    let material = {};

    // 洗涤提示
    if (sizeInfo.washTipsBoList) {
        let wash = [];

        _.forEach(sizeInfo.washTipsBoList, value => {
            wash.push({
                name: value.caption,
                img: value.img
            });
        });

        if (!_.isEmpty(wash)) {
            material.wash = wash;
        }
    }

    // 商品材质[洗涤说明]
    if (sizeInfo.productMaterialList) {
        let detail = [];

        _.forEach(sizeInfo.productMaterialList, value => {
            detail.push({
                img: value.imageUrl,
                name: value.caption,
                enName: value.encaption,
                text: value.remark
            });
        });

        if (!_.isEmpty(detail)) {
            material.detail = detail;
        }
    }

    if (!_.isEmpty(material)) {
        Object.assign(material, {
            titleEn: 'MATERIALS',
            titleCn: '材料洗涤'
        });

        resData.material = material;
    }
    return resData;
};

/**
 * 设置尺寸信息
 * @function setSizeData
 * @param { Object } sizeInfo 尺寸信息
 * @return { Object } 尺寸信息
 */
const setSizeData = (sizeInfo) => {
    let resData = {};

    if (sizeInfo.sizeInfoBo) {
        let referenceName,
            boyReference,
            girlReference,
            gender;
        let size = {
            titleEn: 'SIZEINFO',
            titleCn: '尺码信息'
        };

        sizeInfo.sizeInfoBo = _sizeInfoBoSort(sizeInfo.sizeInfoBo);

        // 参考尺码
        if (_.has(sizeInfo, 'productExtra')) {
            boyReference = sizeInfo.productExtra.boyReference;
            girlReference = sizeInfo.productExtra.girlReference;
        }

        gender = _.has(sizeInfo, 'productDescBo.gender') ? sizeInfo.productDescBo.gender : 3;
        if (gender === 3 && boyReference) {
            referenceName = '参考尺码(男)';
        } else if (gender === 3 && girlReference) {
            referenceName = '参考尺码(女)';
        } else {
            referenceName = '参考尺码';
        }

        // 判断是否显示参考尺码
        let showReference = (boyReference && _.get(sizeInfo.sizeInfoBo, 'sizeBoList[0].boyReferSize', false)) ||
            (girlReference && _.get(sizeInfo.sizeInfoBo, 'sizeBoList[0].girlReferSize', false));

        if (sizeInfo.sizeInfoBo.sizeAttributeBos) {
            let sizeTable = {};

            // 尺码信息头部
            sizeTable.thead = [{name: '吊牌尺码', id: '', width: 126}];

            // 显示参考尺码
            if (showReference) {
                sizeTable.thead.push({name: referenceName, id: '', width: 126});
            }

            _.forEach(sizeInfo.sizeInfoBo.sizeAttributeBos, value => {
                sizeTable.thead.push({
                    name: value.attributeName || ' ',
                    id: value.id,
                    width: 126
                });
            });

            sizeTable.tbody = [];
            _.forEach(sizeInfo.sizeInfoBo.sizeBoList, value => {
                let sizes = [];

                // 吊牌尺码
                sizes.push(value.sizeName);

                // 判断是否显示参考尺码
                if (boyReference && (gender === 1 || gender === 3) && showReference) {
                    sizes.push(value.boyReferSize.referenceName || ' ');
                } else if (girlReference && (gender === 2 || gender === 3) && showReference) {
                    sizes.push(value.girlReferSize.referenceName || ' ');
                } else {
                    if (sizeTable.thead[1] && showReference) {
                        sizeTable.thead[1] = {};
                    }
                }

                // 其他尺码信息
                value.sortAttributes.forEach(attr => {
                    sizes.push((attr.sizeValue || ' '));
                });

                // 尺码信息
                sizeTable.tbody.push(sizes);
            });

            // 参考尺码为空
            if (_.isEmpty(sizeTable.thead[1]) && showReference) {
                // 移除这个值
                sizeTable.thead.splice(1, 1);
            }

            size.param = sizeTable;
        }

        if (!_.isEmpty(sizeInfo.modelBos)) {
            let tasteTable = {};

            tasteTable.thead = [
                {name: '模特', width: 170},
                {name: '身高', width: 126},
                {name: '体重', width: 126},
                {name: '三围', width: 126},
                {name: '吊牌尺码', width: 126},
                {name: '试穿描述', width: 126}
            ];

            tasteTable.tbody = [];
            _.forEach(sizeInfo.modelBos, value => {
                let modelInfo = [
                    {
                        img: value.avatar,
                        name: value.modelName
                    },
                    value.height, value.weight, value.vitalStatistics,
                    value.fitModelBo.fit_size, value.fitModelBo.feel
                ];

                // 是否有备注
                if (_.get(value, 'fitModelBo.fit_remark', false)) {
                    modelInfo.push(value.fitModelBo.fit_remark);
                    tasteTable.thead[7] = {name: '备注', width: 170};
                }
                tasteTable.tbody.push(modelInfo);
            });

            size.taste = tasteTable;
        }

        resData.sizeInfo = size;
    }
    return resData;
};

/**
 * 设置商品详情页介绍
 * @function setDetailData
 * @param { Object } sizeInfo 尺寸信息
 * @return { Object } 商品详情描述
 */
const setDetailData = sizeInfo => {
    let resData = {};

    // 详情配图
    if (_.has(sizeInfo, 'productIntroBo.productIntro')) {
        let details = {
            titleEn: 'DETAILS',
            titleCn: '商品详情'
        };

        if (sizeInfo.productDescBo && sizeInfo.productDescBo.phrase) {
            details.content = `${sizeInfo.productDescBo.phrase}<br/>`;
        }

        // 图片换规则
        const replacePairs = [
            {
                reg: /<img src=/g,
                str: '<img class="lazy" src="data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP///93d3f' +
            '///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw==" data-original='
            },
            {
                reg: /<img border="0" src=/g,
                str: '<img border="0" class="lazy" src="data:image/gif;base64,R0lGODlhAQABAJEAAAAAAP' +
            '///93d3f///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw==" data-original='
            }
        ];
        let content = sizeInfo.productIntroBo.productIntro;

        // 图片换成懒加载方式
        _.forEach(replacePairs, value => {
            content = _.replace(content, value.reg, value.str);
        });
        details.content += content;

        resData.details = details;
    }
    return resData;
};

module.exports = {
    setSeoInfo,
    setBrandBanner,
    setPathNav,
    setProductData,
    setBrandIntro,
    setDescriptionData,
    setMaterialData,
    setSizeData,
    setDetailData
};