refactor: 优化认证系统和组件类型安全性
- 新增 safeLocalStorage 工具函数增强本地存储安全性 - 简化认证流程,重构用户数据结构和 token 管理 - 修复多个模块的 TypeScript 类型错误和导入问题 - 优化状态管理,重构各模块 store 结构 - 清理冗余代码,移除未使用的组件和函数 - 改进错误处理和边界情况处理 - 更新配置文件以支持最新的类型检查 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,76 +1,64 @@
|
||||
import { create } from 'zustand'
|
||||
import { persist } from 'zustand/middleware'
|
||||
import { create } from 'zustand';
|
||||
import { persist } from 'zustand/middleware';
|
||||
|
||||
interface GlobalState {
|
||||
// 主题设置
|
||||
theme: 'light' | 'dark' | 'auto'
|
||||
|
||||
// 语言设置
|
||||
language: 'zh-CN' | 'en-US'
|
||||
|
||||
// 侧边栏状态
|
||||
sidebarCollapsed: boolean
|
||||
|
||||
// 全局加载状态
|
||||
globalLoading: boolean
|
||||
|
||||
// 面包屑导航
|
||||
breadcrumbs: Array<{
|
||||
title: string
|
||||
path?: string
|
||||
}>
|
||||
|
||||
// 通知消息
|
||||
notifications: Array<{
|
||||
id: string
|
||||
type: 'success' | 'error' | 'warning' | 'info'
|
||||
title: string
|
||||
message: string
|
||||
duration?: number
|
||||
timestamp: number
|
||||
}>
|
||||
|
||||
// 当前选中的租户(如果是多租户系统)
|
||||
currentTenant: {
|
||||
id: string
|
||||
name: string
|
||||
code: string
|
||||
} | null
|
||||
// 租户信息接口
|
||||
export interface Tenant {
|
||||
id: string;
|
||||
name: string;
|
||||
code: string;
|
||||
}
|
||||
|
||||
interface GlobalActions {
|
||||
// 主题操作
|
||||
setTheme: (theme: 'light' | 'dark' | 'auto') => void
|
||||
|
||||
// 语言操作
|
||||
setLanguage: (language: 'zh-CN' | 'en-US') => void
|
||||
|
||||
// 侧边栏操作
|
||||
toggleSidebar: () => void
|
||||
setSidebarCollapsed: (collapsed: boolean) => void
|
||||
|
||||
// 全局加载状态
|
||||
setGlobalLoading: (loading: boolean) => void
|
||||
|
||||
// 面包屑操作
|
||||
setBreadcrumbs: (breadcrumbs: Array<{ title: string; path?: string }>) => void
|
||||
addBreadcrumb: (breadcrumb: { title: string; path?: string }) => void
|
||||
|
||||
// 通知操作
|
||||
addNotification: (notification: {
|
||||
type: 'success' | 'error' | 'warning' | 'info'
|
||||
title: string
|
||||
message: string
|
||||
duration?: number
|
||||
}) => void
|
||||
removeNotification: (id: string) => void
|
||||
clearNotifications: () => void
|
||||
|
||||
// 租户操作
|
||||
setCurrentTenant: (tenant: { id: string; name: string; code: string } | null) => void
|
||||
// 面包屑接口
|
||||
export interface Breadcrumb {
|
||||
title: string;
|
||||
path?: string;
|
||||
}
|
||||
|
||||
export const useGlobalStore = create<GlobalState & GlobalActions>()(
|
||||
// 通知接口
|
||||
export interface Notification {
|
||||
id: string;
|
||||
type: 'success' | 'error' | 'warning' | 'info';
|
||||
title: string;
|
||||
message: string;
|
||||
duration?: number;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
// Global state interface
|
||||
export interface GlobalState {
|
||||
theme: 'light' | 'dark' | 'auto';
|
||||
language: 'zh-CN' | 'en-US';
|
||||
sidebarCollapsed: boolean;
|
||||
globalLoading: boolean;
|
||||
breadcrumbs: Breadcrumb[];
|
||||
notifications: Notification[];
|
||||
currentTenant: Tenant | null;
|
||||
|
||||
// Actions
|
||||
setTheme: (theme: 'light' | 'dark' | 'auto') => void;
|
||||
setLanguage: (language: 'zh-CN' | 'en-US') => void;
|
||||
toggleSidebar: () => void;
|
||||
setSidebarCollapsed: (collapsed: boolean) => void;
|
||||
setGlobalLoading: (loading: boolean) => void;
|
||||
setBreadcrumbs: (breadcrumbs: Breadcrumb[]) => void;
|
||||
addBreadcrumb: (breadcrumb: Breadcrumb) => void;
|
||||
addNotification: (notification: Omit<Notification, 'id' | 'timestamp'>) => void;
|
||||
removeNotification: (id: string) => void;
|
||||
clearNotifications: () => void;
|
||||
setCurrentTenant: (tenant: Tenant | null) => void;
|
||||
|
||||
// Getters
|
||||
getTheme: () => 'light' | 'dark' | 'auto';
|
||||
getLanguage: () => 'zh-CN' | 'en-US';
|
||||
getSidebarCollapsed: () => boolean;
|
||||
getGlobalLoading: () => boolean;
|
||||
getBreadcrumbs: () => Breadcrumb[];
|
||||
getNotifications: () => Notification[];
|
||||
getCurrentTenant: () => Tenant | null;
|
||||
}
|
||||
|
||||
// Create Global store
|
||||
export const useGlobalStore = create<GlobalState>()(
|
||||
persist(
|
||||
(set, get) => ({
|
||||
// 初始状态
|
||||
@@ -82,55 +70,50 @@ export const useGlobalStore = create<GlobalState & GlobalActions>()(
|
||||
notifications: [],
|
||||
currentTenant: null,
|
||||
|
||||
// 主题操作
|
||||
// Actions
|
||||
setTheme: (theme) => set({ theme }),
|
||||
|
||||
// 语言操作
|
||||
setLanguage: (language) => set({ language }),
|
||||
|
||||
// 侧边栏操作
|
||||
toggleSidebar: () => set((state) => ({ sidebarCollapsed: !state.sidebarCollapsed })),
|
||||
setSidebarCollapsed: (collapsed) => set({ sidebarCollapsed: collapsed }),
|
||||
|
||||
// 全局加载状态
|
||||
setGlobalLoading: (loading) => set({ globalLoading: loading }),
|
||||
|
||||
// 面包屑操作
|
||||
setBreadcrumbs: (breadcrumbs) => set({ breadcrumbs }),
|
||||
addBreadcrumb: (breadcrumb) => set((state) => ({
|
||||
breadcrumbs: [...state.breadcrumbs, breadcrumb]
|
||||
})),
|
||||
|
||||
// 通知操作
|
||||
addNotification: (notification) => {
|
||||
const id = Date.now().toString()
|
||||
const id = Date.now().toString();
|
||||
const newNotification = {
|
||||
...notification,
|
||||
id,
|
||||
timestamp: Date.now(),
|
||||
duration: notification.duration || 4500
|
||||
}
|
||||
};
|
||||
|
||||
set((state) => ({
|
||||
notifications: [...state.notifications, newNotification]
|
||||
}))
|
||||
}));
|
||||
|
||||
// 自动移除通知
|
||||
if (newNotification.duration && newNotification.duration > 0) {
|
||||
setTimeout(() => {
|
||||
get().removeNotification(id)
|
||||
}, newNotification.duration)
|
||||
get().removeNotification(id);
|
||||
}, newNotification.duration);
|
||||
}
|
||||
},
|
||||
|
||||
removeNotification: (id) => set((state) => ({
|
||||
notifications: state.notifications.filter(n => n.id !== id)
|
||||
})),
|
||||
|
||||
clearNotifications: () => set({ notifications: [] }),
|
||||
setCurrentTenant: (tenant) => set({ currentTenant: tenant }),
|
||||
|
||||
// 租户操作
|
||||
setCurrentTenant: (tenant) => set({ currentTenant: tenant })
|
||||
// Getters
|
||||
getTheme: () => get().theme,
|
||||
getLanguage: () => get().language,
|
||||
getSidebarCollapsed: () => get().sidebarCollapsed,
|
||||
getGlobalLoading: () => get().globalLoading,
|
||||
getBreadcrumbs: () => get().breadcrumbs,
|
||||
getNotifications: () => get().notifications,
|
||||
getCurrentTenant: () => get().currentTenant,
|
||||
}),
|
||||
{
|
||||
name: 'global-storage',
|
||||
@@ -142,4 +125,6 @@ export const useGlobalStore = create<GlobalState & GlobalActions>()(
|
||||
})
|
||||
}
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
export default useGlobalStore;
|
||||
Reference in New Issue
Block a user