码迷,mamicode.com
首页 > Web开发 > 详细

js 工具类

时间:2017-10-23 01:00:29      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:params   bsp   回调   fat   debug   span   gpo   caller   string   

Log

/**
 * Created by e2670 on 2017/10/6.
 */
/**
 * Log utils
 */
var FLog = {
    isLogPos: true,
    /**
     * trace log
     * @param params
     */
    t: function (params) {
        if (FLog.isLogPos) {
            console.log(FLog.getLogLine());
        }
        console.trace(params);
    },
    /**
     * debug log
     * @param params
     */
    d: function (params) {
        if (FLog.isLogPos) {
            console.log(FLog.getLogLine());
        }
        console.debug(params);
    },
    /**
     * info log
     * @param params
     */
    i: function (params) {
        if (FLog.isLogPos) {
            console.log(FLog.getLogLine());
        }
        console.info(params);
    },
    /**
     * warn log
     * @param params
     */
    w: function (params) {
        if (FLog.isLogPos) {
            console.log(FLog.getLogLine());
        }
        console.warn(params);
    },
    /**
     * error log
     * @param params
     */
    e: function (params) {
        if (FLog.isLogPos) {
            console.log(FLog.getLogLine());
        }
        console.error(params);
    },
    /**
     * fatal log
     * @param params
     */
    f: function (params) {
        if (FLog.isLogPos) {
            console.log(FLog.getLogLine());
        }
        console.fatal(params);
    },
    /**
     * trace log
     * @param params
     */
    trace: function (params) {
        if (FLog.isLogPos) {
            console.log(FLog.getLogLine());
        }
        console.trace(params);
    },
    /**
     * debug log
     * @param params
     */
    debug: function (params) {
        if (FLog.isLogPos) {
            console.log(FLog.getLogLine());
        }
        console.debug(params);
    },
    /**
     * info log
     * @param params
     */
    info: function (params) {
        if (FLog.isLogPos) {
            console.log(FLog.getLogLine());
        }
        console.info(params);
    },
    /**
     * warn log
     * @param params
     */
    warn: function (params) {
        if (FLog.isLogPos) {
            console.log(FLog.getLogLine());
        }
        console.warn(params);
    },
    /**
     * error log
     * @param params
     */
    error: function (params) {
        if (FLog.isLogPos) {
            console.log(FLog.getLogLine());
        }
        console.error(params);
    },
    /**
     * fatal log
     * @param params
     */
    fatal: function (params) {
        if (FLog.isLogPos) {
            console.log(FLog.getLogLine());
        }
        console.fatal(params);
    },
    getLogLine: function () {
        try {
            Zzz.Zzz;
        } catch (ex) {
            var exMsg = ex.stack.split("\n");
            // var msg = exMsg[3].split("(");
            // return "(" + msg[msg.length - 1];
            return exMsg[3];
        }
    }
};

HTTP

/**
 * Created by e2670 on 2017/10/21.
 * Http请求工具类
 */

var FwHttp = {
    /**
     * 基于XMLHttpRequest封装
     */
    /**
     * get请求异步获取
     * @param url
     * @param callSuc
     * @param callErr
     * exam: FwHttp.getAsync(url,callbackSuccess,callbackError);
     */
    getAsync:function (url,callSuc,callErr) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", url, true);
        xmlhttp.send();
        // 异步方法回调
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                callSuc(xmlhttp.responseText);
            }else {
                callErr(xmlhttp.responseText);
            }
        }
    },
    /**
     * get请求同步获取
     * @param url
     * exam:var res = FwHttp.getSync(url);
     */
    getSync:function (url) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", url, false);
        xmlhttp.send();
        return xmlhttp.responseText;
    },
    /**
     * post请求异步提交
     * @param url
     * @param dataStr
     * @param contentType
     * @param callSuc
     * @param callErr
     */
    postAsync:function (url,dataStr,contentType,callSuc,callErr) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("POST", commitUrl, true);
        xmlhttp.setRequestHeader("Content-type", contentType);
        xmlhttp.send(dataStr);
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                callSuc(xmlhttp.responseText);
            }else {
                callErr(xmlhttp.responseText);
            }
        }
    },
    /**
     * post请求同步提交
     * @param url
     * @param dataStr
     * @param contentType
     * @returns {string}
     */
    postSync:function (url,dataStr,contentType) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("POST", commitUrl, false);
        xmlhttp.setRequestHeader("Content-type", contentType);
        xmlhttp.send(dataStr);
        return xmlhttp.responseText;
    },
    /**
     * post请求异步提交
     * @param url
     * @param dataStr
     * @param callSuc
     * @param callErr
     * FwHttp.postJsonAsync(url,jsonStr,callbackSuccess,callbackError);
     */
    postJsonAsync:function (url,dataStr,callSuc,callErr) {
        FwHttp.postAsync(url,dataStr,"application/json",callSuc,callErr);
    },
    /**
     * post请求同步提交
     * @param url
     * @param dataStr
     * @returns {string}
     * exam: var res = FwHttp.postJsonSync(url,jsonStr);
     */
    postJsonSync:function (url,dataStr) {
        return FwHttp.postSync(url,dataStr,"application/json");
    }
};

 

js 工具类

标签:params   bsp   回调   fat   debug   span   gpo   caller   string   

原文地址:http://www.cnblogs.com/zhen-android/p/7712719.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!