This commit is contained in:
Eric
2026-02-09 11:24:51 +08:00
commit f2173a9fa9
491 changed files with 43791 additions and 0 deletions

View File

@@ -0,0 +1,108 @@
/**
* 存储工具类
* 支持localStorage、sessionStorage和cookie三种存储方式
*/
type StorageType = 'localStorage' | 'sessionStorage' | 'cookie';
/**
* 存储工具类
*/
export declare class Storage {
private storageType;
private prefix;
/**
* 构造函数
* @param storageType 存储类型
* @param prefix 存储前缀,默认'unified_login_'
*/
constructor(storageType?: StorageType, prefix?: string);
/**
* 设置存储项
* @param key 存储键
* @param value 存储值
* @param options 可选参数cookie存储时使用
*/
set(key: string, value: any, options?: {
expires?: number;
path?: string;
domain?: string;
secure?: boolean;
}): void;
/**
* 获取存储项
* @param key 存储键
* @returns 存储值
*/
get(key: string): any;
/**
* 移除存储项
* @param key 存储键
*/
remove(key: string): void;
/**
* 清空所有存储项
*/
clear(): void;
/**
* 检查存储类型是否可用
* @returns boolean 是否可用
*/
isAvailable(): boolean;
/**
* 设置localStorage
*/
private setLocalStorage;
/**
* 获取localStorage
*/
private getLocalStorage;
/**
* 移除localStorage
*/
private removeLocalStorage;
/**
* 清空localStorage中所有带前缀的项
*/
private clearLocalStorage;
/**
* 检查localStorage是否可用
*/
private isLocalStorageAvailable;
/**
* 设置sessionStorage
*/
private setSessionStorage;
/**
* 获取sessionStorage
*/
private getSessionStorage;
/**
* 移除sessionStorage
*/
private removeSessionStorage;
/**
* 清空sessionStorage中所有带前缀的项
*/
private clearSessionStorage;
/**
* 检查sessionStorage是否可用
*/
private isSessionStorageAvailable;
/**
* 设置cookie
*/
private setCookie;
/**
* 获取cookie
*/
private getCookie;
/**
* 移除cookie
*/
private removeCookie;
/**
* 清空所有带前缀的cookie
*/
private clearCookie;
}
export {};
//# sourceMappingURL=storage.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../src/utils/storage.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,KAAK,WAAW,GAAG,cAAc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 +1,316 @@
/**
* 存储工具类
* 支持localStorage、sessionStorage和cookie三种存储方式
*/
/**
* 存储工具类
*/
export class Storage {
/**
* 构造函数
* @param storageType 存储类型
* @param prefix 存储前缀,默认'unified_login_'
*/
constructor(storageType = 'localStorage', prefix = 'unified_login_') {
this.storageType = storageType;
this.prefix = prefix;
}
/**
* 设置存储项
* @param key 存储键
* @param value 存储值
* @param options 可选参数cookie存储时使用
*/
set(key, value, options) {
const fullKey = this.prefix + key;
const stringValue = typeof value === 'string' ? value : JSON.stringify(value);
switch (this.storageType) {
case 'localStorage':
this.setLocalStorage(fullKey, stringValue);
break;
case 'sessionStorage':
this.setSessionStorage(fullKey, stringValue);
break;
case 'cookie':
this.setCookie(fullKey, stringValue, options);
break;
}
}
/**
* 获取存储项
* @param key 存储键
* @returns 存储值
*/
get(key) {
const fullKey = this.prefix + key;
let value;
switch (this.storageType) {
case 'localStorage':
value = this.getLocalStorage(fullKey);
break;
case 'sessionStorage':
value = this.getSessionStorage(fullKey);
break;
case 'cookie':
value = this.getCookie(fullKey);
break;
default:
value = null;
}
if (value === null) {
return null;
}
// 尝试解析JSON
try {
return JSON.parse(value);
}
catch (e) {
// 如果不是JSON直接返回字符串
return value;
}
}
/**
* 移除存储项
* @param key 存储键
*/
remove(key) {
const fullKey = this.prefix + key;
switch (this.storageType) {
case 'localStorage':
this.removeLocalStorage(fullKey);
break;
case 'sessionStorage':
this.removeSessionStorage(fullKey);
break;
case 'cookie':
this.removeCookie(fullKey);
break;
}
}
/**
* 清空所有存储项
*/
clear() {
switch (this.storageType) {
case 'localStorage':
this.clearLocalStorage();
break;
case 'sessionStorage':
this.clearSessionStorage();
break;
case 'cookie':
this.clearCookie();
break;
}
}
/**
* 检查存储类型是否可用
* @returns boolean 是否可用
*/
isAvailable() {
try {
switch (this.storageType) {
case 'localStorage':
return this.isLocalStorageAvailable();
case 'sessionStorage':
return this.isSessionStorageAvailable();
case 'cookie':
return typeof document !== 'undefined';
default:
return false;
}
}
catch (e) {
return false;
}
}
// ------------------------ localStorage 操作 ------------------------
/**
* 设置localStorage
*/
setLocalStorage(key, value) {
if (this.isLocalStorageAvailable()) {
localStorage.setItem(key, value);
}
}
/**
* 获取localStorage
*/
getLocalStorage(key) {
if (this.isLocalStorageAvailable()) {
return localStorage.getItem(key);
}
return null;
}
/**
* 移除localStorage
*/
removeLocalStorage(key) {
if (this.isLocalStorageAvailable()) {
localStorage.removeItem(key);
}
}
/**
* 清空localStorage中所有带前缀的项
*/
clearLocalStorage() {
if (this.isLocalStorageAvailable()) {
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key && key.startsWith(this.prefix)) {
localStorage.removeItem(key);
i--; // 索引调整
}
}
}
}
/**
* 检查localStorage是否可用
*/
isLocalStorageAvailable() {
if (typeof localStorage === 'undefined') {
return false;
}
try {
const testKey = '__storage_test__';
localStorage.setItem(testKey, testKey);
localStorage.removeItem(testKey);
return true;
}
catch (e) {
return false;
}
}
// ------------------------ sessionStorage 操作 ------------------------
/**
* 设置sessionStorage
*/
setSessionStorage(key, value) {
if (this.isSessionStorageAvailable()) {
sessionStorage.setItem(key, value);
}
}
/**
* 获取sessionStorage
*/
getSessionStorage(key) {
if (this.isSessionStorageAvailable()) {
return sessionStorage.getItem(key);
}
return null;
}
/**
* 移除sessionStorage
*/
removeSessionStorage(key) {
if (this.isSessionStorageAvailable()) {
sessionStorage.removeItem(key);
}
}
/**
* 清空sessionStorage中所有带前缀的项
*/
clearSessionStorage() {
if (this.isSessionStorageAvailable()) {
for (let i = 0; i < sessionStorage.length; i++) {
const key = sessionStorage.key(i);
if (key && key.startsWith(this.prefix)) {
sessionStorage.removeItem(key);
i--; // 索引调整
}
}
}
}
/**
* 检查sessionStorage是否可用
*/
isSessionStorageAvailable() {
if (typeof sessionStorage === 'undefined') {
return false;
}
try {
const testKey = '__storage_test__';
sessionStorage.setItem(testKey, testKey);
sessionStorage.removeItem(testKey);
return true;
}
catch (e) {
return false;
}
}
// ------------------------ cookie 操作 ------------------------
/**
* 设置cookie
*/
setCookie(key, value, options) {
if (typeof document === 'undefined') {
return;
}
let cookieString = `${key}=${encodeURIComponent(value)}`;
if (options) {
// 设置过期时间(秒)
if (options.expires) {
const date = new Date();
date.setTime(date.getTime() + options.expires * 1000);
cookieString += `; expires=${date.toUTCString()}`;
}
// 设置路径
if (options.path) {
cookieString += `; path=${options.path}`;
}
// 设置域名
if (options.domain) {
cookieString += `; domain=${options.domain}`;
}
// 设置secure
if (options.secure) {
cookieString += '; secure';
}
}
document.cookie = cookieString;
}
/**
* 获取cookie
*/
getCookie(key) {
if (typeof document === 'undefined') {
return null;
}
const name = `${key}=`;
const decodedCookie = decodeURIComponent(document.cookie);
const ca = decodedCookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return null;
}
/**
* 移除cookie
*/
removeCookie(key) {
this.setCookie(key, '', { expires: -1 });
}
/**
* 清空所有带前缀的cookie
*/
clearCookie() {
if (typeof document === 'undefined') {
return;
}
const cookies = document.cookie.split(';');
for (const cookie of cookies) {
const eqPos = cookie.indexOf('=');
const key = eqPos > -1 ? cookie.substr(0, eqPos).trim() : cookie.trim();
if (key.startsWith(this.prefix)) {
this.removeCookie(key);
}
}
}
}
//# sourceMappingURL=storage.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,55 @@
/**
* URL处理工具
* 用于生成授权URL、解析URL参数等功能
*/
/**
* 生成随机字符串
* @param length 字符串长度默认32位
* @returns 随机字符串
*/
export declare function generateRandomString(length?: number): string;
/**
* 解析URL查询参数
* @param url URL字符串默认为当前URL
* @returns 查询参数对象
*/
export declare function parseQueryParams(url?: string): Record<string, string>;
/**
* 构建URL查询参数
* @param params 查询参数对象
* @returns 查询参数字符串
*/
export declare function buildQueryParams(params: Record<string, any>): string;
/**
* 生成OAuth2授权URL
* @param authorizationEndpoint 授权端点URL
* @param clientId 客户端ID
* @param redirectUri 重定向URL
* @param options 可选参数
* @returns 授权URL
*/
export declare function generateAuthorizationUrl(authorizationEndpoint: string, clientId: string, redirectUri: string, options?: {
responseType?: string;
scope?: string;
state?: string;
[key: string]: any;
}): string;
/**
* 检查当前URL是否为授权回调
* @param url URL字符串默认为当前URL
* @returns 是否为授权回调
*/
export declare function isCallbackUrl(url?: string): boolean;
/**
* 获取当前URL的路径名
* @param url URL字符串默认为当前URL
* @returns 路径名
*/
export declare function getPathname(url?: string): string;
/**
* 获取当前URL的主机名
* @param url URL字符串默认为当前URL
* @returns 主机名
*/
export declare function getHostname(url?: string): string;
//# sourceMappingURL=url.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"url.d.ts","sourceRoot":"","sources":["../../src/utils/url.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,GAAE,MAAW,GAAG,MAAM,CAOhE;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,GAAE,MAA6B,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAgB3F;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAQpE;AAED;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CACtC,qBAAqB,EAAE,MAAM,EAC7B,QAAQ,EAAE,MAAM,EAChB,WAAW,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE;IACR,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB,GACA,MAAM,CAmBR;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,GAAG,GAAE,MAA6B,GAAG,OAAO,CAGzE;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,GAAG,GAAE,MAA6B,GAAG,MAAM,CAGtE;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,GAAG,GAAE,MAA6B,GAAG,MAAM,CAGtE"}

