list.js 22.1 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
/**
 * 品类|品牌|店铺 model
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/07/21
 */
'use strict';
const utils = '../../../utils';
const logger = global.yoho.logger;
const crypto = global.yoho.crypto;
const _ = require('lodash');
const helpers = global.yoho.helpers;
const api = global.yoho.API;
const searchModel = require('./search');
const productProcess = require(`${utils}/product-process`);

/**
 * 频道
 * @type {{}}
 */
const yhChannel = {
    boys: 1,
    girls: 2,
    kids: 3,
    lifestyle: 4
};

/* 多品牌店铺列表数据信息处理*/
const _processBrandShops = (list) => {
    let formatDat = [];

    _.forEach(list, item => {
        if (item.shop_id) {
            formatDat.push({
                url: helpers.urlFormat('/product/index/brand/', {
                    shop_id: item.shop_id
                }),
                thumb: helpers.image(item.brand_ico, 75, 40),
                name: item.brand_name
            });
        }
    });

    return formatDat;
};

/**
 * 销售类目链接拼接 APP
 * @param origin
 * @param shopId
 * @private
 */
const _modifyWapUrl = (origin, shopId) => {
    if (parseInt(_.get(origin, 'linkType', 0), 10) === 1) {
        return helpers.urlFormat('', {filter_poolId: origin.categoryId, shop_id: shopId, title: origin.categoryName}, 'search'); //eslint-disable-line
    } else {
        if (_.has(origin, 'url')) {
            return origin.url;
        } else {
            return '';
        }
    }
};

/**
 * 销售类目链接拼接 APP
 * @param origin
 * @param shopId
 * @private
 */
const _modifyAppUrl = (origin, shopId) => {
    if (parseInt(_.get(origin, 'linkType', 0), 10) === 1) {
        return helpers.urlFormat('/search/list', {filter_poolId: origin.categoryId}) +
            `?openby:yohobuy={"action":"go.poollist","params":{"shop_id":"${shopId}","title":"${origin.categoryName}","productPool":"${origin.categoryId}"}}`; //eslint-disable-line
    } else {
        if (_.has(origin, 'url')) {
            return origin.url;
        } else {
            return '';
        }
    }
};


/**
 * 获取店铺装修的所有资源接口
 * @param {int} shopId 店铺id
 * @return array
 */
const _getShopDecorator = (shopId) => {
    return api.get('', {
        method: 'app.shopsdecorator.getList',
        shop_id: shopId
    }, {
        cache: true,
        code: 200
    }).then((result) => {
        return (result && result.data) || {};
    });
};

/**
 * 获取店铺信息
 * @param {int} shopId 店铺id
 * @param {int} uid 用户id 判断用户是否收藏店铺
 * @return array
 */
const _getShopInfo = (shopId, uid) => {
    if (uid === 'undefined') {
        uid = 0;
    }
    return api.get('', {
        method: 'app.shops.getIntro',
        shop_id: shopId,
        uid: uid || 0
    }, {code: 200}).then((result) => {
        return result.data;
    });
};

/**
 * 获取店铺下面的所有分类
 * @param {int} shopId 店铺id
 * @param {string} channel 频道
 * @return array
 */
const _getShopCategory = (shopId, channel, gender) => {
    return api.get('', {
        method: 'app.shop.getSortInfo',
        yh_channel: yhChannel[channel],
        gender: gender || '1,3',
        shop_id: shopId
    }, {code: 200}).then(result => {
        return result.data;
    });
};

/**
 * 獲取店鋪優惠券
 * @private
 */
const _shopCouponsList = (shopId, uid) => {
    let params = {
        method: 'shop.coupons.list',
        shop_id: shopId,
    };

    if (uid) {
        _.assign(params, {uid: uid});
    }

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

            _.forEach(result.data, value => {
                let couPonstatus = parseInt(value.status, 10);

                if (couPonstatus === 1 || couPonstatus === 3) {
                    shopCoupons.push({
                        validity: value.couponValidity,
                        id: crypto.encryption('', value.coupon_id + ''),
                        name: value.coupon_name,
                        pic: value.coupon_pic,
                        money: parseInt(value.money, 10),
                        status: couPonstatus === 1
                    });
                }
            });

            return shopCoupons;
        } else {
            return [];
        }
    });
};

