V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
qingjiang
V2EX  ›  程序员

求:有没有会 ant design pro v6 的

  •  
  •   qingjiang · 57 天前 · 676 次点击
    这是一个创建于 57 天前的主题,其中的信息可能已经有所发展或是发生改变。

    后端使用的 satoken ,每个请求都需要拿到 token 才能查询到数据

    我在 app.ts 中配置了请求前拦截,但是返回的 token ,其他请求头部 Authorization 并没有拿到浏览器本地存的 token

    /**
     * requestInterceptors 接收一个数组,数组的每一项为一个 request 拦截器。等同于 umi-request 的 request.interceptors.request.use() 。
     */
    const authHeaderInterceptor = (url: string, options: RequestOptionsInit) => {
      console.log('authHeaderInterceptor url, options: ', url, options  );
      // 如果是登录页面,不需要添加 token
      if (url.indexOf('/login/') !== -1 || url.indexOf('/logout') !== -1) {
        return {
          url: `${url}`,
          options: { ...options, interceptors: true },
        };
      } else {
        const token = getToken();
        console.log('url, token: ', url, token);
        let authHeader = {};
        // 如果 token 存在,就添加到请求头
        if (localStorage.getItem('ADMIN_TOKEN')) {
          authHeader = { Authorization: `Bearer ${localStorage.getItem('ADMIN_TOKEN')}` };
        }
    
        return {
          url: `${url}`,
          options: { ...options, interceptors: true, headers: authHeader },
        };
      }
    };
    
    
    /**
     * requestInterceptors 接收一个数组,数组的每一项为一个 request 拦截器。等同于 umi-request 的 request.interceptors.request.use() 。
     */
    const authHeaderInterceptor = (url: string, options: RequestConfig) => {
      console.log('authHeaderInterceptor url, options: ', url, options  );
      // 如果是登录页面,不需要添加 token
      if (url.indexOf('/login/') !== -1 || url.indexOf('/logout') !== -1) {
        return {
          url: `${url}`,
          options: { ...options, interceptors: true },
        };
      } else {
        const token = getToken();
        console.log('url, token: ', url, token);
        let authHeader = {};
        // 如果 token 存在,就添加到请求头
        if (localStorage.getItem('ADMIN_TOKEN')) {
          authHeader = { Authorization: `Bearer ${localStorage.getItem('ADMIN_TOKEN')}` };
        }
    
        return {
          url: `${url}`,
          options: { ...options, interceptors: true, headers: authHeader },
        };
      }
    };
    

    求解

    1 条回复
    qingjiang
        1
    qingjiang  
    OP
       57 天前
    我自己打自己脸了,接口请求路径是 /login/account ,之前是 /login
    服了啥也不懂,突然悟了

    const authHeaderInterceptor = (url: string, options: RequestConfig) => {
    console.log('authHeaderInterceptor url, options: ', url, options );
    console.log('url.indexOf: ',url.indexOf('/login/') +",", url.indexOf('/login/account'));
    // 如果是登录页面,不需要添加 token

    if (url.indexOf('/login/account') !== -1 || url.indexOf('/login/logout') !== -1) {
    return {
    url: `${url}`,
    options: { ...options, interceptors: true },
    };
    } else {
    const token = getToken();
    console.log('url, token: ', url, token);
    let authHeader = {};
    // 如果 token 存在,就添加到请求头
    if (token) {
    authHeader = { Authorization: `Bearer ${token}` };
    }

    return {
    url: `${url}`,
    options: { ...options, interceptors: true, headers: authHeader },
    };
    }
    };
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2883 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 15:24 · PVG 23:24 · LAX 08:24 · JFK 11:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.