View File

@@ -0,0 +1,100 @@
/**
* URL处理工具
* 用于生成授权URL、解析URL参数等功能
*/
/**
* 生成随机字符串
* @param length 字符串长度默认32位
* @returns 随机字符串
*/
export function generateRandomString(length = 32) {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
/**
* 解析URL查询参数
* @param url URL字符串默认为当前URL
* @returns 查询参数对象
*/
export function parseQueryParams(url = window.location.href) {
const params = {};
const queryString = url.split('?')[1];
if (!queryString) {
return params;
}
const pairs = queryString.split('&');
for (const pair of pairs) {
const [key, value] = pair.split('=');
if (key) {
params[decodeURIComponent(key)] = decodeURIComponent(value || '');
}
}
return params;
}
/**
* 构建URL查询参数
* @param params 查询参数对象
* @returns 查询参数字符串
*/
export function buildQueryParams(params) {
const pairs = [];
for (const [key, value] of Object.entries(params)) {
if (value !== undefined && value !== null) {
pairs.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
}
}
return pairs.length ? `?${pairs.join('&')}` : '';
}
/**
* 生成OAuth2授权URL
* @param authorizationEndpoint 授权端点URL
* @param clientId 客户端ID
* @param redirectUri 重定向URL
* @param options 可选参数
* @returns 授权URL
*/
export function generateAuthorizationUrl(authorizationEndpoint, clientId, redirectUri, options) {
const { responseType = 'code', scope, state = generateRandomString(32), ...extraParams } = options || {};
const params = {
client_id: clientId,
redirect_uri: redirectUri,
response_type: responseType,
state,
...(scope ? { scope } : {}),
...extraParams
};
const queryString = buildQueryParams(params);
return `${authorizationEndpoint}${queryString}`;
}
/**
* 检查当前URL是否为授权回调
* @param url URL字符串默认为当前URL
* @returns 是否为授权回调
*/
export function isCallbackUrl(url = window.location.href) {
const params = parseQueryParams(url);
return !!params.code || !!params.error;
}
/**
* 获取当前URL的路径名
* @param url URL字符串默认为当前URL
* @returns 路径名
*/
export function getPathname(url = window.location.href) {
const urlObj = new URL(url);
return urlObj.pathname;
}
/**
* 获取当前URL的主机名
* @param url URL字符串默认为当前URL
* @returns 主机名
*/
export function getHostname(url = window.location.href) {
const urlObj = new URL(url);
return urlObj.hostname;
}
//# sourceMappingURL=url.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"url.js","sourceRoot":"","sources":["../../src/utils/url.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAAC,SAAiB,EAAE;IACtD,MAAM,KAAK,GAAG,gEAAgE,CAAC;IAC/E,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;IACnE,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAc,MAAM,CAAC,QAAQ,CAAC,IAAI;IACjE,MAAM,MAAM,GAA2B,EAAE,CAAC;IAC1C,MAAM,WAAW,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IACtC,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,MAAM,KAAK,GAAG,WAAW,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACrC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG,IA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