From 4626bf2771cca0ec0be5c93a38137dde8306ee81 Mon Sep 17 00:00:00 2001 From: kae Date: Fri, 22 May 2026 18:59:15 +0900 Subject: [PATCH] update api --- .umirc.ts | 2 +- src/app.tsx | 19 ++++++++++++++++++- src/pages/Dashboard/index.tsx | 4 ++-- src/pages/Inventory/Import/index.tsx | 2 +- src/pages/Inventory/Stats/index.tsx | 6 +++--- src/pages/Login/index.tsx | 15 +++++++-------- src/pages/System/Configs/index.tsx | 2 +- src/pages/System/Menus/index.tsx | 2 +- 8 files changed, 34 insertions(+), 18 deletions(-) diff --git a/.umirc.ts b/.umirc.ts index 372343f..9c1b0cd 100644 --- a/.umirc.ts +++ b/.umirc.ts @@ -10,7 +10,7 @@ export default defineConfig({ title: '木羽清风仓储系统', locale: false, }, - headScripts: [ + styles: [ 'https://fonts.googleapis.com/css2?family=Calistoga&family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap', ], proxy: { diff --git a/src/app.tsx b/src/app.tsx index e2b71a4..8ef3a36 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -5,11 +5,13 @@ import type { CurrentUser } from '@/types/api'; import { antdTheme } from '@/types/theme'; const LOGIN_PATH = '/login'; +const USER_CACHE_KEY = 'muyu_user_state'; let redirectingToLogin = false; function clearSessionAndRedirect(tip?: string) { localStorage.removeItem('token'); localStorage.removeItem('tenantId'); + localStorage.removeItem(USER_CACHE_KEY); if (tip && !redirectingToLogin) { message.warning(tip); } @@ -34,16 +36,30 @@ export async function getInitialState(): Promise { return {}; } + // Return cached state immediately to avoid blocking first render. + // An expired token will be caught lazily by the response interceptor on the next real API call. + const raw = localStorage.getItem(USER_CACHE_KEY); + if (raw) { + try { + return JSON.parse(raw) as InitialState; + } catch { + localStorage.removeItem(USER_CACHE_KEY); + } + } + + // Cache miss (first load after login or cache cleared) — blocking fetch. try { const res = await fetch('/api/v1/auth/info', { headers: { Authorization: `Bearer ${token}` }, }); const data = await res.json(); if (data.code === 0 && data.data) { - return { + const state: InitialState = { currentUser: data.data, menuPaths: data.data.menuPaths || [], }; + localStorage.setItem(USER_CACHE_KEY, JSON.stringify(state)); + return state; } } catch { // network failure — fall through to redirect @@ -85,6 +101,7 @@ export const layout: RunTimeLayoutConfig = (initialState) => ({ logout: () => { localStorage.removeItem('token'); localStorage.removeItem('tenantId'); + localStorage.removeItem(USER_CACHE_KEY); history.push(LOGIN_PATH); }, menuDataRender: (menuData: any[]) => { diff --git a/src/pages/Dashboard/index.tsx b/src/pages/Dashboard/index.tsx index 02947ac..2d284ab 100644 --- a/src/pages/Dashboard/index.tsx +++ b/src/pages/Dashboard/index.tsx @@ -22,7 +22,7 @@ interface StatCardProps { const StatCard: React.FC = ({ icon, title, value, suffix, prefix, gradient }) => ( { ].map((item) => ( { return ( - + { {stats.map((s) => ( - +
{ - + dataSource={colorData} rowKey="name" @@ -91,7 +91,7 @@ const StatsPage: React.FC = () => { - + dataSource={locationData} rowKey="name" diff --git a/src/pages/Login/index.tsx b/src/pages/Login/index.tsx index 846b988..55b777d 100644 --- a/src/pages/Login/index.tsx +++ b/src/pages/Login/index.tsx @@ -5,6 +5,8 @@ import { LockOutlined, UserOutlined, BankOutlined } from '@ant-design/icons'; import { getUserInfo, login } from '@/services/api'; import type { LoginParams } from '@/types/api'; +const USER_CACHE_KEY = 'muyu_user_state'; + const LoginPage: React.FC = () => { const { setInitialState } = useModel('@@initialState'); const [loading, setLoading] = useState(false); @@ -46,17 +48,14 @@ const LoginPage: React.FC = () => { res.data.userInfo?.tenantId || values.tenantId || 't_default_001', ); const infoRes = await getUserInfo(); + let state: { currentUser: any; menuPaths: string[] }; if (infoRes?.code === 0 && infoRes.data) { - await setInitialState({ - currentUser: infoRes.data, - menuPaths: infoRes.data.menuPaths || [], - }); + state = { currentUser: infoRes.data, menuPaths: infoRes.data.menuPaths || [] }; } else { - await setInitialState({ - currentUser: res.data.userInfo, - menuPaths: [], - }); + state = { currentUser: res.data.userInfo, menuPaths: [] }; } + await setInitialState(state); + localStorage.setItem(USER_CACHE_KEY, JSON.stringify(state)); message.success('登录成功'); history.push('/dashboard'); } else { diff --git a/src/pages/System/Configs/index.tsx b/src/pages/System/Configs/index.tsx index 0b46e06..346d264 100644 --- a/src/pages/System/Configs/index.tsx +++ b/src/pages/System/Configs/index.tsx @@ -60,7 +60,7 @@ const ConfigsPage: React.FC = () => { return ( - + columns={columns} dataSource={data} diff --git a/src/pages/System/Menus/index.tsx b/src/pages/System/Menus/index.tsx index ab4cf51..ea6cee4 100644 --- a/src/pages/System/Menus/index.tsx +++ b/src/pages/System/Menus/index.tsx @@ -177,7 +177,7 @@ const MenusPage: React.FC = () => { return ( - +