xref: /MusicFree/src/utils/storage.ts (revision cf2d630eca034addcb92030a5268105bce30ef82)
1242960d3S猫头猫import {errorLog} from '@/utils/log';
2242960d3S猫头猫import AsyncStorage from '@react-native-async-storage/async-storage';
3242960d3S猫头猫
4242960d3S猫头猫export async function setStorage(key: string, value: any) {
5242960d3S猫头猫    try {
6242960d3S猫头猫        await AsyncStorage.setItem(key, JSON.stringify(value, null, ''));
7242960d3S猫头猫    } catch (e: any) {
8*b50427a2S猫头猫        errorLog(`存储失败${key}`, e?.message);
9242960d3S猫头猫    }
10242960d3S猫头猫}
11242960d3S猫头猫
12242960d3S猫头猫export async function getStorage(key: string) {
13242960d3S猫头猫    try {
14242960d3S猫头猫        const result = await AsyncStorage.getItem(key);
15242960d3S猫头猫        if (result) {
16242960d3S猫头猫            return JSON.parse(result);
17242960d3S猫头猫        }
18242960d3S猫头猫    } catch {}
19242960d3S猫头猫    return null;
20242960d3S猫头猫}
21242960d3S猫头猫
22242960d3S猫头猫export async function getMultiStorage(keys: string[]) {
23242960d3S猫头猫    if (keys.length === 0) {
24242960d3S猫头猫        return [];
25242960d3S猫头猫    }
26242960d3S猫头猫    const result = await AsyncStorage.multiGet(keys);
27242960d3S猫头猫
28242960d3S猫头猫    return result.map(_ => {
29242960d3S猫头猫        try {
30242960d3S猫头猫            if (_[1]) {
31242960d3S猫头猫                return JSON.parse(_[1]);
32242960d3S猫头猫            }
33242960d3S猫头猫            return null;
34242960d3S猫头猫        } catch {
35242960d3S猫头猫            return null;
36242960d3S猫头猫        }
37242960d3S猫头猫    });
38242960d3S猫头猫}
39242960d3S猫头猫
40242960d3S猫头猫export async function removeStorage(key: string) {
41242960d3S猫头猫    return AsyncStorage.removeItem(key);
42242960d3S猫头猫}
43