//在登陆页面保存用户的权限信息// 权限的设置,查询权限表, 让权限表的id==用户表的id$authority = Db::table('tp_authority')->where([ 'id'=>$res['auth_id']])->find();//将数据保存到session,并转我数组session('authority', explode(',',$authority['check_name'])); //使用构造函数public function __construct() { // 保留父类的构造函数 parent::__construct(); session_start(); define('C',Request::instance()->controller()); define('A',Request::instance()->action()); // 权限验证 $this->check_authority(); }// 权限验证 public function check_authority(){ // 判断有没有权限 if(A != 'login' && Session('authority')){ // 查询菜单表的数据 $menu = Db::table('tp_menus')->where([ 'module'=>'admin' ])->select(); // 准备空数组保存数据,但要给默认数据 $arr = ['index/index','login/login','login/loginOut','user/ajax','user/checkname','order/ajax']; foreach($menu as $k => $v){ // 判断用户权限数据是否在菜单表中 if(in_array($v['id'],Session('authority'))){ // 用户的所有权限 $arr[] = $v['controller'].'/'.$v['action']; } } // dump(C.'/'.A);die; // 当用户知道地址的时候,给与判断 if(!in_array(strtolower(C).'/'.A,$arr)){ // echo '<script>alert("权限不足")</script>'; echo '<script>alert("权限不足");window.location.href="'.url('admin/login/loginOut').'"</script>'; } } }