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 Twins from './pages/Twins.tsx'
|
||||||
import PlaceholderPage from './components/PlaceholderPage.tsx'
|
import PlaceholderPage from './components/PlaceholderPage.tsx'
|
||||||
import { getConfig, getPackage } from './api/user.ts'
|
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
|
// http://localhost:5173?token=c3f52a99-c519-427a-af05-364c5f3e59cc
|
||||||
function App() {
|
function App() {
|
||||||
|
|
@ -21,8 +21,8 @@ function App() {
|
||||||
|
|
||||||
async function initConfig() {
|
async function initConfig() {
|
||||||
if (initialToken) {
|
if (initialToken) {
|
||||||
window.localStorage.setItem('token', initialToken || '')
|
window.sessionStorage.setItem(TOKEN_STORAGE_KEY, initialToken)
|
||||||
} else if (!window.localStorage.getItem('token')) {
|
} else if (!window.sessionStorage.getItem(TOKEN_STORAGE_KEY)) {
|
||||||
navigate('/twins', { replace: true })
|
navigate('/twins', { replace: true })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,7 @@ declare global {
|
||||||
}
|
}
|
||||||
|
|
||||||
const SYS_INFO_STORAGE_KEY = 'sysInfo'
|
const SYS_INFO_STORAGE_KEY = 'sysInfo'
|
||||||
|
export const TOKEN_STORAGE_KEY = 'token'
|
||||||
export const TEMP_TOKEN_STORAGE_KEY = 'tempToken'
|
export const TEMP_TOKEN_STORAGE_KEY = 'tempToken'
|
||||||
|
|
||||||
function isRecord(value: unknown): value is Record<string, RequestPrimitive> {
|
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-channel', toHeaderValue(sysInfo.channel))
|
||||||
headers.set('x-click-id', toHeaderValue(params.get('bd_vid') ?? sysInfo.bd_vid ?? ''))
|
headers.set('x-click-id', toHeaderValue(params.get('bd_vid') ?? sysInfo.bd_vid ?? ''))
|
||||||
headers.set('x-app-id', toHeaderValue(__APP_ID__))
|
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){
|
if(token){
|
||||||
headers.set('x-token', 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) {
|
function buildQueryString(params?: RequestParams) {
|
||||||
if (!params) {
|
if (!params) {
|
||||||
return ''
|
return ''
|
||||||
|
|
@ -290,6 +301,10 @@ async function request<T>(url: string, method: string, options: RequestOptions =
|
||||||
const responseData: unknown = await response.json()
|
const responseData: unknown = await response.json()
|
||||||
|
|
||||||
if (isApiResponseLike(responseData) && responseData.code !== 0) {
|
if (isApiResponseLike(responseData) && responseData.code !== 0) {
|
||||||
|
if (responseData.code === 1001003) {
|
||||||
|
clearStoredTokens()
|
||||||
|
}
|
||||||
|
|
||||||
const errorMessage = getBusinessErrorMessage(responseData.message)
|
const errorMessage = getBusinessErrorMessage(responseData.message)
|
||||||
showToast(errorMessage)
|
showToast(errorMessage)
|
||||||
throw new Error(errorMessage)
|
throw new Error(errorMessage)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue