/** * 存储参数到浏览器sessionStorage * @param key * @param value */ export function saveToBrowserSession(key: string, value: Record) { try { sessionStorage.setItem(key, JSON.stringify(value)) } catch (error) { console.error('Failed to save to browser session:', error) } } export function getBrowserSession(key: string) { try { const data = JSON.parse(sessionStorage.getItem(key)) return data } catch (error) { console.error('Failed to get browser session:', error) return null } } export function removeBrowserSession(key: string) { try { sessionStorage.removeItem(key) return true } catch (error) { console.error('Failed to remove browser session:', error) return null } } export function addUrlSearch(search: Location['search'], hash?: Location['hash']) { if (search === undefined) return // 1. 解析当前 URL const url = new URL(window.location.href) // 2. 操作查询参数 url.search = search // 覆盖已有参数或新增参数 url.hash = hash || url.hash // 3. 更新地址栏(保留哈希值) window.history.replaceState({}, '', url.href) } /** * 从 URL 中移除指定的查询参数 * @param params 要移除的参数名数组 */ export function removeUrlParams(params: string[]) { if (typeof window === 'undefined') return try { const url = new URL(window.location.href) // 移除指定的参数 params.forEach((param) => { url.searchParams.delete(param) }) // 使用 replaceState 更新 URL,不会在浏览器历史中留下记录 window.history.replaceState({}, '', url.href) } catch (error) { console.error('Failed to remove URL params:', error) } }