list.js 22.3 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
/*
 * @Author: sefon
 * @Date: 2016-07-24 11:48:22
 */

'use strict';
const utils = '../../../utils';
const searchApi = require('./search-api');
const headerModel = require('../../../doraemon/models/header');
const productProcess = require(`${utils}/product-process`);
const searchHandler = require('./search-handler');
const shopHandler = require('./shop-handler');
const helpers = global.yoho.helpers;
const _ = require('lodash');

const limitNum = 60; // 商品每页显示数目

// 品牌页folder名称
const brandFolderSeries = '经典系列';

// 搜索分类信息需要的参数
const positionId = 10;

// 获取分类左侧广告id
const sortAdsId = 79;


const _getGender = (channel) => {
    let gender;

    switch (channel) {
        case 'boys':
            gender = '1,3';
            break;
        case 'girls':
            gender = '2,3';
            break;
        default:
            gender = '1,2,3';
            break;
    }

    return gender;
};

/**
 * 获取商品分类列表数据
 */
exports.getListData = (params, channel) => {
    // 调用接口
    let apiMethod = [
        headerModel.requestHeaderData(channel),
        searchApi.getSortList(),
        searchApi.getProductList(params)
    ];

    // 搜索分类介绍和广告
    if (params.msort && params.misort) {
        let IntroParams = {
            max_sort_id: params.msort,
            middle_sort_id: params.misort,
            gender: params.gender || '1,3'
        };

        apiMethod.push(searchApi.getSortIntro(Object.assign(IntroParams, {position_id: positionId})));
        apiMethod.push(searchApi.getSortAds(Object.assign(IntroParams, {position_id: sortAdsId})));
    }

    return Promise.all(apiMethod).then(result => {
        let finalResult = {
            headerData: Object.assign(result[0].headerData, {
                header: true
            }),
            list: {
                leftContent: {}
            }
        };

        // 获取左侧类目数据
        if (result[1].code === 200) {
            finalResult.list = Object.assign(searchHandler.handlePathNavData(result[1].data.sort, params, 'sort'), {
                leftContent: searchHandler.handleSortData(result[1].data.sort, params)
            });
        }

        // 获取商品数据和顶部筛选条件
        if (result[2].code === 200) {
            Object.assign(finalResult.list, {
                // filters: searchHandler.handleFilterData(result[2].data.filter, params),
                filters: searchHandler.handleFilterDataAll(result[2].data, params),
                opts: searchHandler.handleOptsData(params, result[2].data.total, result[2].data.filter),
                totalCount: result[2].data.total,
                footPager: searchHandler.handlePagerData(result[2].data.total, params),
                goods: productProcess.processProductList(result[2].data.product_list,
                    Object.assign({showDiscount: false}, params))
            });

            // 获取高级筛选条件
            /* if (finalResult.list && finalResult.list.filters) {
                Object.assign(finalResult.list.filters, searchHandler.handleSeniorFilterData({
                    style: _.get(result[2], 'data.filter.style', []),
                    standard: _.get(result[2], 'data.standard', [])
                }, params));
            }*/
        }

        // 分类介绍
        if (result[3] && result[3].code === 200) {
            finalResult.list.sortIntro = searchHandler.handleSortIntro(result[3].data);
        }

        // 分类广告
        if (result[4] && result[4].code === 200) {
            Object.assign(finalResult.list.leftContent, searchHandler.handleSortAds(result[4].data));
        }
        return finalResult;
    });
};

/**
 * 获取新品到着数据
 */
