摘要:<?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;
}
}html>
欢迎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;}
后台管理系统
{$admin.username}【{$role.title}】退出
{volist name="$menus" id="vo"}
{$vo.title}
{volist name="vo.children" id="cvo"}
{$cvo.title}{/volist}
{/volist}
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');
});
}
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);
}
}
批改老师:欧阳批改时间:2019-06-10 09:17:18
老师总结:完成的不错,后台cms管理系统,最重要的就是权限。继续加油。