/**
 * Created by wangmh on 2017/6/28.
 */
var app = {
    loading : function(is, position) {
        app.isLoading = is;
        toastr.clear();
        $('#loading').css('display', is ? 'block' : 'none');
        is && position && ($('#loading').addClass(position));
    },
    init : function () {
        var error = function (msg) {
            alert(msg);
        }
        var data = app.getParams(location.href)
        app.checkParams(data);
    },
    getParams : function(href){
        var args = href.split("?");
        if(args[0] == href){
            error("没有参数");
            return null;
        }
        var str = args[1];
        args = str.split("&");
        var data = {appId:"", t:-1, p:"", u:null, returnURL:"http://www.ubtob.com"};
        for(var i=0; i<args.length; i++){
            str = args[i];
            if(str.length <= 1) continue;
            var arg = str.split("=");
            data[arg[0]] = arg[1];
        }
        return data;
    },
    error : function(message, title, position) {
        toastr.clear();
        toastr.error(message, title, {
            positionClass : 'toast-' + 'top-center'
        });
        window.location.href="http://www.ubtob.com";
    },
    checkParams : function(formData){
        app.loading(true);
        $.ajax('sso/mobile/login/auto', {
            data: {
                appId:formData.appId,
                tel:formData.t,
                password:formData.p,
                enuu:formData.u,
                returnUrl:formData.returnURL
            },
            dataType: 'json',
            type: 'POST',
            success: function (msg) {
                app.loading(false);
                if(msg.success) {
                    window.location.href=msg.content.returnUrl || 'http://www.ubtob.com';
                } else if (msg.errMsg) {
                    app.error(msg.errMsg);
                }
            },
            error: function (res) {
                app.loading(false);
                app.error(res);
            }
        });
    }
};
$(document).ready(function() {
    app.init();
});