author:咔咔
wechat:fangkangfk
html导航单显示
{include file="../../../application/admin/view/public/head" /} <style type="text/css"> .hs-iframe{width:100%;height:100%;} .layui-tab{position:absolute;left:0;top:0;height:100%;width:100%;z-index:10;margin:0;border:none;overflow:hidden;} .layui-tab-title li:first-child > i { display: none; } .layui-tab-content{padding:0 0 0 10px;height:100%;} .layui-tab-item{height:100%;} .layui-nav-tree .layui-nav-child a{height:38px;line-height: 38px;} .footer{position:fixed;left:0;bottom:0;z-index:998;} </style> <div class="layui-layout layui-layout-admin"> <div class="layui-header"> <div class="fl header-logo">番茄视频后台控制台</div> <div class="fl header-fold"><a href="javascript:;" title="打开/关闭左侧导航" class="aicon ai-caidan" id="foldSwitch"><i class="layui-icon"></i></a></div> <ul class="layui-nav fl nobg main-nav"> {volist name="menus" id="vo"} {if condition="($i eq 1)"} <li class="layui-nav-item layui-this"> {else /} <li class="layui-nav-item"> {/if} <a href="javascript:;">{$vo['name']}</a></li> {/volist} </ul> <ul class="layui-nav fr nobg head-info" lay-filter=""> <li class="layui-nav-item"> <a href="javascript:void(0);">{$Think.session.USER_INFO_SESSION.au_user_name} </a> <dl class="layui-nav-child"> <dd><a href="javascript:void(0);" id="lockScreen">锁屏</a></dd> <dd><a href="{:url('login/loginOut')}">退出登陆</a></dd> </dl> </li> <li class="layui-nav-item"><a href="http://www.maccms.com/" target="_blank">官网</a></li> <li class="layui-nav-item"><a href="http://bbs.maccms.com/" target="_blank">论坛</a></li> <li class="layui-nav-item"><a href="__ROOT__/" target="_blank">前台</a></li> <li class="layui-nav-item"><a href="{:url('index/clear')}" class="j-ajax" refresh="yes">清缓存</a></li> </ul> </div> <div class="layui-side layui-bg-black" id="switchNav"> <div class="layui-side-scroll"> {volist name="menus" id="v"} {if condition="($i eq 1)"} <ul class="layui-nav layui-nav-tree"> {else /} <ul class="layui-nav layui-nav-tree" style="display:none;"> {/if} <li class="layui-nav-item layui-nav-itemed"> <a href="javascript:;"><i ></i>{$v['name']}<span class="layui-nav-more"></span></a> <dl class="layui-nav-child"> {volist name="v['sub']" id="vv" key="kk"} <dd><a class="admin-nav-item" data-id="{$key}{$kk}" href="{$vv['url']}"><i ></i> {$vv['name']}</a></dd> {/volist} </dl> </li> </ul> {/volist} </div> </div> <div class="layui-body" id="switchBody"> <div class="layui-tab layui-tab-card" lay-filter="macTab" lay-allowClose="true"> <ul class="layui-tab-title"> <li lay-id="111" class="layui-this">欢迎页面</li> </ul> <div class="layui-tab-content"> <div class="layui-tab-item layui-show"> <iframe lay-id="111" src="{:url('index/welcome')}" width="100%" height="100%" frameborder="0" scrolling="yes" class="hs-iframe"></iframe> </div> </div> </div> </div> <div class="layui-footer footer"> <div class="fl"></div> <div class="fr"> © 2008-2018 <a href="http://www.maccms.com/" target="_blank">MacCMS.COM.</a> All Rights Reserved.</div> </div> </div> {include file="../../../application/admin/view/public/foot" /} <!--请在下方写此页面业务相关的脚本--> <script> window.localStorage.clear(); var LAYUI_OFFSET = 60; </script> <script type="text/javascript"> layui.use(['element', 'layer'], function() { var $ = layui.jquery, element = layui.element, layer = layui.layer; $('.layui-tab-content').height($(window).height() - 145); var tab = { add: function(title, url, id) { element.tabAdd('macTab', { title: title, content: '<iframe width="100%" height="100%" lay-id="'+id+'" frameborder="0" src="'+url+'" scrolling="yes" class="x-iframe"></iframe>', id: id }); }, change: function(id) { element.tabChange('macTab', id); } }; $('.admin-nav-item').click(function(event) { var that = $(this); var id = that.attr('data-id'); if ($('iframe[lay-id="'+id+'"]')[0]) { tab.change(id); event.stopPropagation(); $("iframe[lay-id='"+id+"']")[0].contentWindow.location.reload(true);//切换后刷新框架 return false; } if ($('iframe').length == 10) { layer.msg('最多可打开10个标签页'); return false; } that.css({color:'#fff'}); tab.add(that.text(), that.attr('href'), that.attr('data-id')); tab.change(that.attr('data-id')); event.stopPropagation(); return false; }); $(document).on('click', '.layui-tab-close', function() { $('.layui-nav-child a[data-id="'+$(this).parent('li').attr('lay-id')+'"]').css({color:'rgba(255,255,255,.7)'}); }); }); </script> </body> </html>
这是效果图
下来我们开始写控制器:
在写权限之前,我们先缕缕思路
1.我们需要在用户登录的时候获取用户的所有的权限
2.过滤系统管理员,不判断其权限
3.根据登录时的角色信息,来验证权限
下来我们先看登录存储用户权限信息:
这个时候我们需要在base基类里边做权限验证
这里使用的是tp5获取的控制器和方法名,这里有一个注意点就是需要将所有的地址全部转为小写,这一步就是判断用户是否有权限,有权限了会继续执行,没有权限的话,会直接返回上一个页面
public function check_auth($controller,$action) { $c = strtolower($controller); $a = strtolower($action); // 获取用户权限 $authId = $this->session->getUserRole(); // 获取用户信息 $adminData = $this->session->getUserInfo(); $adminAuth = Db::name('auth')->where('a_id',$authId)->value('a_auth'); $auths = $adminAuth . ',index/index,index/welcome,'; $cur = $c.'/'.$a; if($adminData->au_id =='1'){ return true; } elseif(strpos(strtolower($auths),$cur)===false){ return false; } else{ return true; } }
做到这里是不是感觉代码就到这完了,其实不然,我们还需要写菜单栏的代码,因为设置权限了以后,会有一部分导航是看不见的
这个功能点,我们在哪里做呢!我们都是知道Layui这个框架是是使用的ifaram,所以导航栏只会刷新一次,所以我们需要在index控制写
这个方法的注释已经写得很明晰了。我简单的解释一下
首先我们会从配置文件中将所有的权限都读取出来,然后进行循环组装跟移除此用户没有权限的导航。
show为1的是显示的导航列表,我们获取到配置文件的权限信息,组装成我们数据库存储的格式
然后拿着这个控制器跟方法名来使用我们base的检验权限的方法,来移除不属于角色的权限,这就是第一个if做的事情
我们的模块有增删改这三个功能,当然我们的权限控制也是需要控制这些列表的。但是这些列表是不会显示出来的,所以我们会将show为0的所有列表删除掉
这个时候我们的大的导航栏在权限过滤完之后是由一部分是空的,所以在将这一部分删除即可
/** * 获取菜单 * @return array */ public function getMenu() { $menus = Config::get('auth'); foreach($menus as $k1=>$v1){ foreach($v1['sub'] as $k2=>$v2){ // 获取所有的一级菜单 if($v2['show'] == 1) { $url = url( 'admin/'.$v2['controller'] . '/' . $v2['action']); if ($this->check_auth($v2['controller'], $v2['action'])) { $menus[$k1]['sub'][$k2]['url'] = $url; } else { // 没有权限的全部删除 unset($menus[$k1]['sub'][$k2]); } } else{ // 不显示的不需要给组装url unset($menus[$k1]['sub'][$k2]); } } // 在将权限过滤完之后,没有权限的一级就没有二级,所以删除区级即可 if(empty($menus[$k1]['sub'])){ unset($menus[$k1]); } } return $menus; }
我们权限存储的方式
<?php return array( '1' => array('name' => '首页', 'icon' => 'xe625', 'sub' => array( '11' => array("show"=>1,"name" => '欢迎页面', 'controller' => 'index', 'action' => 'welcome'), '12' => array("show"=>1,"name" => '系统配置', 'controller' => 'index', 'action' => 'welcome'), )), '11' => array('name' => '管理员', 'icon' => 'xe62c', 'sub' => array( '114' => array("show"=>1,'name' => '管理员', 'controller' => 'Administrators', 'action' => 'index'), '63' => array("show"=>1,'name' => '管理员权限组', 'controller' => 'Auth', 'action' => 'index'), '115' => array("show"=>1,'name' => '管理员操作日志', 'controller' => 'userTask', 'action' => 'index'), )), '3' => array('name' => '基础', 'icon' => 'xe62c', 'sub' => array( '26' => array("show"=>1,'name' => '启动和引导页', 'controller' => 'boot', 'action' => 'index'), '261' => array("show"=>0,'name' => '启动页添加', 'controller' => 'boot', 'action' => 'addBootUp'), '262' => array("show"=>0,'name' => '启动页修改', 'controller' => 'boot', 'action' => 'editBootUp'), '263' => array("show"=>0,'name' => '启动页删除', 'controller' => 'boot', 'action' => 'delBootUp'), '264' => array("show"=>0,'name' => '启动页状态', 'controller' => 'boot', 'action' => 'bootUpStatus'), '31' => array("show"=>1,'name' => '广告管理', 'controller' => 'ad', 'action' => 'index'), '311' => array("show"=>0,'name' => '广告添加', 'controller' => 'ad', 'action' => 'addBanner'), '312' => array("show"=>0,'name' => '广告修改', 'controller' => 'ad', 'action' => 'editBanner'), '313' => array("show"=>0,'name' => '广告删除', 'controller' => 'ad', 'action' => 'delBanner'), '314' => array("show"=>0,'name' => '广告视频', 'controller' => 'ad', 'action' => 'videoInfoList'), '30' => array("show"=>1,'name' => '域名管理', 'controller' => 'domain', 'action' => 'index'), '301' => array("show"=>0,'name' => '域名添加', 'controller' => 'domain', 'action' => 'addDomain'), '302' => array("show"=>0,'name' => '域名修改', 'controller' => 'domain', 'action' => 'editDomain'), '303' => array("show"=>0,'name' => '域名删除', 'controller' => 'domain', 'action' => 'delDoamin'), '34' => array("show"=>1,'name' => '用户图像库', 'controller' => 'headpic', 'action' => 'index'), '341' => array("show"=>0,'name' => '用户图像库添加', 'controller' => 'headpic', 'action' => 'add'), '342' => array("show"=>0,'name' => '用户图像库修改', 'controller' => 'headpic', 'action' => 'edit'), '343' => array("show"=>0,'name' => '用户图像库删除', 'controller' => 'headpic', 'action' => 'del'), '32' => array("show"=>1,'name' => '公告管理', 'controller' => 'domain', 'action' => 'index'), '33' => array("show"=>1,'name' => '系统消息', 'controller' => 'domain', 'action' => 'index'), '27' => array("show"=>1,'name' => '版本更新', 'controller' => 'Versions', 'action' => 'index'), '2701' => array("show"=>0,'name' => '--版本修改', 'controller' => 'Versions', 'action' => 'editVersions'), '2703' => array("show"=>0,'name' => '--版本删除', 'controller' => 'Versions', 'action' => 'delVersions'), '2704' => array("show"=>0,'name' => '--版本添加', 'controller' => 'Versions', 'action' => 'addVersions'), )), '4' => array('name' => '视频', 'icon' => 'xe625', 'sub' => array( '31' => array("show"=>1,'name' => '类型管理', 'controller' => 'videoType', 'action' => 'index'), '311' => array("show"=>0,'name' => '修改类型', 'controller' => 'videoType', 'action' => 'editType'), '312' => array("show"=>0,'name' => '删除类型', 'controller' => 'videoType', 'action' => 'delVideoType'), '313' => array("show"=>0,'name' => '添加类型', 'controller' => 'videoType', 'action' => 'addVideoType'), '314' => array("show"=>0,'name' => '一级分类管理', 'controller' => 'videoType', 'action' => 'subclassList'), '315' => array("show"=>0,'name' => '一级分类修改', 'controller' => 'videoType', 'action' => 'editSubClass'), '316' => array("show"=>0,'name' => '一级分类添加', 'controller' => 'videoType', 'action' => 'addSubclass'), '317' => array("show"=>0,'name' => '一级分类删除', 'controller' => 'videoType', 'action' => 'delSubclass'), '318' => array("show"=>0,'name' => '二级分类管理', 'controller' => 'videoType', 'action' => 'secondTypeIndex'), '319' => array("show"=>0,'name' => '添加二级分类', 'controller' => 'videoType', 'action' => 'addSecondType'), '3110' => array("show"=>0,'name' => '修改二级分类', 'controller' => 'videoType', 'action' => 'editSecondType'), '32' => array("show"=>1,'name' => '专题管理', 'controller' => 'subject', 'action' => 'index'), '321' => array("show"=>0,'name' => '专题列表', 'controller' => 'subject', 'action' => 'subjectList'), '322' => array("show"=>0,'name' => '专题修改', 'controller' => 'subject', 'action' => 'editSubject'), '323' => array("show"=>0,'name' => '专题添加', 'controller' => 'subject', 'action' => 'addSubject'), '324' => array("show"=>0,'name' => '专题删除', 'controller' => 'subject', 'action' => 'delSubject'), '325' => array("show"=>0,'name' => '专题视频列表', 'controller' => 'subject', 'action' => 'videoList'), '326' => array("show"=>0,'name' => '专题视频添加', 'controller' => 'subject', 'action' => 'subjectVideoAdd'), '327' => array("show"=>0,'name' => '专题视频删除', 'controller' => 'subject', 'action' => 'delSubjectVideo'), '328' => array("show"=>0,'name' => '专题视频更换', 'controller' => 'subject', 'action' => 'editSubjectVideo'), '329' => array("show"=>0,'name' => '专题广告列表', 'controller' => 'subject', 'action' => 'adList'), '3210' => array("show"=>0,'name' => '专题广告添加', 'controller' => 'subject', 'action' => 'addAd'), '3211' => array("show"=>0,'name' => '专题广告修改', 'controller' => 'subject', 'action' => 'editAd'), '3212' => array("show"=>0,'name' => '专题广告删除', 'controller' => 'subject', 'action' => 'delAd'), '40' => array("show"=>1,'name' => '标签库', 'controller' => 'tag', 'action' => 'index'), '401' => array("show"=>0,'name' => '标签添加', 'controller' => 'tag', 'action' => 'add'), '402' => array("show"=>0,'name' => '标签修改', 'controller' => 'tag', 'action' => 'edit'), '403' => array("show"=>0,'name' => '标签删除', 'controller' => 'tag', 'action' => 'del'), '41' => array("show"=>1,'name' => '热词管理', 'controller' => 'hotWord', 'action' => 'index'), '411' => array("show"=>0,'name' => '热词添加', 'controller' => 'hotWord', 'action' => 'add'), '412' => array("show"=>0,'name' => '热词修改', 'controller' => 'hotWord', 'action' => 'edit'), '413' => array("show"=>0,'name' => '热词删除', 'controller' => 'hotWord', 'action' => 'del'), '38' => array("show"=>1,'name' => '视频管理', 'controller' => 'video', 'action' => 'index'), '381' => array("show"=>0,'name' => '视频添加', 'controller' => 'video', 'action' => 'add'), '382' => array("show"=>0,'name' => '视频修改', 'controller' => 'video', 'action' => 'edit'), '383' => array("show"=>0,'name' => '视频删除', 'controller' => 'video', 'action' => 'del'), '39' => array("show"=>1,'name' => '评论管理', 'controller' => 'video', 'action' => 'index'), )), '6' => array('name' => '用户', 'icon' => 'xe62c', 'sub' => array( '63' => array("show"=>1,'name' => '用户管理', 'controller' => 'user', 'action' => 'index'), '6301' => array("show"=>0,'name' => '--会员信息查看', 'controller' => 'user', 'action' => 'userView'), '6302' => array("show"=>0,'name' => '--会员删除', 'controller' => 'user', 'action' => 'delUser'), '6303' => array("show"=>0,'name' => '--会员信息修改', 'controller' => 'user', 'action' => 'editUser'), '64' => array("show"=>1,'name' => '任务记录', 'controller' => 'userTask', 'action' => 'index'), '6401' => array("show"=>0,'name' => '用户任务删除', 'controller' => 'userTask', 'action' => 'delUserTask'), '65' => array("show"=>1,'name' => '特权兑换记录', 'controller' => 'userExchange', 'action' => 'index'), '6501' => array("show"=>0,'name' => '用户任务删除', 'controller' => 'userTask', 'action' => 'delUserTask'), // '67' => array("show"=>1,'name' => '视频播放记录', 'controller' => 'user', 'action' => 'index'), // '68' => array("show"=>1,'name' => '视频收藏记录', 'controller' => 'user', 'action' => 'index'), '66' => array("show"=>1,'name' => 'App下载记录', 'controller' => 'user', 'action' => 'index'), '69' => array("show"=>1,'name' => '用户反馈', 'controller' => 'user', 'action' => 'index'), )), '5' => array('name' => '推广', 'icon' => 'xe616', 'sub' => array( '28' => array("show"=>1,'name' => '任务配置', 'controller' => 'task', 'action' => 'index'), '2801' => array("show"=>0,'name' => '--任务修改', 'controller' => 'task', 'action' => 'editTask'), '2803' => array("show"=>0,'name' => '--任务删除', 'controller' => 'task', 'action' => 'delTask'), '2804' => array("show"=>0,'name' => '--任务添加', 'controller' => 'task', 'action' => 'addTask'), '26' => array("show"=>1,'name' => '特权配置', 'controller' => 'Exchange', 'action' => 'index'), '2601' => array("show"=>0,'name' => '特权修改', 'controller' => 'Exchange', 'action' => 'addExchange'), '2603' => array("show"=>0,'name' => '特权删除', 'controller' => 'Exchange', 'action' => 'editExchange'), '2604' => array("show"=>0,'name' => '特权添加', 'controller' => 'Exchange', 'action' => 'delExchange'), '29' => array("show"=>1,'name' => '等级配置', 'controller' => 'task', 'action' => 'index'), )), // '9' => array('name' => '采集', 'icon' => 'xe727', 'sub' => array( // '91' => array("show"=>1,'name' => '联盟资源库', 'controller' => 'collect', 'action' => 'union'), // '9101' => array("show"=>0,'name' => '--采集入口', 'controller' => 'collect', 'action' => 'api'), // '9102' => array("show"=>0,'name' => '--断点采集', 'controller' => 'collect', 'action' => 'load'), // '9103' => array("show"=>0,'name' => '--绑定分类', 'controller' => 'collect', 'action' => 'bind'), // '9104' => array("show"=>0,'name' => '--采集视频', 'controller' => 'collect', 'action' => 'vod'), // '9105' => array("show"=>0,'name' => '--采集文章', 'controller' => 'collect', 'action' => 'art'), // // '92' => array("show"=>1,'name' => '定时挂机采集', 'controller' => 'collect', 'action' => 'timing'), // // '93' => array("show"=>1,'name' => '自定义资源库', 'controller' => 'collect', 'action' => 'index'), // '9301' => array("show"=>0,'name' => '--自定义资源库信息维护', 'controller' => 'collect', 'action' => 'info'), // '9302' => array("show"=>0,'name' => '--自定义资源库删除', 'controller' => 'collect', 'action' => 'del'), // // '94' => array("show"=>1,'name' => '自定义采集', 'controller' => 'cj', 'action' => 'index'), // '9401' => array("show"=>0,'name' => '--自定义采集信息维护', 'controller' => 'cj', 'action' => 'info'), // '9402' => array("show"=>0,'name' => '--自定义采集删除', 'controller' => 'cj', 'action' => 'del'), // '9403' => array("show"=>0,'name' => '--自定义采集发布方案', 'controller' => 'cj', 'action' => 'program'), // '9404' => array("show"=>0,'name' => '--自定义采集采集网址', 'controller' => 'cj', 'action' => 'col_url'), // '9405' => array("show"=>0,'name' => '--自定义采集采集内容', 'controller' => 'cj', 'action' => 'col_content'), // '9406' => array("show"=>0,'name' => '--自定义采集发布内容', 'controller' => 'cj', 'action' => 'publish'), // '9407' => array("show"=>0,'name' => '--自定义采集导出', 'controller' => 'cj', 'action' => 'export'), // '9408' => array("show"=>0,'name' => '--自定义采集导入', 'controller' => 'cj', 'action' => 'import'), // // )), // '10' => array('name' => '数据库', 'icon' => 'xe621', 'sub' => array( // '101' => array("show"=>1,'name' => '数据库管理', 'controller' => 'database', 'action' => 'index'), // '10001' => array("show"=>0,'name' => '--数据库备份', 'controller' => 'database', 'action' => 'export'), // '10002' => array("show"=>0,'name' => '--数据库还原', 'controller' => 'database', 'action' => 'import'), // '10003' => array("show"=>0,'name' => '--数据库优化', 'controller' => 'database', 'action' => 'optimize'), // '10004' => array("show"=>0,'name' => '--数据库修复', 'controller' => 'database', 'action' => 'repair'), // '10005' => array("show"=>0,'name' => '--数据库删除备份', 'controller' => 'database', 'action' => 'del'), // '10006' => array("show"=>0,'name' => '--数据库表信息', 'controller' => 'database', 'action' => 'columns'), // // '102' => array("show"=>1,'name' => '执行SQL语句', 'controller' => 'database', 'action' => 'sql'), // '103' => array("show"=>1,'name' => '数据批量替换', 'controller' => 'database', 'action' => 'rep'), // )), );



