request.js
2.19 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
import Request from '@/js_sdk/luch-request/luch-request/index.js'
import cfg from '@/config.js'
const http = new Request()
http.interceptors.request.use(
(config) => {
// 可使用async await 做异步操作
// config.header = {
// ...config.header,
// a: 1 // 演示拦截器header加参
// }
// 演示custom 用处
// if (config.custom.auth) {
// config.header.token = 'token'
// }
config.header['Authorization'] =
'eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX3R5cGUiOiIwMCIsInVzZXJfaWQiOjEsImxvZ2luX3R5cGUiOiJMaW5rV2VDaGF0QVBJIiwidXNlcl9uYW1lIjoiYWRtaW4iLCJ1c2VyX2tleSI6IjYyZjRiYWZmLWY2NzYtNGNlOS1iNDNhLTZhMjhlM2QwYTEwNiIsImNvcnBfbmFtZSI6IuS7n-W-ruenkeaKgCIsImNvcnBfaWQiOiJ3dzYyMmZjODUyZjc5YzNmMTMifQ.CvkoSw7K1eT-AjmfFYX5e2MlxKFNjfqzPpIwPlEuNDRL3ZASTW7t6UlGUl5CbYGI-F-3EPcVvFm9PcEk_YKtlQ'
// if (config.custom.loading) {
// uni.showLoading()
// }
/**
/* 演示
if (!token) { // 如果token不存在,return Promise.reject(config) 会取消本次请求
return Promise.reject(config)
}
**/
return config
},
(config) => {
// 可使用async await 做异步操作
return Promise.reject(config)
},
)
http.interceptors.response.use(
(response) => {
/* 对响应成功做点什么 可使用async await 做异步操作*/
let data = response.data
if (data.code == 200) {
return data
}
uni.hideLoading()
uni.showToast({
icon: 'none',
title: data.msg,
duration: 3000,
})
return Promise.reject(response) // return Promise.reject 可使promise状态进入catch
},
(response) => {
/* 对响应错误做点什么 (statusCode !== 200)*/
console.log(response)
return Promise.reject(response)
},
)
export async function post(url, data) {
return http.request({
method: 'POST', // 请求方法必须大写
url,
data,
})
}
export async function get(url, params) {
return http.get(url, {
params,
})
}
export default function(config = {}) {
config.baseURL = cfg.BASE_API
// 兼容get方法使用data传参
if ((config.method || 'get').toLowerCase() === 'get' && !config.params) {
config.params = config.data
}
return http.request(config)
}