You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.1 KiB
92 lines
2.1 KiB
import { HEADER_TOKEN_KEY } from '@/libs/constant'
|
|
import axios from 'axios'
|
|
import { FtMessage } from 'libs/message'
|
|
import { getToken } from 'libs/token'
|
|
|
|
const http = axios.create({
|
|
baseURL: import.meta.env.FT_API_BASE,
|
|
timeout: 1000 * 60,
|
|
})
|
|
|
|
// 请求拦截器
|
|
http.interceptors.request.use(
|
|
(config) => {
|
|
if (getToken()) {
|
|
config.headers![HEADER_TOKEN_KEY] = getToken()
|
|
}
|
|
return config
|
|
},
|
|
(error: any) => {
|
|
return Promise.reject(error)
|
|
},
|
|
)
|
|
|
|
// 响应拦截器
|
|
http.interceptors.response.use(
|
|
(response) => {
|
|
if (
|
|
response.status === 200
|
|
&& response.data.code !== '0'
|
|
) {
|
|
// 返回错误拦截
|
|
FtMessage.error(response.data.msg)
|
|
return Promise.reject(response)
|
|
}
|
|
else if (
|
|
response.config.url?.includes('/files/download')
|
|
|| response.config.url?.includes('downloadStream')
|
|
) {
|
|
return response.data
|
|
}
|
|
else if (response.data instanceof Blob) {
|
|
return response.data
|
|
}
|
|
return response.data.data // 返回数据体
|
|
},
|
|
(error: any) => {
|
|
console.log(error)
|
|
if (error.response && error.response.status === 401) {
|
|
FtMessage.error('账号权限过期')
|
|
// TODO 登出
|
|
}
|
|
else {
|
|
if (error.message.includes('timeout')) {
|
|
FtMessage.error('请求超时')
|
|
}
|
|
else if (error.message.includes('Network')) {
|
|
FtMessage.error('网络连接错误')
|
|
}
|
|
else {
|
|
FtMessage.error('接口请求失败')
|
|
}
|
|
error.response = {
|
|
data: {
|
|
res: false,
|
|
},
|
|
}
|
|
return Promise.reject(error.response)
|
|
}
|
|
},
|
|
)
|
|
|
|
// 封装 GET 请求
|
|
export function get<T>(url: string, params?: any): Promise<T> {
|
|
return http.get(url, { params })
|
|
}
|
|
|
|
// 封装 POST 请求
|
|
export function post<T>(url: string, data?: any): Promise<T> {
|
|
return http.post(url, data)
|
|
}
|
|
|
|
// 封装 PUT 请求
|
|
export function put<T>(url: string, data?: any): Promise<T> {
|
|
return http.put(url, data)
|
|
}
|
|
|
|
// 封装 DELETE 请求
|
|
export function del<T>(url: string, params?: any): Promise<T> {
|
|
return http.delete(url, { params })
|
|
}
|
|
|
|
export default http
|