feat: 给 request uploadFile 添加拦截器

This commit is contained in:
jqtmviyu 2025-04-19 12:58:05 +08:00
parent 4a9bd62713
commit a9184c643c

39
src/utils/http.ts Normal file
View 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)