/**
 * 用戶領券
 * @param uid
 * @param couponId
 * @returns {*|Promise.<TResult>}
 */
const receiveCoupon = (uid, couponId) => {
    return api.get('', {
        method: 'app.promotion.getCoupon',
        couponId: couponId,
        uid: uid
    }).then(result => {
        return result;
    });
};

/**
 * 获取店铺下面的所有品牌
 * @param {int} shopId 店铺id
 */
const getShopBrands = (shopId) => {
    return api.get('', {
        method: 'app.shops.getShopsBrands',
        shop_id: shopId
    }, {code: 200}).then((result) => {
        if (result[0]) {
            return result[0].brand_id;
        }
    });
};

/**
 * 通过 skn 搜索商品
 * @param productSkn
 * @returns {*|Promise.<TResult>}
 * @private
 */
const searchProductBySkn = (productSkn) => {
    return api.get('', {
        method: 'h5.product.batch',
        productSkn: productSkn,
    }, {code: 200}).then(result => {
        return _.get(result, 'data.product_list', []);
    });
};


/**
 * 组织店铺页面数据
 * @param {array} data 接口返回的店铺页所需数据
 * @param {int} shopId 店铺id
 * @param {int} isApp app版本
 * @return {shopId: int, appVersion: int}
 */
const _formShopData = (data, shopId, isApp) => {
    let formatData = {
            shopId: shopId,
            appVersion: isApp
        },
        single = false;
    let brandId;

    // 组织店铺装修
    if (data.decorator) {

        _.forEach(data.decorator.list, floor => {
            let resData = JSON.parse(floor.resource_data);

            if (floor.resource_name) {
                floor[_.camelCase(floor.resource_name)] = true;
            }

            // 店铺banner
            if (floor.shopTopBannerApp) {
                formatData.branerImg = resData[0].shopSrc;
            }

            // 资源位小图 接口返回两组,取每组第一张
            if (floor.oneRowTwoColImagesApp) {

                let spring = [];

                _.forEach(resData, (item) => {
                    if (item.data) {
                        spring.push({
                            url: isApp ? _modifyAppUrl(item.data[0], shopId) :
                                _modifyWapUrl(item.data[0], shopId),
                            springType: helpers.image(item.data[0].src)
                        });
                    }
                });

                formatData.spring = spring;
            }

            // 店铺品牌一览
            if (floor.brandBrowse) {
                let brandNumber = resData.length;
                let brand = {
                    list: []
                };

                brandId = '';

                // 少于2个不展示 单品店:单品店根据品牌id查询
                if (brandNumber < 2) {
                    brandId = formatData.brand = resData[0].id;
                    single = true;
                } else {
                    _.forEach(resData, (item) => {
                        if (item.brandDomain) {
                            brand.list.push({
                                url: helpers.urlFormat('', '', item.brandDomain) + (isApp ? `?openby:yohobuy={"action":"go.brand","params":{"shop_id":${shopId},"brand_id":${item.id}}}` : ''), //eslint-disable-line
                                img: helpers.image(item.brandIco, 640, 400),
                                brandName: item.brandName
                            });
                            brandId += item.id + ',';
                        }
                    });

                    formatData.brandList = brand;

                    // 大于5个返回更多品牌链接
                    if (brandNumber > 5) {
                        formatData.brandList.url = helpers.urlFormat('/product/index/allBrand', {
                            shop_id: shopId
                        });
                    }
                }
            }

            // 资源位大图幻灯
            if (floor.largeSlideImgApp) {
                let bannerTop = {
                    data: []
                };

                _.forEach(resData, (item) => {
                    if (item.data[0]) {
                        bannerTop.data.push({
                            url: isApp ? _modifyAppUrl(item.data[0], shopId) :
                                _modifyWapUrl(item.data[0], shopId),
                            src: item.data[0].src
                        });
                    }
                });

                formatData.bannerTop = bannerTop;
            }

            // 热门品类
            if (floor.recommendApp) {
                let hotCategory = {
                    data: {
                        title: {
                            title: '热门品类'
                        },
                        list: []
                    }
                };

                _.forEach(resData, cate => {
                    hotCategory.data.list.push({
                        url: isApp ? _modifyAppUrl(cate, shopId) :
                            _modifyWapUrl(cate, shopId),
                        src: helpers.image(cate.src),
                        name: cate.name
                    });
                });

                formatData.hotCategory = hotCategory;
            }

            // 人气单品
            if (floor.hotProductsApp) {
                let productSkn = '';

                _.forEach(resData, value => {
                    productSkn += value.productSkn + ',';
                });

                formatData.hotListproductSkn = productSkn;
            }
        });
    }

    // 店铺基本信息
    if (data.shopInfo) {
        let allGoodsParam = {
            title: '全部商品'
        };

        let moreUrl = helpers.urlFormat('', {
            shop_id: shopId,
            order: 's_n_d',
            title: '人气单品'
        }, 'search'); // 人气单品的链接

        if (isApp) {
            moreUrl = 'openby:yohobuy={"action":"go.list","params":{"shop_id":"' + shopId + '","title":"人气单品"}}';
        }

        formatData = _.assign({
            shopIntro: _.get(data, 'shopInfo.shop_intro', ''),
            logoImg: _.get(data, 'shopInfo.shop_logo', ''),
            storeName: (_.get(data, 'shopInfo.is_show_shop_name', '') === 'Y') ?
                _.get(data, 'shopInfo.shop_name', '') : '',
            seoTitle: _.get(data, 'shopInfo.shop_name', ''),
            collect: _.get(data, 'shopInfo.is_favorite', '') === 'Y',
            url: helpers.urlFormat('', {
                shop_id: shopId
            }, 'search'), // 搜索链接
            more_url: moreUrl
        }, formatData);

        if (single) { // 单品点
            allGoodsParam.brand = brandId;
        } else { // 店铺id
            formatData.shopId = shopId;
            allGoodsParam.shopId = shopId;
        }

        if (!isApp) {
            formatData = _.assign({
                allGoods: helpers.urlFormat('', allGoodsParam, 'search'),
                shopIntroHref: helpers.urlFormat('/product/index/intro', {
                    shop_id: shopId
                }) // 店铺简介页地址
            }, formatData);
        } else {
            formatData = _.assign({
                allGoods: `${helpers.urlFormat('', allGoodsParam, 'search')}&openby:yohobuy={"action":"go.list","params":{"title":"全部商品", "actiontype":"0","shop_id":${shopId},"page":"1"}}`,//eslint-disable-line
                shopIntroHref: helpers.urlFormat('/product/index/intro', {
                    shop_id: shopId,
                    app_version: isApp
                }),
                more_url: formatData.more_url + `?openby:yohobuy{"action":"go.list","params":{"shop_id":${shopId},"title":"人气单品"}}`//eslint-disable-line
            }, formatData);
        }
    }

    // 店铺下面的所有分类
    if (data.shopCategory) {
        let total = data.shopCategory.length;
        let shopCategory = {
            list: []
        };

        if (total > 6) {
            shopCategory.url = helpers.urlFormat('/product/index/category', {
                shop_id: shopId
            });
        } else {
            _.forEach(data.shopCategory, item => {
                shopCategory.list.push({
                    url: helpers.urlFormat('', {
                        shop_id: shopId,
                        sort: item.relation_parameter.sort
                    }, 'search'),
                    categoryId: item.category_id,
                    name: item.category_name
                });
            });
        }

        formatData.shopCategory = shopCategory;
    }

    if (single) {
        formatData.favId = shopId;
        formatData.shopId = '';
    }
    return formatData;
};

