/** 
    @ Name : Careff Javascript Library 
    @ Author : 
    @ Date : 2017-5-1
    @ Email : 
    @ Version : 1.0 
    @ Update : 
    功能： 开始建立自己的JS类库，需要按照功能进行分类；
     */
(function () {
    if (!window.ushttp) {
        window.ushttp = {};
    };
    window.ushttp = {
        NAME: "ushttp",
        VERSION: '1.0',
        DATE: '2017-12-16',
        INDEX: 10,
        BaseURLH5: "/h5/usajax.aspx",
        BaseURL: "/admin/usajax.aspx",
        URLAdd: function (url) {
            var timestamp = Date.parse(new Date());
            timestamp = timestamp / 1000;
            if (url.indexOf("?") > 0) {
                url += "&";
            }
            else {
                url += "?";
            }
            url += "jsdate=" + timestamp;
            url = escape(url);
            //alert(url)
            return url;
        },
        URLCheckToken: function (res) {
            //5869
            //console.debug(res);
            if (res.code == 5869) {
                usbasic.errorMsg(res.msg);
                setTimeout(function () {
                    window.location.href = "/";
                }, 3000);
            }
            //console.debug(msg);
        },
        UsAjaxJSON: function (url, data, isload, callback) {

            url = usbasic.UsURLAddDate(url);
            //alert(url);
            if (isload) {
                usbasic.UsOpenLoad();
            }

            $.ajax({
                type: "POST",
                url: url,
                timeout: 600000, //超时时间设置，单位毫秒
                contentType: "application/json; charset=utf-8",
                data: data,
                dataType: "json",
                async: true,
                success: function (message) {
                    if (isload) {
                        usbasic.UsCloseLoad();
                    }
                    ushttp.URLCheckToken(message);
                    callback(message);
                },
                error: function (message) {
                    if (isload) {
                        usbasic.UsCloseLoad();
                    }
                },
                beforeSend: function (XMLHttpRequest) {
                    this; // 调用本次AJAX请求时传递的options参数
                },
                complete: function (XMLHttpRequest, textStatus) {
                    this; // 调用本次AJAX请求时传递的options参数
                }
            });
        },
        UsAjaxHTML: function (url, data, isload, callback) {
            //alert(data);
            url = usbasic.UsURLAddDate(url);
            if (isload) {
                usbasic.UsOpenLoad();
            }
            $.ajax({
                type: "POST",
                url: url,
                timeout: 600000, //超时时间设置，单位毫秒
                contentType: "application/html; charset=utf-8",
                data: data,
                dataType: "html",
                async: true,
                success: function (message) {
                    if (isload) {
                        usbasic.UsCloseLoad();
                    }
                    callback(message);
                },
                error: function (message) {
                    console.log(message);
                    if (isload) {
                        usbasic.UsCloseLoad();
                    }
                },
                beforeSend: function (XMLHttpRequest) {
                    this; // 调用本次AJAX请求时传递的options参数
                },
                complete: function (XMLHttpRequest, textStatus) {
                    this; // 调用本次AJAX请求时传递的options参数
                }


            });
        },
        UsAjaxJSONV2: function (url, data, isload,basync, callback) {
            //alert(data);
            url = usbasic.UsURLAddDate(url);
            if (isload) {
                usbasic.UsOpenLoad();
            }
            $.ajax({
                type: "POST",
                url: url,
                timeout: 600000, //超时时间设置，单位毫秒
                contentType: "application/html; charset=utf-8",
                data: data,
                dataType: "json",
                async: basync,
                success: function (message) {
                    if (isload) {
                        usbasic.UsCloseLoad();
                    }
                    ushttp.URLCheckToken(message);
                    callback(message);
                },
                error: function (message) {
                    console.log(message);
                    if (isload) {
                        usbasic.UsCloseLoad();
                    }
                },
                beforeSend: function (XMLHttpRequest) {
                    this; // 调用本次AJAX请求时传递的options参数
                },
                complete: function (XMLHttpRequest, textStatus) {
                    this; // 调用本次AJAX请求时传递的options参数
                }


            });
        },
        UsAjaxHTMLGet: function (url, data, isload, callback) {
            //alert(data);
            url = usbasic.UsURLAddDate(url);
            if (isload) {
                usbasic.UsOpenLoad();
            }
            $.ajax({
                type: "get",
                url: url + "&para=" + data,
                contentType: "application/html; charset=utf-8",
                data: data,
                timeout: 31000,
                dataType: "jsonp",
                async: true,
                crossDomain: true,
                cache: false,
                jsonpCallback: "jsonpCallback",
                success: function (message) {
                    callback(message);
                    if (isload) {
                        usbasic.UsCloseLoad();
                    }
                    
                },
                error: function (message) {
                    console.log(message);
                    if (isload) {
                        usbasic.UsCloseLoad();
                    }
                },
                beforeSend: function (XMLHttpRequest) {
                    this; // 调用本次AJAX请求时传递的options参数
                },
                complete: function (XMLHttpRequest, textStatus) {
                    this; // 调用本次AJAX请求时传递的options参数
                }


            });
        }
    };

})();