Commit 02315804ac945feefe4073692a067e0293d4cbbc
1 parent
93a97cd5
fix: axios bug
Showing
1 changed file
with
25 additions
and
26 deletions
yudao-ui-admin-vue3/src/config/axios/index.ts
| 1 | 1 | import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios' |
| 2 | -import { ElMessage, ElMessageBox, ElNotification } from 'element-plus' | |
| 2 | +import { ElMessage, ElNotification } from 'element-plus' | |
| 3 | 3 | import qs from 'qs' |
| 4 | 4 | import { config } from '@/config/axios/config' |
| 5 | -import { getAccessToken, getRefreshToken, getTenantId } from '@/utils/auth' | |
| 5 | +import { getAccessToken, getTenantId, removeToken } from '@/utils/auth' | |
| 6 | 6 | import errorCode from './errorCode' |
| 7 | 7 | import { useI18n } from '@/hooks/web/useI18n' |
| 8 | 8 | |
| ... | ... | @@ -22,7 +22,7 @@ export const isRelogin = { show: false } |
| 22 | 22 | // 请求队列 |
| 23 | 23 | // const requestList = [] |
| 24 | 24 | // 是否正在刷新中 |
| 25 | -let isRefreshToken = false | |
| 25 | +// const isRefreshToken = false | |
| 26 | 26 | |
| 27 | 27 | export const PATH_URL = base_url[import.meta.env.VITE_API_BASEPATH] |
| 28 | 28 | |
| ... | ... | @@ -61,8 +61,7 @@ service.interceptors.request.use( |
| 61 | 61 | // const now = new Date().getTime() |
| 62 | 62 | // params = params.substring(0, url.length - 1) + `?_t=${now}` |
| 63 | 63 | url += '?' |
| 64 | - const keys = Object.keys(params) | |
| 65 | - for (const key of keys) { | |
| 64 | + for (const key of Object.keys(params)) { | |
| 66 | 65 | if (params[key] !== void 0 && params[key] !== null) { |
| 67 | 66 | url += `${key}=${encodeURIComponent(params[key])}&` |
| 68 | 67 | } |
| ... | ... | @@ -82,6 +81,7 @@ service.interceptors.request.use( |
| 82 | 81 | // response 拦截器 |
| 83 | 82 | service.interceptors.response.use( |
| 84 | 83 | async (response: AxiosResponse<Recordable>) => { |
| 84 | + console.info(response) | |
| 85 | 85 | const { data } = response |
| 86 | 86 | if (!data) { |
| 87 | 87 | // 返回“[HTTP]请求没有返回值”; |
| ... | ... | @@ -90,6 +90,13 @@ service.interceptors.response.use( |
| 90 | 90 | const { t } = useI18n() |
| 91 | 91 | // 未设置状态码则默认成功状态 |
| 92 | 92 | const code = data.code || result_code |
| 93 | + // 二进制数据则直接返回 | |
| 94 | + if ( | |
| 95 | + response.request.responseType === 'blob' || | |
| 96 | + response.request.responseType === 'arraybuffer' | |
| 97 | + ) { | |
| 98 | + return response.data | |
| 99 | + } | |
| 93 | 100 | // 获取错误信息 |
| 94 | 101 | const msg = data.msg || errorCode[code] || errorCode['default'] |
| 95 | 102 | if (ignoreMsgs.indexOf(msg) !== -1) { |
| ... | ... | @@ -97,15 +104,16 @@ service.interceptors.response.use( |
| 97 | 104 | return Promise.reject(msg) |
| 98 | 105 | } else if (code === 401) { |
| 99 | 106 | // 如果未认证,并且未进行刷新令牌,说明可能是访问令牌过期了 |
| 100 | - if (!isRefreshToken) { | |
| 101 | - isRefreshToken = true | |
| 102 | - // 1. 如果获取不到刷新令牌,则只能执行登出操作 | |
| 103 | - if (!getRefreshToken()) { | |
| 104 | - return handleAuthorized() | |
| 105 | - } | |
| 106 | - // 2. 进行刷新访问令牌 | |
| 107 | - // TODO: 引入refreshToken会循环依赖报错 | |
| 108 | - } | |
| 107 | + return handleAuthorized() | |
| 108 | + // if (!isRefreshToken) { | |
| 109 | + // isRefreshToken = true | |
| 110 | + // // 1. 如果获取不到刷新令牌,则只能执行登出操作 | |
| 111 | + // if (!getRefreshToken()) { | |
| 112 | + // return handleAuthorized() | |
| 113 | + // } | |
| 114 | + // // 2. 进行刷新访问令牌 | |
| 115 | + // // TODO: 引入refreshToken会循环依赖报错 | |
| 116 | + // } | |
| 109 | 117 | } else if (code === 500) { |
| 110 | 118 | ElMessage.error(t('sys.api.errMsg500')) |
| 111 | 119 | return Promise.reject(new Error(msg)) |
| ... | ... | @@ -149,21 +157,12 @@ service.interceptors.response.use( |
| 149 | 157 | return Promise.reject(error) |
| 150 | 158 | } |
| 151 | 159 | ) |
| 152 | -function handleAuthorized() { | |
| 160 | +const handleAuthorized = () => { | |
| 153 | 161 | const { t } = useI18n() |
| 154 | 162 | if (!isRelogin.show) { |
| 163 | + removeToken() | |
| 155 | 164 | isRelogin.show = true |
| 156 | - ElMessageBox.confirm(t('sys.api.timeoutMessage'), t('common.confirmTitle'), { | |
| 157 | - confirmButtonText: t('login.relogin'), | |
| 158 | - cancelButtonText: t('common.cancel'), | |
| 159 | - type: 'warning' | |
| 160 | - }) | |
| 161 | - .then(() => { | |
| 162 | - isRelogin.show = false | |
| 163 | - }) | |
| 164 | - .catch(() => { | |
| 165 | - isRelogin.show = false | |
| 166 | - }) | |
| 165 | + ElNotification.error(t('sys.api.timeoutMessage')) | |
| 167 | 166 | } |
| 168 | 167 | return Promise.reject(t('sys.api.timeoutMessage')) |
| 169 | 168 | } | ... | ... |