request.js
1.77 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
// import { baseUrl } from "./env";
// 参数: url:请求地址 param:请求参数 method:请求方式 callBack:回调函数
export function request({ url = "", params = {}, method = "GET" }) {
// baseUrl改为可配置地址
if (!uni.getStorageSync('baseUrl')){
uni.setStorageSync('baseUrl', 'https://tms-gateway.zrpfsc.com/courier')
}
let baseUrl = uni.getStorageSync('baseUrl')
// 获取token
const token = uni.getStorageSync("token");
let header = {
// 'Accept': 'application/json',
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json;charset=UTF-8",
Authorization: token,
};
if (url == '/track/upload'){
header['Content-Type'] = 'application/x-www-form-urlencoded'
}
const requestRes = new Promise((resolve, reject) => {
uni.request({
url: baseUrl + url,
data: params,
header: header,
method: method,
}).then((res)=>{
const { data } = res
if (res.statusCode == 401){
uni.showToast({
title: '您的登录已过期!请重新登录后操作!',
duration: 2000,
icon: 'none',
});
uni.redirectTo({
url: '/pages/login/user'
});
return false
}
if (res.statusCode == 400){
uni.showToast({
title: '权限不足,无法登录!',
duration: 2000,
icon: 'none',
});
uni.redirectTo({
url: '/pages/login/user'
});
return false
}
if (data.code == 1) {
uni.showToast({
title: data.msg,
duration: 2000,
icon: 'none',
});
return false
}
if (data.code == 0 || data.code == 200) {
resolve(res.data);
} else {
reject(res.data);
}
}).catch((err)=>{
const error = { data: { msg: err.data } };
reject(error);
});
});
return requestRes;
}