/**
 * 使用基础模板的店铺首页
 * @param params
 * @param shopInfo
 * @returns {*|Promise.<TResult>}
 */
const getBaseShopData = (params, shopInfo) => {

    let finalResult = {
        cartUrl: helpers.urlFormat('/cart/index/index')
    };

    let shopId = params.shop_id || shopInfo.shops_id;

    return api.all([
        _getShopDecorator(shopId),
        getShopBrands(shopId)
    ]).then(result => {

        if (result[0] && _.has(result[0], 'list')) {
            _.forEach(result[0].list, item => {
                if (item.resource_name === 'shopTopBanner_base') {
                    let banner = JSON.parse(item.resource_data);

                    finalResult = _.assign(finalResult, {
                        baseShopHome: {
                            appVersion: _.get(params, 'app_version', ''),
                            isBaseShop: true,
                            id: shopId,
                            intro: shopInfo.shopIntro,
                            collected: shopInfo.isFavorite === 'Y',
                            banner: _.get(banner[0], 'shopSrc', '')
                        }
                    });
                }
            });
        }

        if (params.multBrandShopType === '2') {
            // 转义店铺
            finalResult.shop_id = shopId;
        } else {
            finalResult.brand = result[1];
        }
        return finalResult;
    });
};

/**
 * 使用经典模板的店铺首页
 * @param {object} req
 * @param {int} shopId 店铺id
 * @param {int} uid 用户id
 * @param {string} isApp app版本
 */
