Blame view

public/js/product/search/list.js 32.9 KB
王水玲 authored
1 2 3 4 5 6
/**
 * 商品列表页
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/20
 */
陈峰 authored
7
let $ = require('yoho-jquery'),
王水玲 authored
8 9 10 11
    Hammer = require('yoho-hammer'),
    ellipsis = require('yoho-mlellipsis'),
    lazyLoad = require('yoho-jquery-lazyload');
王水玲 authored
12
// 品牌页参数
陈峰 authored
13
let $brandHeader = $('#brand-header'),
王水玲 authored
14 15
    $introBox = $('#intro-box');
陈峰 authored
16
let filter = require('plugin/filter');
王水玲 authored
17
陈峰 authored
18
let writeSearch = require('./write-search');
王水玲 authored
19
陈峰 authored
20 21
let tip = require('plugin/tip');
let loading = require('plugin/loading');
王水玲 authored
22
陈峰 authored
23
let $goodsContainer = $('#goods-container'),
24
    $defaultgc = $goodsContainer.children('.default-goods'),
王水玲 authored
25
    $ngc = $goodsContainer.children('.new-goods'),
26
    $sgc = $goodsContainer.children('.sale-goods'),
王水玲 authored
27
    $pgc = $goodsContainer.children('.price-goods'),
Zhang authored
28 29
    $dgc = $goodsContainer.children('.discount-goods'),
    $popularity = $goodsContainer.children('.popularity-goods');
王水玲 authored
30
陈峰 authored
31
let winH = $(window).height();
郝肖肖 authored
32
徐炜 authored
33
window.$ = $;
陈峰 authored
34
let $input = $('#search-input').find('input[name="query"]'),
王水玲 authored
35 36
    $clear = $('#search-input .clear-input'),
    $buriedpoint = $('.buriedpoint'),
zhangxiaoru authored
37
    $search = $('#search'),
zhangxiaoru authored
38 39
    $dropList = $('.drop-list'),
    $searchDivide = $('.search-divide');
王水玲 authored
40
陈峰 authored
41 42
let shopId, sort, brand, outlets, app_type, age_level, msort, misort, coin, app_version, students, couponId, searchFrom;
let size, color, style, standard, gender, price, p_d, oldquery, query, limited, specialoffer, specialsale_id, promotion;
43
let couponCode;
zhangxiaoru authored
44
王水玲 authored
45
// 默认筛选条件
陈峰 authored
46
let defaultOpt = require('common/query-param');
王水玲 authored
47
郝肖肖 authored
48 49
require('common');
50
// 首屏加载标志
zhangxiaoru authored
51
let firstScreen = $('.default-goods').children().size() > 0;
陈峰 authored
52
let isRecYas = window.queryString.needSuggestion === 'Y' || $('.word-content').length ? 'Y' : 'N'; // 是否是关键词
53
陈峰 authored
54
let $listNav = $('#list-nav'),
王水玲 authored
55
王水玲 authored
56
    // 导航数据信息
王水玲 authored
57
    navInfo = {
58
        price: {
郭成尧 authored
59
            order: 1,
王水玲 authored
60 61 62 63
            reload: true,
            page: 0,
            end: false
        },
64
        discount: {
王水玲 authored
65 66 67 68 69
            order: 1,
            reload: true,
            page: 0,
            end: false
        },
70 71
        default: {
            order: 0,
王水玲 authored
72
            reload: true,
73
            page: 0,
74 75 76 77 78
            end: false
        },
        new: {
            order: 0,
            reload: true,
79
            page: 0,
80 81 82 83 84
            end: false
        },
        sale: {
            order: 0,
            reload: true,
85
            page: 0,
王水玲 authored
86
            end: false
zhangxiaoru authored
87 88 89 90 91 92
        },
        popularity: {
            order: 0,
            reload: true,
            page: 0,
            end: false
王水玲 authored
93 94
        }
    },
王水玲 authored
95
    $pre = $listNav.find('.active'), // 纪录进入筛选前的active项,初始为选中项
王水玲 authored
96 97 98 99 100
    searching,
    btnIntroHammer,
    introHammer,
    brandColHammer;
陈峰 authored
101
let C_ID, RES_QTY, argument, optype, filext = {};
zhangxiaoru authored
102
陈峰 authored
103
let category = $('#category-point').val();
zhangxiaoru authored
104
陈峰 authored
105
let noResultHbs = require('product/search/no-result-new.hbs');
徐炜 authored
106
zhangxiaoru authored
107 108
C_ID = window._ChannelVary[window.cookie('_Channel')];
徐炜 authored
109
// 首屏无数据
zhangxiaoru authored
110
if (!firstScreen) {
111
    $goodsContainer.html(noResultHbs());
112
    window.rePosFooter();
徐炜 authored
113
}
王水玲 authored
114 115 116

