Files
web-Iot/src/App.tsx

98 lines
2.9 KiB
TypeScript
Raw Normal View History

2026-04-17 17:20:41 +08:00
import React from 'react';
import { RouterProvider } from 'react-router-dom';
import { router } from './router';
2026-08-12 11:29:44 +08:00
import { App as AntdApp, ConfigProvider, theme } from 'antd';
import zhCN from 'antd/locale/zh_CN';
import enUS from 'antd/locale/en_US';
2026-05-15 16:28:42 +08:00
import utils from './lib/utils';
2026-08-12 11:29:44 +08:00
import { useTranslation } from 'react-i18next';
import { I18nextProvider } from 'react-i18next';
import i18n from './i18n';
import { useEffect } from 'react';
2026-11-06 09:37:48 +08:00
import { useSelector } from 'react-redux';
import { useAppDispatch, type RootState } from './store';
import { setSettingField, setGlobalParams } from './store/basicSettingsSlice';
import { getSystemParam } from './api/global';
2026-05-15 16:28:42 +08:00
// 用于静态注入 message/modal/notification
const StaticAntd = () => {
const { message, modal, notification } = AntdApp.useApp();
utils.setMessage(message);
utils.setModal(modal);
utils.setNotification(notification);
return null;
};
2026-04-17 17:20:41 +08:00
2026-08-12 11:29:44 +08:00
const AntdConfigWrapper = ({ children }: { children: React.ReactNode }) => {
const { i18n } = useTranslation();
const isEn = i18n.language?.startsWith('en');
return (
<ConfigProvider
locale={isEn ? enUS : zhCN}
theme={{
algorithm: theme.defaultAlgorithm,
token: {
colorPrimary: '#1890ff',
borderRadius: 2,
padding: 12,
paddingLG: 16,
margin: 12,
marginLG: 16,
},
components: {
Layout: { headerBg: '#fff', siderBg: '#fff' },
Menu: { itemBg: '#fff', itemSelectedBg: '#e6f4ff' },
Card: { paddingLG: 12 },
Table: { padding: 8, paddingXS: 4 },
Form: { itemMarginBottom: 12 },
Space: { gap: 8 },
}
}}
>
{children}
</ConfigProvider>
);
};
2026-04-21 14:57:52 +08:00
2026-08-12 11:29:44 +08:00
export default function App() {
const dispatch = useAppDispatch();
2026-11-06 09:37:48 +08:00
// 系统名称与页面标题保持一致:优先使用接口下发/本地缓存的 systemName
const systemName = useSelector((state: RootState) => state.basicSettings.systemName);
useEffect(() => {
if (systemName) document.title = systemName;
}, [systemName]);
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]);
2026-05-15 16:28:42 +08:00
return (
2026-08-12 11:29:44 +08:00
<I18nextProvider i18n={i18n}>
<AntdConfigWrapper>
<AntdApp>
<StaticAntd />
<RouterProvider router={router} />
</AntdApp>
</AntdConfigWrapper>
</I18nextProvider>
2026-05-15 16:28:42 +08:00
);
2026-04-17 17:20:41 +08:00
}