使用JavaScript实现权限角色页面显示?

实现功能:一个管理系统中有几大类功能模块,根据登录用户的角色来判断显示可以进行的操作。

这里我举例的是图书信息管理系统,该系统功能与用户角色关系表如下:

系统功能与用户角色关系表
系统功能与用户角色关系表

根据该关系,我们来理一下解决思路:

  1. 在主页面的左边为功能导航,根据角色来确定功能导航
  2. js先获取角色
  3. 根据角色,来控制各个功能的显示(show)与隐藏(hide)

实现代码:

  1. jsp页面代码:

 在session中存放了登陆用户信息(reader),因此在jsp页面中用${}直接来获取的登录用户角色:

<label id="role" hidden="hidden">${reader.rdAdminRoles}</label>

一个大的功能模块用<div>;大功能模块下的小操作用<ul><li>:

   <div id="readerMaster">
            <h1 class="type" ><a href="#">借书证管理</a></h1>
            <div class="content">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td><img src="img/bg.jpg" width="100%" height="5"/></td>
                  </tr>
              </table>
                <ul class="MM">
                <li><a href="showReader!showReader.action" target="main">借书证显示</a></li>
                <li><a href="goAddReader!goAddReader.action" target="main">办理图书证</a></li>
              </ul>
              </div>
          </div>

 admin

2.js代码 :

    //权限实现操作
        $(window).load(function () {
            //8-系统管理
            if ($("#role").text() === "0") { //0-读者
                $("#readerMaster").show();
                $("#bookMaster").hide();
                $("#borrowMaster").show();
                $("#userMaster").hide();
                $("#typeMaster").hide();
            }else if ($("#role").text() === "1") { //1-借书证管理员
                $("#readerMaster").show();
                $("#bookMaster").hide();
                $("#borrowMaster").hide();
                $("#userMaster").hide();
                $("#typeMaster").hide();
            }else if ($("#role").text() === "2") {  //2-图书管理员
                $("#readerMaster").hide();
                $("#bookMaster").show();
                $("#borrowMaster").hide();
                $("#userMaster").hide();
                $("#typeMaster").hide();
            }else if ($("#role").text() === "4") {   //4-借阅管理
                $("#readerMaster").hide();
                $("#bookMaster").hide();
                $("#borrowMaster").show();
                $("#userMaster").hide();
                $("#typeMaster").hide();
            }else {
                $("#readerMaster").show();
                $("#bookMaster").show();
                $("#borrowMaster").show();
                $("#userMaster").show();
                $("#typeMaster").show();
            }
        });

 

