popupLogin.js 3.49 KB
var PassportPopupLogin = {
    settings: {
        callbackFn: null
    },
    init: function () {
        var self = this;
        var intInterval = null;
        var oldHashVal = "";
        if (window.postMessage && ((typeof document.documentMode === 'undefined') || document.documentMode > 8)) {
            window.onmessage = function (e) {
                self.switchCenter(e.data);
            };
        } else if (('onhashchange' in window) && ((typeof document.documentMode === 'undefined') || document.documentMode == 8)) {
            // 浏览器支持onhashchange事件
            window.onhashchange = function () {
                self.switchCenter(location.hash.substring(1)); // 对应新的hash执行的操作函数
            };
        } else {
            // 不支持则用定时器检测的办法
            intInterval = setInterval(function () {
                var ischanged = isHashChanged();  // 检测hash值或其中某一段是否更改的函数
                if (ischanged) {
                    intInterval = clearInterval(intInterval);
                    self.switchCenter(location.hash.substring(1));  // 对应新的hash执行的操作函数
                }
            }, 300);
        }
        var isHashChanged = function () {
            return (location.hash != "#" && oldHashVal != location.hash);
        };
        var html = '<div class="login-pop"> <a href="https://passport.nong12.com/uc/popup/login.aspx?referUrl=' + location.href + '" id="s-login-pop"></a></div>';
        $("body").append(html);

        $("#s-login-pop").fancybox({
            width:'380px',
						height:'310px',
            type: "iframe",
            fitToView: false,
            iframe: {
                preload: false,
                scrolling: 'no'
            },
            scrolling: 'visible'
        });
        return self;
    },
    tbClose: function () {
        0 != $('.n-popup').length && jQuery(".n-popup").remove();
    },
    login: function () {
        var self = this;
        self.tbClose();
        $("#s-login-pop").click();
    },
    regist: function () {
        var self = this;
        self.tbClose(),
            window.location = "https://passport.nong12.com/regedit/regedit.aspx";
    },
    lostPassword: function () {
        var self = this;
        self.tbClose(),
            window.location = "https://passport.nong12.com/forgot/forgot.aspx";
    },
    switchCenter: function (val) {
        var self = this;
        self._clear();
        switch (val) {
            case 'login_success':
                self.callFunc();
                break;
            case 'passport_register':
                self.regist();
                break;
            case 'passpor_lostPwd':
                self.lostPassword();
                break;
            default :
                break;
        }
    },
    callFunc: function () {
        var self = this;
        self.tbClose();
        if (self.settings.callbackFn && $.isFunction(self.settings.callbackFn)) {
            self.settings.callbackFn();
        }
    },
    _clear: function () {
        location.hash = "";
    },
    checkLogin : function(fn){
			var flag = false;
			jQuery.ajax({
				type : "POST",
				url : "http://www.nong12.com/common/checkLogin.html",
				dataType : "json",
				async : false,
				xhrFields: {
      		withCredentials: true
   			},
				success : function(data) {
					if(fn && typeof(fn) == 'function'){
						fn(data);
					}
					if(data.code == "000"){
						flag = true;
					}
				}
			});
			return flag;
		}
};