我自己打自己脸了,接口请求路径是 /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 },
};
}
};