/**
 * 本地缓存工具
 * 所有 key 自动加上 QY_ 前缀避免冲突
 */

const PREFIX = 'QY_';

export const setCache = (key: string, value: any) => {
    if (!key) throw new Error('cache key is required');
    uni.setStorageSync(`${PREFIX}${key.toUpperCase()}`, value);
};

export const getCache = (key: string) => {
    if (!key) throw new Error('cache key is required');
    return uni.getStorageSync(`${PREFIX}${key.toUpperCase()}`);
};

export const removeCache = (key: string) => {
    if (!key) throw new Error('cache key is required');
    uni.removeStorageSync(`${PREFIX}${key.toUpperCase()}`);
};

export const clearCache = () => {
    uni.clearStorageSync();
};

// 常用 key 常量
export const CACHE_KEYS = {
    TOKEN: 'token',
    USER_INFO: 'userInfo',
    ROLE: 'role',
} as const;
