摘要:<?php //Base.php namespace app\admin\controller; use think\Controller; use Util\SysDb; class Base extends Controller{ public function __construct(){ pare
<?php
//Base.php
namespace app\admin\controller;
use think\Controller;
use Util\SysDb;
class Base extends Controller{
public function __construct(){
parent::__construct();
$this->_admin = session('admin');
if(!$this->_admin){
header('Location:/index.php/admin/account/login');
exit;
}
$this->assign('admin',$this->_admin);
$this->db = new SysDb;
// 判断用户是否有权限
$group = $this->db->table('admin_groups')->where(array('gid'=>$this->_admin['gid']))->item();
if(!$group){
$this->request_error('对不起,您没有权限');
}
$rights = json_decode($group['rights']);
// 当前访问的菜单
$controller = request()->controller();
$method = request()->action();
$res = $this->db->table('admin_menus')->where(array('controller'=>$controller,'method'=>$method))->item();
if(!$res){
$this->request_error('对不起,您访问的功能不存在');
}
if($res['status'] == 1){
$this->request_error('对不起,该功能已禁止使用');
}
if(!in_array($res['mid'],$rights)){
$this->request_error('对不起,您没有权限');
}
}
private function request_error($msg){
if(request()->isAjax()){
exit(json_encode(array('code'=>1,'msg'=>$msg)));
}
exit($msg);
}
}
//home.php
<?php
namespace app\admin\controller;
use think\Controller;
use Util\SysDb;
class Home extends Base{
public function index(){
$role = $this->db->table('admin_groups')->where(array('gid'=>$this->_admin['gid']))->item();
if($role){
$role['rights'] = $role['rights']?json_decode($role['rights'],true):[];
}
if($role['rights']){
$where = 'mid in('.implode(',', $role['rights']).') and ishidden=0 and status=0';
$menus = $this->db->table('admin_menus')->where($where)->cates('mid');
$menus && $menus = $this->gettreeitems($menus);
}
$data['menus'] = $menus;
$data['role'] = $role;
return $this->fetch('',$data);
}
public function welcome(){
return $this->fetch();
}
private function gettreeitems($items){
$tree = [];
foreach ($items as $item) {
if(isset($items[$item['pid']])){
$items[$item['pid']]['children'][] = &$items[$item['mid']];
}else{
$tree[] = &$items[$item['mid']];
}
}
return $tree;
}
}
批改老师:韦小宝批改时间:2019-01-15 13:01:59
老师总结:整体写的很不错 就是代码的注释有点少了 课后记得要反复的去理解理解哦