This commit is contained in:
parent
83e10da103
commit
d394d8d8f2
|
|
@ -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 })
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<string, RequestPrimitive> {
|
||||
|
|
@ -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<T>(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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue