多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 知识点 1、后台common控制器设置 2、权限设置 3、分离列表显示和排序处理 4、后台首页权限判断 5、分离配置列表和编辑 6、前台首页注册登录权限 7、自定义弹窗提示信息 8、获取配置信息 [TOC] ## 一、后台common控制器设置 ### (一)思路分析 首先,对common.php设置权限(登录才能访问) 其次,其它控制器必须继承common.php ~~~ public function _initialize() { // 用户未登录则跳转前台登录页面 if(session('uid') == null){ session(null); $this->redirect('index/user/login'); } } ~~~ ### (二)相关代码修改 #### 1、后台控制器继承common.php #### 2、common.php控制器delete函数修改 ~~~ public function _initialize() { // 用户未登录则跳转前台登录页面 if(session('uid') == null){ session(null); $this->redirect('index/user/login'); } } ~~~ #### 3、安全退出 ~~~ public function loginout(){ session(null); $this->redirect('index/user/login'); } ~~~ #### 4、模板里面输出session变量 ~~~ {:session('username')} ~~~ ## 二、权限设置 ### (一)判断是够超级管理员 #### 1、配置文件 这里今后需要变动文件路径 位置:\application\admin\config.php ~~~ return [ 'auth_superadmin' => '37', //Auth权限认证超管ID ]; ~~~ #### 2、引入文件 文件:common.php ~~~ use util\Auth; class Common extends Controller { public function _initialize() { // 用户未登录则跳转前台登录页面 if(session('uid') == null){ session(null); $this->redirect('index/user/login'); } // 判断权限 if(!in_array(session('uid'), explode(',',config('auth_superadmin')))){ $auth = new Auth(); $rule = strtolower(request()->module().'/'.request()->controller().'/'.request()->action()); if(!$auth->check($rule, session('uid'))){ return error('您没有相应操作权限!'); } } } } ~~~ 备注:TP5中_initialize()函数是不支持return方法的 ### (二)判断权限函数 #### 1、引入类库 use util\Auth; #### 2、设置权限 文件:\application\common.php ~~~ function checkAuth() { if(!in_array(session('uid'), explode(',',config('auth_superadmin')))){ $auth = new Auth(); $rule = strtolower(request()->module().'/'.request()->controller().'/'.request()->action()); return $auth->check($rule, session('uid')); }else{ return true; } } ~~~ #### 3、权限调用 ~~~ // 判断权限 if(!checkAuth()){ return error('您没有相应的操作权限!'); } ~~~ ## 三、分离列表显示和排序处理 ### (一)原来代码 ~~~ public function index($tab = 1, $id = 0){ // 1个分类(从表) 属于 1个模型(主表) 属于[belongsTo] // 1个模型 有 多个分类 if(request()->isPost()){ foreach (input('post.listorder/a') as $key => $value) { Db::name('menu')->where('id',$key)->update(['listorder'=>$value]); } return success('排序更新成功!',url('index',['tab'=>$tab])); }else{ $menuArray = MenuModel::order('listorder')->select(); foreach ($menuArray as $key => $value) { $menuList[] = $value->toArray(); //对象转数组 } $tree = new Tree(); $tree->tree($menuList,'id','parentid','name'); $menu = $tree->getArray(); $this->assign('menu',$menu); // 编辑菜单,默认加载 if( 3 == $tab ){ // 获取所要编辑菜单的信息 $info = Db::name('menu')->where('id',$id)->find(); if($info!=null && is_array($info)){ $this->assign('info',$info); } } } return view(); } ~~~ ### (二)现在代码 #### 1、列表显示 ~~~ public function index($tab = 1, $id = 0){ // 判断权限 if(!checkAuth()){ echo "<script>parent.window.location.href='/admin/index/index';</script>"; exit; } $menuArray = MenuModel::order('listorder')->select(); foreach ($menuArray as $key => $value) { $menuList[] = $value->toArray(); //对象转数组 } $tree = new Tree(); $tree->tree($menuList,'id','parentid','name'); $menu = $tree->getArray(); $this->assign('menu',$menu); // 编辑菜单,默认加载 if( 3 == $tab ){ // 获取所要编辑菜单的信息 $info = Db::name('menu')->where('id',$id)->find(); if($info!=null && is_array($info)){ $this->assign('info',$info); } } return view(); } ~~~ #### 2、排序功能 ~~~ public function sort() { if(request()->isPost()){ // 判断权限 if(!checkAuth()){ return error('您没有相应的操作权限!'); } foreach (input('post.listorder/a') as $key => $value) { Db::name('menu')->where('id',$key)->update(['listorder'=>$value]); } return success('排序更新成功!',url('index',['tab'=>$tab])); } } ~~~ #### 3、模板修改 主要涉及排序模板提交处理地址,列表不存在修改 #### 4、权限调用 ~~~ if(!checkAuth()){ return error('您没有相应的操作权限!'); } ~~~ #### 5、列表权限调用特别 ~~~ // 判断权限 if(!checkAuth()){ echo "<script>parent.window.location.href='/admin/index/index';</script>"; exit; } ~~~ ## 四、后台首页权限判断 ### (一)思路分析 标识:admin/index/index 思路:前台登录时判断权限,即有没有机会进入后台首页。 我个人认为,完全不必要这样判断,只需判断该账户是不是指定管理员,是就进入后台首页,否则进入个人会员中心。 这个有个缺陷,就是管理员必须手动指定,缺乏灵活性。如果是正式CMS系统,还是按上面的思路进行权限判断。 ### (二)具体实现 需要改动两个地方 首先,修改超管配置文件 原来配置文件路径是:\application\admin\config.php 这里只适合后台调用该配置文件内容 ~~~ return [ 'auth_superadmin' => '37', //Auth权限认证超管ID ]; ~~~ 现在配置文件路径:\application\config.php 这里配置文件的内容前后台都可以调用 就是这个道理 其次,修改前台登录操作 原来代码 ~~~ public function login(){ if(request()->isPost()){ $data = input('post.');//p($data);die; //判断用户名是否存在 //$map['username'] = $data['username']; //$user = Db::name('user')->where($map)->find(); //判断用户名或手机号是否存在 $user = Db::name('user')->where('username|mobile',$data['username'])->find(); if($user){ if($user['password'] == md5($data['password'])){ //写入Session session('uid',$user['id']); session('username',$user['username']); // 临时安全设置 $url = $user['username'] == 'manage' ? url('/manage') : url('/'); //返回成功信息 $data['status'] = 200; //$data['url'] = url('/manage'); $data['url'] = $url; return json($data); }else{ //返回错误信息 $data['status'] = 202; $data['msg'] = '登录密码错误!'; return json($data); } }else{ //返回错误信息 $data['status'] = 202; $data['msg'] = '用户名不存在!'; return json($data); } }else{ return view('../application/index/view/default/user/login.html'); } } ~~~ 现在代码 ~~~ public function login(){ if(request()->isPost()){ $data = input('post.'); //判断用户名或手机号是否存在 $user = Db::name('user')->where('username|mobile',$data['username'])->find(); if($user){ if($user['password'] == md5($data['password'])){ //写入Session session('uid',$user['id']); // 判断权限 if(!checkAuth()){ session(null); //返回错误信息 $data['status'] = 202; $data['msg'] = '您没有权限登录系统后台!'; return json($data); }else{ //返回成功信息 $data['status'] = 200; $data['url'] = url('/admin'); return json($data); } }else{ //返回错误信息 $data['status'] = 202; $data['msg'] = '登录密码错误!'; return json($data); } }else{ //返回错误信息 $data['status'] = 202; $data['msg'] = '用户不存在!'; return json($data); } }else{ return view('../application/index/view/default/user/login.html'); } } ~~~ 同样,注册时也需要进行权限判断 还有,就是后台凡是需要权限判断的,都加相关代码,无非两种 第一,post提交的权限判断,很简单,代码如下: ~~~ if(!checkAuth()){ return error('您没有相应的操作权限!'); } ~~~ 第二,不是post提交权限判断,即列表权限判断 ~~~ if(!checkAuth()){ echo "<script>parent.window.location.href='/admin/index/index';</script>"; exit; } ~~~ ## 五、分离配置列表和编辑 原来代码 ~~~ public function index($tab=1){ if(request()->isPost()){ // 判断权限 if(!checkAuth()){ return error('您没有相应的操作权限!'); } $config = new ConfigModel; if ($config->saveConfig(input('post.'))) { return success('配置更新成功',url('index',['tab'=>$tab])); }else{ return error('配置更新失败',url('index',['tab'=>$tab])); } }else{ $config = ConfigModel::column('varname,value'); $this->assign('site',$config); return view(); } } ~~~ 现在代码 ~~~ public function index() { $configList = ConfigModel::column('varname,value'); $this->assign('site',$configList); return view(); } public function edit($tab = "1") { if(request()->isPost()){ // 判断权限 if(!checkAuth()){ return error('您没有相应的操作权限!'); } $config = new ConfigModel; if($config->saveConfig(input('post.'))){ return success('配置更新成功!',url('index',['tab'=>$tab])); }else{ return error('配置更新失败!',url('index',['tab'=>$tab])); } } } ~~~ ## 六、前台首页注册登录权限 ~~~ public function login(){ if(request()->isPost()){ $data = input('post.'); //判断用户名或手机号是否存在 $user = Db::name('user')->where('username|mobile',$data['username'])->find(); if($user){ if($user['password'] == md5($data['password'])){ //写入Session session('uid',$user['id']); // 判断权限 if(!in_array(session('uid'), explode(',',config('auth_superadmin')))){ $auth = new Auth(); $rule = 'manage/index/content'; if($auth->check($rule, session('uid'))){ //返回成功信息 $data['status'] = 200; $data['url'] = url('/manage'); return json($data); }else{ session(null); //返回错误信息 $data['status'] = 202; $data['msg'] = '您没有权限登录系统后台!'; return json($data); } }else{ //返回成功信息 $data['status'] = 200; $data['url'] = url('/manage'); return json($data); } }else{ //返回错误信息 $data['status'] = 202; $data['msg'] = '登录密码错误!'; return json($data); } }else{ //返回错误信息 $data['status'] = 202; $data['msg'] = '用户不存在!'; return json($data); } }else{ return view('../application/index/view/default/user/login.html'); } } ~~~ ## 七、自定义弹窗提示信息 之前事件响应是post方式的submit,现在如果是get方式如何实现自定义弹窗提示? ### (一)后台处理 ~~~ public function content($id = 0) { if($id){ // 判断权限 if(!checkAuth()){ $id = 0; echo "<script src='/static/admin/js/jquery.min.js?v=2.1.4'></script>". "<script>$(document).ready(function(){ layer.open({content: '您没有相应的操作权限!',btn: ['确定'],icon: 2,shade: 0.1}); });</script>"; } $catname = getCatInfoById($id, 'catname'); $this->assign('catname',$catname); $this->assign('id',$id); } return view(); } ~~~ 判断权限也可以换成 ~~~ // 判断权限 if(!checkAuth()){ return $this->error('您没有相应的操作权限!'); } ~~~ 备注:函数中是不能模板变量的 ### (二)模板 ~~~ "url": "{:url('getDataTables',['id'=>input('id')])}", 换成 "url": "{:url('getDataTables',['id'=>$id])}", <script> $(document).ready(function() { $("#dataTables-example").dataTable({ "serverSide": true, "ajax": { "url": "{:url('getDataTables',['id'=>$id])}", "data": function(d) { d.extra_search = "title|username"; } }, "ordering": false, //禁用全局排序 "order": [0, '`listorder` desc'], "lengthMenu": [5, 10, 20, 50, 100], // "dom": '<l <"#normalToos">f>t<ip>', "dom": "<'row'<'#normalToos.col-xs-4'><'col-xs-8'f>>" + "<'row'<'col-xs-12't>>" + "<'row'<'col-xs-6'li><'col-xs-6'p>>", "language": { "zeroRecords": "没有检索到数据", "lengthMenu": "每页 _MENU_ 条记录&nbsp;&nbsp;", "search": "搜索 ", "info": "共 _PAGES_ 页,_TOTAL_ 条记录,当前显示 _START_ 到 _END_ 条", "paginate": { "previous": "上一页", "next": "下一页", } }, "columns": [{ render: function(data, type, row, meta) { return '<input type="checkbox" class="i-checks" name="ids[' + row.id + ']">'; } }, { data: "id" }, { data: "title" }, { data: "username" }, { data: "inputtime" }, { data: "views" }, { data: "operate" }, ], "drawCallback": function() { normal_init(); }, "initComplete": function() { $("#normalToos").append("<div class='m-b-xs'>" + "<div class='btn-group' id='exampleTableEventsToolbar' role='group'>" + "<a class='btn btn-sm btn-outline btn-default' title='添加' target='_parent' href='{:url('add',['id'=>input('id',0)])}'>" + "<i class='glyphicon glyphicon-plus' aria-hidden='true'></i></a>" + "<button type='submit' class='btn btn-sm btn-outline btn-default' title='删除'>" + "<i class='glyphicon glyphicon-trash' aria-hidden='true'></i></button></div></div>"); } }); }); </script> ~~~ ## 八、获取配置信息 ### 1、后台处理 ~~~ public function _initialize() { // 用户未登录则跳转前台登录页面 if(session('uid') == null){ session(null); $this->redirect('index/user/login'); } // 获取配置信息 if(!cache('config')){ $config = db('config')->column('varname,value'); cache('config',$config); } $this->assign('config',cache('config')); } ~~~ ### 2、模板调用 ~~~ <title>{$config.sitename} - 后台主页</title> ~~~