const getShopData = (req, shopId, uid, isApp) => {
    let shopData = {};
    let channel = req.yoho.channel;

    return _getShopInfo(shopId, uid).then(shopInfoResult => {

        /* 基础店铺返回程序内的跳转信号,跳转到基础店铺 */
        if (shopInfoResult && shopInfoResult.shop_template_type && parseInt(shopInfoResult.shop_template_type, 10) === 1) {
            return {
                goBrand: shopInfoResult
            };
        }

        return Promise.all([
            _getShopDecorator(shopId),
            searchModel.getFilterSearchData({
                shop_id: shopId,
                channel: channel
            }),
            _shopCouponsList(shopId, uid)
        ]).then((result) => {
            shopData = {
                decorator: result[0], // 店铺装修资源数据
                shopInfo: shopInfoResult // 店铺信息
            };

            /* 获取一次分类和商品数据 */
            let shopFilterSearchData = {
                filter: [],
                goods: []
            };

            if (result[1]) {
                shopFilterSearchData.filter = productProcess.processFilter(result[1].filter || []);
                shopFilterSearchData.goods = productProcess.processProductList(result[1].product_list || [], {isApp: isApp});
            }

            /* 店鋪優惠券 */
            let shopCoupons = result[2] || [];

            // 店铺分类
            return _getShopCategory(shopId, channel).then(shopCategory => {

                shopData = _.assign({
                    shopCategory: shopCategory
                }, shopData);

                // noinspection JSCheckFunctionSignatures
                return Object.assign(
                    _formShopData(shopData, shopId, isApp),
                    shopFilterSearchData,
                    {
                        shopId: shopId,
                        uid: uid ? crypto.encryption('', uid + '') : '',
                        coverChannel: channel,
                        shopCoupons: shopCoupons,
                        shopCouponsOne: shopCoupons.length === 1
                    }
                );
            });
        });

    });
};

/**
 * 异步判断店铺是否收藏
 * @param req
 * @param shopId
 * @param uid
 * @returns {Promise.<TResult>|*}
 */
const getShopFav = (req, shopId, uid) => {
    return _getShopInfo(shopId, uid).then(shopInfoResult => {
        return shopInfoResult;
    });
};

/**
 * 获取品牌信息数据
 * @param  {int} brandId 品牌ID
 * @return array         banner数据
 */
const getBrandIntro = (brandId, uid) => {
    let param = {
        uid: uid
    };

    return api.get('', _.assign({
        method: 'app.brand.getBrandIntro',
        brand_id: brandId
    }, param), {
        code: 200,
        cache: true
    }).then(result => {
        if (result && result.data) {
            let list = result.data;

            return {
                id: list.brand_id,
                intro: list.brand_intro,
                collected: list.is_favorite && list.is_favorite === 'Y',
                title: list.brand_name ? list.brand_name : ''
            };
        } else {
            return false;
        }
    });
};

/**
 * 获取品牌banner数据
 * @param  {int} brandId 品牌ID
 * @return array         banner数据
 */
