abstract:<?php namespace app\admins\controller; use think\Controller; use Util\SysDb; class Home extends Base{ public function index(){ &nbs
<?php namespace app\admins\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; } }
<!DOCTYPE html> <html> <head> <title>欢迎</title> <link rel="stylesheet" type="text/css" href="/static/plugins/layui/css/layui.css"> <script type="text/javascript" src="/static/plugins/layui/layui.js"></script> <style type="text/css"> body{margin: 0px;} .header{width: 100%;height: 50px;line-height: 50px;background: #01AAED;color:#ffffff;} .title{margin-left: 20px;font-size: 20px;} .userinfo{float: right;margin-right: 10px;} .userinfo a{text-decoration: none;color: #ffffff;} .menu{width:200px;background: #333744;position: absolute;} .main{position: absolute;left: 200px;right: 0px;} .layui-collapse{border: none;} .layui-colla-item{border-top: none;} .layui-colla-title{background: #42485b;color: #ffffff;} .layui-colla-content{border-top: none;padding: 0px;} </style> </head> <body> <!--header--> <div class="header"> <span class="title">后台管理系统</span> <span class="userinfo">{$admin.username}【{$role.title}】<a href="javascript:;" onclick="logout()">退出</a></span> </div> <!--menu--> <div class="menu" id="menu"> <div class="layui-collapse" lay-accordion> {volist name="$menus" id="vo"} <div class="layui-colla-item"> <h2 class="layui-colla-title">{$vo.title}</h2> <div class="layui-colla-content"> <?php if(isset($vo['children']) && $vo['children']){?> <ul class="layui-nav layui-nav-tree"> {volist name="vo.children" id="cvo"} <li class="layui-nav-item"><a href="javascript:;" onclick="menufire(this)" src="/index.php/admins/{$cvo.controller}/{$cvo.method}">{$cvo.title}</a></li> {/volist} </ul> <?php }?> </div> </div> {/volist} </div> </div> <!--主操作区--> <div class="main"> <iframe src="/index.php/admins/home/welcome" onload="resetMainHeight(this)" style="width: 100%;height: 100%;" frameborder="0" scrolling="0"></iframe> </div> </body> </html> <script type="text/javascript"> layui.use(['element','layer'], function(){ var element = layui.element; $ = layui.jquery; layer = layui.layer; resetMenuHeight(); }); // 重新设置页面高度 function resetMenuHeight(){ var height = document.documentElement.clientHeight - 50; $('#menu').height(height); } // 重新设置主操作区高度 function resetMainHeight(obj){ var height = parent.document.documentElement.clientHeight - 53; $(obj).parent('div').height(height); } // 菜单点击 function menufire(obj){ // 获取url var src = $(obj).attr('src'); // 设置iframe的src $('iframe').attr('src',src); } // 退出登录 function logout(){ // 退出前确认 layer.confirm('确定要退出吗?',{ icon:3, btn:['确定','取消'] },function(){ $.get('/index.php/admins/account/logout',function(res){ if(res.code>0){ layer.msg(res.msg,{'icon':2}); }else{ layer.msg(res.msg,{'icon':1}); setTimeout(function(){window.location.href='/index.php/admins/account/login';},1000); } },'json'); }); } </script>
<?php namespace app\admins\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/admins/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); } }
Correcting teacher:查无此人Correction time:2019-06-10 09:17:18
Teacher's summary:完成的不错,后台cms管理系统,最重要的就是权限。继续加油。