feat: P4-3 eip4-web 前端渲染宿主脚手架(amis 6.13.0 本地化/登录JWT/待办/双闸渲染闸)
- 脚手架:React 18 + Vite 5 + TS;amis 6.13.0 本地 SDK(public/sdk-local,不依赖 CDN,
照 PoC amis-local.html 范式 amisRequire('amis/embed'))
- 登录页:POST /api/auth/login + JWT 存储(内存+localStorage 折中,XSS 权衡见 README/token.ts)
- 页面渲染 /page/:templateId:取转换后 schema 渲染;开发期静态 mock(public/schemas/ 放
角色维护/通知管理归档/用户组管理 3 个真实转换产物,均过官方 schema.json 校验闸);
生产期 schema 接口契约已设计(VITE_SCHEMA_SOURCE 切换)
- 待办页 /todo:GET /api/todo + 503 熔断降级展示
- amis fetcher 适配:复用宿主 axios 注入 Bearer,对接 /api/entity amis CRUD 格式
- axios 拦截器:Bearer 注入 + 401 清会话跳登录
- 联调验证(playwright 无头):登录跳转→渲染用户组(本地库 3 行)→角色维护(真实产物)→
待办 503 降级→401 跳登录(prod 严格链 8090),5 张截图留 docs/
- 工程化:npm run build → dist/(应用 208KB/gzip 71KB + 本地 SDK 61MB,Nginx 可托管);
README/报告/gitignore
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
67
src/components/AmisRenderer.tsx
Normal file
67
src/components/AmisRenderer.tsx
Normal file
@@ -0,0 +1,67 @@
|
||||
// ============================================================================
|
||||
// AmisRenderer —— 本地 SDK(amisRequire('amis/embed'))渲染宿主
|
||||
// ----------------------------------------------------------------------------
|
||||
// 双闸「渲染闸」前端承接点:
|
||||
// schema 进入前已过「校验闸」(官方 schema.json,后端/转换器侧);
|
||||
// 本组件用 amis 真实 SDK 渲染 = 运行时渲染冒烟,渲染异常即闸失败。
|
||||
// fetcher 适配:
|
||||
// amis 内置 fetcher 直接发 schema 里的 api(如 /api/entity/SYS_USERGROUP/list)。
|
||||
// 此处替换为自定义 fetcher,统一注入 Bearer(由 http 拦截器完成)并复用宿主 axios,
|
||||
// 后端 /api/entity 已是 amis CRUD 格式 {status,msg,data:{items,total}},无需再适配响应。
|
||||
// ============================================================================
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { http } from '../api/http';
|
||||
|
||||
interface AmisRendererProps {
|
||||
schema: Record<string, unknown>;
|
||||
}
|
||||
|
||||
// amis fetcher 入参(简化,贴 amis SDK 的 fetcherConfig 形态)
|
||||
interface AmisFetcherOptions {
|
||||
url: string;
|
||||
method?: string;
|
||||
data?: unknown;
|
||||
headers?: Record<string, string>;
|
||||
responseType?: string;
|
||||
config?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export function AmisRenderer({ schema }: AmisRendererProps) {
|
||||
const rootRef = useRef<HTMLDivElement>(null);
|
||||
const instanceRef = useRef<{ unmount?: () => void } | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!rootRef.current || typeof amisRequire !== 'function') return;
|
||||
|
||||
const amis = amisRequire('amis/embed');
|
||||
|
||||
// 自定义 fetcher:复用宿主 axios(已带 Bearer 注入 + 401 跳登录拦截)
|
||||
const fetcher = (options: AmisFetcherOptions) => {
|
||||
const { url, method = 'get', data, responseType } = options;
|
||||
const m = method.toLowerCase();
|
||||
if (m === 'get') {
|
||||
return http.get(url, { responseType: responseType as never });
|
||||
}
|
||||
return http.request({ url, method: m as never, data, responseType: responseType as never });
|
||||
};
|
||||
|
||||
const instance = amis.embed(
|
||||
rootRef.current,
|
||||
schema,
|
||||
{},
|
||||
{
|
||||
fetcher,
|
||||
// 与后端 amis CRUD 约定对齐:status===0 为成功
|
||||
isCancel: (e: unknown) => (http as unknown as { isCancel?: (x: unknown) => boolean }).isCancel?.(e) ?? false,
|
||||
},
|
||||
);
|
||||
instanceRef.current = instance;
|
||||
|
||||
return () => {
|
||||
instanceRef.current?.unmount?.();
|
||||
instanceRef.current = null;
|
||||
};
|
||||
}, [schema]);
|
||||
|
||||
return <div ref={rootRef} style={{ height: '100%' }} />;
|
||||
}
|
||||
61
src/components/AppLayout.tsx
Normal file
61
src/components/AppLayout.tsx
Normal file
@@ -0,0 +1,61 @@
|
||||
// 登录后的应用外壳:顶栏(品牌 + 导航 + 当前用户 + 登出)+ 内容区
|
||||
import { type ReactNode } from 'react';
|
||||
import { Link, useNavigate } from 'react-router-dom';
|
||||
import { clearSession, getProfile } from '../auth/token';
|
||||
import { MOCK_PAGES } from '../api/schema';
|
||||
|
||||
export function AppLayout({ children }: { children: ReactNode }) {
|
||||
const navigate = useNavigate();
|
||||
const profile = getProfile();
|
||||
|
||||
const logout = () => {
|
||||
clearSession();
|
||||
navigate('/login', { replace: true });
|
||||
};
|
||||
|
||||
return (
|
||||
<div style={{ height: '100%', display: 'flex', flexDirection: 'column' }}>
|
||||
<header
|
||||
style={{
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
gap: 16,
|
||||
padding: '0 20px',
|
||||
height: 52,
|
||||
background: '#1f2d3d',
|
||||
color: '#fff',
|
||||
flex: '0 0 auto',
|
||||
}}
|
||||
>
|
||||
<strong style={{ fontSize: 16 }}>EIP4 渲染宿主</strong>
|
||||
<nav style={{ display: 'flex', gap: 12, fontSize: 14 }}>
|
||||
<Link to="/todo" style={{ color: '#cfe3ff' }}>
|
||||
待办
|
||||
</Link>
|
||||
{MOCK_PAGES.map((p) => (
|
||||
<Link key={p.id} to={`/page/${p.id}`} style={{ color: '#cfe3ff' }}>
|
||||
{p.title.split('(')[0]}
|
||||
</Link>
|
||||
))}
|
||||
</nav>
|
||||
<span style={{ marginLeft: 'auto', fontSize: 13, opacity: 0.85 }}>
|
||||
{profile ? `${profile.name}(${profile.account})` : '未登录'}
|
||||
</span>
|
||||
<button
|
||||
onClick={logout}
|
||||
style={{
|
||||
background: 'transparent',
|
||||
border: '1px solid #6c7a89',
|
||||
color: '#fff',
|
||||
borderRadius: 4,
|
||||
padding: '4px 10px',
|
||||
cursor: 'pointer',
|
||||
}}
|
||||
>
|
||||
登出
|
||||
</button>
|
||||
</header>
|
||||
<main style={{ flex: '1 1 auto', overflow: 'auto', background: '#f5f6f7' }}>{children}</main>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
12
src/components/RequireAuth.tsx
Normal file
12
src/components/RequireAuth.tsx
Normal file
@@ -0,0 +1,12 @@
|
||||
// 路由守卫:未登录跳 /login,回跳记录来源路径
|
||||
import { type ReactNode } from 'react';
|
||||
import { Navigate, useLocation } from 'react-router-dom';
|
||||
import { isAuthenticated } from '../auth/token';
|
||||
|
||||
export function RequireAuth({ children }: { children: ReactNode }) {
|
||||
const location = useLocation();
|
||||
if (!isAuthenticated()) {
|
||||
return <Navigate to="/login" replace state={{ from: location.pathname }} />;
|
||||
}
|
||||
return <>{children}</>;
|
||||
}
|
||||
Reference in New Issue
Block a user