const getBrandBanner = (brandId) => {
    return api.get('', {
        method: 'app.brand.banner',
        brand_id: brandId
    }, {
        cache: true
    }).then((result) => {
        if (result && result.code === 200) {
            if (result.data.banner) {
                return helpers.image(result.data.banner, 640, 150);
            } else {
                return '';
            }
        } else {
            logger.error('get brand banner api return code is not 200');
            return {};
        }
    });
};

/**
 * 根据品牌域名获取品牌LOGO
 * @param {string} domain 品牌域名
 * @return array | false
 */
const getBrandLogoByDomain = (domain) => {
    return api.get('', {
        method: 'web.brand.byDomain',
        domain: domain
    }, {
        cache: true
    }).then(result => {
        if (result && result.data) {
            let formatData = result.data;

            return {
                id: formatData.id,
                url: helpers.urlFormat('', null, formatData.brand_domain),
                thumb: helpers.image(formatData.brand_ico, 75, 40),
                name: formatData.brand_name,
                shopId: formatData.shop_id ? formatData.shop_id : 0, // 店铺id
                type: formatData.type ? formatData.type : 0
            };
        } else {
            return false;
        }
    });
};

/**
 * 根据brandId 获取相关店铺列表
 * @param brandId
 * @return array
 */
const getBrandShops = (brandId) => {
    return api.get('', {
        method: 'app.shop.queryShopsByBrandId',
        brand_id: brandId
    }, {
        code: 200,
        cache: true
    }).then(result => {
        if (_.isArray(result.data)) {
            return _processBrandShops(result.data);
        } else {
            return [];
        }
    });
};

/**
 * 收藏
 * @param {int} id ID
 * @param {int} uid 用户ID
 * @param type
 * @return array
 */
const setFavorite = (id, uid, type) => {
    return api.post('', {
        method: 'app.favorite.add',
        fav_id: id,
        id: id,
        uid: uid,
        type: type
    });
};

/**
 * 取消收藏
 *
 * @param {int} id 品牌ID
 * @param {int} uid 用户ID
 * @param type
 * @return array
 */
const setFavoriteCancel = (id, uid, type) => {
    return api.post('', {
        method: 'app.favorite.cancel',
        fav_id: id,
        id: id,
        uid: uid,
        type: type
    });
};

/**
 * 调取店铺简介数据
 * @param {int} shopId
 */
const getShopIntro = (shopId) => {
    return api.get('', {
        method: 'app.shops.getIntro',
        shop_id: shopId
    }, {code: 200, cache: true}).then(result => {
        return result.data;
    });
};

/**
 * 处理店铺品类
 * @param params
 */
const getShopCategory = (params) => {
    let finalResult = {
        class: [],
        category: []
    };

    return _getShopCategory(params.shopId, params.channel, params.gender).then(result => {
        let resultCopy = _.cloneDeep(result);

        _.forEach(resultCopy, value => {
            finalResult.class.push({
                name: value.category_name
            });

            _.forEach(value.sub, (subValue, subKey) => {
                value.sub[subKey].url = helpers.urlFormat('/product/search/list', {
                    shop_id: params.shopId,
                    sort: _.get(subValue, 'relation_parameter.sort', ''),
                    title: subValue.category_name,
                    query: subValue.category_name
                });
            });

            let subCategory = [
                {
                    category_name: '全部' + value.category_name,
                    url: helpers.urlFormat('/product/search/list', {
                        shop_id: params.shopId,
                        sort: _.get(value, 'relation_parameter.sort', ''),
                        title: '全部' + value.category_name,
                        query: value.category_name
                    })
                }
            ];

            subCategory = subCategory.concat(value.sub);

            finalResult.category.push({
                subcategory: subCategory
            });
        });

        finalResult.allProduct = helpers.urlFormat('/product/search/list', {
            shop_id: params.shopId
        });

        return finalResult;
    });
};

module.exports = {
    getBaseShopData,
    getShopData,
    getBrandLogoByDomain,
    getBrandIntro,
    getBrandShops,
    getBrandBanner,
    setFavorite,
    setFavoriteCancel,
    getShopIntro,
    getShopBrands,
    getShopCategory,
    receiveCoupon,
    getShopFav,
    searchProductBySkn
};