exports.getListNewData = (params, channel) => {

    // 调用接口
    let apiMethod = [
        headerModel.requestHeaderData(channel),
        searchApi.getSortList(),
        searchApi.getProductList(Object.assign({new: 'Y'}, params)),
        searchApi.getWeekNew(params)
    ];

    return Promise.all(apiMethod).then(result => {
        let finalResult = {
            headerData: Object.assign(result[0].headerData, {
                header: true
            }),
            list: {
                leftContent: {}
            }
        };

        // 获取左侧类目数据
        if (result[1].code === 200) {
            finalResult.list = Object.assign(searchHandler.handlePathNavData(result[1].data.sort, params, 'new'), {
                leftContent: searchHandler.handleSortData(result[1].data.sort, params)
            });
        }

        // 获取商品数据和顶部筛选条件
        if (result[2].code === 200) {
            Object.assign(finalResult.list, {
                // filters: searchHandler.handleFilterData(result[2].data.filter, params),
                filters: searchHandler.handleFilterDataAll(result[2].data, params),
                opts: searchHandler.handleOptsData(params, result[2].data.total, result[2].data.filter),
                totalCount: result[2].data.total,
                footPager: searchHandler.handlePagerData(result[2].data.total, params),
                goods: productProcess.processProductList(result[2].data.product_list,
                    Object.assign({showDiscount: false}, params))
            });

            // 获取高级筛选条件
            /* if (finalResult.list && finalResult.list.filters) {
                Object.assign(finalResult.list.filters, searchHandler.handleSeniorFilterData({
                    style: _.get(result[2], 'data.filter.style', []),
                    standard: _.get(result[2], 'data.standard', [])
                }, params));
            }*/
        }

        // 新品上架
        if (result[3] && result[3].code === 200) {
            Object.assign(finalResult.list.leftContent, searchHandler.handleWeekNew(result[3].data, params));
        }

        return finalResult;
    });
};

/**
 * 根据品牌域名查询品牌信息
 */
exports.getBrandInfo = (params) => {
    return searchApi.getBrandData(params).then(result => {

        if (result.code !== 200 || !result.data) {
            return {};
        }

        return {
            brandId: result.data.id,
            brandNameEn: result.data.brand_name_en || '',
            brandNameCn: result.data.brand_name_cn || '',
            brandBanner: result.data.brand_banner || '',
            node: result.data.static_content_code || '',
            brandAbout: result.data.brand_intro || '',
            shopTemplateType: result.data.shop_template_type || 0,
            url: helpers.urlFormat('', '', result.data.brand_domain),
            name: result.data.brand_name,
            shopId: result.data.shop_id || 0,
            type: result.data.type || 0,
            brandDomain: result.data.brand_domain
        };

    });
};

/**
 * 获取品牌页面数据
 */
exports.getBrandData = (params, extra, channel) => {

    // 设置品牌默认排序方式
    let order = params.order || 'b_w_desc';

    // 调用接口
    let apiMethod = [
        headerModel.requestHeaderData(channel),
        searchApi.getSortList({brand: extra.brandId}),
        searchApi.getProductList(Object.assign({order: order, brand: extra.brandId}, params))

    ];

    return Promise.all(apiMethod).then(result => {
        let finalResult = {
            headerData: Object.assign(result[0].headerData, {
                header: true
            }),
            brand: {
                brandBanner: searchHandler.handleBrandBanner(extra),
                leftContent: {},
                node: extra.node || ''
            }
        };

        // 获取左侧类目数据
        if (result[1].code === 200) {
            Object.assign(finalResult.brand, {
                leftContent: searchHandler.handleSortData(result[1].data.sort, params)
            }, searchHandler.handlePathNavData(extra, params, 'brand'));
        }

        // 获取商品数据和顶部筛选条件
        if (result[2].code === 200) {
            Object.assign(finalResult.brand, {
                filters: searchHandler.handleFilterDataAll(result[2].data, params),
                opts: searchHandler.handleOptsData(params, result[2].data.total, result[2].data.filter),
                totalCount: result[2].data.total,
                footPager: searchHandler.handlePagerData(result[2].data.total, params),
                goods: productProcess.processProductList(result[2].data.product_list,
                    Object.assign({showDiscount: false}, params))
            });

            // 获取高级筛选条件
            /* if (finalResult.list && finalResult.list.filters) {

                Object.assign(finalResult.list.filters, searchHandler.handleSeniorFilterData({
                    style: _.get(result[2], 'data.filter.style', []),
                    standard: _.get(result[2], 'data.standard', [])
                }, params));
            }*/
        }

        // 品牌页不显示品牌筛选项
        delete finalResult.brand.filters.brand;

        return finalResult;
    });
};

