App.tsx
2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { useEffect, useMemo, useState } from 'react';
import { busEvents, downloadChannelRop } from '@uj/contracts';
import {
SubAppProvider,
downloadCenter,
readInitialPage,
shortName,
subappBus,
} from '@uj/platform-sdk';
import { UEmpty, UPageTransition } from '@uj/ui';
import { financeClient } from './api/core/client';
import Payment from './pages/Payment';
import type { PageParams, RouteDef } from './routes';
import { matchRoute } from './routes';
/**
* 页面表:主壳给的 subPath(形如 payment)在这里换成组件与路径参数。
* 1 条与 scripts/finance-menu-migration.sql 中 menu_type=2 的 route 末段对应;
* 新增页面 = 加一行 + 建一个页面目录 + SQL 加菜单行。
*/
const ROUTES: RouteDef[] = [{ path: 'payment', Page: Payment }];
function renderPage(subPath: string) {
const matched = matchRoute(ROUTES, subPath);
if (!matched) {
return <UEmpty description={`没有匹配「${subPath}」的页面,请从菜单进入`} />;
}
const { Page, params } = matched;
return <Page params={params as PageParams} />;
}
function FinanceApp() {
const [subPath, setSubPath] = useState<string>(readInitialPage);
useEffect(() => {
const bus = subappBus();
if (!bus) return;
const events = busEvents(shortName());
const onNavigate = (...args: unknown[]) => setSubPath(String(args[0] ?? ''));
const onLogout = () => setSubPath('');
bus.$on(events.navigate, onNavigate);
bus.$on(events.logout, onLogout);
// 付款导出走 ROP 主网关;本子应用持有 ROP 凭据,注册为 rop 通道供下载引擎提交/轮询。
downloadCenter.registerChannel(downloadChannelRop(shortName()), (req) =>
financeClient.postRop(req.url, (req.data ?? {}) as Record<string, unknown>),
);
bus.$emit(events.ready);
return () => {
bus.$off(events.navigate, onNavigate);
bus.$off(events.logout, onLogout);
};
}, []);
const page = useMemo(() => renderPage(subPath), [subPath]);
return <UPageTransition pageKey={subPath}>{page}</UPageTransition>;
}
export function App() {
return (
<SubAppProvider>
<div className="uj-scope uj-finance-app">{FinanceApp()}</div>
</SubAppProvider>
);
}