From 5d8b9559d0c44203c807f19a8ba856cde4013513 Mon Sep 17 00:00:00 2001 From: mmc <1556375442@qq.com> Date: Mon, 12 Oct 2026 15:16:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E7=A1=80=E8=AE=BE=E7=BD=AE=20?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E6=8E=A5=E5=85=A5=E4=BF=9D=E5=AD=98=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=20=EF=BC=8C=E5=86=8D=E7=99=BB=E5=BD=95=E7=95=8C?= =?UTF-8?q?=E9=9D=A2=20=E5=B0=B1=E8=B0=83=E7=94=A8=20=E6=8E=A5=E5=8F=A3=20?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=20=E9=BB=98=E8=AE=A4=20=E7=9A=84=20=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.tsx | 26 ++++ src/api/global.ts | 18 +++ .../SystemSetting/BasicSettings.tsx | 134 +++++++++++++----- src/pages/Login.tsx | 8 +- src/store/basicSettingsSlice.ts | 9 +- 5 files changed, 156 insertions(+), 39 deletions(-) create mode 100644 src/api/global.ts diff --git a/src/App.tsx b/src/App.tsx index ecde369..662172e 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -8,6 +8,10 @@ import utils from './lib/utils'; import { useTranslation } from 'react-i18next'; import { I18nextProvider } from 'react-i18next'; import i18n from './i18n'; +import { useEffect } from 'react'; +import { useAppDispatch } from './store'; +import { setSettingField, setGlobalParams } from './store/basicSettingsSlice'; +import { getSystemParam } from './api/global'; // 用于静态注入 message/modal/notification const StaticAntd = () => { @@ -50,6 +54,28 @@ const AntdConfigWrapper = ({ children }: { children: React.ReactNode }) => { }; export default function App() { + const dispatch = useAppDispatch(); + + useEffect(() => { + getSystemParam() + .then((res: any) => { + if (res?.code === 200 && res?.data?.global) { + const globalArr = res.data.global; + // 存储完整数组,保存时基于此结构更新 paramValue + dispatch(setGlobalParams(globalArr)); + // 同时提取 systemName 用于登录页和平台头部显示 + const systemNameItem = globalArr.find((item: any) => item.paramKey === 'systemName'); + if (systemNameItem?.paramValue) { + dispatch(setSettingField({ systemName: systemNameItem.paramValue })); + } + console.log('[System] global config loaded:', globalArr); + } + }) + .catch((err: any) => { + console.error('[System] Failed to load global config:', err); + }); + }, [dispatch]); + return ( diff --git a/src/api/global.ts b/src/api/global.ts new file mode 100644 index 0000000..df4b96c --- /dev/null +++ b/src/api/global.ts @@ -0,0 +1,18 @@ +import request from './request'; + +//新增物模型 +export function getSystemParam() { + return request({ + url: '/system/param/list', + method: 'get', + }); +} + +//保存系统参数 +export function saveSystemParam(data: any[]) { + return request({ + url: '/system/param/save', + method: 'post', + data, + }); +} diff --git a/src/components/SystemSetting/BasicSettings.tsx b/src/components/SystemSetting/BasicSettings.tsx index 7b59e92..c7e4897 100644 --- a/src/components/SystemSetting/BasicSettings.tsx +++ b/src/components/SystemSetting/BasicSettings.tsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect } from 'react'; import { Form, Input, @@ -61,6 +61,7 @@ import { import dayjs from 'dayjs'; import { useTranslation } from 'react-i18next'; import { getRoleList } from '@/api/role'; +import { saveSystemParam } from '@/api/global'; import { useSelector } from 'react-redux'; import { RootState } from '@/src/store'; import { getUser } from '@/api/user'; @@ -87,8 +88,23 @@ const BasicSettings = () => { const [userPage, setUserPage] = useState(1); const [rolePage, setRolePage] = useState(1); const [devicePage, setDevicePage] = useState(1); + + // 本地临时状态,保存时才 dispatch + const [localLocale, setLocalLocale] = useState(locale); + const [localTimezone, setLocalTimezone] = useState(timezone); + const [localUnitSystem, setLocalUnitSystem] = useState(unitSystem); + const [localDateFormat, setLocalDateFormat] = useState(dateFormat); + + // Redux 值变化时同步到本地(如保存后、重置后) + useEffect(() => { + setLocalLocale(locale); + setLocalTimezone(timezone); + setLocalUnitSystem(unitSystem); + setLocalDateFormat(dateFormat); + }, [locale, timezone, unitSystem, dateFormat]); + const tzOptions = getTimezoneOptions(); - const dstActive = isDST(timezone); + const dstActive = isDST(localTimezone); // 响应式字体大小 const fontNormal = 'clamp(12px, 0.75vw, 13px)'; @@ -97,26 +113,73 @@ const BasicSettings = () => { const fontLarge = 'clamp(18px, 1.1vw, 22px)'; const handleSave = () => { + const systemName = form.getFieldValue('systemName') || basicSettings.systemName; const defaultSite = form.getFieldValue('defaultSite') || basicSettings.defaultSite; - dispatch(saveAllSettings({ - systemName: form.getFieldValue('systemName') || basicSettings.systemName, - defaultSite, - defaultPage: form.getFieldValue('defaultPage') || basicSettings.defaultPage, - refreshRate: form.getFieldValue('refreshRate') || basicSettings.refreshRate, - })); + + // 查找默认场站名称 + let defaultStationName = ''; if (defaultSite && stationList && stationList.length > 0) { const target = stationList.find((s: any) => s.id === defaultSite); if (target) { - dispatch(setCurrentStation(target)); + defaultStationName = target.siteName || target.name || ''; } } - if (defaultSite && stationList && stationList.length > 0) { - const target = stationList.find((s: any) => s.id === defaultSite); - if (target) { - dispatch(setCurrentStation(target)); + + // 基于 getSystemParam 返回的完整数组结构,只更新 paramValue + const paramsToSave = (basicSettings.globalParams || []).map((item: any) => { + const updated = { ...item }; + switch (item.paramKey) { + case 'systemName': + updated.paramValue = systemName; + break; + case 'defaultStationName': + updated.paramValue = defaultStationName; + break; + case 'defaultLanguage': + updated.paramValue = localLocale === 'en' ? 'en_US' : 'zh_CN'; + break; + case 'defaultZoneId': + updated.paramValue = localTimezone; + break; + case 'defaultUnit': + updated.paramValue = localUnitSystem === 'imperial' ? '英制' : '公制'; + break; + case 'defaultDateFormat': + updated.paramValue = localDateFormat; + break; } - } - utils.message.success(t('systemSetting.fixed2SettingsSaveFixed2')); + return updated; + }); + + saveSystemParam(paramsToSave).then((res: any) => { + if (res.code === 200) { + // 同步到 Redux/localStorage + dispatch(saveAllSettings({ + systemName, + defaultSite, + defaultPage: form.getFieldValue('defaultPage') || basicSettings.defaultPage, + refreshRate: form.getFieldValue('refreshRate') || basicSettings.refreshRate, + })); + // 同步偏好设置 + dispatch(setLocale(localLocale)); + i18n.changeLanguage(localLocale); + dispatch(setTimezone(localTimezone)); + dispatch(setUnitSystem(localUnitSystem)); + dispatch(setDateFormat(localDateFormat)); + // 同步默认场站 + if (defaultSite && stationList && stationList.length > 0) { + const target = stationList.find((s: any) => s.id === defaultSite); + if (target) { + dispatch(setCurrentStation(target)); + } + } + utils.message.success(t('systemSetting.fixed2SettingsSaveFixed2')); + } else { + utils.message.error(res.msg || t('common.saveFailed')); + } + }).catch((err: any) => { + utils.message.error(err?.message || t('common.saveFailed')); + }); }; // 顶部统计卡片数据 @@ -319,15 +382,15 @@ const BasicSettings = () => { - + - {(stationList || []).slice().sort((a, b) => (a.id || 0) - (b.id || 0)).map((s, idx) => ( - {idx + 1}. {s.siteName || s.name || s.id} + {s.siteName || s.name || s.id} ))} @@ -335,7 +398,7 @@ const BasicSettings = () => { - setLocalLocale(v)}> {t('users.chinese')} English @@ -343,7 +406,7 @@ const BasicSettings = () => { - setLocalTimezone(v)} showSearch optionFilterProp="label"> {tzOptions.map(opt => {opt.label})} @@ -351,13 +414,13 @@ const BasicSettings = () => { - {dstActive ? t('systemSetting.dstActive') : t('systemSetting.dstInactive')} ({getTimezoneOffsetLabel(timezone)}) + {dstActive ? t('systemSetting.dstActive') : t('systemSetting.dstInactive')} ({getTimezoneOffsetLabel(localTimezone)}) - dispatch(setUnitSystem(e.target.value))}> + setLocalUnitSystem(e.target.value)}> {t('systemSetting.metric')} {t('systemSetting.imperial')} @@ -365,7 +428,7 @@ const BasicSettings = () => { - setLocalDateFormat(v)}> YYYY-MM-DD HH:mm:ss YYYY-MM-DD HH:mm MM/DD/YYYY HH:mm:ss @@ -401,18 +464,19 @@ const BasicSettings = () => {
diff --git a/src/pages/Login.tsx b/src/pages/Login.tsx index f895876..41e7d7b 100644 --- a/src/pages/Login.tsx +++ b/src/pages/Login.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { Form, @@ -17,7 +17,8 @@ import { motion } from 'motion/react'; import bgcImg from '../assets/bgc2.png'; import logincard from '../assets/logincard.png'; import { userLogin } from '../store/userSlice'; -import { useAppDispatch } from '../store'; +import { useSelector } from 'react-redux'; +import { useAppDispatch, RootState } from '../store'; import Cookies from 'js-cookie'; import { useTranslation } from 'react-i18next'; @@ -31,6 +32,7 @@ export default function Login() { const { message } = App.useApp(); // 登录 loading 状态 const [loading, setLoading] = useState(false); + const { systemName } = useSelector((state: RootState) => state.basicSettings); // 账号密码登录 const onFinish = async (values: any) => { @@ -89,7 +91,7 @@ export default function Login() {
- {t('login.systemName')} + {systemName || t('login.systemName')}
diff --git a/src/store/basicSettingsSlice.ts b/src/store/basicSettingsSlice.ts index 403d4d8..d2d50ef 100644 --- a/src/store/basicSettingsSlice.ts +++ b/src/store/basicSettingsSlice.ts @@ -1,4 +1,4 @@ -import { createSlice, PayloadAction } from '@reduxjs/toolkit'; +import { createSlice, PayloadAction } from '@reduxjs/toolkit'; export interface BasicSettingsState { systemName: string; @@ -19,6 +19,7 @@ export interface BasicSettingsState { nightModeStart: string; nightModeEnd: string; enableNightMode: boolean; + globalParams: any[]; } const STORAGE_KEY = 'basicSettings'; @@ -42,6 +43,7 @@ const defaultState: BasicSettingsState = { nightModeStart: '23:00', nightModeEnd: '07:00', enableNightMode: false, + globalParams: [], }; const loadFromStorage = (): BasicSettingsState => { @@ -91,6 +93,10 @@ const basicSettingsSlice = createSlice({ Object.assign(state, action.payload); persist(state); }, + setGlobalParams: (state, action: PayloadAction) => { + state.globalParams = action.payload; + persist(state); + }, saveAllSettings: (state, action: PayloadAction>) => { Object.assign(state, action.payload); persist(state); @@ -108,6 +114,7 @@ export const { setDefaultPage, setRefreshRate, setSettingField, + setGlobalParams, saveAllSettings, resetBasicSettings, } = basicSettingsSlice.actions;