1
|
1
|
import base from "@/config/baseUrl";
|
2
|
2
|
import store from "@/store";
|
3
|
|
-import {
|
4
|
|
- judgeLogin
|
5
|
|
-} from "@/config/login";
|
|
3
|
+import { judgeLogin } from "@/config/login";
|
6
|
4
|
|
7
|
5
|
// 初始化请求配置
|
8
|
6
|
uni.$u.http.setConfig((config) => {
|
9
|
|
- const token =
|
10
|
|
- store.state.userInfo.isToken ||
|
11
|
|
- uni.getStorageSync("userInfo").isToken ||
|
12
|
|
- undefined;
|
13
|
|
- // #ifdef H5
|
14
|
|
- window.sessionStorage.getItem("userInfo").isToken;
|
15
|
|
- // #endif
|
16
|
|
- /* config 为默认全局配置*/
|
17
|
|
- // const getConfiguration = uni.getStorageSync('getConfiguration').isConfiguration
|
18
|
|
- // console.log('getConfiguration', getConfiguration);
|
19
|
|
- // config.baseURL = getConfiguration == true ? base.baseDrawioUrl : base.baseUrl /* 根域名 */
|
20
|
|
- config.baseURL = base.baseUrl /* 根域名 */
|
21
|
|
- config.header = {
|
22
|
|
- "Content-Type": "application/json",
|
23
|
|
- 'X-Authorization': "Bearer " + token,
|
24
|
|
- };
|
25
|
|
- config.custom = {
|
26
|
|
- load: true, //是否显示加载动画
|
27
|
|
- isFactory: true, //true:返回的数据成功只返回data false:返回response
|
28
|
|
- catch: true, //默认数据返回不成功进入catch返回
|
29
|
|
- auth: true, //token
|
30
|
|
- };
|
31
|
|
- return config;
|
|
7
|
+ const token =
|
|
8
|
+ store.state.userInfo.isToken ||
|
|
9
|
+ uni.getStorageSync("userInfo").isToken ||
|
|
10
|
+ undefined;
|
|
11
|
+ // #ifdef H5
|
|
12
|
+ window.sessionStorage.getItem("userInfo").isToken;
|
|
13
|
+ // #endif
|
|
14
|
+ /* config 为默认全局配置*/
|
|
15
|
+ // const getConfiguration = uni.getStorageSync('getConfiguration').isConfiguration
|
|
16
|
+ // console.log('getConfiguration', getConfiguration);
|
|
17
|
+ // config.baseURL = getConfiguration == true ? base.baseDrawioUrl : base.baseUrl /* 根域名 */
|
|
18
|
+ config.baseURL = base.baseUrl; /* 根域名 */
|
|
19
|
+ config.header = {
|
|
20
|
+ "Content-Type": "application/json",
|
|
21
|
+ "X-Authorization": "Bearer " + token,
|
|
22
|
+ };
|
|
23
|
+ config.custom = {
|
|
24
|
+ load: true, //是否显示加载动画
|
|
25
|
+ isFactory: true, //true:返回的数据成功只返回data false:返回response
|
|
26
|
+ catch: true, //默认数据返回不成功进入catch返回
|
|
27
|
+ auth: true, //token
|
|
28
|
+ };
|
|
29
|
+ return config;
|
32
|
30
|
});
|
33
|
31
|
|
34
|
32
|
// 请求拦截
|
35
|
33
|
uni.$u.http.interceptors.request.use(
|
36
|
|
- (config) => {
|
37
|
|
- if (store.state.userInfo.isThirdLogin) {
|
38
|
|
- uni.switchTab({
|
39
|
|
- url: "../pages/personal/personal",
|
40
|
|
- });
|
41
|
|
- }
|
42
|
|
- // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
|
43
|
|
- config.data = config.data || {};
|
44
|
|
- // 根据custom参数中配置的是否需要token,添加对应的请求头
|
45
|
|
- const getConfiguration = uni.getStorageSync('getConfiguration').isConfiguration
|
46
|
|
- if (config?.custom?.auth) {
|
47
|
|
- config.header['X-Authorization'] =
|
48
|
|
- "Bearer " + store.state.userInfo.isToken ||
|
49
|
|
- uni.getStorageSync("userInfo").isToken ||
|
50
|
|
- undefined;
|
51
|
|
- config.baseURL = getConfiguration == true ? base.baseDrawioUrl : base.baseUrl
|
52
|
|
- }
|
53
|
|
- if (config?.custom?.load) {
|
54
|
|
- //打开加载动画
|
55
|
|
- store.commit("setLoadingShow", true);
|
56
|
|
- }
|
57
|
|
- if (getConfiguration) {
|
58
|
|
- uni.setStorageSync('config', config)
|
59
|
|
- } else {
|
60
|
|
- uni.removeStorageSync('config')
|
61
|
|
- }
|
62
|
|
- return config;
|
63
|
|
- },
|
64
|
|
- (config) => {
|
65
|
|
- return Promise.reject(config);
|
66
|
|
- }
|
|
34
|
+ (config) => {
|
|
35
|
+ if (store.state.userInfo.isThirdLogin) {
|
|
36
|
+ uni.switchTab({
|
|
37
|
+ url: "../pages/personal/personal",
|
|
38
|
+ });
|
|
39
|
+ }
|
|
40
|
+ // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
|
|
41
|
+ config.data = config.data || {};
|
|
42
|
+ // 根据custom参数中配置的是否需要token,添加对应的请求头
|
|
43
|
+ const getConfiguration =
|
|
44
|
+ uni.getStorageSync("getConfiguration").isConfiguration;
|
|
45
|
+ if (config?.custom?.auth) {
|
|
46
|
+ config.header["X-Authorization"] =
|
|
47
|
+ "Bearer " + store.state.userInfo.isToken ||
|
|
48
|
+ uni.getStorageSync("userInfo").isToken ||
|
|
49
|
+ undefined;
|
|
50
|
+ config.baseURL =
|
|
51
|
+ getConfiguration == true ? base.baseDrawioUrl : base.baseUrl;
|
|
52
|
+ }
|
|
53
|
+ if (config?.custom?.load) {
|
|
54
|
+ //打开加载动画
|
|
55
|
+ store.commit("setLoadingShow", true);
|
|
56
|
+ }
|
|
57
|
+ if (getConfiguration) {
|
|
58
|
+ uni.setStorageSync("config", config);
|
|
59
|
+ } else {
|
|
60
|
+ uni.removeStorageSync("config");
|
|
61
|
+ }
|
|
62
|
+ return config;
|
|
63
|
+ },
|
|
64
|
+ (config) => {
|
|
65
|
+ return Promise.reject(config);
|
|
66
|
+ }
|
67
|
67
|
);
|
68
|
68
|
|
69
|
69
|
// 响应拦截
|
70
|
70
|
uni.$u.http.interceptors.response.use(
|
71
|
|
- (response) => {
|
72
|
|
- // 关闭加载动画
|
73
|
|
- store.commit("setLoadingShow", false);
|
74
|
|
- const data = response.data;
|
75
|
|
- const custom = response.config?.custom;
|
76
|
|
- // code: 200、请求成功 其他,没有更多参数 401、被迫下线重新登录、
|
77
|
|
- if (response.statusCode == 200) {
|
78
|
|
- return Promise.resolve(data);
|
79
|
|
- } else if (response.statusCode == 401) {
|
80
|
|
- // 清空登录信息
|
81
|
|
- store.commit("emptyUserInfo");
|
82
|
|
- // 20秒节流,弹窗登录
|
83
|
|
- uni.$u.throttle(judgeLogin(), 20000);
|
84
|
|
- return new Promise(() => {});
|
85
|
|
- } else {}
|
86
|
|
- },
|
87
|
|
- (response) => {
|
88
|
|
- // 关闭加载动画
|
89
|
|
- store.commit("setLoadingShow", false);
|
90
|
|
- let show = true;
|
91
|
|
- let errorData = '请检查网络或服务器'
|
92
|
|
- let message = response.data?.message || response.data?.msg || response?.errMsg
|
93
|
|
- if (message == "request:fail url not in domain list") {
|
94
|
|
- errorData = "检查请求域名是否添加了域名白名单";
|
95
|
|
- } else if (message == "request:fail timeout") {
|
96
|
|
- errorData = "请求超时:请检查网络";
|
97
|
|
- } else if (response.data.status == 401) {
|
98
|
|
- const isLoginFlag = uni.getStorageSync("userInfo")
|
99
|
|
- if (!store.state.userInfo.isThirdLogin) {
|
100
|
|
- const routers = getCurrentPages()
|
101
|
|
- const currentRoute = routers[routers.length - 1].route
|
102
|
|
- const isLoginPage = currentRoute.includes('publicLoginSubPage/public/login')
|
103
|
|
- !isLoginPage && uni.reLaunch({
|
104
|
|
- url: "/publicLoginSubPage/public/login",
|
105
|
|
- });
|
106
|
|
- // 清空登录信息
|
107
|
|
- store.commit("emptyUserInfo");
|
108
|
|
- show = false;
|
109
|
|
- } else {
|
110
|
|
- uni.switchTab({
|
111
|
|
- url: "../pages/personal/personal",
|
112
|
|
- });
|
113
|
|
- show = false;
|
114
|
|
- }
|
115
|
|
-
|
116
|
|
- } else {
|
117
|
|
- errorData = message || "";
|
118
|
|
- }
|
119
|
|
- if (show) uni.$u.toast(errorData);
|
120
|
|
- return Promise.reject(response);
|
121
|
|
- }
|
122
|
|
-); |
|
71
|
+ (response) => {
|
|
72
|
+ // 关闭加载动画
|
|
73
|
+ store.commit("setLoadingShow", false);
|
|
74
|
+ const data = response.data;
|
|
75
|
+ const custom = response.config?.custom;
|
|
76
|
+ // code: 200、请求成功 其他,没有更多参数 401、被迫下线重新登录、
|
|
77
|
+ if (response.statusCode == 200) {
|
|
78
|
+ return Promise.resolve(data);
|
|
79
|
+ } else if (response.statusCode == 401) {
|
|
80
|
+ // 清空登录信息
|
|
81
|
+ store.commit("emptyUserInfo");
|
|
82
|
+ // 20秒节流,弹窗登录
|
|
83
|
+ uni.$u.throttle(judgeLogin(), 20000);
|
|
84
|
+ return new Promise(() => {});
|
|
85
|
+ } else {
|
|
86
|
+ }
|
|
87
|
+ },
|
|
88
|
+ (response) => {
|
|
89
|
+ // 关闭加载动画
|
|
90
|
+ store.commit("setLoadingShow", false);
|
|
91
|
+ let show = true;
|
|
92
|
+ let errorData = "请检查网络或服务器";
|
|
93
|
+ let message =
|
|
94
|
+ response.data?.message || response.data?.msg || response?.errMsg;
|
|
95
|
+ if (message == "request:fail url not in domain list") {
|
|
96
|
+ errorData = "检查请求域名是否添加了域名白名单";
|
|
97
|
+ } else if (message == "request:fail timeout") {
|
|
98
|
+ errorData = "请求超时:请检查网络";
|
|
99
|
+ } else if (response.data.status == 401) {
|
|
100
|
+ const isLoginFlag = uni.getStorageSync("userInfo");
|
|
101
|
+ if (!store.state.userInfo.isThirdLogin) {
|
|
102
|
+ const routers = getCurrentPages();
|
|
103
|
+ const currentRoute = routers[routers.length - 1].route;
|
|
104
|
+ const isLoginPage = currentRoute.includes(
|
|
105
|
+ "publicLoginSubPage/public/login"
|
|
106
|
+ );
|
|
107
|
+ !isLoginPage &&
|
|
108
|
+ uni.reLaunch({
|
|
109
|
+ url: "/publicLoginSubPage/public/login",
|
|
110
|
+ });
|
|
111
|
+ // 清空登录信息
|
|
112
|
+ store.commit("emptyUserInfo");
|
|
113
|
+ show = false;
|
|
114
|
+ } else {
|
|
115
|
+ uni.switchTab({
|
|
116
|
+ url: "../pages/personal/personal",
|
|
117
|
+ });
|
|
118
|
+ show = false;
|
|
119
|
+ }
|
|
120
|
+ } else if (message == "request:ok") {
|
|
121
|
+ show = false;
|
|
122
|
+ errorData = "";
|
|
123
|
+ } else {
|
|
124
|
+ errorData = message || "";
|
|
125
|
+ }
|
|
126
|
+ if (show) uni.$u.toast(errorData);
|
|
127
|
+ return Promise.reject(response);
|
|
128
|
+ }
|
|
129
|
+); |
...
|
...
|
|