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:
2025-11-13 14:52:52 +08:00
parent dfc29ce01f
commit 68d9d97142
29 changed files with 1122 additions and 3001 deletions

View File

@@ -1,458 +1,130 @@
import { create } from 'zustand'
import {
IrrigationSystem,
IrrigationZone,
IrrigationSchedule,
MonitoringData,
ControlCommand
} from '@api/modules/irrigation'
import { irrigationApi } from '@api'
import { QueryRequest } from '@api/types'
import { create } from 'zustand';
interface IrrigationState {
// 系统数据
systemList: IrrigationSystem[]
selectedSystem: IrrigationSystem | null
systemLoading: boolean
systemPagination: {
page: number
pageSize: number
total: number
}
// 分区数据
zoneList: IrrigationZone[]
selectedZone: IrrigationZone | null
zoneLoading: boolean
// 调度数据
scheduleList: IrrigationSchedule[]
selectedSchedule: IrrigationSchedule | null
scheduleLoading: boolean
schedulePagination: {
page: number
pageSize: number
total: number
}
// 监控数据
monitoringData: MonitoringData[]
realTimeData: MonitoringData[]
monitoringLoading: boolean
// 控制命令
controlCommands: ControlCommand[]
controlLoading: boolean
// 灌溉系统接口
export interface IrrigationSystem {
id: string;
name: string;
type: string;
status: 'active' | 'inactive' | 'maintenance';
location: string;
capacity: number;
flow_rate: number;
installation_date?: string | null;
last_maintenance_date?: string | null;
created_at: string;
updated_at: string;
}
interface IrrigationActions {
// 系统操作
fetchSystemList: (params?: QueryRequest) => Promise<void>
fetchSystemDetail: (id: string) => Promise<void>
createSystem: (data: Omit<IrrigationSystem, 'id' | 'createdAt' | 'updatedAt'>) => Promise<void>
updateSystem: (id: string, data: Partial<IrrigationSystem>) => Promise<void>
deleteSystem: (id: string) => Promise<void>
startIrrigation: (systemId: string, zoneId?: string, duration?: number) => Promise<void>
stopIrrigation: (systemId: string, zoneId?: string) => Promise<void>
setSelectedSystem: (system: IrrigationSystem | null) => void
// 分区操作
fetchZoneList: (systemId: string, params?: QueryRequest) => Promise<void>
fetchZoneDetail: (id: string) => Promise<void>
createZone: (data: Omit<IrrigationZone, 'id' | 'createdAt' | 'updatedAt'>) => Promise<void>
updateZone: (id: string, data: Partial<IrrigationZone>) => Promise<void>
deleteZone: (id: string) => Promise<void>
setSelectedZone: (zone: IrrigationZone | null) => void
// 调度操作
fetchScheduleList: (params?: QueryRequest) => Promise<void>
fetchScheduleDetail: (id: string) => Promise<void>
createSchedule: (data: Omit<IrrigationSchedule, 'id' | 'createdAt' | 'updatedAt'>) => Promise<void>
updateSchedule: (id: string, data: Partial<IrrigationSchedule>) => Promise<void>
deleteSchedule: (id: string) => Promise<void>
pauseSchedule: (id: string) => Promise<void>
resumeSchedule: (id: string) => Promise<void>
setSelectedSchedule: (schedule: IrrigationSchedule | null) => void
// 监控操作
fetchMonitoringData: (zoneId: string, params?: QueryRequest & { sensorType?: string }) => Promise<void>
fetchRealTimeData: (zoneId: string) => Promise<void>
// 清理操作
clearData: () => void
// 灌溉区域接口
export interface IrrigationZone {
id: string;
name: string;
system_id: string;
area: number;
crop_type: string;
soil_moisture_threshold: number;
irrigation_duration: number;
status: 'active' | 'inactive';
coordinates?: string | null;
created_at: string;
updated_at: string;
}
export const useIrrigationStore = create<IrrigationState & IrrigationActions>((set, get) => ({
// 初始状态
systemList: [],
selectedSystem: null,
systemLoading: false,
systemPagination: {
page: 1,
pageSize: 10,
total: 0
// 灌溉调度接口
export interface IrrigationSchedule {
id: string;
zone_id: string;
start_time: string;
duration: number;
frequency: string;
water_amount: number;
status: 'pending' | 'active' | 'completed' | 'cancelled';
created_at: string;
updated_at: string;
}
// Irrigation state interface
export interface IrrigationState {
irrigationSystems: IrrigationSystem[];
currentSystem: IrrigationSystem | null;
irrigationZones: IrrigationZone[];
currentZone: IrrigationZone | null;
irrigationSchedules: IrrigationSchedule[];
currentSchedule: IrrigationSchedule | null;
// Actions
setIrrigationSystems: (systems: IrrigationSystem[]) => void;
setCurrentSystem: (system: IrrigationSystem | null) => void;
setIrrigationZones: (zones: IrrigationZone[]) => void;
setCurrentZone: (zone: IrrigationZone | null) => void;
setIrrigationSchedules: (schedules: IrrigationSchedule[]) => void;
setCurrentSchedule: (schedule: IrrigationSchedule | null) => void;
// Getters
getIrrigationSystems: () => IrrigationSystem[];
getCurrentSystem: () => IrrigationSystem | null;
getIrrigationZones: () => IrrigationZone[];
getCurrentZone: () => IrrigationZone | null;
getIrrigationSchedules: () => IrrigationSchedule[];
getCurrentSchedule: () => IrrigationSchedule | null;
}
// Create Irrigation store
export const useIrrigationStore = create<IrrigationState>((set, get) => ({
irrigationSystems: [],
currentSystem: null,
irrigationZones: [],
currentZone: null,
irrigationSchedules: [],
currentSchedule: null,
setIrrigationSystems: (systems: IrrigationSystem[]) => {
set({ irrigationSystems: systems });
},
zoneList: [],
selectedZone: null,
zoneLoading: false,
scheduleList: [],
selectedSchedule: null,
scheduleLoading: false,
schedulePagination: {
page: 1,
pageSize: 10,
total: 0
setCurrentSystem: (system: IrrigationSystem | null) => {
set({ currentSystem: system });
},
monitoringData: [],
realTimeData: [],
monitoringLoading: false,
controlCommands: [],
controlLoading: false,
// 系统操作
fetchSystemList: async (params) => {
try {
set({ systemLoading: true })
const response = await irrigationApi.getSystemList({
page: params?.page || 1,
pageSize: params?.pageSize || 10,
...params
})
set({
systemList: response.data.items,
systemPagination: {
page: response.data.page,
pageSize: response.data.pageSize,
total: response.data.total
},
systemLoading: false
})
} catch (error) {
console.error('获取灌溉系统列表失败:', error)
set({ systemLoading: false })
}
setIrrigationZones: (zones: IrrigationZone[]) => {
set({ irrigationZones: zones });
},
fetchSystemDetail: async (id) => {
try {
set({ systemLoading: true })
const response = await irrigationApi.getSystemDetail(id)
set({
selectedSystem: response.data,
systemLoading: false
})
} catch (error) {
console.error('获取灌溉系统详情失败:', error)
set({ systemLoading: false })
}
setCurrentZone: (zone: IrrigationZone | null) => {
set({ currentZone: zone });
},
createSystem: async (data) => {
try {
const response = await irrigationApi.createSystem(data)
const currentList = get().systemList
set({
systemList: [response.data, ...currentList]
})
} catch (error) {
console.error('创建灌溉系统失败:', error)
}
setIrrigationSchedules: (schedules: IrrigationSchedule[]) => {
set({ irrigationSchedules: schedules });
},
updateSystem: async (id, data) => {
try {
const response = await irrigationApi.updateSystem(id, data)
const currentList = get().systemList
const updatedList = currentList.map(item =>
item.id === id ? response.data : item
)
set({
systemList: updatedList,
selectedSystem: response.data
})
} catch (error) {
console.error('更新灌溉系统失败:', error)
}
setCurrentSchedule: (schedule: IrrigationSchedule | null) => {
set({ currentSchedule: schedule });
},
deleteSystem: async (id) => {
try {
await irrigationApi.deleteSystem(id)
const currentList = get().systemList
const updatedList = currentList.filter(item => item.id !== id)
set({
systemList: updatedList,
selectedSystem: null
})
} catch (error) {
console.error('删除灌溉系统失败:', error)
}
getIrrigationSystems: () => {
return get().irrigationSystems;
},
startIrrigation: async (systemId, zoneId, duration) => {
try {
set({ controlLoading: true })
const response = await irrigationApi.startIrrigation(systemId, zoneId, duration)
const currentCommands = get().controlCommands
set({
controlCommands: [response.data, ...currentCommands],
controlLoading: false
})
} catch (error) {
console.error('启动灌溉失败:', error)
set({ controlLoading: false })
}
getCurrentSystem: () => {
return get().currentSystem;
},
stopIrrigation: async (systemId, zoneId) => {
try {
set({ controlLoading: true })
const response = await irrigationApi.stopIrrigation(systemId, zoneId)
const currentCommands = get().controlCommands
set({
controlCommands: [response.data, ...currentCommands],
controlLoading: false
})
} catch (error) {
console.error('停止灌溉失败:', error)
set({ controlLoading: false })
}
getIrrigationZones: () => {
return get().irrigationZones;
},
setSelectedSystem: (system) => set({ selectedSystem: system }),
// 分区操作
fetchZoneList: async (systemId, params) => {
try {
set({ zoneLoading: true })
const response = await irrigationApi.getZoneList(systemId, {
page: params?.page || 1,
pageSize: params?.pageSize || 20,
...params
})
set({
zoneList: response.data.items,
zoneLoading: false
})
} catch (error) {
console.error('获取灌溉分区列表失败:', error)
set({ zoneLoading: false })
}
getCurrentZone: () => {
return get().currentZone;
},
fetchZoneDetail: async (id) => {
try {
set({ zoneLoading: true })
const response = await irrigationApi.getZoneDetail(id)
set({
selectedZone: response.data,
zoneLoading: false
})
} catch (error) {
console.error('获取灌溉分区详情失败:', error)
set({ zoneLoading: false })
}
getIrrigationSchedules: () => {
return get().irrigationSchedules;
},
createZone: async (data) => {
try {
const response = await irrigationApi.createZone(data)
const currentList = get().zoneList
set({
zoneList: [response.data, ...currentList]
})
} catch (error) {
console.error('创建灌溉分区失败:', error)
}
getCurrentSchedule: () => {
return get().currentSchedule;
},
}));
updateZone: async (id, data) => {
try {
const response = await irrigationApi.updateZone(id, data)
const currentList = get().zoneList
const updatedList = currentList.map(item =>
item.id === id ? response.data : item
)
set({
zoneList: updatedList,
selectedZone: response.data
})
} catch (error) {
console.error('更新灌溉分区失败:', error)
}
},
deleteZone: async (id) => {
try {
await irrigationApi.deleteZone(id)
const currentList = get().zoneList
const updatedList = currentList.filter(item => item.id !== id)
set({
zoneList: updatedList,
selectedZone: null
})
} catch (error) {
console.error('删除灌溉分区失败:', error)
}
},
setSelectedZone: (zone) => set({ selectedZone: zone }),
// 调度操作
fetchScheduleList: async (params) => {
try {
set({ scheduleLoading: true })
const response = await irrigationApi.getScheduleList({
page: params?.page || 1,
pageSize: params?.pageSize || 10,
...params
})
set({
scheduleList: response.data.items,
schedulePagination: {
page: response.data.page,
pageSize: response.data.pageSize,
total: response.data.total
},
scheduleLoading: false
})
} catch (error) {
console.error('获取灌溉调度列表失败:', error)
set({ scheduleLoading: false })
}
},
fetchScheduleDetail: async (id) => {
try {
set({ scheduleLoading: true })
const response = await irrigationApi.getScheduleDetail(id)
set({
selectedSchedule: response.data,
scheduleLoading: false
})
} catch (error) {
console.error('获取灌溉调度详情失败:', error)
set({ scheduleLoading: false })
}
},
createSchedule: async (data) => {
try {
const response = await irrigationApi.createSchedule(data)
const currentList = get().scheduleList
set({
scheduleList: [response.data, ...currentList]
})
} catch (error) {
console.error('创建灌溉调度失败:', error)
}
},
updateSchedule: async (id, data) => {
try {
const response = await irrigationApi.updateSchedule(id, data)
const currentList = get().scheduleList
const updatedList = currentList.map(item =>
item.id === id ? response.data : item
)
set({
scheduleList: updatedList,
selectedSchedule: response.data
})
} catch (error) {
console.error('更新灌溉调度失败:', error)
}
},
deleteSchedule: async (id) => {
try {
await irrigationApi.deleteSchedule(id)
const currentList = get().scheduleList
const updatedList = currentList.filter(item => item.id !== id)
set({
scheduleList: updatedList,
selectedSchedule: null
})
} catch (error) {
console.error('删除灌溉调度失败:', error)
}
},
pauseSchedule: async (id) => {
try {
const response = await irrigationApi.pauseSchedule(id)
const currentList = get().scheduleList
const updatedList = currentList.map(item =>
item.id === id ? response.data : item
)
set({
scheduleList: updatedList,
selectedSchedule: response.data
})
} catch (error) {
console.error('暂停灌溉调度失败:', error)
}
},
resumeSchedule: async (id) => {
try {
const response = await irrigationApi.resumeSchedule(id)
const currentList = get().scheduleList
const updatedList = currentList.map(item =>
item.id === id ? response.data : item
)
set({
scheduleList: updatedList,
selectedSchedule: response.data
})
} catch (error) {
console.error('恢复灌溉调度失败:', error)
}
},
setSelectedSchedule: (schedule) => set({ selectedSchedule: schedule }),
// 监控操作
fetchMonitoringData: async (zoneId, params) => {
try {
set({ monitoringLoading: true })
const response = await irrigationApi.getMonitoringData(zoneId, {
page: params?.page || 1,
pageSize: params?.pageSize || 50,
...params
})
set({
monitoringData: response.data.items,
monitoringLoading: false
})
} catch (error) {
console.error('获取监控数据失败:', error)
set({ monitoringLoading: false })
}
},
fetchRealTimeData: async (zoneId) => {
try {
const response = await irrigationApi.getRealTimeData(zoneId)
set({
realTimeData: response.data
})
} catch (error) {
console.error('获取实时数据失败:', error)
}
},
clearData: () => set({
zoneList: [],
selectedZone: null,
scheduleList: [],
selectedSchedule: null,
monitoringData: [],
realTimeData: [],
controlCommands: []
})
}))
export default useIrrigationStore;