http.js
3.15 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import axios from "axios";
import { Dialog } from "vant";
import store from "@/store";
import wx from "wx";
// 测试 https://eshield.test.glsx.com.cn
// 开发 https://eshield.dev.glsx.com.cn
// 线上
/* eslint-disable */
const BASE_URL = process.env.NODE_ENV == "development" ? "/api" : location.origin;
const instance = axios.create({
// 创建axios实例,在这里可以设置请求的默认配置
timeout: 10 * 60 * 1000, // 设置超时时间60s
retry: 3,
retryDelay: 1000,
});
// 统一设置post请求头
instance.defaults.headers.post["Content-Type"] = "application/json";
/** 添加请求拦截器 **/
instance.interceptors.request.use(
(config) => {
/* eslint-disable */
if (
config.url.indexOf("/miniappserver/monitor/search") <= -1 ||
(config.url.indexOf("/miniappserver/monitor/search") > -1 &&
config.params.pageNumber == 1)) {
// 下拉加载不显示loading
store.commit("showLoading");
}
config.headers["Authorization"] = localStorage.getItem("token") || "";
return config;
},
(error) => {
store.commit("hideLoading");
console.log("请求错误,原因:", error);
return Promise.reject(error);
}
);
/** 添加响应拦截器 **/
instance.interceptors.response.use(
(response) => {
store.commit("hideLoading");
const serverCode = response.data.code,
token = response.headers["authorization"];
if (token || token === "") {
localStorage.setItem("token", token);
}
if (serverCode === 200) {
if (response.status) {
return Promise.resolve(response);
}
return Promise.resolve(response.data);
} else {
Dialog.confirm({
message: response.data.message,
showCancelButton: false
}).then(() => {
if (serverCode == 603 || serverCode == 604) { // 登录失效
wx.miniProgram.navigateTo({
url: `../../pages/login/index?isLoginOut=true`,
});
}
});
return Promise.reject(response.data.message);
}
},
(error) => {
store.commit("hideLoading");
const errRes = error.response;
if (errRes) {
Dialog({ message: "请求失败" });
return Promise.reject(error);
} else {
Dialog({ message: "请求失败" });
return Promise.reject(new Error("请求失败"));
}
}
);
const getBaseUrl = () => {
return new Promise((resolve) => {
resolve(BASE_URL);
});
};
const HTTP_POST = (baseUrl, options) => {
instance({
url: baseUrl + options.url,
method: "post",
data: JSON.stringify(options.data),
})
.then((res) => {
options.success(res.data);
})
.catch((err) => {
options.error(err);
});
};
const HTTP_GET = (baseUrl, options) => {
instance({
url: baseUrl + options.url,
method: "get",
params: options.data,
})
.then((res) => {
options.success(res.data);
})
.catch((err) => {
options.error(err);
});
};
export default class HTTP {
axiosPost(options) {
getBaseUrl().then((baseUrl) => {
HTTP_POST(baseUrl, options);
});
}
axiosGet(options) {
getBaseUrl().then((baseUrl) => {
HTTP_GET(baseUrl, options);
});
}
}