/** 小程序 web-view 通过 URL ?token= 传入登录态;落盘避免路由跳转后丢失 */ const TOKEN_KEY = 'zkxw_oa_h5_token' function readQueryToken() { try { const q = new URLSearchParams(window.location.search || '') return String(q.get('token') || '').trim() } catch { return '' } } /** 启动时调用:把 URL 中的 token 写入 sessionStorage,并尽量从地址栏摘掉敏感参数 */ export function captureTokenFromUrl() { const fromQuery = readQueryToken() if (fromQuery) { try { sessionStorage.setItem(TOKEN_KEY, fromQuery) } catch { /* ignore quota / private mode */ } try { const url = new URL(window.location.href) if (url.searchParams.has('token')) { url.searchParams.delete('token') const next = `${url.pathname}${url.search}${url.hash}` window.history.replaceState(window.history.state, '', next) } } catch { /* ignore */ } return fromQuery } return getToken() } export function getToken() { try { const cached = String(sessionStorage.getItem(TOKEN_KEY) || '').trim() if (cached) return cached } catch { /* ignore */ } return readQueryToken() }