@@ -4,7 +4,7 @@ import axios, {
44 type AxiosResponse ,
55 type InternalAxiosRequestConfig ,
66} from 'axios'
7- import { authConfig } from '@/config/request.config '
7+ import { clearAuth , getAuthToken , getClientIdFromStorage } from '@/utils/auth '
88import type { ApiResponse , RequestOptions , ErrorResponse } from '@/types/request.types'
99
1010export class RequestCore {
@@ -17,18 +17,6 @@ export class RequestCore {
1717 this . setupInterceptors ( )
1818 }
1919
20- /**
21- * 获取认证令牌
22- */
23- private getAuthToken ( ) : string | null {
24- try {
25- return localStorage . getItem ( authConfig . tokenKey )
26- } catch ( error ) {
27- console . warn ( 'Failed to get auth token from localStorage:' , error )
28- return null
29- }
30- }
31-
3220 /**
3321 * 设置请求拦截器
3422 */
@@ -37,13 +25,16 @@ export class RequestCore {
3725 this . requestInterceptor = this . instance . interceptors . request . use (
3826 ( config : InternalAxiosRequestConfig ) => {
3927 // 获取认证令牌
40- const token = this . getAuthToken ( )
28+ const token = getAuthToken ( )
29+ const clientId = getClientIdFromStorage ( )
4130
4231 // 添加认证头
4332 if ( token ) {
4433 config . headers = config . headers || { }
4534 config . headers . Authorization = `Bearer ${ token } `
46- config . headers . clientId = authConfig . clientId
35+ if ( clientId ) {
36+ config . headers . clientid = clientId
37+ }
4738 }
4839
4940 // 这里可以添加其他全局请求处理逻辑
@@ -116,8 +107,7 @@ export class RequestCore {
116107 * 处理认证失败
117108 */
118109 private handleUnauthorized ( ) : void {
119- // 清除本地认证信息
120- localStorage . removeItem ( authConfig . tokenKey )
110+ clearAuth ( )
121111
122112 // 这里可以添加跳转到登录页的逻辑
123113 console . warn ( 'Authentication failed, redirecting to login...' )
0 commit comments