plusstar-index.page.js 9.42 KB
var $ = require('yoho-jquery'),
    tip = require('../plugin/tip'),
    Swiper = require('yoho-swiper'),
    debounce = require('lodash/debounce');

var plusstar = {},
    $footer = $('#yoho-footer');

var scrollFn,
    RECPOSE,
    isApp,
    speckParamApp = {
        udid: '',
        apt: '',
        sid: ''
    };

require('yoho-jquery-lazyload');
require('../common');

plusstar = {
    common: {
        codeDefault: '',
        productSkns: []
    },
    init: function() {
        var that = this,
            $liDom,
            $tabUlDom;

        $tabUlDom = $('.plusstar-page .tab-nav ul');

        // 重置tab code位置
        if (window.localStorage) {
            $tabUlDom.find('li').each(function() {
                localStorage.setItem($(this).data('code'), 0);
            });
        }

        $tabUlDom.find('li').css({
            width: 100 / $tabUlDom.find('li').length + '%'
        });

        // 事情委托机制
        $tabUlDom.bind('click', function(event) {
            $liDom = $(event.target).closest('li');

            if ($liDom.hasClass('focus')) {
                return true;
            }

            $(this).find('li').removeClass('focus');
            $liDom.addClass('focus');
            that.ParentLiDom = $liDom;// 保留当前tab先中的对象
            that.tabNav($liDom.data('code'));

            // 点击潮流优选上方的TAB按钮时
            if (window._yas && window._yas.sendCustomInfo) {
                window._yas.sendCustomInfo({
                    op: 'YB_FASHION_TAB_C',
                    param: JSON.stringify($.extend(speckParamApp, {
                        TAB_ID: $liDom.index() + 1,
                        PRD_ID: ''
                    }))
                }, true);

                window._yas.sendCustomInfo({
                    op: 'YB_FASHION_HOME_L',
                    param: JSON.stringify($.extend(speckParamApp, {
                        TAB_ID: that.ParentLiDom.index() + 1,
                        PRD_ID: ''
                    }))
                }, true);
            }
        });

        // start -- 默认选中
        $liDom = $tabUlDom.find('li.focus');
        if ($liDom.length <= 0) {
            // 默认第一个
            $liDom = $('.plusstar-page .tab-nav ul li:eq(0)');
        }
        $tabUlDom.find('li').removeClass('focus');
        $liDom.addClass('focus');
        that.tabNav($liDom.data('code'));

        // ent -- 默认选中
        that.ParentLiDom = $liDom;// 保留当前tab先中的对象
        setTimeout(function() {
            that._yas();
        }, 1000);
    },
    _yas: function() {
        // http://redmine.yoho.cn/issues/12224
        var that = this;

        if (!window._yas || !window._yas.sendCustomInfo) {
            return false;
        }

        RECPOSE = that.ParentLiDom.index() !== 0 ? 100015 : 100014;
        $('.plusstar-resources').bind('click', function(event) {
            var $dom, $domA, index;

            if ($(event.target).closest('.good-info').length > 0) {
                // 商品单击埋点
                $dom = $(event.target).closest('.good-info');
                index = $dom.index() + 1;
                window.givePoint($.extend(speckParamApp, {
                    REC_POSE: RECPOSE,
                    PRD_ID: $dom.data('good-id'),
                    ORDER_CODE: '',
                    PRD_NUM: index,
                    ACTION_ID: 1
                }));
            } else if ($(event.target).closest('.banner-top').length > 0) {
                // 头部banner楼层埋点
                $dom = $(event.target).closest('li');
                index = $dom.index() + 1;
                window._yas.sendCustomInfo({
                    op: 'YB_FASHION_FLR_C',
                    param: JSON.stringify($.extend(speckParamApp, {
                        TAB_ID: that.ParentLiDom.index() + 1,
                        F_ID: $dom.closest('ul').data('id'),
                        F_NAME: '焦点图',
                        PRD_ID: '',
                        F_URL: $dom.find('a').attr('href'),
                        F_INDEX: 1,
                        I_INDEX: index % 2 + 1// banner,li是双倍的
                    }))
                }, true);
            } else if ($(event.target).closest('.speck-title-image a').length > 0) {
                // 各楼层埋点
                $dom = $(event.target).closest('.speck-title-image');
                $domA = $(event.target).closest('.speck-title-image a');

                window._yas.sendCustomInfo({
                    op: 'YB_FASHION_FLR_C',
                    param: JSON.stringify($.extend(speckParamApp, {
                        TAB_ID: that.ParentLiDom.index() + 1,
                        F_ID: $dom.data('fid'),
                        PRD_ID: '',
                        F_NAME: $dom.data('name'),
                        F_URL: $domA.attr('href'),
                        F_INDEX: $dom.index() + 1,
                        I_INDEX: $domA.hasClass('more') ? 0 : ($domA.index() + 1)
                    }))
                }, true);
            }
        });

        // 潮流优选首页加载时
        window._yas.sendCustomInfo({
            op: 'YB_FASHION_HOME_L',
            param: JSON.stringify($.extend(speckParamApp, {
                TAB_ID: that.ParentLiDom.index() + 1,
                PRD_ID: ''
            }))
        }, true);
    },
    tabNav: function(code) {
        var that = this;

        this.common.codeDefault = code;// 记住最后一次的tab code

        $.ajax({
            type: 'GET',
            url: '/guang/plusstar/resources-template',
            data: {
                code: code,
                app_version: isApp
            },
            dataType: 'html',
            success: function(data) {
                var productSkns = '';

                $('.plusstar-resources').html(data);

                if (data === '') {
                    tip.show('没有更多内容了');
                    return true;
                }

                that.resInit();

                $('.plusstar-resources').find('img.lazy').lazyload();

                // 设置滚动条的位置
                if (window.localStorage) {
                    $(document).scrollTop(localStorage.getItem(code) || 0);
                }

                productSkns = $(data).find('.product-skns').val();

                if (productSkns) {
                    that.common.productSkns = productSkns.split(',');
                }

                setTimeout(function() {
                    plusstar.goodsList();
                }, 10);
            },
            error: function() {
                tip.show('网络断开连接了~');
            }
        });
    },
    resInit: function() {
        // 头部banner轮播
        if ($('.banner-swiper').find('li').size() > 1) {
            new Swiper('.banner-swiper', {
                lazyLoading: true,
                lazyLoadingInPrevNext: true,
                loop: true,
                autoplay: 3000,
                autoplayDisableOnInteraction: false,
                paginationClickable: true,
                slideElement: 'li',
                pagination: '.banner-top .pagination-inner'
            });
        }
    },
    goodsList: function() {
        var that = this,
            skn = [];

        $.ajax({
            type: 'POST',
            url: '/guang/plusstar/resources-goodsList',
            timeout: 5000,
            data: {
                productSkn: that.common.productSkns.join(','),
                app_version: isApp,
                yh_channel: that.ParentLiDom.index() + 1
            },
            dataType: 'html',
            success: function(data) {
                skn = [];

                if (data === '') {
                    return true;
                }

                $.each($(data).siblings('.good-info'), function() {
                    skn.push($(this).data('good-id'));
                });

                window.givePoint($.extend(speckParamApp, {
                    REC_POSE: RECPOSE,
                    PRD_ID: skn.join(','),
                    ORDER_CODE: '',
                    PRD_NUM: skn.length,
                    ACTION_ID: 0
                }));

                $('.plusstar-resources .goods').append(data);

                $('.plusstar-resources .goods').find('img.lazy:not([src])').lazyload();
                $('.resources .goods .page-total').remove();
            },
            error: function() {
                tip.show('网络断开连接了~');
            }
        });
    }
};

scrollFn = debounce(function() {
    if (window.localStorage) {
        localStorage.setItem(plusstar.common.codeDefault, $(this).scrollTop());
    }
}, 200);

$(function() {
    isApp = window.queryString.app_version || window.queryString.appVersion;

    if (isApp) {
        speckParamApp = {
            udid: window.queryString.udid || '',
            apt: window.queryString.client_type || '',
            sid: window.queryString.session_id || '',
        };
    }

    // 男:1,女:2,潮童:3,创意生活:4
    speckParamApp.CID = window.queryString.yh_channel || window._ChannelVary[window.cookie('_Channel')] || 1;

    if (!isApp) {
        $('.tab-nav').css({
            position: 'relative'
        });

        $footer.css({
            'max-width': '650px'
        }).before(
            '<div style="height: ' + parseInt($footer.css('height'), 0) + 'px"></div>'
        );
    }

    plusstar.init();

    // 滚动翻页
    $(window).scroll(function() {
        scrollFn();
    });
});