xref: /MusicFree/src/utils/storage.ts (revision b50427a2113c3b0d9efb6cb4e9cb65a118ae9bda)
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 {
6*b50427a2S猫头猫        console.log('KEY', key, 'VALUE', JSON.stringify(value, null, ''));
7242960d3S猫头猫        await AsyncStorage.setItem(key, JSON.stringify(value, null, ''));
8242960d3S猫头猫    } catch (e: any) {
9*b50427a2S猫头猫        errorLog(`存储失败${key}`, e?.message);
10*b50427a2S猫头猫        console.log('????', key, e);
11242960d3S猫头猫    }
12242960d3S猫头猫}
13242960d3S猫头猫
14242960d3S猫头猫export async function getStorage(key: string) {
15242960d3S猫头猫    try {
16242960d3S猫头猫        const result = await AsyncStorage.getItem(key);
17242960d3S猫头猫        if (result) {
18242960d3S猫头猫            return JSON.parse(result);
19242960d3S猫头猫        }
20242960d3S猫头猫    } catch {}
21242960d3S猫头猫    return null;
22242960d3S猫头猫}
23242960d3S猫头猫
24242960d3S猫头猫export async function getMultiStorage(keys: string[]) {
25242960d3S猫头猫    if (keys.length === 0) {
26242960d3S猫头猫        return [];
27242960d3S猫头猫    }
28242960d3S猫头猫    const result = await AsyncStorage.multiGet(keys);
29242960d3S猫头猫
30242960d3S猫头猫    return result.map(_ => {
31242960d3S猫头猫        try {
32242960d3S猫头猫            if (_[1]) {
33242960d3S猫头猫                return JSON.parse(_[1]);
34242960d3S猫头猫            }
35242960d3S猫头猫            return null;
36242960d3S猫头猫        } catch {
37242960d3S猫头猫            return null;
38242960d3S猫头猫        }
39242960d3S猫头猫    });
40242960d3S猫头猫}
41242960d3S猫头猫
42242960d3S猫头猫export async function removeStorage(key: string) {
43242960d3S猫头猫    return AsyncStorage.removeItem(key);
44242960d3S猫头猫}
45