修复场站管理中编辑场站之后 侧边栏电站的显示信息 完善 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

@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react';
import React, { useState, useEffect } from 'react';
import { Outlet, useNavigate, useLocation } from 'react-router-dom';
import {
Layout, Menu, Button, Space, Select, Avatar, Card, Tag
@@ -15,7 +15,7 @@ import logo from '../assets/logo.png';
import { useSelector } from 'react-redux';
import { RootState, useAppDispatch } from '../store';
import { logout } from '../store/userSlice';
import { setCurrentStation, clearStation } from '../store/stationSlice';
import { setCurrentStation, clearStation, setStationList as setStoreStationList } from '../store/stationSlice';
import { getMenuList } from '../api/mune';
import { getStationByUserId } from '../api/stationManage';
import LanguageSwitcher from './LanguageSwitcher';
@@ -36,13 +36,19 @@ export default function AppLayout() {
const dispatch = useAppDispatch();
const { userInfo } = useSelector((state: RootState) => state.user);
const { currentStation, stationId } = useSelector((state: RootState) => state.station);
// 从 store 中同时取出 stationList(优先级最高,保证最新)
const { currentStation, stationId, stationList: storeStationList } = useSelector((state: RootState) => state.station);
// 真正用于展示和选择的列表:优先用 store 中的(StationManage 编辑后会同步),否则用本地接口的
const mergedStationList = storeStationList?.length ? storeStationList : stationList;
const getStationData = () => {
getStationByUserId().then(res => {
if (res.code === 200) {
const list = res.data || [];
setStationList(list);
// 同步到全局 store,确保各组件共享同一份最新列表
dispatch(setStoreStationList(list));
if (!currentStation && list.length > 0) {
dispatch(setCurrentStation(list[0]));
}
@@ -63,7 +69,9 @@ export default function AppLayout() {
};
const handleStationChange = (id: number) => {
const station = stationList.find(s => s.id === id);
// 优先从合并后的列表中取(包含 store 中最新编辑的数据),其次从本地 stationList
const station = mergedStationList.find(s => Number(s.id) === Number(id))
|| stationList.find(s => Number(s.id) === Number(id));
if (station) {
dispatch(setCurrentStation(station));
}
@@ -206,7 +214,7 @@ export default function AppLayout() {
value={stationId}
placeholder={t('layout.selectStation')}
onChange={handleStationChange}
options={stationList.map(s => ({ label: s.siteName, value: s.id }))}
options={mergedStationList.map(s => ({ label: s.siteName, value: s.id }))}
style={{
width: 'clamp(160px, 15vw, 220px)',
backgroundColor: '#0837a8',