ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 知识点: 1、准备工作 2、添加规则 3、规则排序 4、规则删除 5、编辑规则 [TOC] ## 一、准备工作 ### 1、build建Authrule模型 简单 ### 2、引入类库 use think\Controller; use think\Db; use util\Tree; use app\admin\model\Authrule as AuthruleModel; ### 3、操作 ~~~ class Authrule extends Common { public function index($tab = 1, $id = 0){ $authruleArray = AuthruleModel::order('id')->select(); foreach ($authruleArray as $key => $value) { $authruleList[] = $value->toArray(); //对象转数组 } $tree = new Tree(); $tree->tree($authruleList,'id','pid','title'); $authrule = $tree->getArray(); $this->assign('authrule',$authrule); return view(); } } ~~~ ### 4、数据表不存在 解决方法 use think\Model; ~~~ class Authrule extends Model { //当前模型对应的数据表名 protected $table = 'sfox_auth_rule'; } ~~~ ### 5、树型菜单 ~~~ use util\Tree; $authruleArray = AuthruleModel::order('id')->select(); foreach ($authruleArray as $key => $value) { $authruleList[] = $value->toArray(); //对象转数组 } $tree = new Tree(); $tree->tree($authruleList,'id','pid','title'); $authrule = $tree->getArray(); $this->assign('authrule',$authrule); ~~~ tree有四个参数 参一:数组 参二:当前ID 参三:父ID 参数:中文名称 ### 6、模型 ~~~ <?php namespace app\admin\model; use think\Model; class Authrule extends Model { //当前模型对应的数据表名 protected $table = 'sfox_auth_rule'; } ~~~ ### 7、新增pid、child、listorder字段 pid 父ID,默认值0,无符号 child 是否子孙,tinyint 长度1,默认值0,无符号 listorder 排序smallint,默认值0,无符号 ## 二、添加规则 ### 1、模板 片段: ~~~ <option value="{$vo.id}" {$vo.id ? '' : 'disabled'} {if condition="input('pid',0) eq $vo.id"}selected{/if}>{$vo.title}</option> ~~~ ~~~ <div class="form-group"> <label class="col-sm-2 control-label">上级规则</label> <div class="col-sm-10"> <select class="form-control m-b" name="pid"> <option value="0" selected>≡ 顶级 ≡</option> {volist name="authrule" id="vo"} <option value="{$vo.id}" {$vo.id ? '' : 'disabled'} {if condition="input('pid',0) eq $vo.id"}selected{/if}>{$vo.title}</option> {/volist} </select> </div> </div> ~~~ ### 2、添加 ~~~ public function add($tab = 1){ if(request()->isPost()){ $authruleModel = new AuthruleModel; if($authruleModel->allowField(true)->save(input('post.'))){ return success('新规则添加成功!',url('index',['tab'=>1])); }else{ return error('规则添加失败!',url('index',['tab'=>$tab])); } } } ~~~ ## 三、规则排序 ~~~ public function index($tab = 1, $id = 0){ if(request()->isPost()){ foreach (input('post.listorder/a') as $key => $value) { Db::name('auth_rule')->where('id',$key)->update(['listorder'=>$value]); } return success('排序更新成功!',url('index',['tab'=>$tab])); }else{ $authruleArray = AuthruleModel::order('id')->select(); foreach ($authruleArray as $key => $value) { $authruleList[] = $value->toArray(); //对象转数组 } $tree = new Tree(); $tree->tree($authruleList,'id','pid','title'); $authrule = $tree->getArray(); $this->assign('authrule',$authrule); // 编辑规则 if( 3 == $tab ){ // 获取所要编辑菜单的信息 $info = Db::name('auth_rule')->where('id',$id)->find(); if($info!=null && is_array($info)){ $this->assign('info',$info); } } } return view(); } ~~~ ## 四、规则删除 ~~~ public function delete($id = 0){ if(Db::name('auth_rule')->where('id', $id)->delete()){ return success('删除成功!',url('index',['tab'=>1])); }else{ return error('删除失败!',url('index',['tab'=>1])); } } ~~~ ## 五、编辑规则 ### 1、获取所要编辑菜单的信息 ~~~ public function index($tab = 1, $id = 0){ if(request()->isPost()){ foreach (input('post.listorder/a') as $key => $value) { Db::name('auth_rule')->where('id',$key)->update(['listorder'=>$value]); } return success('排序更新成功!',url('index',['tab'=>$tab])); }else{ $authruleArray = AuthruleModel::order('id')->select(); foreach ($authruleArray as $key => $value) { $authruleList[] = $value->toArray(); //对象转数组 } $tree = new Tree(); $tree->tree($authruleList,'id','pid','title'); $authrule = $tree->getArray(); $this->assign('authrule',$authrule); // 编辑规则 if( 3 == $tab ){ // 获取所要编辑菜单的信息 $info = Db::name('auth_rule')->where('id',$id)->find(); if($info!=null && is_array($info)){ $this->assign('info',$info); } } } return view(); } ~~~ ### 2、编辑操作 ~~~ public function edit($tab = 1, $id = 0){ if(request()->isPost()){ $authrule_form = input('post.'); //判断child状态 if(input('post.child')==null){ $authrule_form['child'] = 0; $count = Db::name('auth_rule')->where('pid',$id)->count(); if($count){ return error('该规则拥有子项,无法取消勾选!'); } } // 编辑规则 $authruleModel = new AuthruleModel; if($authruleModel->allowField(true)->isUpdate()->save($authrule_form)){ return success('规则编辑成功!',url('index',['tab'=>1])); }else{ return error('规则信息未修改或编辑失败!'); } } } ~~~