角色权限把控-用户规则权限管理(二)

用户规则权限管理

用于将用户表manger与规则表auth_rule表关系对应到用户组表auth_group中

难点:选中子类,父类的跟着选中的方法实现,理解模型中根据子类查找父类

添加用户权限规则

控制器

public function add()
    {
        if(request()->isPost()){
            $data = input('post.');
            if($data['rules']){
                $data['rules'] = implode(',', $data['rules']);
            }
            $res = db('auth_group')->insert($data);
            if($res){
                $this->success('添加成功',url('index'));
            }else{
                $this->error('添加失败');
            }
        }else{
            //用户列表
            $data = db('manger')->select();
            $this->assign('data',$data);
            //权限列表 主要是无限分列函数的调用
            $AuthRuleModel = new AuthRuleModel();
            $rule = $AuthRuleModel->coltree();
            $this->assign('rule', $rule);
            return view();
        }

    }

模型代码

<?php
namespace app\admin\model;
use think\Model;
class AuthRule extends Model{
    protected $resultSetType = 'collection';
    // 无限分类展示
    public function coltree(){
        //第一步:查询所有数据
        $col=$this->order('sort desc')->select();
        //第二步:调用改造数组方法:参入数组、默认根节点0遍历、默认偏移量0
        return $this->sort($col);
    }
    // 改造数组:参入数组、默认根节点0遍历、默认偏移量0
    public function sort($data,$pid=0){
        //第三步:定义static静态数组
        /*static关键字的作用如下:
        1、放在函数内部修饰变量;
        2、放在类里修饰属性或方法;
        3、放在类的方法里修饰变量;
        4、修饰全局作用域的变量;*/
        static $arr=array();
        //第四步:foreach遍历数组集
        foreach ($data as $key => $value) {
            //第五步:判断$pid与数组遍历pid是否相等
            //从第一次$pid=0开始查找,找到后存入新数组$arr中
            if($value['pid']==$pid){
                //第五步03:偏移量与分类名连接显示突出显示层级关系
//                $value['level']=$level;
//                $value['title']= '|-'.str_repeat('-',$level).$value['title'];
                //第五步01:从第一次$pid=0开始查找,找到后存入新数组$arr中
                //权限管理中查询父类id
                $value['dataid'] = $this->getParentid($value['id']);
                $arr[]=$value;
                //第五步02:存入新数组后回调函数自己
                //思考:回调后$data如果再次循环,会不会把原放入$arr的数组再次放入出现重复?
                //答案是不会,解决办法在第二个参数$value['id']
                $this->sort($data,$value['id']);
            }
        }
        //第六步:返回改造好的新数组
        return $arr;
    }

    //获取父分类id,实现选中子类自动选中所有父分类
    // 获取父分类id
    public function getParentid($did){
        $AuthRule=$this->select();
        return $this->_getParentid($AuthRule,$did,true);
    }
    public function _getParentid($AuthRule,$did,$clear=false){
        static $arr=array();
        if($clear){
            $arr=array();
        }
        foreach ($AuthRule as $key => $value) {
            if($value['id']==$did){
                $arr[]=$value['id'];
                $this->_getParentid($AuthRule,$value['pid']);
            }
        }
        asort($arr);
        return $str=implode('-',$arr);
    }

}

前端模板(主要在js实现多选)

{layout name="public/layout" /}
<div class="col-md-10">
    <ol class="breadcrumb">
        <li><a href="#"><span class="glyphicon glyphicon-home"></span>&nbsp;首页</a></li>
        <li><a href="#">权限管理</a></li>
        <li class="active">添加权限</li>
        <a href="" style="float:right;height:25px;" class="btn btn-success"><span class="glyphicon glyphicon-refresh"></span></a>
        <span style="clear:both"></span>
    </ol>
    <div class="panel panel-default">
        <div class="panel-body">
            <form action="" method="POST">
                <div class="form-group">
                    <label for="">管理员名称</label>
                    <select name="title" class="form-control">
                        {volist name="data" id="data"}
                            <option value="{$data.id}">{$data.username}</option>
                        {/volist}
                    </select>
                </div>
                <div class="form-group">
                    <label for="">状态:</label>
                    <label class="radio-inline">
                        <input type="radio" name="status" id="" value="0"> 禁用
                    </label>
                    <label class="radio-inline">
                        <input type="radio" name="status" id="" value="1" checked> 开启
                    </label>
                </div>
                <div class="form-group">
                    <label for="">规则</label>
                    <table>
                        {volist name="rule" id="tree"}
                        <tr>
                            <td>
                                <?php echo str_repeat("&nbsp;", $tree['level']*5) ?>
                                <input type="checkbox" dataid="{$tree.dataid}" value="{$tree.id}" class="checkbox-parent {$tree.level==0?'':'checkbox-child'}" name="rules[]">
                                <span>{$tree.title}</span>
                            </td>
                        </tr>
                        {/volist}
                    </table>
                </div>
                <div class="form-group">
                    <input value="提交" class="btn btn-success" type="submit">
                    <input type="reset" value="重置" class="btn btn-danger">
                </div>
            </form>
        </div>
        <div class="panel-footer">

        </div>
    </div>
</div>
<script>
    /* 权限配置 */
    $(function () {
        //动态选择框,上下级选中状态变化
        $('input.checkbox-parent').on('change', function () {
            var dataid = $(this).attr("dataid");
            $('input[dataid^=' + dataid + ']').prop('checked', $(this).is(':checked'));
        });
        $('input.checkbox-child').on('change', function () {
            var dataid = $(this).attr("dataid");
            dataid = dataid.substring(0, dataid.lastIndexOf("-"));
            var parent = $('input[dataid=' + dataid + ']');
            if ($(this).is(':checked')) {
                parent.prop('checked', true);
            } else {
                //父级
                if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
                    parent.prop('checked', false);
                }
            }
        });
    });

</script>

效果图
在这里插入图片描述

展示列表

控制器

public function index()
    {
        $data = db('auth_group')->select();
        $this->assign('data', $data);
        return view();
    }

前端模板

{layout name="public/layout" /}
<div class="col-md-10">
	<ol class="breadcrumb">
		<li><a href="#"><span class="glyphicon glyphicon-home"></span>&nbsp;首页</a></li>
		<li><a href="#">权限管理</a></li>
		<li class="active">权限列表</li>
		<a href="" style="float:right;height:25px;" class="btn btn-success"><span class="glyphicon glyphicon-refresh"></span></a>
		<span style="clear:both"></span>
	</ol>
	<div class="panel panel-default">
		<div class="panel-heading">
			<button class="btn btn-primary" ><span class="glyphicon glyphicon-plus"></span>&nbsp;&nbsp;<a href="{:url('add')}" style="color:white">添加权限</a></button>
		</div>
		<div class="panel-body">
			<table class="table table-bordered table-hover">
				<tr>
					<th>ID</th>
					<th>管理员名称</th>
					<th>状态</th>
					<th>操作</th>
				</tr>
				{volist name="data" id="vo"}
				<tr>
					<td>{$vo.id}</td>
					<td>{$vo.title}</td>
					<td>{$vo.status==1?'启用':'禁用'}</td>
					<td>
						<a class="glyphicon glyphicon-trash" href="{:url('del',array('id'=>$vo.id))}"></a>&nbsp;&nbsp;|&nbsp;&nbsp; <a class="glyphicon glyphicon-tag" href="{:url('update',array('id'=>$vo.id))}"></a>
					</td>
				</tr>
				{/volist}
			</table>
		</div>
		<div class="panel-footer">
		</div>
	</div>
</div>

效果图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值