request.js
1.01 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
const BASE_URL = 'http://course.zktljz.com';
/**
* url
* method
* data
* header
*/
export const request = (options) => {
return new Promise((resolve, reject) => {
const token = uni.getStorageSync("token")
uni.request({
url: BASE_URL + options.url,
method: options.method || 'GET',
data: options.data || {},
header: {
'Content-Type': 'application/json',
Authorization: token || "",
...options.header,
},
success: (res) => {
if (res?.data.code === 401) {
uni.setStorageSync('token', "")
uni.showToast({
title: '登录过期',
icon: 'none'
});
uni.navigateTo({
url: '/pages/login/login'
})
reject(res);
}
if (res.statusCode === 200) {
resolve(res.data);
} else {
// uni.showToast({ title: '请求错误', icon: 'none' });
reject(res);
}
},
fail: (err) => {
uni.showToast({
title: '网络错误',
icon: 'none'
});
reject(err);
},
});
});
};