footer.js 4.8 KB
/**
 * 尾部
 * @author: wangqing<robin.wang@yoho.cn>
 * @date: 2015/12/01
 */

var $ = require('yoho-jquery'),
    lazyLoad = require('yoho-jquery-lazyload');

var $returnTop = $('.return-top');
var $qrcodeHoverBox = $('.qrcode-hover-box');
var $rightFloatingLayer = $('.right-floating-layer');

lazyLoad($('img.lazy'));

/**
 * 订阅
 * @return {[type]} [description]
 */
function actionSubscription() {
    var $subscriberBox = $('#subscriber-box'),
        $subscriberBtn = $('#subscriber-btn'),
        emailReg = /^[.\-_a-zA-Z0-9]+@[\-_a-zA-Z0-9]+\.[a-zA-Z0-9]/;

    var iconCode = {
        mail: '&#xe601;',
        tick: '&#xe605;'
    };

    $subscriberBox.focus(function() {
        $(this).val('').css('color', '');
        $subscriberBtn.removeClass('done').html(iconCode.mail);
    });

    $subscriberBtn.click(function() {
        var email = $.trim($subscriberBox.val());

        if (email !== '' && emailReg.test(email)) {
            try {
                $.ajax({
                    url: '//new.yohobuy.com/common/emailsubscriber',
                    dataType: 'jsonp',
                    data: {
                        email: email,
                        tmp: Math.random(),
                        uid: window.getUid()
                    },
                    success: function(data) {
                        if (data.data.result === 1) {
                            $subscriberBox.val('已订阅到:' + email);
                            $subscriberBtn.addClass('done').html(iconCode.tick);
                        } else {
                            $subscriberBox.css('color', 'red');
                        }
                    }
                });
            } catch (e) {
                console.log(e.message);
            }
        } else {
            $subscriberBox.css('color', 'red');
        }
    });
}

function actionhomeFootChange() {
    var $vote = $('.vote'),
        $feedBackPage = $('#feed-back-page'),
        count = $vote.children('li').length;

    // 意见反馈
    $feedBackPage.on('click', 'span', function() {
        var $this = $(this);

        if ($this.hasClass('cur')) {
            return;
        }

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

        $vote.children().not('.hide').addClass('hide')
            .end()
            .eq($this.index()).removeClass('hide');
    });

    $vote.on('click', '.feed-back-btn', function() {
        var $this = $(this),
            $li = $this.closest('li'),
            index = $li.index(),
            _solution = [];

        var _answer = $li.find('.feedback-answer').val(),
            _feedback = $li.find('.feedback-id').val(),
            _question = $li.find('.question-id').val();

        $li.find(':checked').each(function() {
            _solution.push($(this).val());
        });

        $.ajax({
            url: '//new.yohobuy.com/common/suggestfeedback',
            dataType: 'jsonp',
            data: {
                feedback_id: _feedback || 0,
                question_id: _question || 0,
                answer: _answer || '',
                solution: _solution.join(',')
            },
            success: function(data) {
                var next = index + 1;

                if (~~data.data.result === 1) {
                    if (index === count - 1) {
                        alert('感谢您的参与!'); // eslint-disable-line
                        return;
                    }

                    $li.addClass('hide');

                    $vote.children(':eq(' + (index + 1) + ')').removeClass('hide');
                    $feedBackPage.children('.cur').removeClass('cur');
                    $feedBackPage.children(':eq(' + next + ')').addClass('cur');
                }
            }
        });
    });
}

function rePosReturn() {
    if ($(window).height() > $(document).height()) {
        $rightFloatingLayer.addClass('hide');
    }

    // 只在窗口高度大于文档高度的时候,隐藏返回顶部
    // else {
    //     $returnTop.removeClass('hide');
    // }
}

// 返回顶部
$returnTop.click(function() {
    $('html,body').animate({
        scrollTop: 0
    }, 500);
});

$qrcodeHoverBox.mouseenter(function() {
    _hmt.push(['_trackEvent', '二维码', '右侧悬浮', '', '']);
}).click(function() {
    _hmt.push(['_trackEvent', '二维码', '右侧悬浮', '', '']);
});

$(window).scroll(function() {
    if ($(window).scrollTop() === 0) {
        $rightFloatingLayer.addClass('hide');
    } else {
        $rightFloatingLayer.removeClass('hide');
    }
});

// 如果初始是隐藏的,监听图片加载,重新确定return-top的高度
if ($rightFloatingLayer.hasClass('hide')) {
    $('img').load(rePosReturn);
}

// 初始化
actionSubscription();
actionhomeFootChange();

// 暴露给有可能通AJAX改变内容的页面去用
window.rePosReturnTop = rePosReturn;