feat: 添加 H5 端 openid 一致性检查,更新路由守卫逻辑以清除不一致的缓存
This commit is contained in:
@@ -1,134 +1,149 @@
|
||||
import { createBrowserRouter, Navigate, Outlet } from "react-router";
|
||||
import { LoginPage } from "./pages/LoginPage";
|
||||
import { OnboardingPage } from "./pages/OnboardingPage";
|
||||
import { HomePage } from "./pages/HomePage";
|
||||
import { CameraCapturePage } from "./pages/CameraCapturePage";
|
||||
import { DataVerificationPage } from "./pages/DataVerificationPage";
|
||||
import { AIAnalysisPage } from "./pages/AIAnalysisPage";
|
||||
import { ReportDetailPage } from "./pages/ReportDetailPage";
|
||||
import { DevNavigationPage } from "./pages/DevNavigationPage";
|
||||
import { AIChatPage } from "./pages/AIChatPage";
|
||||
import { HealthRecordsPage } from "./pages/HealthRecordsPage";
|
||||
import { GrowthCurvePage } from "./pages/GrowthCurvePage";
|
||||
import { TimelinePage } from "./pages/TimelinePage";
|
||||
import { CalendarPage } from "./pages/CalendarPage";
|
||||
import { DoctorQuestionsPage } from "./pages/DoctorQuestionsPage";
|
||||
import { SharePage } from "./pages/SharePage";
|
||||
import { ExpertConsultationPage } from "./pages/ExpertConsultationPage";
|
||||
import { ProfilePage } from "./pages/ProfilePage";
|
||||
import { VoiceRecordPage } from "./pages/VoiceRecordPage";
|
||||
import { NotificationPage } from "./pages/NotificationPage";
|
||||
import { MobileLayout } from "./components/MobileLayout";
|
||||
import { getToken } from "./services/api";
|
||||
|
||||
/** 路由守卫:未登录跳转登录页 */
|
||||
function ProtectedRoute() {
|
||||
const token = getToken();
|
||||
const urlToken = new URLSearchParams(window.location.search).get('token');
|
||||
|
||||
// 来自小程序的 token → 让 LoginPage 处理
|
||||
if (urlToken) {
|
||||
return <Navigate to={`/?token=${encodeURIComponent(urlToken)}`} replace />;
|
||||
}
|
||||
|
||||
if (!token) {
|
||||
return <Navigate to="/" replace />;
|
||||
}
|
||||
return <Outlet />;
|
||||
}
|
||||
|
||||
export const router = createBrowserRouter([
|
||||
{
|
||||
// 全局 H5 移动端布局容器
|
||||
element: <MobileLayout />,
|
||||
children: [
|
||||
// 公开路由
|
||||
{
|
||||
path: "/dev",
|
||||
Component: DevNavigationPage,
|
||||
},
|
||||
{
|
||||
path: "/",
|
||||
Component: LoginPage,
|
||||
},
|
||||
{
|
||||
path: "/onboarding",
|
||||
Component: OnboardingPage,
|
||||
},
|
||||
// 需要登录的路由
|
||||
{
|
||||
element: <ProtectedRoute />,
|
||||
children: [
|
||||
{
|
||||
path: "/home",
|
||||
Component: HomePage,
|
||||
},
|
||||
{
|
||||
path: "/profile",
|
||||
Component: ProfilePage,
|
||||
},
|
||||
{
|
||||
path: "/camera",
|
||||
Component: CameraCapturePage,
|
||||
},
|
||||
{
|
||||
path: "/review",
|
||||
Component: DataVerificationPage,
|
||||
},
|
||||
{
|
||||
path: "/verify",
|
||||
Component: DataVerificationPage,
|
||||
},
|
||||
{
|
||||
path: "/analysis/:reportId",
|
||||
Component: AIAnalysisPage,
|
||||
},
|
||||
{
|
||||
path: "/report/:reportId",
|
||||
Component: ReportDetailPage,
|
||||
},
|
||||
{
|
||||
path: "/chat/:reportId",
|
||||
Component: AIChatPage,
|
||||
},
|
||||
{
|
||||
path: "/voice-record",
|
||||
Component: VoiceRecordPage,
|
||||
},
|
||||
{
|
||||
path: "/health-records",
|
||||
Component: HealthRecordsPage,
|
||||
},
|
||||
{
|
||||
path: "/growth-curve",
|
||||
Component: GrowthCurvePage,
|
||||
},
|
||||
{
|
||||
path: "/timeline",
|
||||
Component: TimelinePage,
|
||||
},
|
||||
{
|
||||
path: "/calendar",
|
||||
Component: CalendarPage,
|
||||
},
|
||||
{
|
||||
path: "/doctor-questions/:reportId",
|
||||
Component: DoctorQuestionsPage,
|
||||
},
|
||||
{
|
||||
path: "/share/:reportId",
|
||||
Component: SharePage,
|
||||
},
|
||||
{
|
||||
path: "/expert-consultation",
|
||||
Component: ExpertConsultationPage,
|
||||
},
|
||||
{
|
||||
path: "/notifications",
|
||||
Component: NotificationPage,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
], { basename: "/app_03" });
|
||||
import { createBrowserRouter, Navigate, Outlet } from "react-router";
|
||||
import { LoginPage } from "./pages/LoginPage";
|
||||
import { OnboardingPage } from "./pages/OnboardingPage";
|
||||
import { HomePage } from "./pages/HomePage";
|
||||
import { CameraCapturePage } from "./pages/CameraCapturePage";
|
||||
import { DataVerificationPage } from "./pages/DataVerificationPage";
|
||||
import { AIAnalysisPage } from "./pages/AIAnalysisPage";
|
||||
import { ReportDetailPage } from "./pages/ReportDetailPage";
|
||||
import { DevNavigationPage } from "./pages/DevNavigationPage";
|
||||
import { AIChatPage } from "./pages/AIChatPage";
|
||||
import { HealthRecordsPage } from "./pages/HealthRecordsPage";
|
||||
import { GrowthCurvePage } from "./pages/GrowthCurvePage";
|
||||
import { TimelinePage } from "./pages/TimelinePage";
|
||||
import { CalendarPage } from "./pages/CalendarPage";
|
||||
import { DoctorQuestionsPage } from "./pages/DoctorQuestionsPage";
|
||||
import { SharePage } from "./pages/SharePage";
|
||||
import { ExpertConsultationPage } from "./pages/ExpertConsultationPage";
|
||||
import { ProfilePage } from "./pages/ProfilePage";
|
||||
import { VoiceRecordPage } from "./pages/VoiceRecordPage";
|
||||
import { NotificationPage } from "./pages/NotificationPage";
|
||||
import { MobileLayout } from "./components/MobileLayout";
|
||||
import { getToken, clearToken, decodeToken } from "./services/api";
|
||||
|
||||
/** 路由守卫:未登录跳转登录页 */
|
||||
function ProtectedRoute() {
|
||||
const token = getToken();
|
||||
const urlToken = new URLSearchParams(window.location.search).get('token');
|
||||
const urlOpenid = new URLSearchParams(window.location.search).get('openid');
|
||||
|
||||
// 来自小程序的 token → 让 LoginPage 处理
|
||||
if (urlToken) {
|
||||
return <Navigate to={`/?token=${encodeURIComponent(urlToken)}`} replace />;
|
||||
}
|
||||
|
||||
// URL 中有 openid 且与本地缓存的 token 中的 openid 不一致 → 清除缓存重新登录
|
||||
if (urlOpenid && token) {
|
||||
try {
|
||||
const payload = decodeToken(token);
|
||||
if (payload.openid !== urlOpenid) {
|
||||
console.log('[auth] openid 不一致: URL=' + urlOpenid + ', cache=' + payload.openid + ',清除缓存');
|
||||
clearToken();
|
||||
return <Navigate to="/" replace />;
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[auth] token 解码失败', e);
|
||||
}
|
||||
}
|
||||
|
||||
if (!token) {
|
||||
return <Navigate to="/" replace />;
|
||||
}
|
||||
return <Outlet />;
|
||||
}
|
||||
|
||||
export const router = createBrowserRouter([
|
||||
{
|
||||
// 全局 H5 移动端布局容器
|
||||
element: <MobileLayout />,
|
||||
children: [
|
||||
// 公开路由
|
||||
{
|
||||
path: "/dev",
|
||||
Component: DevNavigationPage,
|
||||
},
|
||||
{
|
||||
path: "/",
|
||||
Component: LoginPage,
|
||||
},
|
||||
{
|
||||
path: "/onboarding",
|
||||
Component: OnboardingPage,
|
||||
},
|
||||
// 需要登录的路由
|
||||
{
|
||||
element: <ProtectedRoute />,
|
||||
children: [
|
||||
{
|
||||
path: "/home",
|
||||
Component: HomePage,
|
||||
},
|
||||
{
|
||||
path: "/profile",
|
||||
Component: ProfilePage,
|
||||
},
|
||||
{
|
||||
path: "/camera",
|
||||
Component: CameraCapturePage,
|
||||
},
|
||||
{
|
||||
path: "/review",
|
||||
Component: DataVerificationPage,
|
||||
},
|
||||
{
|
||||
path: "/verify",
|
||||
Component: DataVerificationPage,
|
||||
},
|
||||
{
|
||||
path: "/analysis/:reportId",
|
||||
Component: AIAnalysisPage,
|
||||
},
|
||||
{
|
||||
path: "/report/:reportId",
|
||||
Component: ReportDetailPage,
|
||||
},
|
||||
{
|
||||
path: "/chat/:reportId",
|
||||
Component: AIChatPage,
|
||||
},
|
||||
{
|
||||
path: "/voice-record",
|
||||
Component: VoiceRecordPage,
|
||||
},
|
||||
{
|
||||
path: "/health-records",
|
||||
Component: HealthRecordsPage,
|
||||
},
|
||||
{
|
||||
path: "/growth-curve",
|
||||
Component: GrowthCurvePage,
|
||||
},
|
||||
{
|
||||
path: "/timeline",
|
||||
Component: TimelinePage,
|
||||
},
|
||||
{
|
||||
path: "/calendar",
|
||||
Component: CalendarPage,
|
||||
},
|
||||
{
|
||||
path: "/doctor-questions/:reportId",
|
||||
Component: DoctorQuestionsPage,
|
||||
},
|
||||
{
|
||||
path: "/share/:reportId",
|
||||
Component: SharePage,
|
||||
},
|
||||
{
|
||||
path: "/expert-consultation",
|
||||
Component: ExpertConsultationPage,
|
||||
},
|
||||
{
|
||||
path: "/notifications",
|
||||
Component: NotificationPage,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
], { basename: "/app_03" });
|
||||
|
||||
Reference in New Issue
Block a user