diff --git a/packages/utils/Storage/getLocalStorage.ts b/packages/utils/Storage/getLocalStorage.ts new file mode 100644 index 0000000..0cab9be --- /dev/null +++ b/packages/utils/Storage/getLocalStorage.ts @@ -0,0 +1,20 @@ +import { isSupport } from '../Type/index' +import { removeLocalStorage } from './removeLocalStorage' + +/** + * 从localStorage中获取存储的数据,如果数据已过期,则删除该数据 + * @param {string} key - 要获取的数据的键 + * @returns {any} 返回存储的数据,如果数据不存在或已过期则返回null + * @example + * console.log(getLocalStorage("userInfo")); // 输出存储的用户信息或null + */ +export function getLocalStorage(key: string) { + if (isSupport('localStorage')) { + const data = JSON.parse(localStorage.getItem(key) ?? '{}') + if (data.expiredTime > new Date().getTime()) { + return data.data + } + removeLocalStorage(key) + return null + } +} diff --git a/packages/utils/Storage/index.ts b/packages/utils/Storage/index.ts new file mode 100644 index 0000000..f877fab --- /dev/null +++ b/packages/utils/Storage/index.ts @@ -0,0 +1 @@ +export * from './setLocalStorage' diff --git a/packages/utils/Storage/removeLocalStorage.ts b/packages/utils/Storage/removeLocalStorage.ts new file mode 100644 index 0000000..b6738aa --- /dev/null +++ b/packages/utils/Storage/removeLocalStorage.ts @@ -0,0 +1,9 @@ +/** + * 从localStorage中删除指定的数据 + * @param {string} key - 要删除的数据的键 + * @example + * removeLocalStorage("userInfo"); // 删除存储的用户信息 + */ +export function removeLocalStorage(key: string) { + localStorage.removeItem(key) +} diff --git a/packages/utils/Storage/setLocalStorage.ts b/packages/utils/Storage/setLocalStorage.ts new file mode 100644 index 0000000..39870df --- /dev/null +++ b/packages/utils/Storage/setLocalStorage.ts @@ -0,0 +1,16 @@ +import { isSupport } from '../Type/index' + +/** + * 将数据存储在localStorage中,如果浏览器支持的话 + * @param {string} key - 存储数据使用的键 + * @param {string} data - 要存储的数据 + * @param {number} expiredTime - 数据过期时间(秒) + * @example + * setLocalStorage("userInfo", JSON.stringify({name: "John"}), 3600); // 将用户信息存储1小时 + */ +export function setLocalStorage(key: string, data: string, expiredTime: number) { + if (isSupport('localStorage')) { + const expired = new Date().getTime() + expiredTime * 1000 + localStorage.setItem(key, JSON.stringify({ data, expired })) + } +} diff --git a/packages/utils/Type/index.ts b/packages/utils/Type/index.ts index 42e9fcd..4be2e65 100644 --- a/packages/utils/Type/index.ts +++ b/packages/utils/Type/index.ts @@ -1,2 +1,3 @@ export * from './isString' -export * from './isNumber' \ No newline at end of file +export * from './isNumber' +export * from './isSupport' diff --git a/packages/utils/Type/isSupport.ts b/packages/utils/Type/isSupport.ts new file mode 100644 index 0000000..9d7b3b3 --- /dev/null +++ b/packages/utils/Type/isSupport.ts @@ -0,0 +1,16 @@ +/** + * 检查当前浏览器是否支持指定的API + * @param {string} api - 要检查的API名称 + * @param {boolean} [error=true] - 是否在不支持API时输出错误信息 + * @returns {boolean} 如果浏览器支持该API,则返回true,否则返回false + * @example + * console.log(isSupport("localStorage")); // 输出 true 或 false + * console.log(isSupport("localStorage", false)); // 输出 true 或 false,不输出错误信息 + */ +export function isSupport(api: string, error = true): boolean { + if (!(api in window)) { + error && console.error(`${api} is not supported by this browser.`) + return false + } + return true +}