system_ajax.js
4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
let sys_header = {
token: window.appConfig.token,
}
/**
* thisInfo: this api需要支持跨越
* config:
* allowConcurrentRequests:属性随便设置一个值,属性不存在 不检查并发请求(接口请求频繁时,需要添加此属性)
* callBackFn:回调函数
* 备注:最新方法改造 调用者页面不需要什么变量绑定,如果是多层级只需要申明第一级
* 比如:sysData.Quarter["xxx"],data只需要申明Quarter:sysData: {Quarter:{} }
*/
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, //WMS组是true,本地或者刘甫组 设置false和跨域服务Token有关系
logTitle: "服务器api接口数据Before",
isUrlALL: false, // isUrlALL为true完整的地址由调用者提供,false 内置公共方法拼接
isSuccessBefore: true,
type: "get",
timeout:10000, //超时时间
isLoad: false, //是否开启转圈圈 支持多个请求转圈圈,最后一个成功后才会关闭
isHanderAjaxSuccessActionLoad: false //更新头部时间并刷新页面,默认当前页面只要有一个设置true即可
};
Object.assign(opt, config)
if (!thisInfo.sysData) {
alert(`ajax sysData 配置错误:不能是空!`);
return false;
}
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 (url.split("//").length > 2) {
alert(`ajax url配置错误:存在连续个//【${url}】`);
return false;
}
if (opt.isLoad) ''.uLoading(thisInfo, true); // 开启计数
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: opt.timeout,
Referer: opt.Referer
})
} else {
instance = thisInfo.$axios
.get(url, {
headers: opt.headers ? sys_header : null,
timeout: opt.timeout,
Referer: opt.Referer
});
}
instance.then(res => {
if (allowRequestsFlag) ajaxAllowRequestsConf[config.urlSuffix] = false; // 请求结束,重置状态
if (opt.isLoad) ''.uLoading(thisInfo, false); // 关闭计数
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 {
//整体替换会触发vue 视图更新
thisInfo.sysData = { ...thisInfo.sysData, ...data };
}
if (opt.isHanderAjaxSuccessActionLoad) ''.ajaxSuccessActionLoad(res)
if (callBackFn != null) callBackFn(res);
}).catch(err => {
if (allowRequestsFlag) ajaxAllowRequestsConf[config.urlSuffix] = false;
if (opt.isLoad) ''.uLoading(thisInfo, false);
"".ajaxError(err, url);
});
};