WXL
11 小时以前 b7b8202e3ecb7f720eefd7a226b2ee8166fc5057
utils/request.js
@@ -1,5 +1,6 @@
// @/utils/request.js
// 添加全局标记
let isRedirectingToLogin = false;
/**
 * 显示Toast提示(兼容uview-plus)
 * @param {string} message 提示内容
@@ -57,12 +58,38 @@
 * 跳转到登录页
 */
const navigateToLogin = () => {
  uni.redirectTo({
    url:
      config.loginPage +
      "?redirect=" +
      encodeURIComponent(getCurrentPagePath()),
  });
  if (isRedirectingToLogin) {
    console.log('正在跳转登录页中,跳过');
    return;
  }
  isRedirectingToLogin = true;
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1];
  const isLoginPage = currentPage && currentPage.route &&
                     currentPage.route.includes('login/Login');
  if (isLoginPage) {
    console.log('当前已在登录页,不跳转');
    isRedirectingToLogin = false;
    return;
  }
  console.log('跳转到登录页');
  setTimeout(() => {
    uni.redirectTo({
      url: config.loginPage + "?redirect=" + encodeURIComponent(getCurrentPagePath()),
      success: () => {
        console.log('跳转登录页成功');
        isRedirectingToLogin = false;
      },
      fail: () => {
        console.log('跳转登录页失败');
        isRedirectingToLogin = false;
      }
    });
  }, 100);
};
/**
@@ -94,13 +121,25 @@
  // 如果请求在白名单中,直接放行
  if (isInWhiteList(options.url)) {
    console.log('白名单接口,跳过token校验:', options.url);
    return options;
  }
  // 如果未登录且不是白名单接口,跳转登录
  // console.log(token,'token');
  // ✅ 关键修改:在登录页时不进行跳转
  if (!token) {
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    const isLoginPage = currentPage && currentPage.route &&
                       (currentPage.route.includes('login/Login') ||
                        currentPage.route.includes('login/DingTalkLogin'));
    if (isLoginPage) {
      console.log('当前是登录页,允许无token请求');
      return options;
    }
    // 不在登录页,才跳转
    console.log('未登录且不在登录页,跳转到登录页');
    navigateToLogin();
    throw new Error("未登录");
  }