system_ajax.js 3.84 KB
let tokenValue = "".cookie("Token");
let sys_header = {
    token: tokenValue !== "" ? tokenValue : window.appConfig.token,
};
/**
 * thisInfo: this api需要支持跨越
 * config:具体参数   
 * isUrlALL 为true  读取完整的地址  wms url默认是包含mobile 值的
 * isSuccessBefore:默认值 true  
 * isHanderAjaxSuccessActionLoad 默认值 false  head组件头部 传入 true
 * headers:WMS组是true,本地或者刘甫组 设置false  和跨域服务有关系
 * callBackFn:回调函数
 */
String.prototype.ajax = function (thisInfo, config, callBackFn = null) {
    if (typeof thisInfo.$axios == "undefined") {
        alert("ajax 方法 当前this 实例不存在$axios属性!");
        console.trace();
        return false;
    }
    //默认参数
    var opt = {
        headers: true,
        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 (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 ("".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 (uloading != null) uloading.close()
            "".ajaxError(err, url);
        });
};