/**
 * 获取品牌页面数据
 */
exports.getBrandAbout = (domain, uid, channel) => {
    channel = channel || 'boys';

    return Promise.all([
        headerModel.requestHeaderData(channel),
        searchApi.getBrandData({domain: domain})
    ]).then(result => {
        let home;
        let finalResult = {
            headerData: Object.assign(result[0].headerData, {
                header: true
            })
        };

        switch (channel) {
            case 'girls':
                home = helpers.urlFormat('/woman', {}, 'new');
                break;
            case 'lifestyle':
                home = helpers.urlFormat('/lifestyle', {}, 'new');
                break;
            case 'kids':
                home = helpers.urlFormat('/kids', {}, 'new');
                break;
            default:
                home = helpers.urlFormat('');
                break;
        }

        if (result[1].code === 200) {
            let data = result[1].data || {};
            let extra = {
                brandId: data.id,
                brandNameEn: data.brand_name_en || '',
                brandNameCn: data.brand_name_cn || '',
                brandBanner: data.brand_banner || '',
                node: data.static_content_code || '',
                brandAbout: data.brand_intro || '',
                shopTemplateType: data.shop_template_type || 0,
                url: helpers.urlFormat('', '', data.brand_domain),
                name: data.brand_name,
                shopId: data.shop_id || 0,
                type: data.type || 0,
                brandDomain: data.brand_domain
            };

            finalResult.brand = {
                brandBanner: searchHandler.handleBrandBanner(extra),
                pathNav: [
                    {
                        href: home,
                        name: `${_.toUpper(channel)}首页`,
                        pathTitle: 'YOHO!BUY 有货'
                    },
                    {
                        href: helpers.urlFormat('/brands'),
                        name: '品牌一览',
                        pathTitle: '品牌一览'
                    },
                    {
                        name: `${extra.brandNameEn} ${extra.brandNameCn}`
                    }
                ],
                brandAbout: {
                    brandIntro: extra.brandAbout
                }
            };
        }

        return finalResult;

    });

};

/**
 * 获取品牌页面水牌
 */
exports.getNodeContentData = (params) => {
    return searchApi.getNodeContent(params).then(result => {

        if (result.code === 200 && result.data) {
            return result.data;
        }
    });
};

/**
 * 获取品牌页面系列
 */
exports.getAdnav = (params) => {

    return Promise.all([searchApi.getBrandFolder(params), searchApi.getBrandSeries(params)]).then(result => {

        let dest = {
            picTitle: '',
            list: []
        };

        if (result[0].code === 200 && result[0].data) {
            dest.picTitle = brandFolderSeries;
            Object.assign(dest.list, searchHandler.handleFolderData(result[0].data));
        }

        if (result[1].code === 200 && result[1].data) {
            dest.picTitle = brandFolderSeries;
            Object.assign(dest.list, searchHandler.handleFolderData(result[0].data));
        }


        return dest;
    });
};

/**
 *  获取店铺基本信息
 */

exports.getShopInfo = (shopId, uid) => {

    return searchApi.getShopInfo(shopId, uid).then(result => {
        if (result.code === 200) {
            return {
                brandName: result.data.shop_name,
                brandCont: result.data.shop_intro,
                domain: result.data.shop_domain,
                logo: result.data.shop_logo,
                isFavorite: result.data.is_favorite === 'Y',
                shopTemplateType: result.data.shop_template_type,
                multBrandShopType: result.data.mult_brand_shop_type,
                showShopName: result.data.is_show_shop_name === 'Y'
            };
        } else {
            return {};
        }
    });
};

/**
 * 获取经典模板店铺数据
 * @param shopId
 * @param uid
 * @param params
 * @param shopInfo 店铺介绍
 */
