From d394d8d8f287b3531d5c0895f075fbc7ae786188 Mon Sep 17 00:00:00 2001 From: zhangjianjun Date: Mon, 20 Jul 2026 15:02:23 +0800 Subject: [PATCH] ud --- src/App.tsx | 6 +++--- src/utils/request.ts | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index d62cbee..ef9962f 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -6,7 +6,7 @@ import PayResultPage from './pages/PayResultPage.tsx' import Twins from './pages/Twins.tsx' import PlaceholderPage from './components/PlaceholderPage.tsx' import { getConfig, getPackage } from './api/user.ts' -import { TEMP_TOKEN_STORAGE_KEY } from './utils/request.ts' +import { TEMP_TOKEN_STORAGE_KEY, TOKEN_STORAGE_KEY } from './utils/request.ts' // http://localhost:5173?token=c3f52a99-c519-427a-af05-364c5f3e59cc function App() { @@ -21,8 +21,8 @@ function App() { async function initConfig() { if (initialToken) { - window.localStorage.setItem('token', initialToken || '') - } else if (!window.localStorage.getItem('token')) { + window.sessionStorage.setItem(TOKEN_STORAGE_KEY, initialToken) + } else if (!window.sessionStorage.getItem(TOKEN_STORAGE_KEY)) { navigate('/twins', { replace: true }) } diff --git a/src/utils/request.ts b/src/utils/request.ts index 367a595..c12d788 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -38,6 +38,7 @@ declare global { } const SYS_INFO_STORAGE_KEY = 'sysInfo' +export const TOKEN_STORAGE_KEY = 'token' export const TEMP_TOKEN_STORAGE_KEY = 'tempToken' function isRecord(value: unknown): value is Record { @@ -166,12 +167,22 @@ function setCommonHeaders(headers: Headers) { headers.set('x-channel', toHeaderValue(sysInfo.channel)) headers.set('x-click-id', toHeaderValue(params.get('bd_vid') ?? sysInfo.bd_vid ?? '')) headers.set('x-app-id', toHeaderValue(__APP_ID__)) - const token = window.localStorage.getItem('token') || window.sessionStorage.getItem(TEMP_TOKEN_STORAGE_KEY) + const token = + window.sessionStorage.getItem(TOKEN_STORAGE_KEY) || window.sessionStorage.getItem(TEMP_TOKEN_STORAGE_KEY) if(token){ headers.set('x-token', token) } } +function clearStoredTokens() { + if (typeof window === 'undefined') { + return + } + + window.sessionStorage.removeItem(TOKEN_STORAGE_KEY) + window.sessionStorage.removeItem(TEMP_TOKEN_STORAGE_KEY) +} + function buildQueryString(params?: RequestParams) { if (!params) { return '' @@ -290,6 +301,10 @@ async function request(url: string, method: string, options: RequestOptions = const responseData: unknown = await response.json() if (isApiResponseLike(responseData) && responseData.code !== 0) { + if (responseData.code === 1001003) { + clearStoredTokens() + } + const errorMessage = getBusinessErrorMessage(responseData.message) showToast(errorMessage) throw new Error(errorMessage)