type CurrentEnv = | "localhost" | "test" | "test1" | "test2" | "test3" | "test4" | "test5" | "gray" | "online"; type CurrentEnvBig = 'localhost' | 'test' | 'gray' | 'online' /** 判断当前test环境,包括test、test1-10 */ export const TEST_REG = /(test(\d+)?)/g; export function strRandom(n: number): string { const chars = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ]; let res = ""; for (let i = 0; i < n; i++) { const id = Math.ceil(Math.random() * 35); // 10+24 因为Math.random()取不到1,所以需要往上+1,也许吧 res = res + chars[id]; } return res; } /** * 获取当前环境 * @returns */ export const getCurrentEnv = (): CurrentEnv => { const origin = window.location.origin; if (origin.includes("localhost") || origin.includes("127.0.0.1")) { return "localhost"; } const match = location.origin.match(TEST_REG) ?? []; if (match.length === 1) { return match[0] as CurrentEnv; // 返回 test、 testN 中一个 } if (location.host.search("gray.") > -1) { return "gray"; } return "online"; }; /** * 获取当前大环境,test1-n 归为test * @returns */ export const getCurrentEnvBig = (): CurrentEnvBig => { const origin = window.location.origin if (origin.includes('localhost') || origin.includes('127.0.0.1') || origin.includes('192.168.')) { return 'localhost' } const match = location.origin.match(TEST_REG) ?? [] if (match.length === 1) { return 'test' } if (location.host.search('gray.') > -1) { return 'gray' } return 'online' } export function getUrlTicket(): string | undefined { const r = window.location.search.substring(1).match(/ticket=([^?&#]*)/g); if (r !== null && r.length > 0) { const ticketStr = r.pop()?.replace("ticket=", ""); if (ticketStr !== undefined) { return decodeURI(ticketStr); } } } // 从sessionStorage获取对象 export function getSessionStorage(name: string): T | null { const localStr = sessionStorage.getItem(name); if (localStr) { try { return JSON.parse(localStr) as T; } catch { return null; } } return null; } // 将数据写入sessionStorage export function setSessionStorage(name: string, data: unknown): void { sessionStorage.setItem(name, JSON.stringify(data)); } export function removeSessionStorage(name: string) { sessionStorage.removeItem(name); }