exports.getShopData = (shopId, channel, params, shopInfo) => {
    let gender = _getGender(channel);

    params = params || {};
    params.shopId = shopId;

    return Promise.all([
        headerModel.requestHeaderData(channel), // 头部数据
        searchApi.getShopDecorator(shopId), // 店铺装修数据
        searchApi.getProductList(Object.assign({
            shop_id: shopId
        }, params)), // 搜索店铺商品
        searchApi.getShopBrands(shopId) // 店铺品牌数据
    ]).then(result => {
        let finalResult = {};

        Object.assign(finalResult,
            result[0], // 头部数据
            searchHandler.handlePathNavData(shopInfo, params, 'shop') // 面包屑导航
        );

        _.set(finalResult, 'headerData.header', true);

        // 店铺装修
        if (result[1].code === 200) {
            Object.assign(finalResult, shopHandler.getShopDecorator(result[1].data, params, shopId));

            _.set(finalResult, 'shopTopBanner.brandIntro', {
                shopId: shopId,
                brandName: shopInfo.brandName || '',
                isFavorite: shopInfo.isFavorite || false,
                brandCont: shopInfo.brandCont || ''
            });
        } else {
            return Promise.reject('No ShopDecorator data');
        }

        // 获取商品数据和顶部筛选条件
        if (result[2].code === 200) {
            let allGoods = {
                name: '全部商品 ALL',
                sort: searchHandler.handleOptsData(params, _.get(result[2], 'data.total', 0)),
                list: productProcess.processProductList(_.get(result[2], 'data.product_list', []), {
                    newCoverSort: true,
                    showDiscount: false,
                    gender: gender,
                    href: `/product/shoplist?navBar=1&shopId=${shopId}`
                })
            };

            _.set(allGoods, 'sort.newPage', true); // 启用新的分页导航

            finalResult.allGoods = allGoods;
        }

        let sknList = []; // 资源位配置商品skn
        let prodList = _.concat(_.get(finalResult, 'newArrivel.list', []),
            _.get(finalResult, 'hotSingle.list', []));

        _.forEach(prodList, value => {
            sknList.push(value.productSkn);
        });

        let shopBrandIds = [], // 店铺的所有品牌id
            articleApiMethod = [], // 通过品牌获取相关文章接口
            apiMethod = [
                searchApi.getProductList({query: _.join(_.uniq(sknList), ',')})
            ];

        if (result[3].code === 200 && result[3].data) {
            _.forEach(result[3].data, value => {
                shopBrandIds.push(value.brand_id);
                articleApiMethod.push(
                    searchApi.getArticleByBrand(value.brand_id, 'udid') // 品牌推荐文章
                );
            });
        }

        // 根据品牌获取分类 (腾讯云测试没有该接口,暂时不调用分类)
        apiMethod.push(searchApi.getSortList({brand: shopBrandIds}));
        apiMethod = _.concat(apiMethod, articleApiMethod);

        return Promise.all(apiMethod).then(subRes => {
            // 设置资源位商品封面图
            if (subRes[0].code === 200) {
                let coverList = {};

                prodList = productProcess.processProductList(_.get(subRes[0], 'data.product_list', []), {
                    newCoverSort: true,
                    showDiscount: false,
                    gender: gender
                });

                _.forEach(prodList, value => {
                    coverList[value.productSkn] = value.defaultImages;
                });

                if (_.has(finalResult, 'newArrivel.list')) {
                    _.forEach(finalResult.newArrivel.list, value => {
                        if (coverList[value.productSkn]) {
                            value.img = coverList[value.productSkn];
                        }
                    });
                }

                if (_.has(finalResult, 'hotSingle.list')) {
                    _.forEach(finalResult.hotSingle.list, value => {
                        if (coverList[value.productSkn]) {
                            value.img = coverList[value.productSkn];
                        }
                    });
                }
            }

            if (subRes[1].code === 200) {
                let groupSort = _.get(subRes[1], 'data.sort', []);

                finalResult.leftContent = searchHandler.handleSortData(groupSort, params);

                if (finalResult.allGoods) {
                    Object.assign(finalResult.allGoods, searchHandler.setShopSort(groupSort, params));
                }
            }

            return finalResult;
        });
    });
};

