add dark mode

This commit is contained in:
landaiqing
2024-08-08 16:39:27 +08:00
parent a4b502717c
commit 562071cdc5
26 changed files with 479 additions and 112 deletions

View File

@@ -10,7 +10,7 @@ const config: globalConfig = {
isEncrypt: true, //支持加密、解密数据处理
};
const setStorage = (key: string, value: any, expire: number = 24 * 60): boolean => {
const setStorage = (key: string, value: null | string, expire: number = 24 * 60): boolean => {
//设定值
if (value === "" || value === null || value === undefined) {
//空值重置
@@ -58,7 +58,7 @@ const getStorageFromKey = (key: string) => {
};
const getAllStorage = () => {
//获取所有值
const storageList: any = {};
const storageList: any= {};
const keys = Object.keys(window[config.type]);
keys.forEach((key) => {
const value = getStorageFromKey(autoRemovePreFix(key));

View File

@@ -10,7 +10,7 @@ const SECRET_IV = CryptoJS.enc.Utf8.parse("e3bbe7e3ba84431a"); //十六位十六
* @param data
* @param output
*/
export const encrypt = (data: string, output?: any) => {
export const encrypt = (data: string, output?: undefined) => {
const dataHex = CryptoJS.enc.Utf8.parse(data);
const encrypted = CryptoJS.AES.encrypt(dataHex, SECRET_KEY, {
iv: SECRET_IV,
@@ -24,11 +24,11 @@ export const encrypt = (data: string, output?: any) => {
* 解密
* @param data
*/
export const decrypt = (data: string | null) => {
export const decrypt = (data: string | unknown) => {
if (data === null) {
return;
}
const encryptedHex = CryptoJS.enc.Hex.parse(data);
const encryptedHex = CryptoJS.enc.Hex.parse(data as string);
const encryptedHexStr = CryptoJS.enc.Base64.stringify(encryptedHex);
const decrypted = CryptoJS.AES.decrypt(encryptedHexStr, SECRET_KEY, {
iv: SECRET_IV,
@@ -41,29 +41,18 @@ export const decrypt = (data: string | null) => {
export const handleLocalforage = {
config: async (options?: LocalForageOptions) => localforage.config(options || {}),
setItem: async (key: string, value: string): Promise<void> => {
await localforage.setItem(key, encrypt(value));
setItem: (key: string, value: string) => {
localforage.setItem(key, encrypt(value));
},
getItem: async function getItem<T>(key: string): Promise<T | string | null> {
try {
const value: any = decrypt(await localforage.getItem(key)) as any;
// 如果值是 undefined返回 null
if (value === undefined) {
return null;
}
// 如果值是 T 类型,直接返回
if (typeof value === "object" && value !== null) {
return value as T;
}
// 如果值是 string 类型,直接返回
return value as string;
} catch (error) {
console.error("Error retrieving data from localforage:", error);
return null;
}
getItem: (key: string) => {
let value: unknown = null;
localforage.getItem(key).then((res: unknown) => {
value = res;
});
return decrypt(value) as string;
},
removeItem: async (key: string): Promise<void> => {
await localforage.removeItem(key);
removeItem: (key: string) => {
localforage.removeItem(key);
},
clear: async () => {
return await localforage.clear();