umi6.x + react + antd的项目增加403(无权限页面拦截),404,错误处理页面

server/2024/10/18 14:21:14/
  1. 首先在src/pages下创建403,404,ErrorBoundary
403
import { Button, Result } from 'antd';
import { history } from '@umijs/max';const UnAccessible = () => (<Resultstatus="403"title="403"subTitle="抱歉,您无权限访问当前页面"extra={<Button type="primary" onClick={()=>{history.push('/')}}>返回主页</Button>}/>
);
export default UnAccessible;
404
import { Button, Result } from 'antd';
import { history } from '@umijs/max';const NotFound = () => (<Resultstatus="404"title="404"subTitle="抱歉,无法找到你需要的页面"extra={<Button type="primary" onClick={()=>{history.push('/')}}>返回主页</Button>}/>
);
export default NotFound;
ErrorBoundary(错误边界)
import { Result, Button, Tooltip, Typography } from 'antd';
import React from 'react';
// eslint-disable-next-line @typescript-eslint/ban-types
export default class ErrorBoundary extends React.Component {state = { hasError: false, errorInfo: '' };static getDerivedStateFromError(error) {return { hasError: true, errorInfo: error.message };}componentDidCatch(error, errorInfo) {// You can also log the error to an error reporting service// eslint-disable-next-line no-consoleconsole.log(error, errorInfo);}render() {if (this.state.hasError) {// You can render any custom fallback UIreturn (<Resultstatus="500"title={<b style={{fontSize:14}}>抱歉,服务发生错误!请刷新页面</b>}subTitle={<Tooltip title={this.state.errorInfo}><Typography.Paragraph copyable={{text:this.state.errorInfo}}>错误信息</Typography.Paragraph></Tooltip>}extra={<Buttontype="primary"onClick={() => {window.location.reload();}}>刷新页面</Button>}/>);}return this.props.children;}
}
  1. 在app.js配置
    在这里插入图片描述
  2. 对于没有权限的页面,在浏览器输入地址,前端拦截,需要access.js方法拦截,src/access.js
    4中routes.js中配置的access就是作为key(accessObj获取的key要和routes.js中配置的access一致)
/* eslint-disable array-callback-return */
import allData from '../config/routes';//获取权限 key 根据 path 生成
function convertToAccessArray(data) {let accessArray = [];data.map(obj => {if (obj.path && obj.path !== '/' && obj.path !== '/home') {// 去掉路径中的斜杠,并且将首字母大写const access = obj.path.replace(/\//g, '').charAt(0).toUpperCase() + obj.path.replace(/\//g, '').slice(1);accessArray.push(access);}if (obj.routes) {const childAccessArray = convertToAccessArray(obj.routes);accessArray = accessArray.concat(childAccessArray);}});return accessArray;
}export default (initialState) => {const {menuData} = initialState;//后端返回的页面(路由)const AccessList = convertToAccessArray(menuData?.routes??[]);//全部页面const AllList = convertToAccessArray(allData?.routes);//结果对象 accessKey 对应配置的 routes 里面的 access// {//   accessKey: true or false// }const accessObj = {};//添加权限AllList?.map(it=>{accessObj[it] = AccessList.includes(it);})return accessObj;
};

//后端返回的页面(路由)格式类似:

[{"name": "首页","key": "2024042410100000000","path": "/home","icon": null,"routes": []},{"name": "IoT管理","key": "2024042410200000000","path": "/iot","icon": null,"routes": [{"name": "设备管理","key": "2024042410200200000","path": "/iot/device","icon": "icon-shebeiguanli","routes": [{"name": "设备台账","key": "2024042410200201000","path": "/iot/device/account","icon": null,"routes": []}]}]}
]
  1. 如果要对没有权限的页面进行拦截,还需要在routes.js配置access
{path: '/',routes: [{path: '/',redirect: '/home',},{name: '首页',path: '/home',component: './Home',},{name: 'IoT管理',path: '/iot',access: 'Iot',routes: [{name: '设备清单',path: '/iot/devicelist',icon: 'icon-zhiduguifan',component: './Iot/DeviceList',access: 'Iotdevicelist',}],},{path:'/*',name: '404',component: './404',hideInMenu: true,},],
}

http://www.ppmy.cn/server/39406.html

相关文章

安装vmware station记录

想学一下linux,花了3个多小时&#xff0c;才配置好了&#xff0c;记录一下 安装vm12,已配置linux系统 报错&#xff0c;VMware Workstation 与 Device/Credential Guard 不兼容解决方案&#xff0c;网上说有不成功的&#xff0c;电脑蓝屏&#xff0c;选择装vm16试试 vm16 在…

《构建高效审批系统:架构设计与实践》

在现代企业管理中&#xff0c;审批系统扮演着至关重要的角色&#xff0c;它不仅能够规范业务流程&#xff0c;提高工作效率&#xff0c;还能够增强企业的管理控制力和信息化水平。本文将探讨如何设计和构建一套高效的审批系统架构&#xff0c;以满足企业日常审批需求&#xff0…

算法--动态规划

动态规划&#xff08;Dynamic Programming, DP&#xff09;是一种算法设计技巧&#xff0c;用于解决具有重叠子问题和最优子结构性质的问题。通过将原问题分解为相对简单的子问题的方式来求解复杂问题&#xff0c;动态规划避免了计算重复子问题&#xff0c;从而提高了算法的效率…

Python 全栈体系【四阶】(四十一)

第五章 深度学习 九、图像分割 1. 基本介绍 1.1 什么是图像分割 图像分割&#xff08;Segmentation&#xff09;是图像处理和机器视觉一个重要分支&#xff0c;其目标是精确理解图像场景与内容。图像分割是在像素级别上的分类&#xff0c;属于同一类的像素都要被归为一类&a…

《CKA/CKAD应试指南/从docker到kubernetes 完全攻略》学习笔记 第13章 网络管理

目录 前言 13.1 实验准备 13.2 创建ingress类型的网络策略 13.2.1 允许特定标签的pod能访问 <

Go语言中context原理及使用

Golang中&#xff0c;context为我们提供了在跨API边界和进程之间传递请求作用域的deadline&#xff0c;取消信号&#xff0c;和其他请求响应的值的能力。 context包定义了Context类型&#xff0c;它在API边界和进程之间提供了一种传递传递请求作用域的deadline&#xff0c;取消…

centos安装mysql-client

直接安装&#xff1a; yum install mysql-community-client报了错误No package mysql-community-client available. 原因&#xff1a;CentOS/RHEL系统默认的软件源中并不包含MySQL软件包&#xff0c;需要通过添加第三方存储库来获取MySQL相关软件 添加源 安装MySQL官方的Yum…

echarts tooltip过长被截断的解决方式

目录 溢出盒子显示弹框不溢出盒子显示弹框 溢出盒子显示弹框 tooltip: {appendToBody: true, // 设置tooltip是否跟随鼠标移动position: function (point: any, params: any, dom: any, rect: any, size: any) {// 固定在顶部return [point[0], 10%];},formatter: function (p…