feat: initialize Evergreen Health mobile app
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import axios from 'axios'
|
||||
import { clearTokens, getToken } from './auth'
|
||||
|
||||
type UnifiedResult<T> = {
|
||||
type?: string
|
||||
message?: string
|
||||
result?: T
|
||||
errors?: unknown
|
||||
}
|
||||
|
||||
const http = axios.create({
|
||||
baseURL: '/',
|
||||
timeout: 15000
|
||||
})
|
||||
|
||||
http.interceptors.request.use((config) => {
|
||||
const token = getToken()
|
||||
if (token) config.headers.Authorization = `Bearer ${token}`
|
||||
return config
|
||||
})
|
||||
|
||||
http.interceptors.response.use(
|
||||
((response: any) => {
|
||||
const body = response.data as UnifiedResult<unknown>
|
||||
if (body && typeof body === 'object' && 'result' in body) {
|
||||
if (body.type && body.type !== 'success') {
|
||||
return Promise.reject(new Error(body.message || '请求失败'))
|
||||
}
|
||||
return body.result
|
||||
}
|
||||
return body
|
||||
}) as any,
|
||||
(error) => {
|
||||
if (error.response?.status === 401) clearTokens()
|
||||
const body = error.response?.data as UnifiedResult<unknown> | undefined
|
||||
const message = body?.message || error.message || '网络请求失败'
|
||||
return Promise.reject(new Error(message))
|
||||
}
|
||||
)
|
||||
|
||||
export default http
|
||||
Reference in New Issue
Block a user