docs: add comments to the src/hooks directory

This commit is contained in:
zhaoying
2026-02-02 16:25:17 +08:00
parent 940c594066
commit 6194222289
3 changed files with 130 additions and 58 deletions

View File

@@ -1,27 +1,44 @@
/*
* @Author: ZhaoYing
* @Date: 2026-02-02 16:24:54
* @Last Modified by: ZhaoYing
* @Last Modified time: 2026-02-02 16:24:54
*/
/**
* useRouteGuard Hook
*
* Provides route authentication and permission checking:
* - Validates user authentication status
* - Checks route permissions against menu structure
* - Redirects unauthorized users
* - Monitors route changes
*
* @hook
*/
import { useEffect } from 'react';
import { useNavigate, useLocation } from 'react-router-dom';
import { useMenu, type MenuItem } from '@/store/menu'
// 模拟认证状态检查函数
/** Check authentication status */
export const checkAuthStatus = (): boolean => {
// 在实际应用中,这里应该检查localStoragecookie中的认证信息
// 这里为了演示,我们假设首页不需要认证,其他页面需要认证
return true; // 暂时返回true以便测试
/** In production, check localStorage or cookie for auth info */
return true; /** Temporarily return true for testing */
};
// 递归检查路由是否存在于菜单数据中
/** Recursively check if route exists in menu data */
export const checkRoutePermission = (menus: MenuItem[], currentPath: string): boolean => {
// 首页和知识库相关页面默认有权限
/** Home and knowledge base pages have default permission */
if (currentPath === '/' || currentPath.includes('knowledge-detail') || currentPath.includes('knowledge-base')) {
return true;
}
for (const menu of menus) {
// 检查当前菜单的path是否匹配
/** Check if current menu path matches */
if (menu.path && currentPath.includes(menu.path)) {
return true;
}
// 递归检查子菜单
/** Recursively check submenus */
if (menu.subs && menu.subs.length > 0) {
if (checkRoutePermission(menu.subs, currentPath)) {
return true;
@@ -32,40 +49,44 @@ export const checkRoutePermission = (menus: MenuItem[], currentPath: string): bo
return false;
};
// 路由守卫Hook用于处理路由权限检查
/**
* Route guard hook for handling route permission checks.
*
* @param source - Menu source type ('space' or 'manage')
*/
export const useRouteGuard = (source: 'space' | 'manage') => {
const navigate = useNavigate();
const location = useLocation();
const { allMenus } = useMenu();
const menus = allMenus[source];
// 确保在路由变化时重新执行所有检查逻辑
/** Re-execute all checks on route changes */
useEffect(() => {
// 模拟认证检查逻辑
/** Simulate authentication check */
const isAuthenticated = checkAuthStatus();
if (!isAuthenticated && location.pathname !== '/') {
// TODO: 未认证用户重定向到登录页(这里是首页)
/** Redirect unauthenticated users to home/login page */
navigate('/', { replace: true });
return;
}
// 认证通过后,检查路由权限
/** After authentication, check route permissions */
if (isAuthenticated && location.pathname !== '/' && location.pathname !== '/not-found') {
const hasPermission = checkRoutePermission(menus, location.pathname);
if (!hasPermission) {
// 无权限访问该路由,重定向到无权限页面
/** No permission, redirect to no-permission page */
// navigate('/no-permission', { replace: true });
}
}
}, [navigate, location.pathname, location.search, location.hash, menus]);
// 返回当前路径和权限状态,确保组件能感知到路由变化
/** Return current path and permission status */
return {
currentPath: location.pathname,
search: location.search,
hash: location.hash,
isChecking: false, // 可以扩展添加加载状态
isChecking: false, /** Can be extended to add loading state */
};
};