u
This commit is contained in:
1 parent
68e0785ec6
commit
57c0d98f11
189 files changed
+34329
No files matched your search
@@ -0,0 +1,122 @@
|
||||
import type { AxiosError, AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from 'axios'
|
||||
import axios from 'axios'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { useUserStore } from '@/store/user'
|
||||
|
||||
function buildBaseUrl() {
|
||||
const isProxy = import.meta.env.VITE_APP_PROXY === 'Y'
|
||||
const mode = import.meta.env.MODE
|
||||
const proxyPrefix = import.meta.env.VITE_APP_PROXY_PREFIX
|
||||
|
||||
const baseURL = import.meta.env.VITE_SERVER_BASEURL
|
||||
|
||||
if (mode === 'prod') {
|
||||
if (isProxy) {
|
||||
return baseURL + proxyPrefix
|
||||
}
|
||||
else {
|
||||
return baseURL
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (isProxy) {
|
||||
return proxyPrefix
|
||||
}
|
||||
else {
|
||||
return baseURL
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const axiosInstance: AxiosInstance = axios.create({
|
||||
baseURL: buildBaseUrl(),
|
||||
timeout: 0,
|
||||
})
|
||||
|
||||
const NoFreePaths = [
|
||||
'/imMessage/**',
|
||||
'/file/**',
|
||||
'/company/getPrivateDetail',
|
||||
]
|
||||
|
||||
// 请求拦截器
|
||||
axiosInstance.interceptors.request.use(
|
||||
(config: InternalAxiosRequestConfig) => {
|
||||
const router = useRouter()
|
||||
const userStore = useUserStore()
|
||||
const path = config.url || ''
|
||||
|
||||
const needAuth = matchPaths(path, NoFreePaths)
|
||||
|
||||
if (needAuth) {
|
||||
if (!userStore.isLogined) {
|
||||
userStore.clearUserInfo()
|
||||
router.push('/login')
|
||||
return Promise.reject(new Error('尚未登录,请先登录!'))
|
||||
}
|
||||
}
|
||||
|
||||
config.headers.Authorization = `Bearer ${userStore.userInfo.token}`
|
||||
config.headers.id = userStore.userInfo.id
|
||||
|
||||
return config
|
||||
},
|
||||
(error: AxiosError) => {
|
||||
return Promise.reject(error)
|
||||
},
|
||||
)
|
||||
|
||||
axiosInstance.interceptors.response.use(
|
||||
(response: AxiosResponse) => {
|
||||
// 判断响应是否为文件下载类型
|
||||
const isDownload = isDownloadResponse(response.headers)
|
||||
if (isDownload) {
|
||||
return response
|
||||
}
|
||||
|
||||
if (response.status !== 200) {
|
||||
return Promise.reject(new Error('系统异常'))
|
||||
}
|
||||
|
||||
const resData = response.data as ResponseData<any>
|
||||
response.data.data = parseJsonOrReturnString(response.data.data)
|
||||
|
||||
if (resData.code === 200) {
|
||||
return Promise.resolve(response.data)
|
||||
}
|
||||
else {
|
||||
return Promise.reject(resData)
|
||||
}
|
||||
},
|
||||
(error: AxiosError) => {
|
||||
console.log(error)
|
||||
},
|
||||
)
|
||||
|
||||
function matchPaths(path: string, patterns: string[]): boolean {
|
||||
return patterns.some((pattern) => {
|
||||
const regex = new RegExp(
|
||||
`${pattern
|
||||
.replace(/\/\*\*/g, '(/.*)?')
|
||||
.replace(/\/\*/g, '(/[^/]*)?')
|
||||
.replace(/\./g, '\\.')}$`,
|
||||
)
|
||||
return regex.test(path)
|
||||
})
|
||||
}
|
||||
|
||||
function isDownloadResponse(headers: any): boolean {
|
||||
const contentType = headers['content-type']
|
||||
return /application\/(?:octet-stream|force-download|pdf|vnd.ms-excel|msexcel|OCTET-STREAM)/.test(contentType || '')
|
||||
}
|
||||
|
||||
function parseJsonOrReturnString(input: any) {
|
||||
try {
|
||||
return JSON.parse(input)
|
||||
}
|
||||
catch {
|
||||
return input
|
||||
}
|
||||
}
|
||||
|
||||
export default axiosInstance
|
||||
Reference in new issue
Block a user