完整代码:

 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
    <meta charset="UTF-8">
    <title>图书馆信息管理系统</title>
    <style>
	*{
	font-family:"微软雅黑";
}
body {
	color: #fff;
	margin: 0px;
	padding-left:25%;
	
}
#container {
	width: 150px;
}
H1 {
	font-size: 18px;
	margin: 0px;
	width: 150px;
	cursor: pointer;
	height: 30px;
	line-height: 20px;
}
H1 a {
	display: block;
	width: 150px;
	color: #fff;
	height: 30px;
	text-decoration: none;
	moz-outline-style: none;
	background-color: #90d7d7;
	line-height: 30px;
	text-align: center;
	margin: 0px;
	padding: 0px;
}
.content {
	width: 150px;
	height: 52px;
}
.MM ul {
	list-style-type: none;
	margin: 0px;
	padding: 0px;
}
.MM li {
	font-size: 16px;
	line-height: 26px;
	color: #fff;
	list-style-type: none;
	display: block;
	text-decoration: none;
	height: 26px;
	width: 150px;
	padding-left: 0px;
}
.MM {
	width: 100%;
	margin: 0px;
	padding: 0px;
	left: 0px;
	top: 0px;
	right: 0px;
	bottom: 0px;
	clip: rect(0px, 0px, 0px, 0px);
}
.MM a:link {
	font-size: 16px;
	line-height: 26px;
	color: #fff;
	background-color: #c9ebec;
	height: 26px;
	width: 150px;
	display: block;
	text-align: center;
	margin: 0px;
	padding: 0px;
	overflow: hidden;
	text-decoration: none;
	display: block;
}
.MM a:visited {
	font-size: 16px;
	line-height: 26px;
	color: #fff;
	background-color: #90d7d7;
	display: block;
	text-align: center;
	margin: 0px;
	padding: 0px;
	height: 26px;
	width: 150px;
	text-decoration: none;
}
.MM a:active {
	font-size: 16px;
	line-height: 26px;
	color: #fff;
	background-color: #90d7d7;
	height: 26px;
	width: 150px;
	display: block;
	text-align: center;
	margin: 0px;
	padding: 0px;
	overflow: hidden;
	text-decoration: none;
}
.MM a:hover {
	font-size: 16px;
	line-height: 26px;
	font-weight: bold;
	color: #fff;
	background-color: #90d7d7;
	text-align: center;
	display: block;
	margin: 0px;
	padding: 0px;
	height: 26px;
	width: 150px;
	text-decoration: none;
}
</style>
    <script type="text/javascript" src="js/jquery-1.8.3.js"></script>
    <script type="text/javascript">
        //点击显示大功能下的小操作
	$(function(){
	$("h1").click(function(){
           $(this).next(".content").fadeToggle("fast");
        });
		});
	//权限实现操作
        $(window).load(function () {
            //8-系统管理
            if ($("#role").text() === "0") { //0-读者
                $("#readerMaster").show();
                $("#bookMaster").hide();
                $("#borrowMaster").show();
                $("#userMaster").hide();
                $("#typeMaster").hide();
            }else if ($("#role").text() === "1") { //1-借书证管理员
                $("#readerMaster").show();
                $("#bookMaster").hide();
                $("#borrowMaster").hide();
                $("#userMaster").hide();
                $("#typeMaster").hide();
            }else if ($("#role").text() === "2") {  //2-图书管理员
                $("#readerMaster").hide();
                $("#bookMaster").show();
                $("#borrowMaster").hide();
                $("#userMaster").hide();
                $("#typeMaster").hide();
            }else if ($("#role").text() === "4") {   //4-借阅管理
                $("#readerMaster").hide();
                $("#bookMaster").hide();
                $("#borrowMaster").show();
                $("#userMaster").hide();
                $("#typeMaster").hide();
            }else {
                $("#readerMaster").show();
                $("#bookMaster").show();
                $("#borrowMaster").show();
                $("#userMaster").show();
                $("#typeMaster").show();
            }
        });
    </script>
    </head>
    <body>
    <table width="150" height="600" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="150" colspan="3"><label id="role" hidden="hidden">${reader.rdAdminRoles}</label></td>
      </tr>
      <tr>
      <tr>
        <td width="150" valign="top"><div id="container">
            <div id="readerMaster">
            <h1 class="type" ><a href="#">借书证管理</a></h1>
            <div class="content">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td><img src="img/bg.jpg" width="100%" height="5"/></td>
                  </tr>
              </table>
                <ul class="MM">
                <li><a href="showReader!showReader.action" target="main">借书证显示</a></li>
                <li><a href="goAddReader!goAddReader.action" target="main">办理图书证</a></li>
              </ul>
              </div>
          </div>
            <div id="bookMaster">
            <h1 class="type"><a href="#">图书管理</a></h1>
            <div class="content">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td><img src="img/bg.jpg" width="100%" height="5"/></td>
                  </tr>
              </table>
                <ul class="MM">
                <li><a href="showBook!showBook.action" target="main">图书显示</a></li>
                <li><a href="goAddBook!goAddBook.action" target="main">新书入库</a></li>
              </ul>
              </div>
          </div>
            <div id="borrowMaster">
            <h1 class="type" ><a href="#">借阅管理</a></h1>
            <div class="content">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td><img src="img/bg.jpg" width="100%" height="5"/></td>
                  </tr>
              </table>
                <ul class="MM">
                <li><a href="showBorrow!showBorrow.action" target="main">借阅信息显示</a></li>
                <li><a href="goAddBorrow!goAddBorrow.action" target="main">借阅书籍</a></li>
              </ul>
              </div>
          </div>          
            <div id="typeMaster">
            <h1 class="type" ><a href="#">读者类别管理</a></h1>
            <div class="content">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td><img src="img/bg.jpg" width="100%" height="5"/></td>
                  </tr>
              </table>
                <ul class="MM">
                <li><a href="showReaderType!showReaderType.action" target="main">读者类别显示</a></li>
                <li><a href="goAddReaderType!goAddReaderType.action" target="main">读者类别添加</a></li>
              </ul>
              </div>
          </div>
            <div id="userMaster">
            <h1 class="type" ><a href="#">用户管理</a></h1>
            <div class="content">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td><img src="img/bg.jpg" width="100%" height="5"/></td>
                  </tr>
              </table>
                <ul class="MM">
                <li><a href="showUser!showUser.action" target="main">用户信息显示</a></li>
              </ul>
              </div>
          </div>
          </div></td>
      </tr>
    </table>
</body>
</html>
 

 

 

 

 

 

  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在React中实现权限角色路由可以通过以下步骤来完成: 1. 首先,你需要定义一个权限配置文件,该文件包含了每个路由对应的权限信息。例如,你可以创建一个名为`routes.js`的文件,并在其中定义路由和权限的映射关系。 2. 在你的应用程序的根组件中,你需要使用React Router来设置路由。你可以使用`<Route>`组件来定义每个路由,并使用`<Switch>`组件来确保只有一个路由被渲染。 3. 在每个路由组件中,你可以使用`hasPermission`函数来检查当前用户是否有权限访问该路由。如果用户有权限,你可以渲染对应的组件;如果用户没有权限,你可以渲染一个提示组件。 下面是一个示例代码,演示了如何在React中实现权限角色路由: ```javascript // routes.js const routes = [ { path: '/dashboard', component: Dashboard, auth: ['admin', 'user'] // 只有admin和user角色可以访问 }, { path: '/profile', component: Profile, auth: ['admin', 'user'] // 只有admin和user角色可以访问 }, { path: '/settings', component: Settings, auth: ['admin'] // 只有admin角色可以访问 }, { path: '/login', component: Login, auth: [] // 不需要权限就能访问 } ]; // App.js import { Route, Switch, Redirect } from 'react-router-dom'; const hasPermission = (auth, userRoles) => { if (auth.length === 0) { return true; // 不需要权限就能访问 } return userRoles.some(role => auth.includes(role)); // 检查用户角色是否包含在权限列表中 }; const App = () => { const userRoles = ['admin']; // 当前用户的角色 return ( <Switch> {routes.map(route => ( <Route key={route.path} path={route.path} render={() => hasPermission(route.auth, userRoles) ? ( <route.component /> ) : ( <UnauthorizedComponent /> ) } /> ))} <Redirect to="/login" /> // 默认重定向到登录页面 </Switch> ); }; ``` 这样,当用户访问某个路由时,系统会根据用户的角色和路由的权限配置来判断是否有权限访问。如果有权限,会渲染对应的组件;如果没有权限,会渲染一个未授权的提示组件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值