修复场站管理中编辑场站之后 侧边栏电站的显示信息 完善 home页面的天气信息

This commit is contained in:
mmc
2026-09-09 17:13:51 +08:00
parent 3797701d43
commit 5c0adefcc7
6 changed files with 165 additions and 29 deletions

View File

@@ -10,9 +10,20 @@ const loadStationFromStorage = () => {
}
};
// 从 localStorage 加载场站列表(可选,加速首屏)
const loadStationListFromStorage = () => {
try {
const list = localStorage.getItem('stationList');
return list ? JSON.parse(list) : [];
} catch {
return [];
}
};
const initialState = {
currentStation: loadStationFromStorage(), // 当前选中场站(完整对象)
stationId: loadStationFromStorage()?.id || null, // 方便直接用 id
stationList: loadStationListFromStorage(), // 全局场站列表(用于跨组件共享最新数据)
};
const stationSlice = createSlice({
@@ -24,19 +35,107 @@ const stationSlice = createSlice({
state.currentStation = action.payload;
state.stationId = action.payload?.id || null;
localStorage.setItem('currentStation', JSON.stringify(action.payload));
// 同时更新 stationList 中对应项(保证列表也是最新)
if (action.payload?.id && state.stationList?.length) {
const idx = state.stationList.findIndex(s => Number(s.id) === Number(action.payload.id));
if (idx >= 0) {
state.stationList[idx] = { ...state.stationList[idx], ...action.payload };
localStorage.setItem('stationList', JSON.stringify(state.stationList));
}
}
},
// 设置全局场站列表
setStationList: (state, action) => {
state.stationList = action.payload || [];
localStorage.setItem('stationList', JSON.stringify(state.stationList));
// 如果当前 currentStation 在列表中,同步更新为列表中的最新版本
if (state.stationId && state.stationList?.length) {
const latest = state.stationList.find(s => Number(s.id) === Number(state.stationId));
if (latest) {
state.currentStation = latest;
localStorage.setItem('currentStation', JSON.stringify(latest));
}
}
},
// 部分更新当前选中场站(不覆盖全部字段)
updateCurrentStation: (state, action) => {
if (!state.currentStation) return;
state.currentStation = { ...state.currentStation, ...action.payload };
state.stationId = state.currentStation?.id || state.stationId;
localStorage.setItem('currentStation', JSON.stringify(state.currentStation));
// 同步更新 stationList 中对应项
if (state.currentStation?.id && state.stationList?.length) {
const idx = state.stationList.findIndex(s => Number(s.id) === Number(state.currentStation.id));
if (idx >= 0) {
state.stationList[idx] = { ...state.stationList[idx], ...action.payload };
localStorage.setItem('stationList', JSON.stringify(state.stationList));
}
}
},
// 更新列表中的单个场站(编辑场站后调用)
updateStationInList: (state, action) => {
const updatedStation = action.payload;
if (!updatedStation?.id) return;
if (!state.stationList) state.stationList = [];
const idx = state.stationList.findIndex(s => Number(s.id) === Number(updatedStation.id));
if (idx >= 0) {
state.stationList[idx] = { ...state.stationList[idx], ...updatedStation };
} else {
state.stationList.push(updatedStation);
}
localStorage.setItem('stationList', JSON.stringify(state.stationList));
// 如果更新的场站正好是当前选中的,同步 currentStation
if (state.stationId && Number(state.stationId) === Number(updatedStation.id)) {
state.currentStation = { ...state.currentStation, ...updatedStation };
localStorage.setItem('currentStation', JSON.stringify(state.currentStation));
}
},
// 从列表中删除场站
removeStationFromList: (state, action) => {
const deletedId = action.payload;
if (!state.stationList) return;
state.stationList = state.stationList.filter(s => Number(s.id) !== Number(deletedId));
localStorage.setItem('stationList', JSON.stringify(state.stationList));
// 如果删除的是当前选中场站,清空 currentStation
if (state.stationId && Number(state.stationId) === Number(deletedId)) {
state.currentStation = null;
state.stationId = null;
localStorage.removeItem('currentStation');
}
},
// 清空场站(退出登录时用)
clearStation: (state) => {
state.currentStation = null;
state.stationId = null;
state.stationList = [];
localStorage.removeItem('currentStation');
localStorage.removeItem('stationList');
},
},
});
export const { setCurrentStation, clearStation } = stationSlice.actions;
export const {
setCurrentStation,
setStationList,
updateCurrentStation,
updateStationInList,
removeStationFromList,
clearStation,
} = stationSlice.actions;
export default stationSlice.reducer;
//使用const { currentStation, stationId } = useSelector((state: RootState) => state.station);
//使用const { currentStation, stationId, stationList } = useSelector((state: RootState) => state.station);