if (!function_exists("tier")){
//封闭方法
function tier($arr,$pid=0,$level=1){
//建立空数组
static $res=[];
//循环
foreach ($arr as $val){
//判断
if ($val['pid']==$pid){
$val['level']=$level;
//存入数组中
$res[]=$val;
//递归调用查找后代
tier($arr,$val['id'],$level+1);
}
}
return $res;
}
}
if(!function_exists('get_tree_list')){
//引用方式实现 父子级树状结构
function get_tree_list($list){
//将每条数据中的id值作为其下标
$temp = [];
foreach($list as $v){
$v['son'] = [];
$temp[$v['id']] = $v;
}
//获取分类树
foreach($temp as $k=>$v){
$temp[$v['pid']]['son'][] = &$temp[$v['id']];
}
return isset($temp[0]['son']) ? $temp[0]['son'] : [];
}
}
public function nodeList()
{
// $data=User::with(['Role','Role.Node'])->get()->toArray();
$res=Node::nodeList()->toArray();
// dd($res);
$data=tier($res);
return view('node/admin-permission',compact('data'));
}
public function roleAddShow()
{
// $res=User::with(['Role','Role.Node'])->get()->toArray();
$res=Node::nodeList()->toArray();
$data=get_tree_list($res);
return view('role/admin-role-add',compact('data'));
}
public function roleUpDateShow($id)
{
// 查询id角色表
$res=Role::roleUpDateShow($id);
// 查询中间表
$data=RoleNode::selectNode($id)->toArray();
$arr=[];
foreach ($data as $val){
$arr[]=['node_id'=>$val['node_id']];
}
// 查询选中权限
$nodeChecked=Node::selectNodeChecked($arr)->toArray();
$nodeChecked=array_column($nodeChecked,"id");
// 查询所有权限
$node=Node::selectNodeId()->toArray();
$nodeList=get_tree_list($node);
return view('role/admin-role-update',compact('res','nodeList','nodeChecked'));
}