// 定义用户接口 (参考 OpenAPI VasUserOut) export interface User { id: string; email: string; phone?: string; nickname?: string; role?: string; // 关键字段: 'admin' | 'user' avatar_url?: string; } // 获取当前用户 export const getCurrentUser = (): User | null => { if (typeof window === 'undefined') return null; const userStr = localStorage.getItem('user_info'); if (!userStr) return null; try { return JSON.parse(userStr); } catch (e) { return null; } }; // 检查是否是管理员 export const isAdmin = (): boolean => { const user = getCurrentUser(); // 注意:请确认你数据库里的角色字符串是 'admin' 还是 'administrator' 或 'superuser' return user?.role === 'admin'; }; // 退出登录 export const logout = () => { if (typeof window === 'undefined') return; localStorage.removeItem('rsid'); localStorage.removeItem('user_info'); // 触发 storage 事件以便其他组件更新 window.dispatchEvent(new Event('storage')); window.location.href = '/login'; };