uniapp请求拦截器
"你觉得孤独就对了,那是你认识自己的机会;你觉得不被理解就对了,那是你认清朋友的机会;你觉得黑暗就对了,那是你发现光芒的机会;你觉得无助就对了,那样你才能知道谁是你的贵人;你觉得迷茫就对了,谁的青春不迷茫。"
uniapp简单http请求封装。
const BASE_URL = 'https://api.sy12306.com/';
export const myRequest = (options) => {
// 添加默认参数
let params = {
client_id: 769,
app_id: 100,
format: 'json',
ts: Date.parse(new Date())
}
options.data = typeof options.data == 'undefined' ? params : Object.assign(options.data, params);
// 配置请求类型
let header = options.method == 'GET' ? {
'X-Requested-With': 'XMLHttpRequest',
"Accept": "application/json",
"Content-Type": "application/json; charset=UTF-8"
} : {
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
return new Promise((resolve, reject) => {
uni.request({
url: BASE_URL + options.url,
method: options.method || 'GET',
data: options.data,
timeout: 10000,
dataType: 'json',
header,
success: (res) => {
if (res.data.code != 200) {
return uni.showToast({
title: '获取数据失败'
})
}
resolve(res)
},
fail: (err) => {
uni.showToast({
title: '请求接口失败'
})
reject(err)
}
})
})
}
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
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
上次更新: 2020/10/14, 21:10:00