27 lines
635 B
TypeScript
27 lines
635 B
TypeScript
/** 供 `apiJson` 调用,避免直接依赖 React;由 AppProviders 注册实现。 */
|
|
type AuthEventsImpl = {
|
|
on401: (msg: string) => void;
|
|
on403: (msg: string) => void;
|
|
};
|
|
|
|
let impl: Partial<AuthEventsImpl> = {};
|
|
let last401At = 0;
|
|
|
|
export function registerAuthEvents(next: Partial<AuthEventsImpl>) {
|
|
impl = next;
|
|
}
|
|
|
|
/** 同一秒内合并多次 401,避免并发请求重复弹窗 */
|
|
export function emit401Unauthorized(msg: string) {
|
|
const now = Date.now();
|
|
if (now - last401At < 800) {
|
|
return;
|
|
}
|
|
last401At = now;
|
|
impl.on401?.(msg);
|
|
}
|
|
|
|
export function emit403Forbidden(msg: string) {
|
|
impl.on403?.(msg);
|
|
}
|