ellipsis.init();
zzzzzzz authored
117
// pageCache 获取是否收藏
郭成尧 authored
118
(function() {
zzzzzzz authored
119 120 121 122 123 124 125
    if ($('.domain').val()) {
        $.ajax({
            url: '/product/index/brandFav',
            data: {
                domain: $('.domain').val()
            },
            dataType: 'json',
郭成尧 authored
126
            success: function(data) {
郭成尧 authored
127
                if (data.data) {
zzzzzzz authored
128 129 130
                    $('.brand-header .btn-col').addClass('coled');
                }
            },
郭成尧 authored
131
            error: function() {
zzzzzzz authored
132 133 134 135 136 137
                tip.show('网络断开连接了~');
            }
        });
    }
}());
王水玲 authored
138 139
// 搜索输入联动
function inputAction() {
陈峰 authored
140 141 142
    let $searchAssociate = $('.search-associate');
    let $icon = $('.search-icon');
    let $searchItems = $('.search-items');
王水玲 authored
143
郭成尧 authored
144
    $input.on('input', function() {
王水玲 authored
145 146 147 148 149 150 151 152 153 154 155 156 157 158
        if ($input.val() === '') {
            $icon.css('color', '#b2b2b2');
            $clear.addClass('hide');
            $searchAssociate.html('');
            $searchItems.show();
            $searchAssociate.hide();
        } else {
            $icon.css('color', '#666');
            $clear.removeClass('hide');
            $searchAssociate.show();
        }

        // 联动搜索
        $.ajax({
zhangxiaoru authored
159
            url: '/product/search/fuzzyDatas',
王水玲 authored
160 161 162 163
            data: {
                keyword: $input.val()
            },
            dataType: 'json',
郭成尧 authored
164
            success: function(data) {
陈峰 authored
165 166
                let ajaxHtml = '';
                let i;
王水玲 authored
167 168 169 170

                if (data.length > 0) {
                    for (i = 0; i < data.length; i++) {
                        ajaxHtml += '<li><span class="keyword">' + data[i].keyword + '</span><span class="count">' +
郭成尧 authored
171
                            data[i].count + ' items<i class="iconfont">&#xe614;</i></span></li>';
王水玲 authored
172 173 174 175 176 177 178 179
                    }

                    $searchAssociate.html(ajaxHtml);
                    $searchItems.hide();
                } else {
                    $searchAssociate.html('');
                }
郭成尧 authored
180
                $searchAssociate.find('li').on('touchend', function() {
王水玲 authored
181 182 183 184
                    $buriedpoint.val($(this).find('.keyword').html());
                    $search.closest('form').submit();
                });
            },
郭成尧 authored
185
            error: function() {
王水玲 authored
186 187 188 189 190 191 192 193
                tip.show('网络断开连接了~');
            }
        });
    });
}

inputAction();
郭成尧 authored
194
$clear.on('touchend', function() {
王水玲 authored
195 196 197 198 199 200
    $input.val('').trigger('input');
});

/**
 * 手动触发搜索
 */
郭成尧 authored
201
$search.on('touchend', function() {
王水玲 authored
202 203 204 205 206 207 208 209
    $(this).closest('form').submit();
    return false;
});

/**
 * 获取url参数
 */
function getQueryString(name) {
陈峰 authored
210 211
    let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    let r = decodeURI(window.location.search).substr(1).match(reg);
王水玲 authored
212
王水玲 authored
213
    if (r !== null) {
214
        return decodeURIComponent(r[2]);
王水玲 authored
215 216 217 218 219 220 221
    }
    return null;
}

shopId = getQueryString('shop_id');
sort = getQueryString('sort');
outlets = getQueryString('outlets');
zhangxiaoru authored
222
age_level = getQueryString('ageLevel');
郭成尧 authored
223
app_type = getQueryString('app_type');
郭成尧 authored
224 225
msort = getQueryString('msort');
misort = getQueryString('misort');
zzzzzzz authored
226 227
coin = getQueryString('coin');
app_version = getQueryString('app_version') || getQueryString('appVersion');
zhangxiaoru authored
228 229 230 231 232 233 234 235
size = getQueryString('size');
color = getQueryString('color');
style = getQueryString('style');
standard = getQueryString('standard');
gender = getQueryString('gender');
price = getQueryString('price');
p_d = getQueryString('p_d');
brand = getQueryString('brand');
236 237
oldquery = getQueryString('query');
query = $('.query-param[data-attr="query"]').val() || oldquery;
zhangxiaoru authored
238 239 240 241
limited = getQueryString('limited');
specialoffer = getQueryString('specialoffer');
specialsale_id = getQueryString('specialsale_id');
promotion = getQueryString('promotion');
242
students = getQueryString('students');
郝肖肖 authored
243
couponId = getQueryString('coupon_id');
244
couponCode = getQueryString('coupon_code');
245
searchFrom = getQueryString('from');
郭成尧 authored
246
zhangxiaoru authored
247
if ($defaultgc.children().length > 0) {
陈峰 authored
248
    let fsgcgoods = [],
249 250
        firstop,
        firstyasparm = {};
zhangxiaoru authored
251
zhangxiaoru authored
252
    $defaultgc.find('.good-info').each(function() {
陈峰 authored
253
        let fsgcgood = $(this).data('good-id');
zhangxiaoru authored
254 255 256 257 258 259 260 261 262 263 264 265 266 267

        if (fsgcgood) {
            fsgcgoods.push(fsgcgood);
        }
    });

    if (category) {
        firstop = 'YB_CATEGORY_GOODS_LIST_L';
        firstyasparm = Object.assign(firstyasparm, {
            CATEGORY_ID: window.queryString.msort || window.queryString.sort || ''
        });
    } else {
        firstop = 'YB_KEYWORD_GOODS_LIST_L';
        firstyasparm = Object.assign(firstyasparm, {
郝肖肖 authored
268 269
            KEYWORD: query || '',
            IS_REC: isRecYas
zhangxiaoru authored
270 271 272 273 274
        });
    }

    firstyasparm = Object.assign(firstyasparm, {
        C_ID: C_ID,
zhangxiaoru authored
275
        RES_QTY: $defaultgc.children().length,
zhangxiaoru authored
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
        PRD_LIST: JSON.stringify(fsgcgoods).replace(/\[|\]/g, ''),
        PAGE_NUM: 1,
        FILTER_VALUE: '',
        SORT_TYPE: 'default'
    });

    setTimeout(function() {
        if (window._yas && window._yas.sendCustomInfo) {
            window._yas.sendCustomInfo({
                op: firstop,
                param: JSON.stringify(firstyasparm)
            }, true);
        }
    }, 200);
}
zhangxiaoru authored
291
王水玲 authored
292 293 294 295 296
/**
 * 筛选注册的回调,筛选子项点击后逻辑
 * 需要执行search的场景:1.点选筛选项;2.relaod为true时切换导航;3.下拉加载
 * @param opt {type, id}
 */
