index.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import store from '../../store/index';
import config from '@/config.js'
import http from './interface'
import user from './user.js'
import receipt from './receipt.js'
import dict from './dict.js'
import task from './task.js'
import material from './material.js'
import inventory from './inventory.js'
import cycle from './cycle.js'
import shipment from "./shipment.js"
import menu from './menu.js'
import app from './app.js'
//------------------------------------------------------------------------------------------
//--------------------------------------- 拦截器 ----------------------------------
//------------------------------------------------------------------------------------------
//服务器地址
try {
const url = uni.getStorageSync('base_url');
if (url) {
http.config.baseUrl = url
} else {
http.config.baseUrl = config.server
}
} catch (e) {
// error
}
//设置请求前拦截器
http.interceptor.request = (config) => {
//添加通用参数
let token = uni.getStorageSync('userData').access_token;
delete config.header['Authorization'];
if (token) {
config.header['Authorization'] = 'Bearer ' + token
}
}
//设置请求结束后拦截器
http.interceptor.response = (res) => {
let code = res.statusCode;
let err = null;
//未连接到网络
if (!code) {
uni.showModal({
title: '提示',
content: 'APP服务端未响应!',
confirmColor: '#4cd964',
showCancel: false,
success: function(msg) {
if (msg.confirm) {
console.log('用户点击确定');
}
}
});
return res;
}
if (code != 200) {
//判断网络请求状态 执行相应操作
switch (true) {
case code >= 200 && code < 300:
break;
case code >= 300 && code < 400:
break;
case code >= 400 && code < 500:
err = "客户端请求错误!"
break;
case code == 504:
err = "网络请求超时!"
break;
case code >= 500 && code < 600:
err = "服务器响应错误!"
break;
}
if (err) {
uni.showModal({
title: '提示',
content: err,
confirmColor: '#4cd964',
showCancel: false,
success: function(msg) {
if (msg.confirm) {
console.log('用户点击确定');
}
}
});
return res;
}
}
//服务器返回错误的格式
if (typeof code == "undefined") {
uni.showModal({
title: '提示',
content: '服务器返回错误',
confirmColor: '#4cd964',
showCancel: false,
success: function(msg) {
if (msg.confirm) {
console.log('用户点击确定');
}
}
});
return res;
}
//判断返回状态 执行相应操作
switch (res.data.code) {
//登录过期
case 401:
store.dispatch("logout");
uni.showModal({
title: '提示',
content: '登录过期,请重新登录!',
confirmText: "去登录",
confirmColor: '#4cd964',
success: function(msg) {
if (msg.confirm) {
uni.redirectTo({
url: "/pages/login/login"
})
} else if (msg.cancel) {
console.log('用户点击取消');
}
}
});
break;
default:
break;
}
return res;
}
// 默认全部导出
export default {
user,
receipt,
dict,
task,
material,
inventory,
cycle,
shipment,
menu,
app
}