system_ajax.js
3.84 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
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);
});
};