4132b80ee0
- 新增对话列表、聊天、角色详情、书架详情及个人资料独立路由 - 导航使用真实链接,补齐历史返回、登录深链与会话草稿恢复 - 搜索筛选由 URL 驱动,修复浏览器返回时丢失筛选的问题 - 补齐路由校验回归、页面加载与异常入口,并同步 README
65 lines
2.3 KiB
TypeScript
65 lines
2.3 KiB
TypeScript
/** 数字人页面的固定地址与登录回跳校验,禁止把外部输入作为任意跳转目标。 */
|
|
|
|
import type { Route } from "next";
|
|
|
|
/** 顶部和底部导航共享同一份页面映射,保留旧工作台 /me 的兼容入口。 */
|
|
export const digitalTabs = {
|
|
discover: "/discover",
|
|
chats: "/chats",
|
|
library: "/library",
|
|
me: "/profile",
|
|
} as const;
|
|
|
|
/** 数字人导航项。 */
|
|
export type DigitalTab = keyof typeof digitalTabs;
|
|
|
|
/** 与后端资源 ID 的格式保持一致,非法深链直接显示不存在。 */
|
|
export function validDigitalId(id: string) {
|
|
return /^[a-zA-Z0-9_-]{1,64}$/.test(id);
|
|
}
|
|
|
|
/** 会话永久地址;导航和创建分支都使用同一个入口。 */
|
|
export function chatPath(id: string): `/chats/${string}` {
|
|
return `/chats/${encodeURIComponent(id)}`;
|
|
}
|
|
|
|
/** 角色与书架内容分别拥有可直接访问的详情页。 */
|
|
export function contentPath(card: {
|
|
id: string;
|
|
kind: string;
|
|
}): `/characters/${string}` | `/library/${string}` {
|
|
return `/${card.kind === "character" ? "characters" : "library"}/${encodeURIComponent(card.id)}`;
|
|
}
|
|
|
|
/** 只接受数字人业务页面,拒绝外站、接口、登录循环与畸形路径。 */
|
|
export function safeDigitalReturnTo(value: string | null | undefined): Route {
|
|
if (
|
|
!value ||
|
|
!value.startsWith("/") ||
|
|
/^\/[/\\]/.test(value) ||
|
|
/[\\\u0000-\u0020\u007f]/.test(value)
|
|
)
|
|
return "/discover";
|
|
const url = new URL(value, "https://stellar.invalid");
|
|
if (
|
|
url.origin !== "https://stellar.invalid" ||
|
|
!/^\/(?:discover|profile|chats(?:\/[a-zA-Z0-9_-]{1,64})?|library(?:\/[a-zA-Z0-9_-]{1,64})?|characters\/[a-zA-Z0-9_-]{1,64})$/.test(
|
|
url.pathname,
|
|
)
|
|
)
|
|
return "/discover";
|
|
return (url.pathname + url.search + url.hash) as Route;
|
|
}
|
|
|
|
/** 未登录时携带原始页面,登录成功后恢复具体会话而不是固定回首页。 */
|
|
export function digitalLoginPath(returnTo: string): `/login?next=${string}` {
|
|
return `/login?next=${encodeURIComponent(safeDigitalReturnTo(returnTo))}`;
|
|
}
|
|
|
|
/** 过期会话先清理 HttpOnly Cookie,避免代理把登录页重定向回失效会话。 */
|
|
export function expiredDigitalSessionPath(
|
|
returnTo: string,
|
|
): `/api/auth/logout?next=${string}` {
|
|
return `/api/auth/logout?next=${encodeURIComponent(digitalLoginPath(returnTo))}`;
|
|
}
|