<?phpnamespace App\Http\Middleware;use Closure;use Illuminate\Support\Facades\Auth;use Illuminate\Support\Facades\DB;class RightValidate{public function handle($request, Closure $next){//通过$request获取当前访问的菜单的id$route = $request->route()->getActionName();$res = explode('@',$route);$action = $res[1];$result = explode('\\',$res[0]);$controller = $result[count($result)-1];$curmenu = DB::table('xpcms_admin_menu')->where(['controller'=>$controller,'action'=>$action])->item();if (!$curmenu){return response('<script>alert("当前菜单不存在")</script>',200);}$_admin = $request->session()->get('admin');$group_id = $_admin['group_id'];//此处若使用自$group = DB::table('xpcms_admin_group')->where('gid',$group_id)->first();if (!$group){return response('<script>alert("该角色不存在");window.location.href="javascript:history.go(-1)";</script>',200);}$rights = $group->rights;$rights = json_decode($rights);//echo '<pre>';//print_r($group);print_r($curmenu['mid']);var_dump($rights);if (!$rights){return response('<script>alert("您所在的权限组无权限");window.location.href="javascript:history.go(-1)";</script>',200);}if (!in_array($curmenu['mid'],$rights)){return response('<script>alert("请确认您是否有此权限");window.location.href="javascript:history.go(-1)";</script>',200);}return $next($request);}}
在手动添加菜单id时,不小心多了一个逗号,所以在使用json_decode()时,值为Null,所以那个组就等于是没有任何权限,找坑的时候也是费了很多精力。
这样在页面上显示一个响应信息,显得很不协调。
尝试使用return response(‘<script>alert(“您所在的权限组无权限”);</script>‘,200);
这样会有一个弹窗出现,感觉舒服些;
这个空白页面怎么解决呢,不管请求的是什么权限,在请求这个权限之前的那个页面,一定是它拥有的权限,所以,点击确定后,返回前一个页面,是较为合理的操作;具体实现如下:
return response(‘<script>alert(“您所在的权限组无权限”);window.location.href=”javascript:history.go(-1)”;</script>‘,200);
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号