2025-09-11 11:03:14 +08:00
|
|
|
|
import { ElMessage } from 'element-plus'
|
|
|
|
|
|
import axios, {
|
|
|
|
|
|
AxiosError,
|
|
|
|
|
|
type AxiosInstance,
|
|
|
|
|
|
type AxiosRequestConfig,
|
|
|
|
|
|
type AxiosResponse,
|
|
|
|
|
|
type InternalAxiosRequestConfig
|
|
|
|
|
|
} from 'axios'
|
2024-11-15 13:47:07 +08:00
|
|
|
|
import { showFullScreenLoading, tryHideFullScreenLoading } from '@/components/Loading/fullScreen'
|
|
|
|
|
|
import { LOGIN_URL } from '@/config'
|
2025-09-11 11:03:14 +08:00
|
|
|
|
import { type ResultData } from '@/api/interface'
|
2024-11-15 13:47:07 +08:00
|
|
|
|
import { ResultEnum } from '@/enums/httpEnum'
|
|
|
|
|
|
import { checkStatus } from './helper/checkStatus'
|
|
|
|
|
|
import { useUserStore } from '@/stores/modules/user'
|
|
|
|
|
|
import router from '@/routers'
|
2025-09-11 11:03:14 +08:00
|
|
|
|
import { refreshToken } from '@/api/user/login'
|
|
|
|
|
|
import { EventSourcePolyfill } from 'event-source-polyfill'
|
2024-08-22 11:27:06 +08:00
|
|
|
|
|
|
|
|
|
|
export interface CustomAxiosRequestConfig extends InternalAxiosRequestConfig {
|
2025-09-11 11:03:14 +08:00
|
|
|
|
loading?: boolean
|
2024-08-22 11:27:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const config = {
|
2025-09-11 11:03:14 +08:00
|
|
|
|
// 默认地址请求地址,可在 .env.** 文件中修改
|
|
|
|
|
|
baseURL: import.meta.env.VITE_API_URL as string,
|
|
|
|
|
|
// 设置超时时间
|
|
|
|
|
|
timeout: ResultEnum.TIMEOUT as number,
|
|
|
|
|
|
// 跨域时候允许携带凭证
|
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
|
// post请求指定数据类型以及编码
|
|
|
|
|
|
headers: { 'Content-Type': 'application/json;charset=utf-8' }
|
2024-11-15 13:47:07 +08:00
|
|
|
|
}
|
2024-08-22 11:27:06 +08:00
|
|
|
|
|
|
|
|
|
|
class RequestHttp {
|
2025-09-11 11:03:14 +08:00
|
|
|
|
service: AxiosInstance
|
|
|
|
|
|
|
|
|
|
|
|
public constructor(config: AxiosRequestConfig) {
|
|
|
|
|
|
// 创建实例
|
|
|
|
|
|
this.service = axios.create(config)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @description 请求拦截器
|
|
|
|
|
|
* 客户端发送请求 -> [请求拦截器] -> 服务器
|
|
|
|
|
|
* token校验(JWT) : 接受服务器返回的 token,存储到 vuex/pinia/本地储存当中
|
|
|
|
|
|
*/
|
|
|
|
|
|
this.service.interceptors.request.use(
|
|
|
|
|
|
(config: CustomAxiosRequestConfig) => {
|
|
|
|
|
|
isFirst = true
|
|
|
|
|
|
const userStore = useUserStore()
|
|
|
|
|
|
// 当前请求不需要显示 loading,在 api 服务中通过指定的第三个参数: { loading: false } 来控制
|
|
|
|
|
|
config.loading ?? (config.loading = true)
|
|
|
|
|
|
config.loading && showFullScreenLoading()
|
|
|
|
|
|
if (config.headers && typeof config.headers.set === 'function') {
|
|
|
|
|
|
config.headers.set('Authorization', 'Bearer ' + userStore.accessToken)
|
|
|
|
|
|
config.headers.set('Is-Refresh-Token', userStore.isRefreshToken + '')
|
|
|
|
|
|
}
|
|
|
|
|
|
return config
|
|
|
|
|
|
},
|
|
|
|
|
|
(error: AxiosError) => {
|
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
let isFirst = true
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @description 响应拦截器
|
|
|
|
|
|
* 服务器换返回信息 -> [拦截统一处理] -> 客户端JS获取到信息
|
|
|
|
|
|
*/
|
|
|
|
|
|
this.service.interceptors.response.use(
|
|
|
|
|
|
async (response: AxiosResponse) => {
|
|
|
|
|
|
const { data } = response
|
|
|
|
|
|
const userStore = useUserStore()
|
|
|
|
|
|
tryHideFullScreenLoading()
|
|
|
|
|
|
|
|
|
|
|
|
if (data.code === ResultEnum.ACCESSTOKEN_EXPIRED) {
|
|
|
|
|
|
// 用长token去换短token
|
|
|
|
|
|
userStore.setAccessToken(userStore.refreshToken)
|
|
|
|
|
|
userStore.setIsRefreshToken(true)
|
|
|
|
|
|
const result = await refreshToken()
|
|
|
|
|
|
if (result) {
|
|
|
|
|
|
//获取新token成功的话
|
|
|
|
|
|
// 有新的token后,重新请求
|
|
|
|
|
|
userStore.setAccessToken(result.data.accessToken)
|
|
|
|
|
|
userStore.setRefreshToken(result.data.refreshToken)
|
|
|
|
|
|
userStore.setIsRefreshToken(false)
|
|
|
|
|
|
userStore.setExp(1000 * 60 * 60 * 24 * 30)
|
|
|
|
|
|
response.config.headers.Authorization = `Bearer ${result.data.accessToken}` //重新请求前需要将更新后的新token更换掉之前无效的token,不然会死循环
|
|
|
|
|
|
const resp = await this.service.request(response.config)
|
|
|
|
|
|
return resp
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 刷新失效,跳转登录页
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 登陆失效
|
|
|
|
|
|
if (data.code === ResultEnum.OVERDUE) {
|
2025-10-15 08:49:11 +08:00
|
|
|
|
//console.log('登陆失效')
|
2025-09-11 11:03:14 +08:00
|
|
|
|
userStore.setAccessToken('')
|
|
|
|
|
|
userStore.setRefreshToken('')
|
|
|
|
|
|
userStore.setIsRefreshToken(false)
|
|
|
|
|
|
userStore.setUserInfo({ id: '', name: '' })
|
|
|
|
|
|
userStore.setExp(0)
|
|
|
|
|
|
await router.replace(LOGIN_URL)
|
|
|
|
|
|
if (isFirst) {
|
|
|
|
|
|
//临时处理token失效弹窗多次
|
|
|
|
|
|
ElMessage.error(data.message)
|
|
|
|
|
|
isFirst = false
|
|
|
|
|
|
}
|
|
|
|
|
|
return Promise.reject(data)
|
|
|
|
|
|
}
|
|
|
|
|
|
// 全局错误信息拦截(防止下载文件的时候返回数据流,没有 code 直接报错)
|
|
|
|
|
|
if (data.code && data.code !== ResultEnum.SUCCESS) {
|
|
|
|
|
|
if (data.message.includes('&')) {
|
|
|
|
|
|
let formattedMessage = data.message.split('&').join('<br>')
|
|
|
|
|
|
if (data.message.includes(':')) {
|
|
|
|
|
|
formattedMessage = formattedMessage.replace(':', '')
|
|
|
|
|
|
}
|
|
|
|
|
|
ElMessage.error({ message: formattedMessage, dangerouslyUseHTMLString: true })
|
|
|
|
|
|
return Promise.reject(data)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ElMessage.error(data.message)
|
|
|
|
|
|
return Promise.reject(data)
|
|
|
|
|
|
}
|
|
|
|
|
|
// 成功请求(在页面上除非特殊情况,否则不用处理失败逻辑)
|
|
|
|
|
|
|
|
|
|
|
|
if (userStore.exp <= Date.now() && userStore.exp !== 0) {
|
|
|
|
|
|
userStore.setAccessToken('')
|
|
|
|
|
|
userStore.setRefreshToken('')
|
|
|
|
|
|
userStore.setIsRefreshToken(false)
|
|
|
|
|
|
userStore.setUserInfo({ id: '', name: '' })
|
|
|
|
|
|
userStore.setExp(0)
|
|
|
|
|
|
ElMessage.error('登录已过期,请重新登录!')
|
|
|
|
|
|
await router.replace(LOGIN_URL)
|
|
|
|
|
|
return Promise.reject(data)
|
|
|
|
|
|
}
|
2025-09-23 16:14:03 +08:00
|
|
|
|
// 对于blob类型的响应,返回完整的response对象以保留响应头
|
|
|
|
|
|
if (response.config.responseType === 'blob') {
|
|
|
|
|
|
return response
|
|
|
|
|
|
}
|
2025-09-11 11:03:14 +08:00
|
|
|
|
return data
|
|
|
|
|
|
},
|
|
|
|
|
|
async (error: AxiosError) => {
|
|
|
|
|
|
const { response } = error
|
|
|
|
|
|
tryHideFullScreenLoading()
|
2025-10-15 08:49:11 +08:00
|
|
|
|
//console.log('error', error.message)
|
2025-09-11 11:03:14 +08:00
|
|
|
|
// 请求超时 && 网络错误单独判断,没有 response
|
|
|
|
|
|
if (error.message.indexOf('timeout') !== -1) ElMessage.error('请求超时!请您稍后重试')
|
|
|
|
|
|
if (error.message.indexOf('Network Error') !== -1) ElMessage.error('网络错误!请您稍后重试')
|
|
|
|
|
|
// 根据服务器响应的错误状态码,做不同的处理
|
|
|
|
|
|
if (response) checkStatus(response.status)
|
|
|
|
|
|
// 服务器结果都没有返回(可能服务器错误可能客户端断网),断网处理:可以跳转到断网页面
|
|
|
|
|
|
if (!window.navigator.onLine) router.replace('/500')
|
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
2024-08-22 11:27:06 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-11 11:03:14 +08:00
|
|
|
|
* @description 常用请求方法封装
|
2024-08-22 11:27:06 +08:00
|
|
|
|
*/
|
2025-09-11 11:03:14 +08:00
|
|
|
|
get<T>(url: string, params?: object, _object = {}): Promise<ResultData<T>> {
|
|
|
|
|
|
return this.service.get(url, { params, ..._object })
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
post<T>(url: string, params?: object | string, _object = {}): Promise<ResultData<T>> {
|
|
|
|
|
|
return this.service.post(url, params, _object)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
put<T>(url: string, params?: object, _object = {}): Promise<ResultData<T>> {
|
|
|
|
|
|
return this.service.put(url, params, _object)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
delete<T>(url: string, params?: any, _object = {}): Promise<ResultData<T>> {
|
|
|
|
|
|
return this.service.delete(url, { params, ..._object })
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
download(url: string, params?: object, _object = {}): Promise<BlobPart> {
|
2025-09-23 16:14:03 +08:00
|
|
|
|
return this.service.post(url, params, { ..._object, responseType: 'blob' }).then(res => res.data)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
downloadWithHeaders(url: string, params?: object, _object = {}): Promise<AxiosResponse<Blob>> {
|
2025-09-11 11:03:14 +08:00
|
|
|
|
return this.service.post(url, params, { ..._object, responseType: 'blob' })
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
upload(url: string, params?: object, _object = {}): Promise<BlobPart> {
|
|
|
|
|
|
return this.service.post(url, params, {
|
|
|
|
|
|
..._object,
|
|
|
|
|
|
headers: { 'Content-Type': 'multipart/form-data' }
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-22 11:27:06 +08:00
|
|
|
|
/**
|
2025-09-11 11:03:14 +08:00
|
|
|
|
* 针对excel的上传,默认返回的是blob类型,Excel没问题时返回json特殊处理
|
2024-08-22 11:27:06 +08:00
|
|
|
|
*/
|
2025-09-11 11:03:14 +08:00
|
|
|
|
uploadExcel(url: string, params?: object, _object = {}): Promise<BlobPart> {
|
2025-10-09 14:34:45 +08:00
|
|
|
|
return this.service
|
|
|
|
|
|
.post(url, params, {
|
|
|
|
|
|
..._object,
|
|
|
|
|
|
headers: { 'Content-Type': 'multipart/form-data' },
|
|
|
|
|
|
responseType: 'blob'
|
|
|
|
|
|
})
|
|
|
|
|
|
.then(res => res.data)
|
2025-09-11 11:03:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 添加SSE连接方法
|
|
|
|
|
|
sse(url: string, params?: any): EventSource {
|
2024-11-15 13:47:07 +08:00
|
|
|
|
const userStore = useUserStore()
|
2025-09-11 11:03:14 +08:00
|
|
|
|
// 构造带参数的URL
|
|
|
|
|
|
let requestUrl = config.baseURL + url
|
|
|
|
|
|
if (params) {
|
|
|
|
|
|
const searchParams = new URLSearchParams()
|
|
|
|
|
|
for (const key in params) {
|
|
|
|
|
|
if (Object.prototype.hasOwnProperty.call(params, key)) {
|
|
|
|
|
|
searchParams.append(key, String(params[key]))
|
|
|
|
|
|
}
|
2025-02-07 14:28:15 +08:00
|
|
|
|
}
|
2025-09-11 11:03:14 +08:00
|
|
|
|
requestUrl += '?' + searchParams.toString()
|
2024-08-22 11:27:06 +08:00
|
|
|
|
}
|
2025-01-16 19:45:48 +08:00
|
|
|
|
|
2025-09-11 11:03:14 +08:00
|
|
|
|
// 创建EventSource连接
|
|
|
|
|
|
const eventSource = new EventSourcePolyfill(requestUrl, {
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
Authorization: 'Bearer ' + userStore.accessToken
|
2025-09-19 16:18:10 +08:00
|
|
|
|
},
|
2025-10-09 14:34:45 +08:00
|
|
|
|
// 增加超时时间到1200秒
|
|
|
|
|
|
heartbeatTimeout: 1200000
|
2025-09-11 11:03:14 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 设置默认的Authorization头部
|
|
|
|
|
|
eventSource.addEventListener('open', function () {
|
2025-10-15 08:49:11 +08:00
|
|
|
|
//console.log('SSE连接已建立')
|
2025-09-11 11:03:14 +08:00
|
|
|
|
})
|
2025-09-19 16:18:10 +08:00
|
|
|
|
// 添加错误处理
|
|
|
|
|
|
eventSource.addEventListener('error', function (err) {
|
|
|
|
|
|
console.error('SSE连接错误:', err)
|
|
|
|
|
|
})
|
2025-01-16 19:45:48 +08:00
|
|
|
|
|
2025-09-11 11:03:14 +08:00
|
|
|
|
return eventSource
|
|
|
|
|
|
}
|
2024-08-22 11:27:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-15 13:47:07 +08:00
|
|
|
|
export default new RequestHttp(config)
|