@autional/shared 0.1.0-rc.60 → 0.1.0-rc.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.ts +0 -2
- package/src/portal/registry.ts +349 -349
- package/src/hooks/__tests__/useSessionActivity.test.tsx +0 -89
- package/src/hooks/useSessionActivity.ts +0 -106
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -97,8 +97,6 @@ export type { BootstrapState } from './hooks/useBootstrap';
|
|
|
97
97
|
export { useSodMode, useIsAuditRestricted } from './hooks/useSodMode';
|
|
98
98
|
export type { SodMode } from './hooks/useSodMode';
|
|
99
99
|
export { useCurrentRole } from './hooks/useCurrentRole';
|
|
100
|
-
export { useSessionActivity } from './hooks/useSessionActivity';
|
|
101
|
-
export type { UseSessionActivityOptions } from './hooks/useSessionActivity';
|
|
102
100
|
export {
|
|
103
101
|
usePortalCatalog,
|
|
104
102
|
isPortalVisible,
|
package/src/portal/registry.ts
CHANGED
|
@@ -1,349 +1,349 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 门户注册表 —— **门户元数据的单一真源**。
|
|
3
|
-
*
|
|
4
|
-
* 过去「门户清单」被写了十几次(`PORTAL_DEFAULTS` / `SLUG_PORTALS` /
|
|
5
|
-
* `getAppPortalSlugs()` / 重定向白名单 / `isPortalVisible` 的角色集 /
|
|
6
|
-
* `DEFAULT_EXCLUDE` / `PORTAL_KEY_MAP`,以及 6 个站点各持一份 `CONSOLE_PORTALS`)。
|
|
7
|
-
* 每加一个门户要改 7 个仓,且靠"进名单"才可见(小圈子)。
|
|
8
|
-
*
|
|
9
|
-
* 现在只有这张表。其余全部由它派生:
|
|
10
|
-
*
|
|
11
|
-
* 表字段 → 派生
|
|
12
|
-
* host / base → config.PORTAL_DEFAULTS、getPortalUrl()
|
|
13
|
-
* slugScoped → config.SLUG_PORTALS(入口是否拼 /<tenantSlug>)
|
|
14
|
-
* routerSlug → config.getAppPortalSlugs()(/<tenant>/<portal> 路由 basename)
|
|
15
|
-
* legacyEnvKey → config.PORTAL_KEY_MAP(旧 env.js VITE_*_URL)
|
|
16
|
-
* — → getAllowedRedirectOrigins()(redirect 白名单 = 全部 code)
|
|
17
|
-
* audience → hooks.isPortalVisible()(谁能看见:入口门禁的镜像)
|
|
18
|
-
* inSwitcher → usePortalCatalog DEFAULT_EXCLUDE(默认排除 = !inSwitcher)
|
|
19
|
-
* family='console' → getConsolePortalCodes()(控制台切换器全集,替代各站白名单)
|
|
20
|
-
*
|
|
21
|
-
* 新增门户 = 在这里加一行(若后端目录已透出 host/base,则连这一行都可省)。
|
|
22
|
-
* **禁止**在其它文件再写门户 code 字面量数组/Set。
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
/** 可见性受众 = 登录入口门禁的镜像(谁能登录,谁才看见)。 */
|
|
26
|
-
export type PortalAudience = 'public' | 'admin' | 'platform';
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* 门户族群:
|
|
30
|
-
* - `console`:管理面/用户面控制台(互相出现在切换器里)
|
|
31
|
-
* - `content`:内容站(developer / status / trust / brand / docs …)
|
|
32
|
-
* - `app`:App 形态(authenticator:TOTP/push,不列入桌面切换器)
|
|
33
|
-
* - `auth` / `landing`:认证与落地站,非切换目标
|
|
34
|
-
*/
|
|
35
|
-
export type PortalFamily = 'console' | 'content' | 'app' | 'auth' | 'landing';
|
|
36
|
-
|
|
37
|
-
export interface PortalDefinition {
|
|
38
|
-
/** 门户 code(后端 applications.code,全环境同值) */
|
|
39
|
-
code: string;
|
|
40
|
-
/** 子域前缀;空串 = 落在根域(landing) */
|
|
41
|
-
host: string;
|
|
42
|
-
/** 子域下的固定路径前缀(env 可覆盖,如 admin 的 '/admin') */
|
|
43
|
-
base: string;
|
|
44
|
-
/** 入口是否拼租户段 `/<slug>`(拼错会命中不存在的路由 ⇒ 空白/404) */
|
|
45
|
-
slugScoped: boolean;
|
|
46
|
-
/** 是否参与 `/<tenant>/<portal>` 路由 basename 解析 */
|
|
47
|
-
routerSlug: boolean;
|
|
48
|
-
/**
|
|
49
|
-
* `getAppPortalSlugs()` 的返回顺序(历史上是 `admin, developer, security, platform, status, trust`)。
|
|
50
|
-
* 该数组是导出 API,顺序可观测 ⇒ 用显式序号保序,而不是依赖表顺序。
|
|
51
|
-
*/
|
|
52
|
-
routerOrder?: number;
|
|
53
|
-
family: PortalFamily;
|
|
54
|
-
audience: PortalAudience;
|
|
55
|
-
/** 默认进入 `usePortalCatalog` 的门户清单(false ⇒ 进 DEFAULT_EXCLUDE) */
|
|
56
|
-
inSwitcher: boolean;
|
|
57
|
-
/** 旧 env.js 直连 URL 键(VITE_*_URL)→ 向后兼容映射用 */
|
|
58
|
-
legacyEnvKey?: string;
|
|
59
|
-
/** 备注(为什么这个门户这样配置) */
|
|
60
|
-
note?: string;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** 管理面平面角色集(admin / security / config / secret 的门户 = 登录入口门禁同集)。 */
|
|
64
|
-
export const ADMIN_PLANE_ROLES: readonly string[] = [
|
|
65
|
-
'super_admin',
|
|
66
|
-
'admin',
|
|
67
|
-
'security_admin',
|
|
68
|
-
'user_manager',
|
|
69
|
-
];
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* 注册表本体(顺序 = 重定向白名单推导顺序)。
|
|
73
|
-
*
|
|
74
|
-
* 前 13 项与迁移前 `PORTAL_DEFAULTS` 逐项等价(见 `__tests__/registry.test.ts` 的 oracle 断言);
|
|
75
|
-
* 后 4 项(docs / wiki / reference / consent)是此前**漏登记**的内容站 —— 之前 `getPortalUrl('docs')`
|
|
76
|
-
* 会退化成 `${origin}/docs`,任何控制台都链不到文档中心。这里补齐,属新增能力,不改变前 13 项行为。
|
|
77
|
-
*/
|
|
78
|
-
export const PORTALS: readonly PortalDefinition[] = [
|
|
79
|
-
{
|
|
80
|
-
code: 'auth',
|
|
81
|
-
host: 'auth',
|
|
82
|
-
base: '',
|
|
83
|
-
slugScoped: true,
|
|
84
|
-
routerSlug: false,
|
|
85
|
-
family: 'auth',
|
|
86
|
-
audience: 'public',
|
|
87
|
-
inSwitcher: false,
|
|
88
|
-
legacyEnvKey: 'VITE_AUTH_PAGES_URL',
|
|
89
|
-
note: '认证门户:授权码 + PKCE 入口',
|
|
90
|
-
},
|
|
91
|
-
{
|
|
92
|
-
code: 'admin',
|
|
93
|
-
host: 'admin',
|
|
94
|
-
base: '',
|
|
95
|
-
slugScoped: true,
|
|
96
|
-
routerSlug: true,
|
|
97
|
-
routerOrder: 1,
|
|
98
|
-
family: 'console',
|
|
99
|
-
audience: 'admin',
|
|
100
|
-
inSwitcher: true,
|
|
101
|
-
legacyEnvKey: 'VITE_ADMIN_CONSOLE_URL',
|
|
102
|
-
note: '管理控制台;env 可覆盖为 app.<root>/admin',
|
|
103
|
-
},
|
|
104
|
-
{
|
|
105
|
-
code: 'developer',
|
|
106
|
-
host: 'developer',
|
|
107
|
-
base: '',
|
|
108
|
-
slugScoped: false,
|
|
109
|
-
routerSlug: true,
|
|
110
|
-
routerOrder: 2,
|
|
111
|
-
family: 'content',
|
|
112
|
-
audience: 'public',
|
|
113
|
-
inSwitcher: true,
|
|
114
|
-
legacyEnvKey: 'VITE_DEVELOPER_PORTAL_URL',
|
|
115
|
-
},
|
|
116
|
-
{
|
|
117
|
-
code: 'user',
|
|
118
|
-
host: 'user',
|
|
119
|
-
base: '',
|
|
120
|
-
slugScoped: true,
|
|
121
|
-
routerSlug: false,
|
|
122
|
-
family: 'console',
|
|
123
|
-
audience: 'public',
|
|
124
|
-
inSwitcher: true,
|
|
125
|
-
legacyEnvKey: 'VITE_END_USER_PORTAL_URL',
|
|
126
|
-
note: '用户门户:全角色可见(非管理面)',
|
|
127
|
-
},
|
|
128
|
-
{
|
|
129
|
-
code: 'security',
|
|
130
|
-
host: 'security',
|
|
131
|
-
base: '',
|
|
132
|
-
slugScoped: true,
|
|
133
|
-
routerSlug: true,
|
|
134
|
-
routerOrder: 3,
|
|
135
|
-
family: 'console',
|
|
136
|
-
audience: 'admin',
|
|
137
|
-
inSwitcher: true,
|
|
138
|
-
legacyEnvKey: 'VITE_SECURITY_DASHBOARD_URL',
|
|
139
|
-
},
|
|
140
|
-
{
|
|
141
|
-
code: 'status',
|
|
142
|
-
host: 'status',
|
|
143
|
-
base: '',
|
|
144
|
-
slugScoped: false,
|
|
145
|
-
routerSlug: true,
|
|
146
|
-
routerOrder: 5,
|
|
147
|
-
family: 'content',
|
|
148
|
-
audience: 'public',
|
|
149
|
-
inSwitcher: true,
|
|
150
|
-
legacyEnvKey: 'VITE_STATUS_PAGE_URL',
|
|
151
|
-
},
|
|
152
|
-
{
|
|
153
|
-
code: 'landing',
|
|
154
|
-
host: '',
|
|
155
|
-
base: '',
|
|
156
|
-
slugScoped: false,
|
|
157
|
-
routerSlug: false,
|
|
158
|
-
family: 'landing',
|
|
159
|
-
audience: 'public',
|
|
160
|
-
inSwitcher: false,
|
|
161
|
-
legacyEnvKey: 'VITE_LANDING_SITE_URL',
|
|
162
|
-
note: '落根域(无子域前缀)',
|
|
163
|
-
},
|
|
164
|
-
{
|
|
165
|
-
code: 'authenticator',
|
|
166
|
-
host: 'authenticator',
|
|
167
|
-
base: '',
|
|
168
|
-
slugScoped: true,
|
|
169
|
-
routerSlug: false,
|
|
170
|
-
family: 'app',
|
|
171
|
-
audience: 'public',
|
|
172
|
-
inSwitcher: false,
|
|
173
|
-
legacyEnvKey: 'VITE_AUTHENTICATOR_APP_URL',
|
|
174
|
-
note: 'App 形态(TOTP/push),不做桌面门户切换目标',
|
|
175
|
-
},
|
|
176
|
-
{
|
|
177
|
-
code: 'trust',
|
|
178
|
-
host: 'trust',
|
|
179
|
-
base: '',
|
|
180
|
-
slugScoped: false,
|
|
181
|
-
routerSlug: true,
|
|
182
|
-
routerOrder: 6,
|
|
183
|
-
family: 'content',
|
|
184
|
-
audience: 'public',
|
|
185
|
-
inSwitcher: true,
|
|
186
|
-
legacyEnvKey: 'VITE_TRUST_CENTER_URL',
|
|
187
|
-
},
|
|
188
|
-
{
|
|
189
|
-
code: 'platform',
|
|
190
|
-
host: 'platform',
|
|
191
|
-
base: '',
|
|
192
|
-
slugScoped: true,
|
|
193
|
-
routerSlug: true,
|
|
194
|
-
routerOrder: 4,
|
|
195
|
-
family: 'console',
|
|
196
|
-
audience: 'platform',
|
|
197
|
-
inSwitcher: true,
|
|
198
|
-
legacyEnvKey: 'VITE_PLATFORM_CONSOLE_URL',
|
|
199
|
-
note: '仅平台租户成员;租户段挂载(OAuth client 按 slug 解析)',
|
|
200
|
-
},
|
|
201
|
-
{
|
|
202
|
-
code: 'brand',
|
|
203
|
-
host: 'brand',
|
|
204
|
-
base: '',
|
|
205
|
-
slugScoped: false,
|
|
206
|
-
routerSlug: false,
|
|
207
|
-
family: 'content',
|
|
208
|
-
audience: 'public',
|
|
209
|
-
inSwitcher: true,
|
|
210
|
-
note: '选品牌落地站;无旧 env 键',
|
|
211
|
-
},
|
|
212
|
-
{
|
|
213
|
-
code: 'secret',
|
|
214
|
-
host: 'secret',
|
|
215
|
-
base: '',
|
|
216
|
-
slugScoped: false,
|
|
217
|
-
routerSlug: false,
|
|
218
|
-
family: 'console',
|
|
219
|
-
audience: 'admin',
|
|
220
|
-
inSwitcher: true,
|
|
221
|
-
},
|
|
222
|
-
{
|
|
223
|
-
code: 'config',
|
|
224
|
-
host: 'config',
|
|
225
|
-
base: '',
|
|
226
|
-
slugScoped: false,
|
|
227
|
-
routerSlug: false,
|
|
228
|
-
family: 'console',
|
|
229
|
-
audience: 'admin',
|
|
230
|
-
inSwitcher: true,
|
|
231
|
-
},
|
|
232
|
-
{
|
|
233
|
-
code: 'docs',
|
|
234
|
-
host: 'docs',
|
|
235
|
-
base: '',
|
|
236
|
-
slugScoped: false,
|
|
237
|
-
routerSlug: false,
|
|
238
|
-
family: 'content',
|
|
239
|
-
audience: 'public',
|
|
240
|
-
inSwitcher: true,
|
|
241
|
-
note: '此前未登记(getPortalUrl 退化成 <origin>/docs)',
|
|
242
|
-
},
|
|
243
|
-
{
|
|
244
|
-
code: 'wiki',
|
|
245
|
-
host: 'wiki',
|
|
246
|
-
base: '',
|
|
247
|
-
slugScoped: false,
|
|
248
|
-
routerSlug: false,
|
|
249
|
-
family: 'content',
|
|
250
|
-
audience: 'public',
|
|
251
|
-
inSwitcher: true,
|
|
252
|
-
note: '此前未登记',
|
|
253
|
-
},
|
|
254
|
-
{
|
|
255
|
-
code: 'reference',
|
|
256
|
-
host: 'reference',
|
|
257
|
-
base: '',
|
|
258
|
-
slugScoped: false,
|
|
259
|
-
routerSlug: false,
|
|
260
|
-
family: 'content',
|
|
261
|
-
audience: 'public',
|
|
262
|
-
inSwitcher: true,
|
|
263
|
-
note: '此前未登记',
|
|
264
|
-
},
|
|
265
|
-
{
|
|
266
|
-
code: 'consent',
|
|
267
|
-
host: 'consent',
|
|
268
|
-
base: '',
|
|
269
|
-
slugScoped: false,
|
|
270
|
-
routerSlug: false,
|
|
271
|
-
family: 'content',
|
|
272
|
-
audience: 'public',
|
|
273
|
-
inSwitcher: true,
|
|
274
|
-
note: '此前未登记',
|
|
275
|
-
},
|
|
276
|
-
];
|
|
277
|
-
|
|
278
|
-
const BY_CODE: ReadonlyMap<string, PortalDefinition> = new Map(
|
|
279
|
-
PORTALS.map((p) => [p.code, p] as const),
|
|
280
|
-
);
|
|
281
|
-
|
|
282
|
-
/** 全部门户 code(顺序同表)。 */
|
|
283
|
-
export const PORTAL_CODES: readonly string[] = PORTALS.map((p) => p.code);
|
|
284
|
-
|
|
285
|
-
/** 取门户定义;未登记返回 undefined(调用方按旧口径兜底)。 */
|
|
286
|
-
export function getPortalDefinition(code: string): PortalDefinition | undefined {
|
|
287
|
-
return BY_CODE.get(code);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/** 入口是否拼租户段 `/<slug>`。 */
|
|
291
|
-
export function isSlugScopedPortal(code: string): boolean {
|
|
292
|
-
return BY_CODE.get(code)?.slugScoped === true;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
/** 是否控制台族(控制台切换器的候选集)。 */
|
|
296
|
-
export function isConsolePortal(code: string): boolean {
|
|
297
|
-
return BY_CODE.get(code)?.family === 'console';
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
/** 控制台族 code 清单(替代各站 `CONSOLE_PORTALS` 字面量)。 */
|
|
301
|
-
export function getConsolePortalCodes(): string[] {
|
|
302
|
-
return PORTALS.filter((p) => p.family === 'console').map((p) => p.code);
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
/** 默认进入切换器清单的 code(`usePortalCatalog` 不传 exclude 时的基线)。 */
|
|
306
|
-
export function getSwitcherIncludedCodes(): string[] {
|
|
307
|
-
return PORTALS.filter((p) => p.inSwitcher).map((p) => p.code);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
/** 默认不进切换器清单的 code(`usePortalCatalog` 的 DEFAULT_EXCLUDE)。 */
|
|
311
|
-
export function getSwitcherExcludedCodes(): string[] {
|
|
312
|
-
return PORTALS.filter((p) => !p.inSwitcher).map((p) => p.code);
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
/** 参与 `/<tenant>/<portal>` 路由 basename 解析的 code(按历史顺序 routerOrder 升序)。 */
|
|
316
|
-
export function getRouterPortalCodes(): string[] {
|
|
317
|
-
return PORTALS.filter((p) => p.routerSlug)
|
|
318
|
-
.slice()
|
|
319
|
-
.sort((a, b) => (a.routerOrder ?? Number.MAX_SAFE_INTEGER) - (b.routerOrder ?? Number.MAX_SAFE_INTEGER))
|
|
320
|
-
.map((p) => p.code);
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
/** 旧 env.js 直连 URL 键(VITE_*_URL)→ code 的反向映射源。 */
|
|
324
|
-
export function getLegacyEnvKey(code: string): string | undefined {
|
|
325
|
-
return BY_CODE.get(code)?.legacyEnvKey;
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* 按**受众**判定可见性(入口门禁镜像)。
|
|
330
|
-
*
|
|
331
|
-
* 同一函数服务两个数据源:注册表的 `audience` 与服务端目录里的 `config.portal.audience`。
|
|
332
|
-
* 未知/缺失受众一律按 `public`(叠加原则,不误伤新门户)。
|
|
333
|
-
*/
|
|
334
|
-
export function isPortalVisibleByAudience(
|
|
335
|
-
audience: string | null | undefined,
|
|
336
|
-
ctx: { role: string | null; isPlatformMember: boolean },
|
|
337
|
-
): boolean {
|
|
338
|
-
if (audience === 'platform') return ctx.isPlatformMember;
|
|
339
|
-
if (audience === 'admin') return ctx.role !== null && ADMIN_PLANE_ROLES.includes(ctx.role);
|
|
340
|
-
return true;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
/** 未登记的门户默认放行(叠加原则,不误伤)。 */
|
|
344
|
-
export function isPortalVisibleForAudience(
|
|
345
|
-
code: string,
|
|
346
|
-
ctx: { role: string | null; isPlatformMember: boolean },
|
|
347
|
-
): boolean {
|
|
348
|
-
return isPortalVisibleByAudience(BY_CODE.get(code)?.audience, ctx);
|
|
349
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 门户注册表 —— **门户元数据的单一真源**。
|
|
3
|
+
*
|
|
4
|
+
* 过去「门户清单」被写了十几次(`PORTAL_DEFAULTS` / `SLUG_PORTALS` /
|
|
5
|
+
* `getAppPortalSlugs()` / 重定向白名单 / `isPortalVisible` 的角色集 /
|
|
6
|
+
* `DEFAULT_EXCLUDE` / `PORTAL_KEY_MAP`,以及 6 个站点各持一份 `CONSOLE_PORTALS`)。
|
|
7
|
+
* 每加一个门户要改 7 个仓,且靠"进名单"才可见(小圈子)。
|
|
8
|
+
*
|
|
9
|
+
* 现在只有这张表。其余全部由它派生:
|
|
10
|
+
*
|
|
11
|
+
* 表字段 → 派生
|
|
12
|
+
* host / base → config.PORTAL_DEFAULTS、getPortalUrl()
|
|
13
|
+
* slugScoped → config.SLUG_PORTALS(入口是否拼 /<tenantSlug>)
|
|
14
|
+
* routerSlug → config.getAppPortalSlugs()(/<tenant>/<portal> 路由 basename)
|
|
15
|
+
* legacyEnvKey → config.PORTAL_KEY_MAP(旧 env.js VITE_*_URL)
|
|
16
|
+
* — → getAllowedRedirectOrigins()(redirect 白名单 = 全部 code)
|
|
17
|
+
* audience → hooks.isPortalVisible()(谁能看见:入口门禁的镜像)
|
|
18
|
+
* inSwitcher → usePortalCatalog DEFAULT_EXCLUDE(默认排除 = !inSwitcher)
|
|
19
|
+
* family='console' → getConsolePortalCodes()(控制台切换器全集,替代各站白名单)
|
|
20
|
+
*
|
|
21
|
+
* 新增门户 = 在这里加一行(若后端目录已透出 host/base,则连这一行都可省)。
|
|
22
|
+
* **禁止**在其它文件再写门户 code 字面量数组/Set。
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** 可见性受众 = 登录入口门禁的镜像(谁能登录,谁才看见)。 */
|
|
26
|
+
export type PortalAudience = 'public' | 'admin' | 'platform';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 门户族群:
|
|
30
|
+
* - `console`:管理面/用户面控制台(互相出现在切换器里)
|
|
31
|
+
* - `content`:内容站(developer / status / trust / brand / docs …)
|
|
32
|
+
* - `app`:App 形态(authenticator:TOTP/push,不列入桌面切换器)
|
|
33
|
+
* - `auth` / `landing`:认证与落地站,非切换目标
|
|
34
|
+
*/
|
|
35
|
+
export type PortalFamily = 'console' | 'content' | 'app' | 'auth' | 'landing';
|
|
36
|
+
|
|
37
|
+
export interface PortalDefinition {
|
|
38
|
+
/** 门户 code(后端 applications.code,全环境同值) */
|
|
39
|
+
code: string;
|
|
40
|
+
/** 子域前缀;空串 = 落在根域(landing) */
|
|
41
|
+
host: string;
|
|
42
|
+
/** 子域下的固定路径前缀(env 可覆盖,如 admin 的 '/admin') */
|
|
43
|
+
base: string;
|
|
44
|
+
/** 入口是否拼租户段 `/<slug>`(拼错会命中不存在的路由 ⇒ 空白/404) */
|
|
45
|
+
slugScoped: boolean;
|
|
46
|
+
/** 是否参与 `/<tenant>/<portal>` 路由 basename 解析 */
|
|
47
|
+
routerSlug: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* `getAppPortalSlugs()` 的返回顺序(历史上是 `admin, developer, security, platform, status, trust`)。
|
|
50
|
+
* 该数组是导出 API,顺序可观测 ⇒ 用显式序号保序,而不是依赖表顺序。
|
|
51
|
+
*/
|
|
52
|
+
routerOrder?: number;
|
|
53
|
+
family: PortalFamily;
|
|
54
|
+
audience: PortalAudience;
|
|
55
|
+
/** 默认进入 `usePortalCatalog` 的门户清单(false ⇒ 进 DEFAULT_EXCLUDE) */
|
|
56
|
+
inSwitcher: boolean;
|
|
57
|
+
/** 旧 env.js 直连 URL 键(VITE_*_URL)→ 向后兼容映射用 */
|
|
58
|
+
legacyEnvKey?: string;
|
|
59
|
+
/** 备注(为什么这个门户这样配置) */
|
|
60
|
+
note?: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 管理面平面角色集(admin / security / config / secret 的门户 = 登录入口门禁同集)。 */
|
|
64
|
+
export const ADMIN_PLANE_ROLES: readonly string[] = [
|
|
65
|
+
'super_admin',
|
|
66
|
+
'admin',
|
|
67
|
+
'security_admin',
|
|
68
|
+
'user_manager',
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 注册表本体(顺序 = 重定向白名单推导顺序)。
|
|
73
|
+
*
|
|
74
|
+
* 前 13 项与迁移前 `PORTAL_DEFAULTS` 逐项等价(见 `__tests__/registry.test.ts` 的 oracle 断言);
|
|
75
|
+
* 后 4 项(docs / wiki / reference / consent)是此前**漏登记**的内容站 —— 之前 `getPortalUrl('docs')`
|
|
76
|
+
* 会退化成 `${origin}/docs`,任何控制台都链不到文档中心。这里补齐,属新增能力,不改变前 13 项行为。
|
|
77
|
+
*/
|
|
78
|
+
export const PORTALS: readonly PortalDefinition[] = [
|
|
79
|
+
{
|
|
80
|
+
code: 'auth',
|
|
81
|
+
host: 'auth',
|
|
82
|
+
base: '',
|
|
83
|
+
slugScoped: true,
|
|
84
|
+
routerSlug: false,
|
|
85
|
+
family: 'auth',
|
|
86
|
+
audience: 'public',
|
|
87
|
+
inSwitcher: false,
|
|
88
|
+
legacyEnvKey: 'VITE_AUTH_PAGES_URL',
|
|
89
|
+
note: '认证门户:授权码 + PKCE 入口',
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
code: 'admin',
|
|
93
|
+
host: 'admin',
|
|
94
|
+
base: '',
|
|
95
|
+
slugScoped: true,
|
|
96
|
+
routerSlug: true,
|
|
97
|
+
routerOrder: 1,
|
|
98
|
+
family: 'console',
|
|
99
|
+
audience: 'admin',
|
|
100
|
+
inSwitcher: true,
|
|
101
|
+
legacyEnvKey: 'VITE_ADMIN_CONSOLE_URL',
|
|
102
|
+
note: '管理控制台;env 可覆盖为 app.<root>/admin',
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
code: 'developer',
|
|
106
|
+
host: 'developer',
|
|
107
|
+
base: '',
|
|
108
|
+
slugScoped: false,
|
|
109
|
+
routerSlug: true,
|
|
110
|
+
routerOrder: 2,
|
|
111
|
+
family: 'content',
|
|
112
|
+
audience: 'public',
|
|
113
|
+
inSwitcher: true,
|
|
114
|
+
legacyEnvKey: 'VITE_DEVELOPER_PORTAL_URL',
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
code: 'user',
|
|
118
|
+
host: 'user',
|
|
119
|
+
base: '',
|
|
120
|
+
slugScoped: true,
|
|
121
|
+
routerSlug: false,
|
|
122
|
+
family: 'console',
|
|
123
|
+
audience: 'public',
|
|
124
|
+
inSwitcher: true,
|
|
125
|
+
legacyEnvKey: 'VITE_END_USER_PORTAL_URL',
|
|
126
|
+
note: '用户门户:全角色可见(非管理面)',
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
code: 'security',
|
|
130
|
+
host: 'security',
|
|
131
|
+
base: '',
|
|
132
|
+
slugScoped: true,
|
|
133
|
+
routerSlug: true,
|
|
134
|
+
routerOrder: 3,
|
|
135
|
+
family: 'console',
|
|
136
|
+
audience: 'admin',
|
|
137
|
+
inSwitcher: true,
|
|
138
|
+
legacyEnvKey: 'VITE_SECURITY_DASHBOARD_URL',
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
code: 'status',
|
|
142
|
+
host: 'status',
|
|
143
|
+
base: '',
|
|
144
|
+
slugScoped: false,
|
|
145
|
+
routerSlug: true,
|
|
146
|
+
routerOrder: 5,
|
|
147
|
+
family: 'content',
|
|
148
|
+
audience: 'public',
|
|
149
|
+
inSwitcher: true,
|
|
150
|
+
legacyEnvKey: 'VITE_STATUS_PAGE_URL',
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
code: 'landing',
|
|
154
|
+
host: '',
|
|
155
|
+
base: '',
|
|
156
|
+
slugScoped: false,
|
|
157
|
+
routerSlug: false,
|
|
158
|
+
family: 'landing',
|
|
159
|
+
audience: 'public',
|
|
160
|
+
inSwitcher: false,
|
|
161
|
+
legacyEnvKey: 'VITE_LANDING_SITE_URL',
|
|
162
|
+
note: '落根域(无子域前缀)',
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
code: 'authenticator',
|
|
166
|
+
host: 'authenticator',
|
|
167
|
+
base: '',
|
|
168
|
+
slugScoped: true,
|
|
169
|
+
routerSlug: false,
|
|
170
|
+
family: 'app',
|
|
171
|
+
audience: 'public',
|
|
172
|
+
inSwitcher: false,
|
|
173
|
+
legacyEnvKey: 'VITE_AUTHENTICATOR_APP_URL',
|
|
174
|
+
note: 'App 形态(TOTP/push),不做桌面门户切换目标',
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
code: 'trust',
|
|
178
|
+
host: 'trust',
|
|
179
|
+
base: '',
|
|
180
|
+
slugScoped: false,
|
|
181
|
+
routerSlug: true,
|
|
182
|
+
routerOrder: 6,
|
|
183
|
+
family: 'content',
|
|
184
|
+
audience: 'public',
|
|
185
|
+
inSwitcher: true,
|
|
186
|
+
legacyEnvKey: 'VITE_TRUST_CENTER_URL',
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
code: 'platform',
|
|
190
|
+
host: 'platform',
|
|
191
|
+
base: '',
|
|
192
|
+
slugScoped: true,
|
|
193
|
+
routerSlug: true,
|
|
194
|
+
routerOrder: 4,
|
|
195
|
+
family: 'console',
|
|
196
|
+
audience: 'platform',
|
|
197
|
+
inSwitcher: true,
|
|
198
|
+
legacyEnvKey: 'VITE_PLATFORM_CONSOLE_URL',
|
|
199
|
+
note: '仅平台租户成员;租户段挂载(OAuth client 按 slug 解析)',
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
code: 'brand',
|
|
203
|
+
host: 'brand',
|
|
204
|
+
base: '',
|
|
205
|
+
slugScoped: false,
|
|
206
|
+
routerSlug: false,
|
|
207
|
+
family: 'content',
|
|
208
|
+
audience: 'public',
|
|
209
|
+
inSwitcher: true,
|
|
210
|
+
note: '选品牌落地站;无旧 env 键',
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
code: 'secret',
|
|
214
|
+
host: 'secret',
|
|
215
|
+
base: '',
|
|
216
|
+
slugScoped: false,
|
|
217
|
+
routerSlug: false,
|
|
218
|
+
family: 'console',
|
|
219
|
+
audience: 'admin',
|
|
220
|
+
inSwitcher: true,
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
code: 'config',
|
|
224
|
+
host: 'config',
|
|
225
|
+
base: '',
|
|
226
|
+
slugScoped: false,
|
|
227
|
+
routerSlug: false,
|
|
228
|
+
family: 'console',
|
|
229
|
+
audience: 'admin',
|
|
230
|
+
inSwitcher: true,
|
|
231
|
+
},
|
|
232
|
+
{
|
|
233
|
+
code: 'docs',
|
|
234
|
+
host: 'docs',
|
|
235
|
+
base: '',
|
|
236
|
+
slugScoped: false,
|
|
237
|
+
routerSlug: false,
|
|
238
|
+
family: 'content',
|
|
239
|
+
audience: 'public',
|
|
240
|
+
inSwitcher: true,
|
|
241
|
+
note: '此前未登记(getPortalUrl 退化成 <origin>/docs)',
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
code: 'wiki',
|
|
245
|
+
host: 'wiki',
|
|
246
|
+
base: '',
|
|
247
|
+
slugScoped: false,
|
|
248
|
+
routerSlug: false,
|
|
249
|
+
family: 'content',
|
|
250
|
+
audience: 'public',
|
|
251
|
+
inSwitcher: true,
|
|
252
|
+
note: '此前未登记',
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
code: 'reference',
|
|
256
|
+
host: 'reference',
|
|
257
|
+
base: '',
|
|
258
|
+
slugScoped: false,
|
|
259
|
+
routerSlug: false,
|
|
260
|
+
family: 'content',
|
|
261
|
+
audience: 'public',
|
|
262
|
+
inSwitcher: true,
|
|
263
|
+
note: '此前未登记',
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
code: 'consent',
|
|
267
|
+
host: 'consent',
|
|
268
|
+
base: '',
|
|
269
|
+
slugScoped: false,
|
|
270
|
+
routerSlug: false,
|
|
271
|
+
family: 'content',
|
|
272
|
+
audience: 'public',
|
|
273
|
+
inSwitcher: true,
|
|
274
|
+
note: '此前未登记',
|
|
275
|
+
},
|
|
276
|
+
];
|
|
277
|
+
|
|
278
|
+
const BY_CODE: ReadonlyMap<string, PortalDefinition> = new Map(
|
|
279
|
+
PORTALS.map((p) => [p.code, p] as const),
|
|
280
|
+
);
|
|
281
|
+
|
|
282
|
+
/** 全部门户 code(顺序同表)。 */
|
|
283
|
+
export const PORTAL_CODES: readonly string[] = PORTALS.map((p) => p.code);
|
|
284
|
+
|
|
285
|
+
/** 取门户定义;未登记返回 undefined(调用方按旧口径兜底)。 */
|
|
286
|
+
export function getPortalDefinition(code: string): PortalDefinition | undefined {
|
|
287
|
+
return BY_CODE.get(code);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** 入口是否拼租户段 `/<slug>`。 */
|
|
291
|
+
export function isSlugScopedPortal(code: string): boolean {
|
|
292
|
+
return BY_CODE.get(code)?.slugScoped === true;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** 是否控制台族(控制台切换器的候选集)。 */
|
|
296
|
+
export function isConsolePortal(code: string): boolean {
|
|
297
|
+
return BY_CODE.get(code)?.family === 'console';
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** 控制台族 code 清单(替代各站 `CONSOLE_PORTALS` 字面量)。 */
|
|
301
|
+
export function getConsolePortalCodes(): string[] {
|
|
302
|
+
return PORTALS.filter((p) => p.family === 'console').map((p) => p.code);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** 默认进入切换器清单的 code(`usePortalCatalog` 不传 exclude 时的基线)。 */
|
|
306
|
+
export function getSwitcherIncludedCodes(): string[] {
|
|
307
|
+
return PORTALS.filter((p) => p.inSwitcher).map((p) => p.code);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** 默认不进切换器清单的 code(`usePortalCatalog` 的 DEFAULT_EXCLUDE)。 */
|
|
311
|
+
export function getSwitcherExcludedCodes(): string[] {
|
|
312
|
+
return PORTALS.filter((p) => !p.inSwitcher).map((p) => p.code);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** 参与 `/<tenant>/<portal>` 路由 basename 解析的 code(按历史顺序 routerOrder 升序)。 */
|
|
316
|
+
export function getRouterPortalCodes(): string[] {
|
|
317
|
+
return PORTALS.filter((p) => p.routerSlug)
|
|
318
|
+
.slice()
|
|
319
|
+
.sort((a, b) => (a.routerOrder ?? Number.MAX_SAFE_INTEGER) - (b.routerOrder ?? Number.MAX_SAFE_INTEGER))
|
|
320
|
+
.map((p) => p.code);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** 旧 env.js 直连 URL 键(VITE_*_URL)→ code 的反向映射源。 */
|
|
324
|
+
export function getLegacyEnvKey(code: string): string | undefined {
|
|
325
|
+
return BY_CODE.get(code)?.legacyEnvKey;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* 按**受众**判定可见性(入口门禁镜像)。
|
|
330
|
+
*
|
|
331
|
+
* 同一函数服务两个数据源:注册表的 `audience` 与服务端目录里的 `config.portal.audience`。
|
|
332
|
+
* 未知/缺失受众一律按 `public`(叠加原则,不误伤新门户)。
|
|
333
|
+
*/
|
|
334
|
+
export function isPortalVisibleByAudience(
|
|
335
|
+
audience: string | null | undefined,
|
|
336
|
+
ctx: { role: string | null; isPlatformMember: boolean },
|
|
337
|
+
): boolean {
|
|
338
|
+
if (audience === 'platform') return ctx.isPlatformMember;
|
|
339
|
+
if (audience === 'admin') return ctx.role !== null && ADMIN_PLANE_ROLES.includes(ctx.role);
|
|
340
|
+
return true;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** 未登记的门户默认放行(叠加原则,不误伤)。 */
|
|
344
|
+
export function isPortalVisibleForAudience(
|
|
345
|
+
code: string,
|
|
346
|
+
ctx: { role: string | null; isPlatformMember: boolean },
|
|
347
|
+
): boolean {
|
|
348
|
+
return isPortalVisibleByAudience(BY_CODE.get(code)?.audience, ctx);
|
|
349
|
+
}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
// @vitest-environment jsdom
|
|
2
|
-
// ============================================================
|
|
3
|
-
// 会话心跳 hook 契约锁(2026-11-10 会话策略交接 §3.3):
|
|
4
|
-
// ① 端点由调用方给定(管理面 /admin/sessions/me/activity、用户面 /sessions/me/activity)
|
|
5
|
-
// ② getToken 可注入(config/secret 用本地 @/lib/auth,不复用 shared store)
|
|
6
|
-
// ③ 三道闸门:页面不可见不发 / 最近无交互不发 / 未到间隔不发
|
|
7
|
-
// ④ 缺 sid 不发(服务端按 token 归属,前端不拼 sid)
|
|
8
|
-
// ============================================================
|
|
9
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
10
|
-
import { renderHook } from '@testing-library/react';
|
|
11
|
-
|
|
12
|
-
import { useSessionActivity } from '../useSessionActivity';
|
|
13
|
-
|
|
14
|
-
/** 构造一个带 sid 的假 JWT(签名无关,只解码 payload)。 */
|
|
15
|
-
function fakeToken(payload: Record<string, unknown>): string {
|
|
16
|
-
const b64 = (obj: Record<string, unknown>) =>
|
|
17
|
-
Buffer.from(JSON.stringify(obj)).toString('base64url');
|
|
18
|
-
return `${b64({ alg: 'none' })}.${b64(payload)}.sig`;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const fetchMock = vi.fn(() => Promise.resolve({ ok: true } as Response));
|
|
22
|
-
|
|
23
|
-
beforeEach(() => {
|
|
24
|
-
vi.useFakeTimers();
|
|
25
|
-
vi.stubGlobal('fetch', fetchMock);
|
|
26
|
-
fetchMock.mockClear();
|
|
27
|
-
Object.defineProperty(document, 'visibilityState', { value: 'visible', configurable: true });
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
afterEach(() => {
|
|
31
|
-
vi.useRealTimers();
|
|
32
|
-
vi.unstubAllGlobals();
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
describe('useSessionActivity', () => {
|
|
36
|
-
it('可见 + 有交互 + 到间隔 → 按调用方端点发一次心跳', () => {
|
|
37
|
-
const token = fakeToken({ custom: { sid: 'sid-1' }, tenant_id: 't-1' });
|
|
38
|
-
renderHook(() =>
|
|
39
|
-
useSessionActivity({ endpoint: '/bff/session/api/v1/admin/sessions/me/activity', getToken: () => token }),
|
|
40
|
-
);
|
|
41
|
-
|
|
42
|
-
vi.advanceTimersByTime(5 * 60_000);
|
|
43
|
-
|
|
44
|
-
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
45
|
-
const [url, init] = fetchMock.mock.calls[0] as [string, RequestInit];
|
|
46
|
-
expect(url).toBe('/bff/session/api/v1/admin/sessions/me/activity');
|
|
47
|
-
expect(init.method).toBe('POST');
|
|
48
|
-
expect((init.headers as Record<string, string>).Authorization).toBe('Bearer ' + token);
|
|
49
|
-
expect((init.headers as Record<string, string>)['X-Tenant-ID']).toBe('t-1');
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it('页面不可见 → 不发', () => {
|
|
53
|
-
Object.defineProperty(document, 'visibilityState', { value: 'hidden', configurable: true });
|
|
54
|
-
renderHook(() =>
|
|
55
|
-
useSessionActivity({ endpoint: '/x', getToken: () => fakeToken({ custom: { sid: 's' } }) }),
|
|
56
|
-
);
|
|
57
|
-
vi.advanceTimersByTime(10 * 60_000);
|
|
58
|
-
expect(fetchMock).not.toHaveBeenCalled();
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it('没有 sid → 不发(服务端按 token 归属,前端不拼 sid)', () => {
|
|
62
|
-
renderHook(() =>
|
|
63
|
-
useSessionActivity({ endpoint: '/x', getToken: () => fakeToken({ sub: 'u-1' }) }),
|
|
64
|
-
);
|
|
65
|
-
vi.advanceTimersByTime(10 * 60_000);
|
|
66
|
-
expect(fetchMock).not.toHaveBeenCalled();
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('超过交互窗口无交互 → 停发(人不在就让会话自然过期)', () => {
|
|
70
|
-
renderHook(() =>
|
|
71
|
-
useSessionActivity({ endpoint: '/x', getToken: () => fakeToken({ custom: { sid: 'sid-1' } }) }),
|
|
72
|
-
);
|
|
73
|
-
|
|
74
|
-
vi.advanceTimersByTime(5 * 60_000);
|
|
75
|
-
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
76
|
-
|
|
77
|
-
// 之后 15 分钟无人交互(lastInteraction 停在挂载时刻)
|
|
78
|
-
vi.advanceTimersByTime(15 * 60_000);
|
|
79
|
-
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('disabled → 完全不发', () => {
|
|
83
|
-
renderHook(() =>
|
|
84
|
-
useSessionActivity({ endpoint: '/x', getToken: () => fakeToken({ custom: { sid: 's' } }), disabled: true }),
|
|
85
|
-
);
|
|
86
|
-
vi.advanceTimersByTime(10 * 60_000);
|
|
87
|
-
expect(fetchMock).not.toHaveBeenCalled();
|
|
88
|
-
});
|
|
89
|
-
});
|
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import { useEffect, useRef } from 'react';
|
|
2
|
-
|
|
3
|
-
import { getAccessToken } from '../auth/store';
|
|
4
|
-
import { decodeJwtPayload } from '../auth/jwt-payload';
|
|
5
|
-
|
|
6
|
-
/** 心跳间隔:闲置 30 分钟的 1/6(业界常用 5 分钟)。 */
|
|
7
|
-
const DEFAULT_PING_INTERVAL = 5 * 60_000;
|
|
8
|
-
/** 检查节拍:每秒级太重,1 分钟一次足够(真正的间隔由 pingInterval 控制)。 */
|
|
9
|
-
const DEFAULT_TICK_INTERVAL = 60_000;
|
|
10
|
-
/** 交互新鲜度:超过这个时间没交互就不发心跳(人不在,让会话自然闲置过期)。 */
|
|
11
|
-
const DEFAULT_INTERACTION_FRESH = 5 * 60_000;
|
|
12
|
-
|
|
13
|
-
export interface UseSessionActivityOptions {
|
|
14
|
-
/**
|
|
15
|
-
* 心跳端点。管理面用 `/bff/session/api/v1/admin/sessions/me/activity`,
|
|
16
|
-
* 用户面用 `/bff/session/api/v1/sessions/me/activity`。
|
|
17
|
-
*/
|
|
18
|
-
endpoint: string;
|
|
19
|
-
/**
|
|
20
|
-
* 取 access token。默认用 shared 的 `getAccessToken`;
|
|
21
|
-
* 门户若用本地 auth 模块(如 config/secret 的 `@/lib/auth`),可注入自己的实现。
|
|
22
|
-
*/
|
|
23
|
-
getToken?: () => string | null;
|
|
24
|
-
/** 心跳最小间隔,默认 5 分钟。 */
|
|
25
|
-
pingIntervalMs?: number;
|
|
26
|
-
/** 检查节拍,默认 60 秒。 */
|
|
27
|
-
tickIntervalMs?: number;
|
|
28
|
-
/** 交互新鲜度窗口,默认 5 分钟。 */
|
|
29
|
-
interactionFreshMs?: number;
|
|
30
|
-
/** 关闭开关(灰度/测试用),默认 false。 */
|
|
31
|
-
disabled?: boolean;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function str(source: Record<string, unknown> | null | undefined, key: string): string {
|
|
35
|
-
const value = source?.[key];
|
|
36
|
-
return typeof value === 'string' ? value : '';
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* 会话活动心跳(NIST「activity resets inactivity」口径)。
|
|
41
|
-
*
|
|
42
|
-
* 只在「页面可见 + 最近 N 分钟有真实交互」时上报;失败静默,不影响业务请求。
|
|
43
|
-
* 服务端首次心跳把会话切到闲置策略(opt-in),未接心跳的客户端不受影响。
|
|
44
|
-
* 心跳只续闲置窗口,绝对上限由服务端固定,心跳无法突破。
|
|
45
|
-
*/
|
|
46
|
-
export function useSessionActivity(options: UseSessionActivityOptions): void {
|
|
47
|
-
const {
|
|
48
|
-
endpoint,
|
|
49
|
-
getToken = getAccessToken,
|
|
50
|
-
pingIntervalMs = DEFAULT_PING_INTERVAL,
|
|
51
|
-
tickIntervalMs = DEFAULT_TICK_INTERVAL,
|
|
52
|
-
interactionFreshMs = DEFAULT_INTERACTION_FRESH,
|
|
53
|
-
disabled = false,
|
|
54
|
-
} = options;
|
|
55
|
-
|
|
56
|
-
const lastInteraction = useRef(Date.now());
|
|
57
|
-
const lastPing = useRef(0);
|
|
58
|
-
|
|
59
|
-
// 依赖项放在 ref 里,避免调用方每次渲染传新对象导致定时器重建。
|
|
60
|
-
const config = useRef({ endpoint, getToken, pingIntervalMs, tickIntervalMs, interactionFreshMs, disabled });
|
|
61
|
-
config.current = { endpoint, getToken, pingIntervalMs, tickIntervalMs, interactionFreshMs, disabled };
|
|
62
|
-
|
|
63
|
-
useEffect(() => {
|
|
64
|
-
const mark = () => {
|
|
65
|
-
lastInteraction.current = Date.now();
|
|
66
|
-
};
|
|
67
|
-
const events: Array<keyof WindowEventMap> = ['pointerdown', 'keydown', 'touchstart', 'wheel'];
|
|
68
|
-
events.forEach((name) => window.addEventListener(name, mark, { passive: true }));
|
|
69
|
-
|
|
70
|
-
const ping = () => {
|
|
71
|
-
const cfg = config.current;
|
|
72
|
-
if (cfg.disabled) return;
|
|
73
|
-
const token = cfg.getToken();
|
|
74
|
-
if (!token) return;
|
|
75
|
-
if (typeof document !== 'undefined' && document.visibilityState !== 'visible') return;
|
|
76
|
-
const now = Date.now();
|
|
77
|
-
if (now - lastInteraction.current > cfg.interactionFreshMs) return;
|
|
78
|
-
if (now - lastPing.current < cfg.pingIntervalMs) return;
|
|
79
|
-
const claims = decodeJwtPayload(token);
|
|
80
|
-
const custom = (claims?.custom ?? null) as Record<string, unknown> | null;
|
|
81
|
-
const sid = str(custom, 'sid') || str(custom, 'session_id') || str(claims, 'session_id');
|
|
82
|
-
if (!sid) return;
|
|
83
|
-
lastPing.current = now;
|
|
84
|
-
const headers: Record<string, string> = {
|
|
85
|
-
Accept: 'application/json',
|
|
86
|
-
Authorization: 'Bearer ' + token,
|
|
87
|
-
};
|
|
88
|
-
const tenant = str(claims, 'tenant_id') || str(custom, 'tenant_id');
|
|
89
|
-
const app = str(claims, 'app_id') || str(custom, 'app_id');
|
|
90
|
-
if (tenant) headers['X-Tenant-ID'] = tenant;
|
|
91
|
-
if (app) headers['X-App-ID'] = app;
|
|
92
|
-
void fetch(cfg.endpoint, { method: 'POST', headers, credentials: 'omit' }).catch(() => undefined);
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const timer = window.setInterval(ping, tickIntervalMs);
|
|
96
|
-
const onVisible = () => {
|
|
97
|
-
if (document.visibilityState === 'visible') ping();
|
|
98
|
-
};
|
|
99
|
-
document.addEventListener('visibilitychange', onVisible);
|
|
100
|
-
return () => {
|
|
101
|
-
events.forEach((name) => window.removeEventListener(name, mark));
|
|
102
|
-
window.clearInterval(timer);
|
|
103
|
-
document.removeEventListener('visibilitychange', onVisible);
|
|
104
|
-
};
|
|
105
|
-
}, [tickIntervalMs]);
|
|
106
|
-
}
|