徐炜 authored
297
function search(opt) {
陈峰 authored
298
    let setting = {},
王水玲 authored
299 300 301 302 303 304 305 306 307 308 309 310 311 312
        ext,
        att,
        nav,
        navType,
        page,
        $this,
        $title,
        params;

    if (searching) {
        return;
    }

    if (opt) {
王水玲 authored
313
        // 筛选项变更则重置reload为true
王水玲 authored
314 315 316 317 318 319
        for (att in navInfo) {
            if (navInfo.hasOwnProperty(att)) {
                navInfo[att].reload = true;
            }
        }
王水玲 authored
320
        // 处理active状态
王水玲 authored
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
        $listNav.children('.active').removeClass('active');
        $pre.addClass('active');
        switch (opt.type) {
            case 'shop_id':
                ext = {
                    shop_id: opt.id
                };
                break;
            case 'gender':
                ext = {
                    gender: opt.id
                };
                break;
            case 'brand':
                ext = {
                    brand: opt.id
                };
                break;
            case 'sort':
                ext = {
                    sort: opt.id
                };
                break;
            case 'color':
                ext = {
                    color: opt.id
                };
                break;
zhangxiaoru authored
349
            case 'ageLevel':
王水玲 authored
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
                ext = {
                    age_level: opt.id
                };
                break;
            case 'size':
                ext = {
                    size: opt.id
                };
                break;
            case 'price':
                ext = {
                    price: opt.id
                };
                break;
            case 'discount':
                ext = {
                    discount: opt.id
                };
                break;
            case 'outlets':
                ext = {
                    outlets: opt.id
                };
                break;
zhangxiaoru authored
374 375 376 377 378
            case 'p_d':
                ext = {
                    p_d: opt.id
                };
                break;
王水玲 authored
379 380
            default:
                break;
王水玲 authored
381 382
        }
zhangxiaoru authored
383 384

        $.extend(filext, ext);
王水玲 authored
385
        $.extend(defaultOpt, ext); // 扩展筛选项
王水玲 authored
386 387
    }
王水玲 authored
388
    // 导航类别
王水玲 authored
389
    if ($pre.hasClass('new')) {
390
        navType = 'new';
王水玲 authored
391 392 393 394
    } else if ($pre.hasClass('price')) {
        navType = 'price';
    } else if ($pre.hasClass('discount')) {
        navType = 'discount';
395 396 397 398
    } else if ($pre.hasClass('default')) {
        navType = 'default';
    } else if ($pre.hasClass('sale')) {
        navType = 'sale';
zhangxiaoru authored
399 400
    } else if ($pre.hasClass('popularity')) {
        navType = 'popularity';
王水玲 authored
401 402 403 404 405
    }

    nav = navInfo[navType];

    page = nav.page + 1;
zhangxiaoru authored
406
王水玲 authored
407 408 409
    if (nav.reload) {
        page = 1;
    } else if (nav.end) {
王水玲 authored
410
        // 不需要重新加载并且数据请求结束
王水玲 authored
411 412 413 414 415 416
        return;
    }

    params = {
        type: navType,
        order: nav.order,
417 418
        page: page,
        noLazy: true
王水玲 authored
419 420 421 422 423 424
    };

    if (shopId) {
        params.shop_id = shopId;
    }
郭成尧 authored
425
    if (age_level) {
郭成尧 authored
426
        params.age_level = age_level;
王水玲 authored
427 428 429 430 431 432 433 434 435 436 437 438 439 440
    }

    if (sort) {
        params.sort = sort;
    }

    if (brand) {
        params.brand = brand;
    }

    if (outlets) {
        params.outlets = outlets;
    }
郭成尧 authored
441 442 443 444
    if (app_type) {
        params.app_type = app_type;
    }
郭成尧 authored
445 446 447 448 449 450 451 452
    if (msort) {
        params.msort = msort;
    }

    if (misort) {
        params.misort = misort;
    }
zzzzzzz authored
453 454 455
    if (coin) {
        params.coin = coin;
    }
王水玲 authored
456
zzzzzzz authored
457 458 459 460
    if (app_version) {
        params.app_version = app_version;
    }
zhangxiaoru authored
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
    if (size) {
        params.size = size;
    }

    if (color) {
        params.color = color;
    }

    if (style) {
        params.style = style;
    }

    if (standard) {
        params.standard = standard;
    }

    if (gender) {
        params.gender = gender;
    }

    if (price) {
        params.price = price;
    }

    if (p_d) {
zhangxiaoru authored
486
        params.p_d = p_d;
zhangxiaoru authored
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    }

    if (query) {
        params.query = query;
    }

    if (limited) {
        params.limited = limited;
    }

    if (specialoffer) {
        params.specialoffer = specialoffer;
    }

    if (specialsale_id) {
        params.specialsale_id = specialsale_id;
    }

    if (promotion) {
        params.promotion = promotion;
    }
509 510 511 512
    if (students) {
        params.students = students;
    }
郝肖肖 authored
513 514 515 516
    if (couponId) {
        params.coupon_id = couponId;
    }
517 518 519 520
    if (couponCode) {
        params.coupon_code = couponCode;
    }
521 522 523 524
    if (searchFrom) {
        params.from = searchFrom;
    }
zzzzzzz authored
525
    $.extend(setting, defaultOpt, params);
王水玲 authored
526 527 528
    searching = true;
    loading.showLoadingMask();
zhangxiaoru authored
529
    argument = setting;
zhangxiaoru authored
530
zhangxiaoru authored
531
    $searchDivide.removeClass('hide');
zhangxiaoru authored
532
王水玲 authored
533 534
    $.ajax({
        type: 'GET',
郭成尧 authored
535
        url: location.protocol + '//m.yohobuy.com/product/search/search',
王水玲 authored
536
        data: setting,
537 538 539
        xhrFields: {
            withCredentials: true
        },
郭成尧 authored
540
        success: function(data) {
陈峰 authored
541
            let num,
zhangxiaoru authored
542 543
                $container,
                goodIds = [],
zhangxiaoru authored
544 545
                yasparm,
                PRD_LIST;
王水玲 authored
546 547

            switch (navType) {
548
                case 'new':
王水玲 authored
549 550 551 552 553 554 555 556
                    $container = $ngc;
                    break;
                case 'price':
                    $container = $pgc;
                    break;
                case 'discount':
                    $container = $dgc;
                    break;
557 558 559
                case 'default':
                    $container = $defaultgc;
                    break;
zhangxiaoru authored
560
                case 'popularity':
Zhang authored
561
                    $container = $popularity;
zhangxiaoru authored
562
                    break;
563 564 565
                case 'sale':
                    $container = $sgc;
                    break;
王水玲 authored
566 567
                default:
                    break;
王水玲 authored
568 569
            }
zhangxiaoru authored
570
            yasparm = {
zhangxiaoru authored
571 572 573 574 575 576 577
                C_ID: C_ID,
                PAGE_NUM: setting.page,
                SORT_TYPE: setting.type,
                FILTER_VALUE: opt ? JSON.stringify(filext).replace(/\{|\}|\"/g, '') : ''
            };

            if (category) {
zhangxiaoru authored
578 579 580 581
                optype = 'YB_CATEGORY_GOODS_LIST_L';
                yasparm = Object.assign(yasparm, {
                    CATEGORY_ID: window.queryString.msort || window.queryString.sort || ''
                });
zhangxiaoru authored
582
            } else {
zhangxiaoru authored
583
                optype = 'YB_KEYWORD_GOODS_LIST_L';
zhangxiaoru authored
584
                yasparm = Object.assign(yasparm, {
585 586
                    KEYWORD: setting.query || '',
                    IS_REC: isRecYas
zhangxiaoru authored
587 588 589
                });
            }
zhangxiaoru authored
590
            if (data === '' || (data.list && data.list.length <= 0) || data.length === 0) {
王水玲 authored
591
                nav.end = true;
zhangxiaoru authored
592
                if (nav.reload && !firstScreen) {
593
                    if (data.total === 0) {
594
                        $container.html(noResultHbs());
595
                    }
zhangxiaoru authored
596
zhangxiaoru authored
597
                    yasparm = Object.assign(yasparm, {
zhangxiaoru authored
598 599
                        PRD_LIST: '',
                        RES_QTY: 0
zhangxiaoru authored
600
                    });
zhangxiaoru authored
601 602 603 604 605 606 607 608

                    setTimeout(function() {
                        if (window._yas && window._yas.sendCustomInfo) {
                            window._yas.sendCustomInfo({
                                op: optype,
                                param: JSON.stringify(yasparm)
                            }, true);
                        }
zhangxiaoru authored
609
                    }, 200);
zhangxiaoru authored
610
                }
zhangxiaoru authored
611
            } else {
zhangxiaoru authored
612
                if (nav.reload) {
王水玲 authored
613
                    $container.html(data);
zzzzzzz authored
614
                    num = $container.find('.good-info').length;
zhangxiaoru authored
615
王水玲 authored
616
                    lazyLoad($container.find('.lazy'));
zhangxiaoru authored
617 618

                    RES_QTY = $container.find('.total').data('id');
zhangxiaoru authored
619
zhangxiaoru authored
620
                    $container.find('.good-info').each(function() {
陈峰 authored
621
                        let goodid = $(this).data('good-id');
zhangxiaoru authored
622
zhangxiaoru authored
623
                        if (goodid) {
zhangxiaoru authored
624 625 626 627
                            goodIds.push(goodid);
                        }
                    });
zhangxiaoru authored
628 629
                    PRD_LIST = JSON.stringify(goodIds).replace(/\[|\]/g, '');
zhangxiaoru authored
630
                    yasparm = Object.assign(yasparm, {
zhangxiaoru authored
631
                        PRD_LIST: PRD_LIST,
zhangxiaoru authored
632
                        RES_QTY: RES_QTY
zhangxiaoru authored
633
                    });
zhangxiaoru authored
634
zhangxiaoru authored
635 636 637 638 639 640 641
                    setTimeout(function() {
                        if (window._yas && window._yas.sendCustomInfo) {
                            window._yas.sendCustomInfo({
                                op: optype,
                                param: JSON.stringify(yasparm)
                            }, true);
                        }
zhangxiaoru authored
642
                    }, 200);
王水玲 authored
643 644
                } else {
                    num = $container.find('.good-info').length;
zhangxiaoru authored
645
王水玲 authored
646 647
                    $container.append(data);
zhangxiaoru authored
648
                    if ($container.find('.total')[1]) {
649
                        $container.find('.total').eq(1).remove();
zhangxiaoru authored
650 651
                    }
zhangxiaoru authored
652
                    RES_QTY = $container.find('.total').data('id');
zhangxiaoru authored
653
zhangxiaoru authored
654
                    $container.find('.good-info:gt(' + (num - 1) + ')').each(function() {
陈峰 authored
655
                        let goodid = $(this).data('good-id');
zhangxiaoru authored
656
zhangxiaoru authored
657
                        if (goodid) {
zhangxiaoru authored
658 659
                            goodIds.push(goodid);
                        }
zhangxiaoru authored
660
                    });
zhangxiaoru authored
661
zhangxiaoru authored
662 663
                    PRD_LIST = JSON.stringify(goodIds).replace(/\[|\]/g, '');
zhangxiaoru authored
664
                    yasparm = Object.assign(yasparm, {
zhangxiaoru authored
665
                        PRD_LIST: PRD_LIST,
zhangxiaoru authored
666
                        RES_QTY: RES_QTY
zhangxiaoru authored
667
                    });
zhangxiaoru authored
668
zhangxiaoru authored
669 670 671 672 673 674 675
                    setTimeout(function() {
                        if (window._yas && window._yas.sendCustomInfo) {
                            window._yas.sendCustomInfo({
                                op: optype,
                                param: JSON.stringify(yasparm)
                            }, true);
                        }
zhangxiaoru authored
676
                    }, 200);
zhangxiaoru authored
677
王水玲 authored
678
                    // lazy good-infos who append in
王水玲 authored
679 680 681 682
                    lazyLoad($container.find('.good-info:gt(' + (num - 1) + ') .lazy'));
                }
            }
zhangxiaoru authored
683
            $searchDivide.addClass('hide');
王水玲 authored
684 685 686 687 688 689 690 691
            nav.reload = false;
            nav.page = page;

            searching = false;
            loading.hideLoadingMask();

            window.rePosFooter();
zhangxiaoru authored
692 693
            firstScreen = false;
郭成尧 authored
694
            $('.good-detail-text .name').each(function() {
王水玲 authored
695 696 697 698 699 700 701 702 703
                $this = $(this);
                $title = $this.find('a');

                $title[0].mlellipsis(2);
            });

            // 用于统计点击了商品列表的第几个商品,序号从1开始计算。
            if (window._yas) {
                switch (navType) {
704
                    case 'new':
王水玲 authored
705
                        window._yas(1 * new Date(), '1.0.16', 'yohobuy_m', window._ozuid,
郭成尧 authored
706
                            '', $('.new-goods .good-info .good-detail-img .good-thumb'));
王水玲 authored
707 708 709
                        break;
                    case 'price':
                        window._yas(1 * new Date(), '1.0.16', 'yohobuy_m', window._ozuid,
郭成尧 authored
710
                            '', $('.price-goods .good-info .good-detail-img .good-thumb'));
王水玲 authored
711 712 713
                        break;
                    case 'discount':
                        window._yas(1 * new Date(), '1.0.16', 'yohobuy_m', window._ozuid,
郭成尧 authored
714
                            '', $('.discount-goods .good-info .good-detail-img .good-thumb'));
王水玲 authored
715
                        break;
716 717
                    case 'default':
                        window._yas(1 * new Date(), '1.0.16', 'yohobuy_m', window._ozuid,
徐炜 authored
718
                            '', $('.firstscreen-goods .default-goods .good-info .good-detail-img .good-thumb'));
719 720 721 722 723
                        break;
                    case 'sale':
                        window._yas(1 * new Date(), '1.0.16', 'yohobuy_m', window._ozuid,
                            '', $('.sale-goods .good-info .good-detail-img .good-thumb'));
                        break;
王水玲 authored
724 725
                    default:
                        break;
王水玲 authored
726 727
                }
            }
zzzzzzz authored
728 729
        },
        error: function() {
zhangxiaoru authored
730
            let $divide = $searchDivide;
zzzzzzz authored
731 732 733

            $divide.html('加载失败,点击重试');
            $divide.one('click', function() {
zzzzzzz authored
734
                $divide.html('正在加载...');
zzzzzzz authored
735 736 737 738
                nav.reload = false;
                searching = false;
                search();
            });
王水玲 authored
739 740 741 742
        }
    });
}
郝肖肖 authored
743
require('common/suspend-cart'); // 悬浮购物车
郭成尧 authored
744
/* tar add 160914 */
745
if ($brandHeader.data('isbaseshop') === true) {
郭成尧 authored
746
    Object.assign(defaultOpt, {shop_id: $brandHeader.data('id')});
郭成尧 authored
747
}
王水玲 authored
748
zhangxiaoru authored
749
(function() {
陈峰 authored
750
    let pars = {};
郭成尧 authored
751
zhangxiaoru authored
752 753
    if (age_level) {
        pars.age_level = age_level;
王水玲 authored
754 755
    }
zhangxiaoru authored
756 757 758
    if (size) {
        pars.size = size;
    }
王水玲 authored
759
zhangxiaoru authored
760 761
    if (color) {
        pars.color = color;
王水玲 authored
762
    }
zhangxiaoru authored
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

    if (style) {
        pars.style = style;
    }

    if (standard) {
        pars.standard = standard;
    }

    if (gender) {
        pars.gender = gender;
    }

    if (price) {
        pars.price = price;
    }

    if (p_d) {
        pars.p_d = p_d;
    }

    if (brand) {
        pars.brand = brand;
    }

    if (sort) {
        pars.sort = sort;
    }

    if (msort) {
        pars.msort = msort;
    }

    if (misort) {
        pars.misort = misort;
    }

    if (outlets) {
        pars.outlets = outlets;
    }

    if (app_type) {
        pars.app_type = app_type;
    }

    if (query) {
        pars.query = query;
    }

    if (limited) {
        pars.limited = limited;
    }

    if (specialoffer) {
        pars.specialoffer = specialoffer;
    }

    if (specialsale_id) {
        pars.specialsale_id = specialsale_id;
    }

    if (promotion) {
        pars.promotion = promotion;
    }
郭成尧 authored
828 829 830 831
    if (shopId) {
        pars.shop_id = shopId;
    }
郝肖肖 authored
832 833 834 835
    if (couponId) {
        pars.coupon_id = couponId;
    }
836 837 838 839
    if (couponCode) {
        pars.coupon_code = couponCode;
    }
zhangxiaoru authored
840
    $.extend(defaultOpt, pars);
徐炜 authored
841 842 843
    $.ajax({
        type: 'GET',
        url: location.protocol + '//m.yohobuy.com/product/search/filter',
844 845 846
        xhrFields: {
            withCredentials: true
        },
徐炜 authored
847 848
        data: defaultOpt,
        success: function(data) {
zhangxiaoru authored
849 850 851 852
            if (data === '') {
                return false;
            }
徐炜 authored
853
            $goodsContainer.append(data);
zhangxiaoru authored
854
徐炜 authored
855 856 857 858
            // 初始化filter&注册filter回调
            filter.initFilter({
                fCbFn: search,
                hCbFn: function() {
zhangxiaoru authored
859
徐炜 authored
860 861 862 863 864 865 866
                    // 切换active状态到$pre上
                    $pre.addClass('active');
                    $pre.siblings('.filter').removeClass('active');
                }
            });
        }
    });
867
}());
zhangxiaoru authored
868
王水玲 authored
869 870 871 872 873

lazyLoad($('.lazy'));

writeSearch.bindWirteLocal($('#search-form'));
王水玲 authored
874 875 876 877 878 879
// 导航栏点击逻辑说明:
// 1.点击非active项时切换active状态
// 2.价格和折扣active状态时继续点击切换排序
// 3.筛选无active时点击展开筛选面板
// 4.筛选有active时点击隐藏筛选面板并恢复点击筛选前active项的active状态
// 5.当前active为筛选并且点击其他项时,隐藏筛选面板
王水玲 authored
880
郭成尧 authored
881
$listNav.bind('contextmenu', function() {
王水玲 authored
882 883 884
    return false;
});
zhangxiaoru authored
885
$dropList.on('touchend', 'li', function(e) {
zhangxiaoru authored
886 887 888 889 890 891 892 893 894 895
    let navType,
        nav,
        bpIdData = $(this).attr('data-bp-id') || '',
        $firstLiDom = $listNav.find('li.first-li-more');

    e.preventDefault();

    if ($(this).hasClass('default')) {
        navType = 'default';
    } else {
Zhang authored
896
        navType = 'discount';
zhangxiaoru authored
897
        firstScreen = false;
zhangxiaoru authored
898 899
    }
zhangxiaoru authored
900
    if ($(this).hasClass('active')) {
zhangxiaoru authored
901
        $dropList.addClass('hide');
zhangxiaoru authored
902 903 904
        return;
    }
zhangxiaoru authored
905
    $(this).addClass('active').siblings().removeClass('active');
Zhang authored
906
    $firstLiDom.find('.nav-txt').text($(this).data('text'));
zhangxiaoru authored
907
    $firstLiDom.attr('data-bp-id', bpIdData).addClass('active').siblings().removeClass('active');
Zhang authored
908
    $listNav.find('.first-li-more').removeClass('new default sale discount').addClass(navType);
zhangxiaoru authored
909 910 911

    nav = navInfo[navType];
Zhang authored
912
    if ($(this).hasClass('asc')) {
Zhang authored
913
        nav.order = 1;
Zhang authored
914 915
        nav.reload = true;
    } else {
Zhang authored
916
        nav.order = 0;
Zhang authored
917 918 919
        nav.reload = true;
    }
zhangxiaoru authored
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
    $defaultgc.removeClass('hide');

    $goodsContainer.children('.container:not(.hide)').addClass('hide');

    switch (navType) {
        case 'new':
            $ngc.removeClass('hide');
            break;

        case 'price':
            $pgc.removeClass('hide');
            break;

        case 'discount':
            $dgc.removeClass('hide');
            break;

        case 'default':
            $defaultgc.removeClass('hide');
            break;

        case 'popularity':
Zhang authored
942
            $popularity.removeClass('hide');
zhangxiaoru authored
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
            break;

        case 'sale':
            $sgc.removeClass('hide');
            break;
        default:
            break;
    }

    $pre = $firstLiDom;

    if (nav.reload) {
        $(document).trigger('shouldSendBpData', [bpIdData]);
        search({filtering: true});
    }
zhangxiaoru authored
959 960 961 962 963
    if (!$('.filter-mask').hasClass('hide')) {
        filter.hideFilter();
    }

    $dropList.addClass('hide');
zhangxiaoru authored
964 965
});
郭成尧 authored
966
$listNav.on('touchend touchcancel', function(e) {
陈峰 authored
967
    let $this = $(e.target).closest('li'),
王水玲 authored
968 969 970
        nav,
        navType,
        $active;
陈峰 authored
971
    let bpIdData = $(this).find('.buriedpoint').attr('data-bp-id') || '';
王水玲 authored
972
Zhang authored
973
    if ($this.hasClass('first-li-more') && $this.hasClass('active')) {
zhangxiaoru authored
974
        $dropList.toggleClass('hide');
zhangxiaoru authored
975 976 977 978 979 980

        $this.addClass('active').siblings().removeClass('active');

        return true;
    }
王水玲 authored
981 982
    if ($this.hasClass('filter')) {
王水玲 authored
983
        // 筛选面板切换状态
王水玲 authored
984 985 986
        if ($this.hasClass('active')) {
            filter.hideFilter();
王水玲 authored
987
            // 点击筛选钱的active项回复active
王水玲 authored
988 989 990 991 992 993 994 995 996 997 998 999
            $pre.addClass('active');
            $this.removeClass('active');
        } else {
            $pre = $this.siblings('.active');

            $pre.removeClass('active');
            $this.addClass('active');

            filter.showFilter();
        }
    } else {
Zhang authored
1000
        // 判断选择navtype
王水玲 authored
1001
        if ($this.hasClass('new')) {
1002
            navType = 'new';
王水玲 authored
1003 1004 1005 1006
        } else if ($this.hasClass('price')) {
            navType = 'price';
        } else if ($this.hasClass('discount')) {
            navType = 'discount';
1007 1008 1009 1010
        } else if ($this.hasClass('default')) {
            navType = 'default';
        } else if ($this.hasClass('sale')) {
            navType = 'sale';
Zhang authored
1011 1012
        } else if ($this.hasClass('popularity')) {
            navType = 'popularity';
王水玲 authored
1013 1014 1015 1016 1017 1018
        }

        nav = navInfo[navType];

        if ($this.hasClass('active')) {
1019
            // 默认、最新无排序切换
Zhang authored
1020
            if ($this.hasClass('default') || $this.hasClass('new') || $this.hasClass('popularity')) {
王水玲 authored
1021 1022 1023 1024 1025 1026 1027
                return;
            }

            if ($this.hasClass('price') || $this.hasClass('discount')) {

                // 价格/折扣切换排序状态
                $this.find('.icon > .iconfont').toggleClass('cur');
王水玲 authored
1028 1029 1030
                $pre = $this; // 更新pre为当前项
                nav.reload = true; // 重置reload,HTML会被替换为逆序的HTML
                nav.order = nav.order === 0 ? 1 : 0; // 切换排序
王水玲 authored
1031 1032 1033 1034
            }
        } else {
            $active = $this.siblings('.active');
王水玲 authored
1035
            $pre = $this; // $pre为除筛选导航的其他导航项,若当前active的为筛选,则把$pre置为当前点击项
王水玲 authored
1036 1037 1038

            if ($active.hasClass('filter')) {
王水玲 authored
1039
                // 若之前active项为筛选,则隐藏筛选面板
王水玲 authored
1040 1041 1042
                filter.hideFilter();
            } else {
王水玲 authored
1043
                // 切换container显示
王水玲 authored
1044 1045 1046
                $goodsContainer.children('.container:not(.hide)').addClass('hide');

                switch (navType) {
1047
                    case 'new':
王水玲 authored
1048 1049 1050
                        $ngc.removeClass('hide');
                        break;
Zhang authored
1051 1052 1053 1054
                    case 'popularity':
                        $popularity.removeClass('hide');
                        break;
王水玲 authored
1055 1056 1057 1058 1059 1060 1061
                    case 'price':
                        $pgc.removeClass('hide');
                        break;

                    case 'discount':
                        $dgc.removeClass('hide');
                        break;
1062 1063 1064 1065 1066 1067 1068 1069

                    case 'default':
                        $defaultgc.removeClass('hide');
                        break;

                    case 'sale':
                        $sgc.removeClass('hide');
                        break;
1070 1071
                    default:
                        break;
王水玲 authored
1072 1073 1074 1075 1076 1077 1078 1079
                }
            }

            $active.removeClass('active');
            $this.addClass('active');
        }
        if (nav.reload) {
            $(document).trigger('shouldSendBpData', [bpIdData]);
1080
            search({filtering: true});
王水玲 authored
1081 1082
        }
    }
Zhang authored
1083 1084 1085 1086

    if (!$dropList.hasClass('hide')) {
        $dropList.addClass('hide');
    }
王水玲 authored
1087 1088 1089 1090
});

function scrollHandler() {
1091
    // 当scroll到1/2$goodsContainer高度后继续请求下一页数据
王水玲 authored
1092
    if ($(window).scrollTop() + winH >
1093
        $(document).height() - 0.5 * $goodsContainer.height()) {
zhangxiaoru authored
1094 1095 1096 1097
        if ($('.first-li-more').hasClass('active') && navInfo.default.page === 0) {
            navInfo.default.page = 1;
            navInfo.default.reload = false;
        }
王水玲 authored
1098 1099 1100 1101
        search();
    }
}
王水玲 authored
1102
// srcoll to load more
郭成尧 authored
1103
$(window).scroll(function() {
王水玲 authored
1104 1105 1106 1107 1108
    window.requestAnimationFrame(scrollHandler);
});

if ($brandHeader.length > 0) {
王水玲 authored
1109
    // 品牌介绍
王水玲 authored
1110
    btnIntroHammer = new Hammer($brandHeader.children('.btn-intro')[0]);
郭成尧 authored
1111
    btnIntroHammer.on('tap', function() {
王水玲 authored
1112 1113
        $introBox.removeClass('hide');
王水玲 authored
1114
        // 防止混合scroll发生
王水玲 authored
1115 1116 1117
        $('body').addClass('overflow-hidden');
    });
王水玲 authored
1118
    // 关闭品牌介绍
王水玲 authored
1119
    introHammer = new Hammer(document.getElementById('intro-box'));
郭成尧 authored
1120
    introHammer.on('tap', function(e) {
陈峰 authored
1121
        let $this = $(e.target);
王水玲 authored
1122 1123 1124

        e.srcEvent.preventDefault();
王水玲 authored
1125
        // 关闭品牌介绍box
王水玲 authored
1126 1127 1128 1129 1130 1131
        if ($this.closest('#brand-intro').length === 0 || $this.hasClass('close-intro')) {
            $introBox.addClass('hide');
            $('body').removeClass('overflow-hidden');
        }
    });
王水玲 authored
1132
    // 品牌收藏
王水玲 authored
1133
    brandColHammer = new Hammer($brandHeader.children('.btn-col')[0]);
郭成尧 authored
1134
    brandColHammer.on('tap', function(e) {
陈峰 authored
1135
        let $this = $(e.target).closest('.btn-col');
王水玲 authored
1136
陈峰 authored
1137
        let id = $brandHeader.data('id'),
王水玲 authored
1138 1139 1140 1141 1142 1143 1144 1145 1146
            opt;

        if ($this.hasClass('coled')) {
            opt = 'cancel';
        } else {
            opt = 'ok';
        }

        $.ajax({
1147
            type: 'GET',
郭成尧 authored
1148
            url: location.protocol + '//m.yohobuy.com/product/opt/favoriteBrand',
王水玲 authored
1149 1150
            data: {
                id: id,
王水玲 authored
1151
                opt: opt,
郭成尧 authored
1152
                type: $brandHeader.data('isbaseshop') ? 'shop' : 'brand'
王水玲 authored
1153
            },
郭成尧 authored
1154 1155 1156
            xhrFields: {
                withCredentials: true
            },
郭成尧 authored
1157
            success: function(data) {
王水玲 authored
1158 1159 1160
                if (data.code === 200) {
                    $this.toggleClass('coled');
王水玲 authored
1161
                    // 提示
王水玲 authored
1162 1163 1164 1165 1166 1167
                    if (opt === 'ok') {
                        tip.show('添加收藏成功');
                    } else {
                        tip.show('取消收藏成功');
                    }
                } else if (data.code === 400) {
王水玲 authored
1168
                    location.href = data.data;// 未登录跳转登录页
王水玲 authored
1169 1170 1171 1172
                } else {
                    tip.show(data.message);
                }
            },
郭成尧 authored
1173
            error: function() {
王水玲 authored
1174 1175 1176 1177 1178 1179
                tip.show('网络断开连接了~');
            }
        });
    });
}
郭成尧 authored
1180
$listNav.on('touchstart', 'li', function() {
王水玲 authored
1181 1182
    $listNav.find('li').removeClass('bytouch');
    $(this).addClass('bytouch');
郭成尧 authored
1183
}).on('touchend touchcancel', 'li', function() {
王水玲 authored
1184 1185
    $listNav.find('li').removeClass('bytouch');
});
zhangxiaoru authored
1186
zhangxiaoru authored
1187
$('#goods-container').on('click', '.good-info', function() {
陈峰 authored
1188
    let PRD_ID = $(this).data('good-id'),
zhangxiaoru authored
1189
        PRD_NUM = $(this).index() + 1,
zhangxiaoru authored
1190
        FILTER_VALUE = filext ? JSON.stringify(filext).replace(/\{|\}|\"/g, '') : '',
zhangxiaoru authored
1191
        yasparms,
姜枫 authored
1192
        firstType;
zhangxiaoru authored
1193
zhangxiaoru authored
1194 1195 1196 1197
    yasparms = {
        C_ID: C_ID,
        PRD_ID: PRD_ID,
        PRD_NUM: PRD_NUM,
zhangxiaoru authored
1198
        PAGE_NUM: Math.ceil(PRD_NUM / 60),
zhangxiaoru authored
1199
        FILTER_VALUE: FILTER_VALUE,
zhangxiaoru authored
1200
        SORT_TYPE: argument ? argument.type : firstType
zhangxiaoru authored
1201
    };
zhangxiaoru authored
1202
zhangxiaoru authored
1203
    if (category) {
zhangxiaoru authored
1204
        optype = 'YB_CATEGORY_GOODS_LIST_C';
zhangxiaoru authored
1205
        yasparms = Object.assign(yasparms, {
zhangxiaoru authored
1206
            CATEGORY_ID: window.queryString.msort || window.queryString.sort || ''
zhangxiaoru authored
1207
        });
zhangxiaoru authored
1208
    } else {
1209
        optype = 'YB_KEYWORD_GOODS_LIST_C';
zhangxiaoru authored
1210
        yasparms = Object.assign(yasparms, {
1211 1212
            KEYWORD: argument ? argument.query : query,
            IS_REC: isRecYas
zhangxiaoru authored
1213
        });
zhangxiaoru authored
1214
    }
zhangxiaoru authored
1215 1216 1217

    if (window._yas && window._yas.sendCustomInfo) {
        window._yas.sendCustomInfo({
zhangxiaoru authored
1218 1219
            op: optype,
            param: JSON.stringify(yasparms)
zhangxiaoru authored
1220 1221
        }, true);
    }
zhangxiaoru authored
1222
});
zzzzzzz authored
1223
zhangxiaoru authored
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
$('.more-shop').on('click', function() {
    if ($(this).hasClass('down')) {
        $('.brand-enter').css('max-height', '100%');
        $('.more-shop').removeClass('down').find('span').html('&#xe615;');
    } else {
        $('.brand-enter').removeAttr('style');
        $('.more-shop').addClass('down').find('span').html('&#xe616;');
    }
});

// 进入店铺埋点
$('.brand-way a').on('click', function() {
    if (window._yas && window._yas.sendCustomInfo) {
        window._yas.sendCustomInfo({
            op: 'YB_BRAND_SHOP_C',
            param: JSON.stringify({
                BRAND_ID: $('.brand-enter a').data('brandid'),
                SHOP_ID: $('.brand-enter a').data('shopid'),
                POS_ID: 101
            })
        }, true);
    }
});
1248
require('channel/maybe-like')({recpose: 100101, isExecute: true});