💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 知识点 1、思路分析 2、弹出弹框 3、弹出弹框显示规则 [TOC] ## 一、思路分析 权限设置,是针对权限组,某个角色拥有哪些规则,每个规则都有一个单独的ID auth_group中的字段rules,规则id就保存在rules字段下面,中间用逗号隔开 点击权限设置按钮,弹出一个弹框,显示所有的规则 ## 二、弹出弹框 layer是一款近年来备受青睐的web弹层组件 官网:http://layer.layui.com/ ### (一)模板链接 ~~~ <a class="parentIframe" href="javascript:;" rel="{$vo.id}">权限设置</a> {volist name="authrolelist" id="vo"} <tr> <td>{$vo.id}</td> <td>{$vo.title}</td> <td>{$vo.remark}</td> <td>{$vo.status? '√' : '✖'}</td> <td> <a class="parentIframe" href="javascript:;" rel="{$vo.id}">权限设置</a>&nbsp;&nbsp;|&nbsp;&nbsp; <a href="{:url('index',['id'=>$vo.id,'tab'=>3])}">编辑</a>&nbsp;&nbsp;|&nbsp;&nbsp; <a name="delete" href="{:url('delete',['id'=>$vo.id])}">删除</a> </td> </tr> {/volist} ~~~ ### (二)事件响应 ~~~ <script> $('.parentIframe').on('click',function(){ layer.open({ type: 2, title: '权限设置', area: ['500px', '550px'], fixed: false, //不固定 maxmin: true, content: 'authset' }); }); </script> ~~~ ### (三)新建authset文件 位置:application\admin\view\authrole\authset.html ### (四)后台处理 ~~~ public function authset() { return view(); } ~~~ ## 三、弹出弹框显示规则 整合插件jstree ### (一)简单实例 #### 1、引入类库 ~~~ <link href="__ADMIN__/css/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet"> <link href="__ADMIN__/css/plugins/jsTree/style.min.css" rel="stylesheet"> <link href="__ADMIN__/css/animate.min.css" rel="stylesheet"> <link href="__ADMIN__/css/style.min862f.css?v=4.1.0" rel="stylesheet"> <!--[if lt IE 9]><script src="https://static.jstree.com/3.3.4/assets/html5.js"></script><![endif]--> <link rel="stylesheet" href="https://static.jstree.com/3.3.4/assets/dist/themes/default/style.min.css"> <!--[if lt IE 9]><script src="https://static.jstree.com/3.3.4/assets/respond.js"></script><![endif]--> <script src="__ADMIN__/js/jquery.min.js?v=2.1.4"></script> <script src="__ADMIN__/js/bootstrap.min.js?v=3.3.6"></script> <script src="__ADMIN__/js/content.min.js?v=1.0.0"></script> <script src="__ADMIN__/js/plugins/jsTree/jstree.min.js"></script> ~~~ #### 2、模板代码 ~~~ <div id="jstree"></div> ~~~ #### 3、事件响应 ~~~ <script> $('.parentIframe').on('click',function(){ var role_id = $(this).attr('rel'); layer.open({ type: 2, title: '权限设置', area: ['500px', '550px'], fixed: false, //不固定 maxmin: true, content: 'authset/id/' + role_id }); }); </script> ~~~ ### (二)动态获取信息 #### 1、响应事件 ~~~ <script> $(function() { $('#jstree').jstree({ "plugins" : [ "checkbox" ], "checkbox" : { "keep_selected_style" : false }, "core" : { "data" : {$json} } }); }); </script> ~~~ #### 2、获取规则 ~~~ public function authset() { // 获取规则 $result = Db::name('auth_rule')->order('listorder')->select(); $json = array(); foreach ($result as $value) { $data = array( 'id' => $value['id'], 'parent' => $value['pid']==0 ? '#' : $value['pid'], 'text' => $value['title'], ); $json[] = $data; } $this->assign('json', json_encode($json)); return view(); } ~~~ #### 3、模板表单 ~~~ <form method="post" class="form-horizontal" action="{:url('authset')}" data-type="ajax" style="padding:30px"> <input type="hidden" name="id" value="{:input('id',0)}"> <input type="text" name="rules"> <div id="jstree"></div> <div class="form-group" style="margin: 30px 80px"> <button class="btn btn-primary" type="submit">提交</button> </div> </form> ~~~ #### 4、默认全部展开 ~~~ public function authset() { if(request()->isPost()){ $data = input('post.'); Db::name('auth_group')->strict(false)->update($data); return success('权限设置成功!',url('index',['tab'=>1])); }else{ // 获取规则 $result = Db::name('auth_rule')->order('listorder')->select(); $json = array(); foreach ($result as $value) { $data = array( 'id' => $value['id'], 'parent' => $value['pid']==0 ? '#' : $value['pid'], 'text' => $value['title'], 'state' => ['opened' => true] //默认全部展开 ); $json[] = $data; } $this->assign('json', json_encode($json)); return view(); } } ~~~ #### 5、选中ID ~~~ 表单 <input type="hidden" id="event_result" name="rules"> 事件响应 $(function() { $('#jstree').jstree({ "plugins" : [ "checkbox" ], "checkbox" : { "keep_selected_style" : false }, "core" : { "data" : {$json} } }) .on('changed.jstree', function (e, data) { var i, j, r = []; for(i = 0, j = data.selected.length; i < j; i++) { r.push(data.instance.get_node(data.selected[i]).id); } $('#event_result').val(r.join(',')); }); }); </script> ~~~ #### 6、提交数据后台保存数据库 ~~~ $data = input('post.'); Db::name('auth_group')->strict(false)->update($data); return success('权限设置成功!',url('index',['tab'=>1])); ~~~ #### 7、打开权限默认勾选 ~~~ 'state' => ['opened' => true, 'selected' => in_array($value['id'],$rules)? true : false] public function authset($id = 0) { if(request()->isPost()){ $data = input('post.'); Db::name('auth_group')->strict(false)->update($data); return success('权限设置成功!',url('index',['tab'=>1])); }else{ // 获取当前角色所有规则 $rules =Db::name('auth_group')->where('id',$id)->value('rules'); //规则转数组 $rules = explode(',',$rules); // 获取规则 $result = Db::name('auth_rule')->order('listorder')->select(); $json = array(); foreach ($result as $value) { $data = array( 'id' => $value['id'], 'parent' => $value['pid']==0 ? '#' : $value['pid'], 'text' => $value['title'], 'state' => ['opened' => true, 'selected' => in_array($value['id'],$rules)? true : false] ); $json[] = $data; } $this->assign('json', json_encode($json)); return view(); } } ~~~ #### 8、弹框处理 ~~~ <script src="__ADMIN__/js/plugins/layer/layer.min.js"></script> <script src="__ADMIN__/js/layer_hplus.js"></script> ~~~ 放文件最底部 #### 9、关闭弹框返回 重新定义一个跳转函数successIframe() 自定义successIframe助手函数 位置:application\common.php ~~~ function successIframe($msg = '成功', $url = '') { $data['status'] = 201; $data['msg'] = $msg; return json($data); } ~~~ 修改layer_hplus.js ~~~ $(document).on('submit','form[data-type=ajax]',function(){ //获取数据 var url = $(this).attr('action'); var data = $(this).serializeArray();//序列化表单元素 //弹出询问框 layer.confirm('您确定提交处理吗?',{icon:3, title:'提示'},function(index){ //异步提交 $.ajax({ type: "POST", dataType:"json", url:url, data:data, success:function(obj){ var icon_num = (obj.status==202) ? 2 : 1; if(obj.status==200 || obj.status==202){ layer.open({ content: obj.msg, btn: ['确定'], shade: 0.1, icon: icon_num, yes: function(index, layero){ if(obj.url){ location.href = obj.url; //跳转指定地址 }else{ layer.close(index); } }, cancel: function(){ if(obj.url){ location.href = obj.url; //跳转指定地址 }else{ layer.close(); } }, }); }else if(obj.status==201){ layer.open({ content: obj.msg, btn: ['确定'], shade: 0.1, icon: icon_num, yes: function(index, layero){ var index = parent.layer.getFrameIndex(window.name); parent.layer.close(index); }, cancel: function(){ layer.close(); }, }); } }, error:function(data){ layer.alert('网络故障!'); } }); }); return false; }); ~~~