Files
smart-crop-ui/crop-x-new/src/stores/modules/config.ts
peng 87dbc7548a refactor: 优化认证系统和组件类型安全性
- 新增 safeLocalStorage 工具函数增强本地存储安全性
- 简化认证流程,重构用户数据结构和 token 管理
- 修复多个模块的 TypeScript 类型错误和导入问题
- 优化状态管理,重构各模块 store 结构
- 清理冗余代码,移除未使用的组件和函数
- 改进错误处理和边界情况处理
- 更新配置文件以支持最新的类型检查

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-13 14:52:52 +08:00

129 lines
2.8 KiB
TypeScript

import { create } from 'zustand';
// 租户接口
export interface Tenant {
id: string;
name: string;
code: string;
description?: string | null;
status: 'active' | 'inactive' | 'suspended';
contact_email?: string | null;
contact_phone?: string | null;
max_users?: number | null;
created_at: string;
updated_at: string;
}
// 用户接口
export interface User {
id: string;
username: string;
email: string;
full_name?: string | null;
phone?: string | null;
role: string;
status: 'active' | 'inactive' | 'suspended';
tenant_id: string;
last_login_at?: string | null;
created_at: string;
updated_at: string;
}
// 系统参数接口
export interface SystemParameter {
id: string;
key: string;
value: string;
description?: string | null;
category: string;
data_type: 'string' | 'number' | 'boolean' | 'json';
is_system: boolean;
created_at: string;
updated_at: string;
}
// Config state interface
export interface ConfigState {
tenants: Tenant[];
currentTenant: Tenant | null;
users: User[];
currentUser: User | null;
systemParameters: SystemParameter[];
currentParameter: SystemParameter | null;
// Actions
setTenants: (tenants: Tenant[]) => void;
setCurrentTenant: (tenant: Tenant | null) => void;
setUsers: (users: User[]) => void;
setCurrentUser: (user: User | null) => void;
setSystemParameters: (parameters: SystemParameter[]) => void;
setCurrentParameter: (parameter: SystemParameter | null) => void;
// Getters
getTenants: () => Tenant[];
getCurrentTenant: () => Tenant | null;
getUsers: () => User[];
getCurrentUser: () => User | null;
getSystemParameters: () => SystemParameter[];
getCurrentParameter: () => SystemParameter | null;
}
// Create Config store
export const useConfigStore = create<ConfigState>((set, get) => ({
tenants: [],
currentTenant: null,
users: [],
currentUser: null,
systemParameters: [],
currentParameter: null,
setTenants: (tenants: Tenant[]) => {
set({ tenants });
},
setCurrentTenant: (tenant: Tenant | null) => {
set({ currentTenant: tenant });
},
setUsers: (users: User[]) => {
set({ users });
},
setCurrentUser: (user: User | null) => {
set({ currentUser: user });
},
setSystemParameters: (parameters: SystemParameter[]) => {
set({ systemParameters: parameters });
},
setCurrentParameter: (parameter: SystemParameter | null) => {
set({ currentParameter: parameter });
},
getTenants: () => {
return get().tenants;
},
getCurrentTenant: () => {
return get().currentTenant;
},
getUsers: () => {
return get().users;
},
getCurrentUser: () => {
return get().currentUser;
},
getSystemParameters: () => {
return get().systemParameters;
},
getCurrentParameter: () => {
return get().currentParameter;
},
}));
export default useConfigStore;