diff --git a/src/libs/http(1).ts b/src/libs/http(1).ts deleted file mode 100644 index 42dc3a1..0000000 --- a/src/libs/http(1).ts +++ /dev/null @@ -1,92 +0,0 @@ -import axios from 'axios' -import { FtMessage } from 'libs/message' -import { getToken } from 'libs/token' -import { HEADER_TOKEN_KEY } from '@/libs/constant' - -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(url: string, params?: any): Promise { - return http.get(url, { params }) -} - -// 封装 POST 请求 -export function post(url: string, data?: any): Promise { - return http.post(url, data) -} - -// 封装 PUT 请求 -export function put(url: string, data?: any): Promise { - return http.put(url, data) -} - -// 封装 DELETE 请求 -export function del(url: string, params?: any): Promise { - return http.delete(url, { params }) -} - -export default http diff --git a/src/libs/http.ts b/src/libs/http.ts index e69de29..42dc3a1 100644 --- a/src/libs/http.ts +++ b/src/libs/http.ts @@ -0,0 +1,92 @@ +import axios from 'axios' +import { FtMessage } from 'libs/message' +import { getToken } from 'libs/token' +import { HEADER_TOKEN_KEY } from '@/libs/constant' + +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(url: string, params?: any): Promise { + return http.get(url, { params }) +} + +// 封装 POST 请求 +export function post(url: string, data?: any): Promise { + return http.post(url, data) +} + +// 封装 PUT 请求 +export function put(url: string, data?: any): Promise { + return http.put(url, data) +} + +// 封装 DELETE 请求 +export function del(url: string, params?: any): Promise { + return http.delete(url, { params }) +} + +export default http