exports.getShopListData = (channel, params, uid) => {
    let gender = _getGender(channel),
        shopId = params.shopId,
        navBar = params.navBar || 1;
    let apiArr = [
        headerModel.requestHeaderData(channel), // 头部数据
        searchApi.getShopDecorator(shopId), // 店铺装修数据
        searchApi.getShopInfo(shopId, uid), // 店铺介绍
        searchApi.getProductList(Object.assign({shop_id: shopId}, params)), // 搜索店铺商品
        searchApi.getShopBrands(shopId) // 店铺品牌数据
    ];

    if (_.has(params, 'query')) {
        apiArr.push(searchApi.getProductList(Object.assign({shop_id: shopId},
            params, {query: ''}))); // 搜索店铺所有商品
    }


    return Promise.all(apiArr).then(result => {
        let finalResult = {
            headerData: Object.assign(result[0].headerData, {header: true})
        };

        // 面包屑导航
        Object.assign(finalResult, searchHandler.handlePathNavData({}, params, 'shop'));

        // 店铺装修
        if (result[1].code === 200) {
            Object.assign(finalResult, shopHandler.getShopDecorator(result[1].data, params, shopId));

            // 设置shop nav选中状态
            _.set(finalResult, `navigationBar[${navBar}].current`, true);

            if (result[2].code === 200) {
                let data = _.get(result[2], 'data', {});

                _.set(finalResult, 'shopTopBanner.brandIntro', {
                    shopId: shopId,
                    brandName: data.shop_name || '',
                    isFavorite: data.is_favorite === 'Y',
                    brandCont: data.shop_intro || ''
                });
            }
        } else {
            return Promise.reject('No ShopDecorator data');
        }

        // 获取商品数据和顶部筛选条件
        if (result[3].code === 200) {
            let info = result[3];
            let goodsList = _.get(info, 'data.product_list', []);

            if (goodsList.length) {
                let tip = {
                    start: (_.get(info, 'data.page', 1) - 1) * limitNum + 1,
                    total: _.get(info, 'data.total', 0)
                };

                tip.end = tip.start + limitNum - 1;
                if (tip.end > tip.total) {
                    tip.end = tip.total;
                }

                Object.assign(finalResult, {
                    filters: searchHandler.handleFilterDataAll(info.data, params),
                    opts: searchHandler.handleOptsData(params, tip.total),
                    goods: productProcess.processProductList(goodsList, {
                        newCoverSort: true,
                        showDiscount: false,
                        gender: _getGender(channel)
                    }),
                    footPager: {tip: tip}
                });
            } else {
                finalResult.searchEmpty = true;

                if (result[5] && result[5].code === 200) {
                    let allGoods = {
                        name: '全部商品 ALL',
                        sort: searchHandler.handleOptsData(params, _.get(result[5], 'data.total', 0)),
                        list: productProcess.processProductList(_.get(result[5], 'data.product_list', []), {
                            newCoverSort: true,
                            showDiscount: false,
                            gender: gender
                        })
                    };

                    _.set(allGoods, 'sort.newPage', true); // 启用新的分页导航

                    finalResult.allGoods = allGoods;
                }
            }
        }

        if (result[4].code === 200) {
            let shopBrandIds = []; // 店铺下的所有品牌id数组

            _.forEach(_.get(result[4], 'data', []), value => {
                shopBrandIds.push(value.brand_id);
            });

            return searchApi.getSortList({brand: shopBrandIds}).then(subRes => {
                if (subRes.code === 200) {
                    let groupSort = _.get(subRes, 'data.sort', []);

                    finalResult.leftContent = searchHandler.handleSortData(groupSort, params);

                    if (finalResult.allGoods) {
                        Object.assign(finalResult.allGoods, searchHandler.setShopSort(groupSort, params));
                    }
                }

                return finalResult;
            });

        }

        return finalResult;
    });
};

/**
 * 获取基础模板店铺数据
 */
exports.getBaseShopData = (shopId, uid) => {
    return searchApi.getShopInfo(shopId, uid);
};