js封装ajax等函数

浏览733
     /*
* js工具类
* 使用超过两次的js函数或者对象需要抽离封装
* 2018.09.12
* */
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
//console.log(userAgent)
if (userAgent.indexOf("iPhone") == -1 && userAgent.indexOf("Android") == -1)  {
    //电脑
    //window.location.href = "/webpage/marriage/pc/index.html";
}

(function () {
    Util = {
        post: function (url, param, callBack) {
            if (callBack == undefined) {
                $.post(url, function (result) {
                    if (result.errorCode == 2) {
                    }
                    param(result)
                })
            }
            if (callBack != undefined) {
                $.post(url, param, function (result) {
                    if (result.errorCode == 2) {
                    	
                    }
                    callBack(result)
                })
            }

        },
        get: function (url, param, callBack) {
            if (callBack == undefined) {
                $.get(url, function (result) {
                    if (result.errorCode == 2) {
                    			
                    }
                    param(result)
                })
            }
            if (callBack != undefined) {
                $.get(url, param, function (result) {
                    if (result.errorCode == 2) {

                    }
                    callBack(result)
                })
            }

        },
        ajax: function (url, param, callBack) {
            $.ajax({
                url:url,
                type:"post",
                data:param,
                success:function (result) {
                    if (result.errorCode == 2) {

                    }
                callBack(result)
            }}
            )
        },
        getValueFromUrl: function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        },
        getCurrentDate: function (format, time) {
            var date = (time==undefined || time==null || time=="")?new Date():new Date(time.replace(/-/g, "/"))
            var now = new Date()
            var year = date.getFullYear()
            var month =  (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1)
            var d = (date.getDate()+1 < 10 ? '0'+(date.getDate()) : date.getDate())
            var hour = date.getHours()
            var minute = date.getMinutes()
            var second = date.getSeconds()

            if (format == "yyyy-MM-dd hh:mm:ss") {
                return year+"-"+month+"-"+d+" "+hour+":"+minute+":"+second
            }
            if (format == "yyyy-MM-dd hh:mm") {
                return year+"-"+month+"-"+d+" "+hour+":"+minute
            }
            if (format == "yyyy-MM-dd") {
                return year+"-"+month+"-"+d
            }
            if (format == "MM-dd hh:mm:ss") {
                return month+"-"+d+" "+hour+":"+minute+":"+second
            }
            if (format == "MM-dd hh:mm") {
                return month+"-"+d+" "+hour+":"+minute
            }
            if (format == "hh:mm") {
                return hour+":"+minute
            }
            if (format == "MM-dd") {
                return month+"-"+d
            }
        },
        getChatDate: function (date) {
            date = (date==undefined || date==null || date=="")?new Date():new Date(date.replace(/-/g, "/"))
            var now = new Date()

            var year = date.getFullYear()
            var m =  (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1)
            var d = (date.getDate()+1 < 10 ? '0'+(date.getDate()) : date.getDate())
            var hour = date.getHours()
            var minute = date.getMinutes()
            var second = date.getSeconds()

            //不同年
            if (now.getFullYear() != date.getFullYear()) {
                return  year + "-" + m + "-" + d + "  " + hour + ":" + minute + ":" + second;
            }

            if (now.getMonth() != date.getMonth()) {
                return  m + "-" + d + "  " + hour + ":" + minute + ":" + second;
            }

            if (now.getMonth() == date.getMonth()) {
                //console.log("月份相等!")

                if (now.getDate() != date.getDate()) {
                    return m + "-" + d + "  " + hour + ":" + minute + ":" + second;
                }

                if (now.getDate() == date.getDate()) {
                    //console.log("今天!")
                    return  hour + ":" + minute + ":" + second;
                }
                if (now.getDate() - date.getDate() == 2) {
                    //console.log("昨天!")
                    return  "前天   " + hour + ":" + minute + ":" + second;
                }
                if (now.getDate() - date.getDate() == 1) {
                    //console.log("昨天!")
                    return  "昨天   " + hour + ":" + minute + ":" + second;
                }
            }

            return  year + "-" + m + "-" + d + "  " + hour + ":" + minute + ":" + second;
        },
        escapeHTML: function(a){
            a = "" + a;
            return a.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """).replace(/'/g, "&apos;");;
        },
        unescapeHTML: function(a){
            a = "" + a;
            return a.replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&").replace(/"/g, '"').replace(/&apos;/g, "'");
        },
       getWebSocketPath: function () {return "ws://127.0.0.1/api/websocket/"},
       getOpenIdPath: function (){return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=&redirect_uri=http%3a%2f%2f"},
       goPage: function (pagePath) {window.location.href = pagePath},
    }
}())

  • 暂无任何回答