feat: ✨ 给 request uploadFile 添加拦截器
This commit is contained in:
parent
4a9bd62713
commit
a9184c643c
39
src/utils/http.ts
Normal file
39
src/utils/http.ts
Normal file
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* 给 request uploadFile 添加拦截器
|
||||
*
|
||||
* 1. 基础地址
|
||||
* 2. 超时时间
|
||||
* 3. 请求头
|
||||
* 4. token
|
||||
*/
|
||||
|
||||
import { useMemberStore } from '@/stores/modules/member'
|
||||
|
||||
const baseUrl = 'https://pcapi-xiaotuxian-front-devtest.itheima.net'
|
||||
|
||||
// 拦截器配置
|
||||
|
||||
const interceptorOptions = {
|
||||
// 拦截前触发
|
||||
invoke(options: UniApp.RequestOptions) {
|
||||
// 1. baseUrl
|
||||
if (!options.url.startsWith('http')) {
|
||||
options.url = baseUrl + options.url
|
||||
}
|
||||
// 2. 超时时间, 默认60s
|
||||
options.timeout = 10 * 1000
|
||||
// 3. 请求头
|
||||
options.header = {
|
||||
...options.header,
|
||||
'source-client': 'miniapp',
|
||||
}
|
||||
// 4. token
|
||||
const token = useMemberStore().profile?.token
|
||||
if (token) {
|
||||
options.header.Authorization = token
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
uni.addInterceptor('request', interceptorOptions)
|
||||
uni.addInterceptor('uploadFile', interceptorOptions)
|
Loading…
x
Reference in New Issue
Block a user