云
16 小时以前 c2e8526f8f857be5e3a8c6f2b39c5efd424de33e
src/api/request.ts
@@ -33,10 +33,13 @@
  /**
   * 重新认证逻辑
   */
  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' &&
@@ -58,6 +61,9 @@
      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) {
@@ -220,13 +226,13 @@
  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;
  },
});