删除所有console.log输出,优化评查结果的表格的显示,添加新的页码获取逻辑
This commit is contained in:
+128
-134
@@ -9,14 +9,14 @@ import {
|
||||
isRouteErrorResponse,
|
||||
useRouteError,
|
||||
type MetaFunction,
|
||||
// useLoaderData
|
||||
useLoaderData
|
||||
} from "@remix-run/react";
|
||||
// import {
|
||||
// LoaderFunctionArgs,
|
||||
// redirect,
|
||||
// createCookieSessionStorage,
|
||||
// ActionFunctionArgs
|
||||
// } from "@remix-run/node";
|
||||
import {
|
||||
LoaderFunctionArgs,
|
||||
redirect,
|
||||
createCookieSessionStorage,
|
||||
ActionFunctionArgs
|
||||
} from "@remix-run/node";
|
||||
import { Layout } from "~/components/layout/Layout";
|
||||
import { ErrorBoundary as AppErrorBoundary } from "~/components/error/ErrorBoundary";
|
||||
import { MessageModalProvider } from "~/components/ui/MessageModal";
|
||||
@@ -30,115 +30,127 @@ import LoadingBarContainer from "~/components/ui/LoadingBar";
|
||||
import RouteChangeLoader from "~/components/ui/RouteChangeLoader";
|
||||
// import { useState, useEffect } from "react";
|
||||
|
||||
// 定义用户角色类型
|
||||
export type UserRole = 'common' | 'developer';
|
||||
|
||||
// 定义需要高级权限的路径
|
||||
export const developerOnlyPaths = [
|
||||
'/settings',
|
||||
'/config-lists',
|
||||
'/document-types',
|
||||
'/prompts'
|
||||
];
|
||||
|
||||
// 创建基于Cookie的会话存储
|
||||
// 在实际应用中,应该使用环境变量来设置密钥
|
||||
// const sessionStorage = createCookieSessionStorage({
|
||||
// cookie: {
|
||||
// name: "__session",
|
||||
// httpOnly: true,
|
||||
// path: "/",
|
||||
// sameSite: "lax",
|
||||
// secrets: ["s3cr3t"], // 应该从环境变量读取
|
||||
// secure: process.env.NODE_ENV === "production",
|
||||
// },
|
||||
// });
|
||||
const sessionStorage = createCookieSessionStorage({
|
||||
cookie: {
|
||||
name: "__session",
|
||||
httpOnly: true,
|
||||
path: "/",
|
||||
sameSite: "lax",
|
||||
secrets: ["s3cr3t"], // 应该从环境变量读取
|
||||
secure: process.env.NODE_ENV === "production",
|
||||
},
|
||||
});
|
||||
|
||||
// // 获取会话对象
|
||||
// export async function getSession(request: Request) {
|
||||
// const cookie = request.headers.get("Cookie");
|
||||
// return sessionStorage.getSession(cookie);
|
||||
// }
|
||||
// 获取会话对象
|
||||
export async function getSession(request: Request) {
|
||||
const cookie = request.headers.get("Cookie");
|
||||
return sessionStorage.getSession(cookie);
|
||||
}
|
||||
|
||||
// // 获取用户登录状态
|
||||
// export async function getUserSession(request: Request) {
|
||||
// const session = await getSession(request);
|
||||
// return {
|
||||
// isAuthenticated: session.get("isAuthenticated") === true,
|
||||
// };
|
||||
// }
|
||||
// 获取用户登录状态
|
||||
export async function getUserSession(request: Request) {
|
||||
const session = await getSession(request);
|
||||
return {
|
||||
isAuthenticated: session.get("isAuthenticated") === true,
|
||||
userRole: session.get("userRole") || 'common' as UserRole
|
||||
};
|
||||
}
|
||||
|
||||
// // 创建登录会话
|
||||
// export async function createUserSession(isAuthenticated: boolean, redirectTo: string) {
|
||||
// const session = await sessionStorage.getSession();
|
||||
// session.set("isAuthenticated", isAuthenticated);
|
||||
// 创建登录会话
|
||||
export async function createUserSession(isAuthenticated: boolean, userRole: UserRole, redirectTo: string) {
|
||||
const session = await sessionStorage.getSession();
|
||||
session.set("isAuthenticated", isAuthenticated);
|
||||
session.set("userRole", userRole);
|
||||
|
||||
// return redirect(redirectTo, {
|
||||
// headers: {
|
||||
// "Set-Cookie": await sessionStorage.commitSession(session),
|
||||
// },
|
||||
// });
|
||||
// }
|
||||
return redirect(redirectTo, {
|
||||
headers: {
|
||||
"Set-Cookie": await sessionStorage.commitSession(session),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// // 销毁会话(登出)
|
||||
// export async function logout(request: Request) {
|
||||
// const session = await getSession(request);
|
||||
// 销毁会话(登出)
|
||||
export async function logout(request: Request) {
|
||||
const session = await getSession(request);
|
||||
|
||||
// return redirect("/login", {
|
||||
// headers: {
|
||||
// "Set-Cookie": await sessionStorage.destroySession(session),
|
||||
// },
|
||||
// });
|
||||
// }
|
||||
return redirect("/login", {
|
||||
headers: {
|
||||
"Set-Cookie": await sessionStorage.destroySession(session),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// // 添加action处理登录/登出请求
|
||||
// export async function action({ request }: ActionFunctionArgs) {
|
||||
// const formData = await request.formData();
|
||||
// const intent = formData.get("intent");
|
||||
// 添加action处理登录/登出请求
|
||||
export async function action({ request }: ActionFunctionArgs) {
|
||||
const formData = await request.formData();
|
||||
const intent = formData.get("intent");
|
||||
|
||||
// if (intent === "logout") {
|
||||
// return logout(request);
|
||||
// }
|
||||
if (intent === "logout") {
|
||||
return logout(request);
|
||||
}
|
||||
|
||||
// return null;
|
||||
// }
|
||||
return null;
|
||||
}
|
||||
|
||||
// // 添加loader函数进行全局认证检查
|
||||
// export async function loader({ request }: LoaderFunctionArgs) {
|
||||
// // 获取当前路径
|
||||
// const url = new URL(request.url);
|
||||
// const pathname = url.pathname;
|
||||
// 添加loader函数进行全局认证检查
|
||||
export async function loader({ request }: LoaderFunctionArgs) {
|
||||
// 获取当前路径
|
||||
const url = new URL(request.url);
|
||||
const pathname = url.pathname;
|
||||
|
||||
// // 排除不需要登录验证的路径
|
||||
// const publicPaths = ['/login', '/favicon.ico'];
|
||||
// const isPublicPath = publicPaths.some(path => pathname.startsWith(path));
|
||||
// 排除不需要登录验证的路径
|
||||
const publicPaths = ['/login', '/favicon.ico'];
|
||||
const isPublicPath = publicPaths.some(path => pathname.startsWith(path));
|
||||
|
||||
// // 获取用户会话
|
||||
// const { isAuthenticated } = await getUserSession(request);
|
||||
// 获取用户会话
|
||||
const { isAuthenticated, userRole } = await getUserSession(request);
|
||||
// console.log("Auth status:", { isAuthenticated, userRole, pathname });
|
||||
|
||||
// // 如果访问需要认证的路径但未登录,重定向到登录页
|
||||
// if (!isPublicPath && !isAuthenticated) {
|
||||
// // 保存请求的URL,以便登录后重定向回来
|
||||
// const session = await getSession(request);
|
||||
// session.set("redirectTo", pathname);
|
||||
// 如果访问需要认证的路径但未登录,重定向到登录页
|
||||
if (!isPublicPath && !isAuthenticated) {
|
||||
// 保存请求的URL,以便登录后重定向回来
|
||||
const session = await getSession(request);
|
||||
|
||||
// return redirect("/login", {
|
||||
// headers: {
|
||||
// "Set-Cookie": await sessionStorage.commitSession(session),
|
||||
// },
|
||||
// });
|
||||
// }
|
||||
// 如果路径是/home,则将重定向目标设置为/
|
||||
const redirectTarget = pathname === "/home" ? "/" : pathname;
|
||||
// 保存重定向目标
|
||||
session.set("redirectTo", redirectTarget);
|
||||
|
||||
return redirect("/login", {
|
||||
headers: {
|
||||
"Set-Cookie": await sessionStorage.commitSession(session),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// // 如果已登录且访问登录页,重定向到首页
|
||||
// if (pathname === "/login" && isAuthenticated) {
|
||||
// return redirect("/home");
|
||||
// }
|
||||
// 如果已登录且访问登录页,重定向到首页
|
||||
if (pathname === "/login" && isAuthenticated) {
|
||||
// console.log("Already authenticated, redirecting from login to /");
|
||||
return redirect("/");
|
||||
}
|
||||
|
||||
// // 向组件传递认证状态和当前路径
|
||||
// return Response.json({ isAuthenticated, pathname });
|
||||
// }
|
||||
// 检查访问权限 - 如果是common用户访问了开发者专属页面,重定向到首页
|
||||
if (userRole === 'common' && developerOnlyPaths.some(path => pathname.startsWith(path))) {
|
||||
return redirect("/");
|
||||
}
|
||||
|
||||
// 向组件传递认证状态和当前路径
|
||||
return Response.json({ isAuthenticated, userRole, pathname });
|
||||
}
|
||||
|
||||
// 添加客户端hydration错误处理
|
||||
// if (typeof window !== "undefined") {
|
||||
// window.addEventListener("error", (event) => {
|
||||
// if (event.message && event.message.includes("Hydration failed")) {
|
||||
// console.warn("Hydration error detected, refreshing page...");
|
||||
// setTimeout(() => {
|
||||
// window.location.reload();
|
||||
// }, 100);
|
||||
// event.preventDefault();
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
export const meta: MetaFunction = () => {
|
||||
return [
|
||||
@@ -163,11 +175,8 @@ export function links() {
|
||||
}
|
||||
|
||||
export default function App() {
|
||||
// const { pathname } = useLoaderData<typeof loader>();
|
||||
const { userRole } = useLoaderData<typeof loader>();
|
||||
|
||||
// // 确定哪些路径不需要Layout
|
||||
// const noLayoutPaths = ['/login', '/home'];
|
||||
// const needsLayout = !noLayoutPaths.includes(pathname);
|
||||
|
||||
return (
|
||||
<html lang="zh-CN">
|
||||
@@ -193,18 +202,9 @@ export default function App() {
|
||||
<body className="font-sans">
|
||||
<MessageModalProvider>
|
||||
<ToastProvider>
|
||||
{/* {needsLayout ? (
|
||||
<Layout>
|
||||
<Outlet />
|
||||
</Layout>
|
||||
) : (
|
||||
<Layout userRole={userRole}>
|
||||
<Outlet />
|
||||
)} */}
|
||||
|
||||
<Layout>
|
||||
<Outlet />
|
||||
</Layout>
|
||||
|
||||
</Layout>
|
||||
<RouteChangeLoader />
|
||||
</ToastProvider>
|
||||
</MessageModalProvider>
|
||||
@@ -219,38 +219,32 @@ export default function App() {
|
||||
export function ErrorBoundary() {
|
||||
const error = useRouteError();
|
||||
|
||||
// 为错误页面设置标题和描述
|
||||
let title = "发生错误";
|
||||
let message = "发生了一个未知错误,请稍后重试";
|
||||
|
||||
if (isRouteErrorResponse(error)) {
|
||||
return (
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<Meta />
|
||||
<Links />
|
||||
<title>错误 {error.status}</title>
|
||||
</head>
|
||||
<body>
|
||||
<AppErrorBoundary
|
||||
status={error.status}
|
||||
statusText={error.statusText}
|
||||
message={error.data?.message || "发生了一个错误,请稍后重试"}
|
||||
/>
|
||||
<Scripts />
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
title = `错误 ${error.status}`;
|
||||
message = error.data?.message || "发生了一个错误,请稍后重试";
|
||||
} else {
|
||||
title = "意外错误";
|
||||
message = "服务器发生了意外错误,请稍后重试";
|
||||
}
|
||||
|
||||
return (
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<Meta />
|
||||
<meta charSet="utf-8" />
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
||||
<meta name="description" content={message} />
|
||||
<title>{title}</title>
|
||||
<Links />
|
||||
<title>意外错误</title>
|
||||
</head>
|
||||
<body>
|
||||
<AppErrorBoundary
|
||||
status={500}
|
||||
statusText="服务器错误"
|
||||
message="服务器发生了意外错误,请稍后重试"
|
||||
status={isRouteErrorResponse(error) ? error.status : 500}
|
||||
statusText={isRouteErrorResponse(error) ? error.statusText : "服务器错误"}
|
||||
message={message}
|
||||
/>
|
||||
<Scripts />
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user