工具类 ajax

/*CoreUtil*/
/*工具类,类似java静态工具类*/
var CoreUtil = (function() {
    var coreUtil = {};
    /*ajax请求*/
    coreUtil.sendAjax = function(url, params, ft, method, headers, noAuthorityFt, async, contentType) {
        var roleSaveLoading = top.layer.msg('数据提交中,请稍候', { icon: 16, time: false, shade: 0.8 });
        layui.jquery.ajax({
            url: url,
            cache: false,
            async: async == undefined ? true : async,
            data: params,
            type: method == undefined ? "POST" : method,
            contentType: contentType == undefined ? 'application/json; charset=UTF-8' : contentType,
            dataType: "json",
            beforeSend: function(request) {
                if (headers == undefined) {} else if (headers) {
                    request.setRequestHeader("accessToken", CoreUtil.getData("access_token"));
                    request.setRequestHeader("refreshToken", CoreUtil.getData("refresh_token"));
                } else {
                    request.setRequestHeader("accessToken", CoreUtil.getData("access_token"));
                }
            },
            success: function(res) {
                top.layer.close(roleSaveLoading);
                if (typeof ft == "function") {
                    if (res.code == 200) {
                        if (ft != null && ft != undefined) {
                            ft(res);
                        }
                    } else if (res.code == 4010001 || res.code == 4010002 || res.code == 4010003) {
                        //凭证过期重新登录
                        layer.msg("凭证过期请重新登录")
                        top.window.location.href = "/view/login"
                    } else if (res.code == 4010004) {
                        // 根据后端提示刷新token,记录要重复刷新的参数
                        var reUrl = url;
                        var reParams = params;
                        var reFt = ft;
                        var reMethod = method;
                        var reHeaders = headers;
                        var reContentType = contentType;
                        var reAsync = async;
                        var reNoAuthorityFt = noAuthorityFt;
                        /*刷新token  然后存入缓存*/
                        CoreUtil.sendAjax("/api/user/token", null, function(res) {
                            if (res.code == 200) {
                                CoreUtil.setData("access_token", res.data);
                                top.window.location.href = "/view/home";
                                // setTimeout(function () {
                                //   /*刷新成功后继续重复请求*/
                                //   CoreUtil.sendAjax(reUrl, reParams, reFt, reMethod, reHeaders, reNoAuthorityFt, reContentType, reAsync);
                                // }, 1000);
                            } else {
                                layer.msg("凭证过期请重新登录");
                                top.window.location.href = "/view/login"
                            }
                        }, "GET", true)
                    } else if (res.code == 4030001) {
                        if (ft != null && ft != undefined) {
                            noAuthorityFt(res);
                        }
                    } else {
                        top.layer.msg(res.msg)
                    }
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                top.layer.close(roleSaveLoading);
                layer.msg("服务器好像除了点问题!请稍后试试");
            }
        });
    };
    /*存入本地缓存*/
    coreUtil.setData = function(key, value) {
        layui.sessionData('LocalData', {
            key: key,
            value: value
        })
    };
    /*从本地缓存拿数据*/
    coreUtil.getData = function(key) {
        var localData = layui.sessionData('LocalData');
        return localData[key];
    };
    /*格式化时间格式*/
    coreUtil.formattime = function(val) {
        if (val == null || val == undefined) {
            return "";
        }
        var date = new Date(val);
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        month = month > 9 ? month : ('0' + month);
        var day = date.getDate();
        day = day > 9 ? day : ('0' + day);
        var hh = date.getHours();
        hh = hh > 9 ? hh : ('0' + hh);
        var mm = date.getMinutes();
        mm = mm > 9 ? mm : ('0' + mm);
        var ss = date.getSeconds();
        ss = ss > 9 ? ss : ('0' + ss);
        var time = year + '-' + month + '-' + day + ' ' + hh + ':' + mm + ':' + ss;
        return time;
    };
    return coreUtil;
})(CoreUtil, window);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值