- 分公司类型模板:超管配置隐藏路由/菜单 + 中文文案覆盖,仅对挂类型的分公司生效(后端 service/routes、前端 Provider/守卫/配置页) - 超管模板 iframe 真还原预览:28 个业务页注册、白名单点选编辑、空/示例数据 fixture、白名单漂移检测工具 - 商品/SKU 下拉远程搜索:服务端关键词搜索 + 前端 useRemoteOptionSearch/SearchableSelect 接入(订单/采购/商品等页) - 主系统联调:子系统主动解绑主系统授权(幂等重试/本地兜底)、追加修改 price_amend 视图代理与 amend-price 转发 - 修复:仓内加工重复建单幂等(已完成需求单 guard、order 任务一商品一单 guard) - 修复:出入库记录只取前 100 条 → 服务端分页 + 真实总数 - 其它:库存/订单若干修复、分公司管理响应式布局、i18n 文案、设计文档与回归测试
310 lines
11 KiB
TypeScript
310 lines
11 KiB
TypeScript
import React, { lazy, Suspense } from 'react';
|
||
import { BrowserRouter, Routes, Route, Navigate, useLocation } from 'react-router-dom';
|
||
import { ThemeProvider, useTheme } from './hooks/useTheme';
|
||
import { I18nProvider, useI18n } from './i18n/I18nProvider';
|
||
import { AuthProvider, useAuth, type UserRole } from './hooks/useAuth';
|
||
import { ErrorBoundary } from './components/ErrorBoundary';
|
||
import { Layout } from './components/Layout';
|
||
import { Login } from './pages/auth/Login';
|
||
import { ChangePassword } from './pages/auth/ChangePassword';
|
||
import { NoAccess } from './pages/auth/NoAccess';
|
||
import { getDefaultHomePath } from './utils/authRouting';
|
||
import { MenuLayoutProvider, useMenuLayout, useVisibleMenuItems } from './hooks/useMenuLayout';
|
||
import { findRuntimeMenuLabelByPath } from './utils/menuLayout';
|
||
import { resolveBranchUiHomeDecision } from './utils/branchUiHome';
|
||
import { BranchUiProvider, useBranchUi } from './providers/BranchUiProvider';
|
||
import { UnavailablePage } from './components/UnavailablePage';
|
||
import { translateRuntimeMenuTree } from './i18n/translateMenu';
|
||
import { renderModuleRoutes } from './modules/routes';
|
||
import { importWithRetry } from './utils/lazyRetry';
|
||
import { IframePreviewApp } from './preview/iframe/IframePreviewApp';
|
||
import { isIframePreviewRequest } from './preview/iframe/iframePreviewProtocol';
|
||
|
||
const PurchaserDashboard = lazy(async () => ({
|
||
default: (await importWithRetry(() => import('./pages/purchase/PurchaserDashboard'))).PurchaserDashboard,
|
||
}));
|
||
const PublicTrace = lazy(async () => ({
|
||
default: (await importWithRetry(() => import('./pages/inventory/PublicTrace'))).PublicTrace,
|
||
}));
|
||
/** 免登录送货单公开页(订单分享链接落地页,与 /t/:code 同款:不进登录守卫) */
|
||
const PublicDeliveryNote = lazy(async () => ({
|
||
default: (await importWithRetry(() => import('./pages/orders/PublicDeliveryNote'))).PublicDeliveryNote,
|
||
}));
|
||
|
||
function RouteFallback() {
|
||
return (
|
||
<div className="min-h-[40vh] flex items-center justify-center">
|
||
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-[var(--app-theme-accent)]"></div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
export function HomeRedirect() {
|
||
const { isLoading, profile, hasRole, hasPermission } = useAuth();
|
||
const { menuItems, menuResolved } = useMenuLayout();
|
||
const branchUi = useBranchUi();
|
||
|
||
if (isLoading || !profile) {
|
||
return (
|
||
<div className="min-h-screen flex items-center justify-center">
|
||
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-[var(--app-theme-accent)]"></div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
const defaultPath = getDefaultHomePath(hasRole, hasPermission);
|
||
const decision = resolveBranchUiHomeDecision({
|
||
authLoading: false,
|
||
hasProfile: true,
|
||
branchUiLoading: branchUi.loading,
|
||
applied: branchUi.applied,
|
||
menuResolved,
|
||
defaultPath,
|
||
menuItems,
|
||
hiddenMenuKeys: branchUi.applied ? branchUi.hiddenMenuKeys : [],
|
||
hasPermission,
|
||
hasRole,
|
||
});
|
||
|
||
if (decision.kind === 'loading') {
|
||
return (
|
||
<div className="min-h-screen flex items-center justify-center">
|
||
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-[var(--app-theme-accent)]"></div>
|
||
</div>
|
||
);
|
||
}
|
||
if (decision.kind === 'unavailable') {
|
||
return <UnavailablePage menuKey={decision.menuKey ?? undefined} />;
|
||
}
|
||
return <Navigate to={decision.path} replace />;
|
||
}
|
||
|
||
function DocumentTitle() {
|
||
const location = useLocation();
|
||
// 用渲染期投影后的菜单(隐藏项不参与标题解析,§9.3)
|
||
const menuItems = useVisibleMenuItems();
|
||
const { t, locale } = useI18n();
|
||
const appTitle = t('layout.appTitle');
|
||
|
||
React.useEffect(() => {
|
||
const pageLabel = findRuntimeMenuLabelByPath(
|
||
translateRuntimeMenuTree(menuItems, t, locale),
|
||
location.pathname
|
||
);
|
||
document.title = pageLabel ? `${pageLabel} · ${appTitle}` : appTitle;
|
||
}, [location.pathname, menuItems, appTitle, t, locale]);
|
||
|
||
return null;
|
||
}
|
||
|
||
function LayoutShell() {
|
||
const { isLoading, isAuthenticated, profile } = useAuth();
|
||
const location = useLocation();
|
||
|
||
// 须等 session bootstrap 结束,否则刷新深链时 session 尚空会被误踢去 /login,
|
||
// 再由 PublicRoute 送回默认首页(丢失当前路由)。
|
||
if (isLoading) {
|
||
return (
|
||
<div className="min-h-screen flex items-center justify-center">
|
||
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-[var(--app-theme-accent)]"></div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
if (!isAuthenticated) {
|
||
return <Navigate to="/login" replace state={{ from: location }} />;
|
||
}
|
||
|
||
if (!profile) {
|
||
return <Navigate to="/login" replace state={{ from: location }} />;
|
||
}
|
||
|
||
return (
|
||
<BranchUiProvider>
|
||
<MenuLayoutProvider>
|
||
<DocumentTitle />
|
||
<Layout />
|
||
</MenuLayoutProvider>
|
||
</BranchUiProvider>
|
||
);
|
||
}
|
||
|
||
const ProtectedRoute: React.FC<{
|
||
children: React.ReactNode;
|
||
permission?: string | string[];
|
||
roles?: UserRole | UserRole[];
|
||
/** permission+roles 同时存在时:all=AND(默认),any=OR */
|
||
accessMatch?: 'all' | 'any';
|
||
}> = ({ children, permission, roles, accessMatch = 'all' }) => {
|
||
const { isAuthenticated, isLoading, hasPermission, hasRole, profile } = useAuth();
|
||
const location = useLocation();
|
||
|
||
if (isLoading || (isAuthenticated && !profile)) {
|
||
if (!isLoading && isAuthenticated && !profile) {
|
||
return <Navigate to="/login" replace state={{ from: location }} />;
|
||
}
|
||
return (
|
||
<div className="min-h-screen flex items-center justify-center">
|
||
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-[var(--app-theme-accent)]"></div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
if (!isAuthenticated) {
|
||
return <Navigate to="/login" replace state={{ from: location }} />;
|
||
}
|
||
|
||
// 资料尚未带上角色/权限时先等待,避免误判无权限并踢回默认首页
|
||
if (!profile || (!profile.roles?.length && !profile.permissions?.length)) {
|
||
return (
|
||
<div className="min-h-screen flex items-center justify-center">
|
||
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-[var(--app-theme-accent)]"></div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
const roleOk = !roles || hasRole(roles);
|
||
const permOk = !permission || hasPermission(permission);
|
||
const allowed =
|
||
permission && roles
|
||
? accessMatch === 'any'
|
||
? roleOk || permOk
|
||
: roleOk && permOk
|
||
: roleOk && permOk;
|
||
|
||
if (!allowed) {
|
||
// 无权限:回默认首页(不是刷新丢路由;仅当前页无权时)
|
||
return <Navigate to={getDefaultHomePath(hasRole, hasPermission)} replace />;
|
||
}
|
||
|
||
return <>{children}</>;
|
||
};
|
||
|
||
const PublicRoute: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||
const { isAuthenticated, isLoading, profile, getHomePath } = useAuth();
|
||
const location = useLocation();
|
||
|
||
if (isLoading || (isAuthenticated && !profile)) {
|
||
if (!isLoading && isAuthenticated && !profile) {
|
||
return <>{children}</>;
|
||
}
|
||
return (
|
||
<div className="min-h-screen flex items-center justify-center">
|
||
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-[var(--app-theme-accent)]"></div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
if (isAuthenticated) {
|
||
const from = (location.state as { from?: { pathname?: string; search?: string } } | null)?.from;
|
||
const fromPath = from?.pathname;
|
||
if (fromPath && fromPath !== '/login') {
|
||
return <Navigate to={`${fromPath}${from?.search || ''}`} replace />;
|
||
}
|
||
return <Navigate to={getHomePath()} replace />;
|
||
}
|
||
|
||
return <>{children}</>;
|
||
};
|
||
|
||
function AppContent() {
|
||
const { theme } = useTheme();
|
||
|
||
return (
|
||
<ErrorBoundary>
|
||
<div className="min-h-screen w-full" data-theme={theme}>
|
||
<BrowserRouter>
|
||
<Suspense fallback={<RouteFallback />}>
|
||
<Routes>
|
||
<Route
|
||
path="/t/:code"
|
||
element={
|
||
<Suspense fallback={<RouteFallback />}>
|
||
<PublicTrace />
|
||
</Suspense>
|
||
}
|
||
/>
|
||
<Route
|
||
path="/d/:token"
|
||
element={
|
||
<Suspense fallback={<RouteFallback />}>
|
||
<PublicDeliveryNote />
|
||
</Suspense>
|
||
}
|
||
/>
|
||
<Route
|
||
path="/login"
|
||
element={
|
||
<PublicRoute>
|
||
<Login />
|
||
</PublicRoute>
|
||
}
|
||
/>
|
||
<Route
|
||
path="/change-password"
|
||
element={
|
||
<ProtectedRoute>
|
||
<ChangePassword />
|
||
</ProtectedRoute>
|
||
}
|
||
/>
|
||
{/* 无可用功能兜底页:**刻意不带任何 permission/roles**,任何已登录账号都能进。
|
||
用于「能登录但无任何可进页面」的账号(如客户门户残骸账号),避免
|
||
「无权限 → 回默认首页 → 又是无权页」的自环。见 NoAccess.tsx 顶部说明。 */}
|
||
<Route
|
||
path="/no-access"
|
||
element={
|
||
<ProtectedRoute>
|
||
<NoAccess />
|
||
</ProtectedRoute>
|
||
}
|
||
/>
|
||
<Route path="/supplier/login" element={<Navigate to="/login" replace />} />
|
||
<Route path="/purchaser/login" element={<Navigate to="/login" replace />} />
|
||
<Route path="/delivery/login" element={<Navigate to="/login" replace />} />
|
||
<Route
|
||
path="/purchaser/dashboard"
|
||
element={
|
||
<ProtectedRoute roles="purchaser">
|
||
<Suspense fallback={<RouteFallback />}>
|
||
<PurchaserDashboard />
|
||
</Suspense>
|
||
</ProtectedRoute>
|
||
}
|
||
/>
|
||
<Route path="/" element={<LayoutShell />}>
|
||
<Route index element={<HomeRedirect />} />
|
||
{renderModuleRoutes(ProtectedRoute)}
|
||
{/* 未知 / 已失效地址兜底(2026-10-11):本 SPA 原先没有 `*` 路由,
|
||
一条都没匹配上时**整页白屏**(没有任何提示)。隐藏「市场价格监控」后,
|
||
老书签 `/market-monitor` 正是这种情况(后端正常、请求全 200,用户只看到空白)。
|
||
复用首页落地逻辑:等 session bootstrap 结束再跳,且 `getDefaultHomePath`
|
||
自带 `/no-access` 兜底,不会「回首页又是无权页」自环。 */}
|
||
<Route path="*" element={<HomeRedirect />} />
|
||
</Route>
|
||
</Routes>
|
||
</Suspense>
|
||
</BrowserRouter>
|
||
</div>
|
||
</ErrorBoundary>
|
||
);
|
||
}
|
||
|
||
function App() {
|
||
// U11:iframe 内极简预览 App(无侧栏/顶栏;预览模式/数据替身只作用于 iframe realm)
|
||
if (isIframePreviewRequest()) {
|
||
return <IframePreviewApp />;
|
||
}
|
||
return (
|
||
<I18nProvider>
|
||
<ThemeProvider>
|
||
<AuthProvider>
|
||
<AppContent />
|
||
</AuthProvider>
|
||
</ThemeProvider>
|
||
</I18nProvider>
|
||
);
|
||
}
|
||
|
||
export default App;
|