4.功能三:实现URL地址栏控制(15分) (1)获取到当前访问的控制器和方法(5分) (2)对当前访问的控制器和方法进行判断,有权限继续访问(5分) (3)无权限给出提示(5分)

<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Base extends Controller
{
    //
    public function __construct(Request $request = null)
    {
        //判断用户是否登录
        if (!session('?admin_id')){
            $this->error('请先登录','/admin/login/login');
        }
        //判断是否拥有权限
        //先获取当前访问的控制器和方法
        $controller = \request()->controller();
        $action = \request()->action();
        //当前访问的路径
        $currentPath = strtolower($controller).'/'.strtolower($action);

        $nodePath = ['index/index','index/welcome'];
        //查询当前的权限
        $userNode = session('userNode');
        foreach ($userNode as $k=>$v){
            foreach ($v['child'] as $item){
                $nodePath[] = $item['controller'].'/'.$item['action'];
            }
        }
        //判断是否有权限访问
        if (!in_array($currentPath,$nodePath)){
            $this->error('暂无权限访问','/admin/index/index');
        }
        parent::__construct($request);
    }
}

 

posted @ 2021-09-23 19:24  王越666  阅读(29)  评论(0编辑  收藏  举报