system_ajax.js 4.71 KB
let sys_header = {
    token: window.appConfig.token,
}
/**
 * thisInfo: this api需要支持跨越
 * config:具体参数   
 *       isUrlALL 为true  读取完整的地址  wms url默认是包含mobile 值的
 *       isSuccessBefore:默认值 true  
 *       isHanderAjaxSuccessActionLoad 默认值 false  head组件头部 传入 true
 *       allowConcurrentRequests:属性随便设置一个值,属性不存在 不检查并发请求(接口请求频繁时,需要添加此属性)
 * headers:WMS组是true,本地或者刘甫组 设置false  和跨域服务有关系
 * callBackFn:回调函数
 */
let ajaxAllowRequestsConf = {};
String.prototype.ajax = function (thisInfo, config, callBackFn = null) {
    // 检查是否允许并发请求
    let allowRequestsFlag = typeof config.allowConcurrentRequests !== "undefined"
    if (allowRequestsFlag) {
        ajaxAllowRequestsConf[config.urlSuffix] = ajaxAllowRequestsConf[config.urlSuffix] || false;
        if (ajaxAllowRequestsConf[config.urlSuffix]) {
            // console.log("上一个请求尚未完成,跳过本次请求。");
            return;
        }
        ajaxAllowRequestsConf[config.urlSuffix] = true;
    }
    if (typeof thisInfo.$axios == "undefined") {
        alert("ajax 方法 当前this 实例不存在$axios属性!");
        console.trace();
        return false;
    }
    //默认参数
    var opt = {
        headers: false,
        logTitle: "服务器api接口数据Before",
        isUrlALL: false,
        isSuccessBefore: true,
        type: "get",
        isLoad: false,

        isHanderAjaxSuccessActionLoad: false
    };
    Object.assign(opt, config)

    let url = opt.urlSuffix;
    if (opt.isUrlALL && url.indexOf("http") == -1) {
        alert("ajax 方法 参数【isUrlALL】为true 地址必须是全路径http开头,当前URL:" + url);
        return false;
    }
    if (!opt.isUrlALL) url = "".getUrlPrefix(thisInfo) + opt.urlSuffix;
    if (url == null) {
        alert("ajax 方法 url 不能为空");
        console.trace();
        return false;
    }
    if (url.indexOf("undefined") > -1) {
        alert("ajax url 地址错误,路径中存在undefined。" + url);
        return false;
    }
    if (!thisInfo.sysData) {
        alert(`ajax sysData 配置错误:不能是空!`);
        return false;
    }

    if (url.split("//").length > 2) {
        alert(`ajax url配置错误:存在连续个//【${url}】`);
        return false;
    }


    var uloading = null;
    if (opt.isLoad) {
        uloading = "".uLoading(thisInfo)
    }
    let tempTitle = opt.logTitle;
    var instance = null;
    if (opt.type == "post") {
        instance = thisInfo.$axios
            .post(url, opt.data || {}, {
                headers: opt.headers ? sys_header : null,
                timeout: 10000,
                Referer: opt.Referer
            })
    } else {
        instance = thisInfo.$axios
            .get(url, {
                headers: opt.headers ? sys_header : null,
                timeout: 10000,
                Referer: opt.Referer
            });
    }
    instance.then(res => {
        if (allowRequestsFlag) ajaxAllowRequestsConf[config.urlSuffix] = false; // 请求结束,重置状态
        if (uloading != null) uloading.close()
        if (opt.isSuccessBefore) {
            var isSuccessBefore = "".ajaxSuccessBefore(res, (opt.logTitle = tempTitle));
            if (isSuccessBefore) {
                //初始化页面
                if (opt.initCallBackFn != null) opt.initCallBackFn()
                return false;
            }
        }
        //动态赋值 api返回的数据在data或者result或者data.result,如果有其他情况需要单独在加
        let data = res.data.data;
        if (typeof data == "undefined" || data == null) data = res.result
        if (typeof data == "undefined" || data == null) data = res.data.result
        if (typeof data == "undefined" || data == null) data = res.data.Result
        if (typeof data == "undefined" || data == null) data = res.data
        if ("".typeX(data) == "array") {
            thisInfo.sysData = data
        } else {
            let dataKeys = Object.keys(data);
            for (const key in dataKeys) {
                if (!Object.hasOwnProperty.call(dataKeys, key)) continue
                let keys = dataKeys[key];
                let val = data[dataKeys[key]]
                thisInfo.sysData[keys] = val
            }
        }
        if (opt.isHanderAjaxSuccessActionLoad) ''.ajaxSuccessActionLoad(res)
        if (callBackFn != null) callBackFn(res);
    })
        .catch(err => {
            if (allowRequestsFlag) ajaxAllowRequestsConf[config.urlSuffix] = false;
            if (uloading != null) uloading.close()
            "".ajaxError(err, url);
        });
};