优化数据隔离,进行权限控制

This commit is contained in:
2025-06-03 15:17:09 +08:00
parent 15ef4a3ced
commit 057563ba5e
10 changed files with 244 additions and 94 deletions
+16 -9
View File
@@ -1,5 +1,5 @@
import { useState, useEffect } from 'react';
import { useNavigate, Form } from '@remix-run/react';
import { useNavigate, Form, useLoaderData } from '@remix-run/react';
import { type MetaFunction, type ActionFunctionArgs, LoaderFunctionArgs, redirect } from "@remix-run/node";
import styles from "~/styles/pages/home.css?url";
import dayjs from 'dayjs';
@@ -30,21 +30,27 @@ export async function action({ request }: ActionFunctionArgs) {
// 验证用户登录状态
export async function loader({ request }: LoaderFunctionArgs) {
const { isAuthenticated } = await getUserSession(request);
const { isAuthenticated, userRole } = await getUserSession(request);
if (!isAuthenticated) {
return redirect("/login");
}
return null;
return Response.json({ userRole });
}
export default function Index() {
const navigate = useNavigate();
const { userRole } = useLoaderData<typeof loader>();
const [currentDateTime, setCurrentDateTime] = useState({
date: '',
time: ''
});
// 打印服务器端传递的用户角色
useEffect(() => {
console.log('_index 服务器返回的用户角色:', userRole);
}, [userRole]);
// 更新日期时间
useEffect(() => {
const updateDateTime = () => {
@@ -83,10 +89,8 @@ export default function Index() {
// 处理登出
const handleLogout = () => {
// 清除sessionStorage中的用户角色信息
// 清除sessionStorage中的所有数据
if (typeof window !== 'undefined') {
sessionStorage.removeItem('userRole');
// 可以根据需要清除其他会话数据
sessionStorage.clear();
}
@@ -94,6 +98,9 @@ export default function Index() {
const form = document.getElementById('logout-form') as HTMLFormElement;
if (form) {
form.submit();
} else {
// 如果找不到表单,直接导航到登录页
navigate('/login');
}
};
@@ -117,7 +124,7 @@ export default function Index() {
<span className="datetime">{currentDateTime.date} {currentDateTime.time}</span>
<div className="user">
<img src="/avatar.png" alt="用户头像" className="avatar" />
<span className="username"></span>
<span className="username">{userRole === 'developer' ? '系统管理员' : '普通用户'}</span>
<button
onClick={handleLogout}
className="logout-button"
@@ -163,8 +170,8 @@ export default function Index() {
{/* 智慧法务大模型模块 */}
<div
className="module-card"
onClick={() => handleModuleClick('/prompts', 'model')}
onKeyDown={(e) => handleKeyDown('/prompts', 'model', e)}
onClick={() => handleModuleClick('/', 'model')}
onKeyDown={(e) => handleKeyDown('/', 'model', e)}
role="button"
tabIndex={0}
aria-label="智慧法务大模型"