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
import login from "./login";
const timeZone = {
'timeZone-8': 'GMT+08:00',
'timeZone-9': 'GMT+09:00',
'timeZone5': 'GMT-08:00'
};
const offSet = new Date().getTimezoneOffset() / 60;
export default function xFetch({ url, method, data, type }) {
if (type == 'binding') {
url = `${getApp().globalData.globalUrl2}${url}`
} else {
url = `${getApp().globalData.globalUrl}${url}`
}
return new Promise((resolve, rejects) => {
dd.httpRequest({
headers: {
"Content-Type": "application/json; charset=utf-8",
'timeZone': timeZone[`timeZone${offSet}`],
'Authorization': `Bearer ${getApp().globalData.token}`,
'Accept-Language': 'zh'
},
url: url,
method: method,
dataType: "json",
data: data,
timeout: 15000,
success: function (res) {
if (res.data.code == 0) {
resolve(res);
} else {
if (res.data.code == 401) {
login();
}
alertOnce(res.data.msg || '请求异常,请重试', rejects, res);
}
},
fail: function (err) {
if (err && err.status == 401) {
login();
}
if (err.errorMsg === 'socket is closed') {
} else {
console.log(err)
alertOnce((err && err.errorMsg) || '网络异常,请重试', rejects, err);
}
}
});
});
}
let alert = false;
function alertOnce(content, rejects, err) {
if (alert == false) {
dd.alert({
content: content,
buttonText: "确定",
success: () => {
rejects({ refresh: true });
alert = false;
}
});
}
alert = true;
}