| | |
| | | /** |
| | | * 该文件可自行根据业务逻辑进行调整 |
| | | */ |
| | | import type { RequestClientOptions } from '..\packages\effects\request\src'; |
| | | import type { RequestClientOptions } from '@vben/request'; |
| | | |
| | | import { isTenantEnable, useAppConfig } from '..\packages\effects\hooks\src'; |
| | | import { preferences } from '..\packages\preferences\src'; |
| | | import { isTenantEnable, useAppConfig } from '@vben/hooks'; |
| | | import { preferences } from '@vben/preferences'; |
| | | import { |
| | | authenticateResponseInterceptor, |
| | | defaultResponseInterceptor, |
| | | errorMessageResponseInterceptor, |
| | | RequestClient, |
| | | } from '..\packages\effects\request\src'; |
| | | import { useAccessStore } from '..\packages\stores\src'; |
| | | import { createApiEncrypt } from '..\packages\utils\src'; |
| | | } from '@vben/request'; |
| | | import { useAccessStore } from '@vben/stores'; |
| | | import { createApiEncrypt } from '@vben/utils'; |
| | | |
| | | import { message } from 'ant-design-vue'; |
| | | |
| | |
| | | /** |
| | | * 重新认证逻辑 |
| | | */ |
| | | async function doReAuthenticate() { |
| | | async function doReAuthenticate(error?: { config?: { headers?: Record<string, unknown> } }) { |
| | | console.warn('Access token or refresh token is invalid or expired. '); |
| | | const accessStore = useAccessStore(); |
| | | const authStore = useAuthStore(); |
| | | const requestToken = error?.config?.headers?.Authorization; |
| | | // 已退出或已切换账号时,旧请求的 401 不得清除新会话。 |
| | | if (requestToken && requestToken !== formatToken(accessStore.accessToken)) return; |
| | | accessStore.setAccessToken(null); |
| | | if ( |
| | | preferences.app.loginExpiredMode === 'modal' && |
| | |
| | | throw new Error('Refresh token is null!'); |
| | | } |
| | | const resp = await refreshTokenApi(refreshToken); |
| | | if (accessStore.refreshToken !== refreshToken) { |
| | | throw new Error('登录状态已变化,忽略旧会话的令牌刷新结果'); |
| | | } |
| | | const newToken = resp?.data?.data?.accessToken; |
| | | // add by 芋艿:这里一定要抛出 resp.data,从而触发 authenticateResponseInterceptor 中,刷新令牌失败!!! |
| | | if (!newToken) { |
| | |
| | | fulfilled: (config) => { |
| | | const accessStore = useAccessStore(); |
| | | // 添加租户编号 |
| | | config.headers['tenant-id'] = tenantEnable |
| | | ? accessStore.tenantId |
| | | : undefined; |
| | | if (!('tenant-id' in config.headers)) { |
| | | config.headers['tenant-id'] = tenantEnable ? accessStore.tenantId : undefined; |
| | | } |
| | | // 只有登录时,才设置 visit-tenant-id 访问租户 |
| | | config.headers['visit-tenant-id'] = tenantEnable |
| | | ? accessStore.visitTenantId |
| | | : undefined; |
| | | if (!('visit-tenant-id' in config.headers)) { |
| | | config.headers['visit-tenant-id'] = tenantEnable ? accessStore.visitTenantId : undefined; |
| | | } |
| | | return config; |
| | | }, |
| | | }); |