Skip to content
关注公众号,获取新课通知

权限列表


控制器 app/controller/admin/Rule.php

php
public function index()
{
	$param = $this->request->param();
	$limit = intval(getValByKey('limit',$param,100));
	$page = getValByKey('page',$param,1);
	$totalCount = $this->M->count();
	$list = $this->M->page($page,$limit)->order([
		'id'=>'desc'
	])->select();
    return showSuccess([
		'list'=>list_to_tree2($list->toArray(),'rule_id'),
		'totalCount'=>$totalCount,
		'rules'=>list_to_tree($list->toArray(),'rule_id')
	]);
}

公共函数 app/common.php

php
/**
* 数据集组合分类树(一维数组)
* @param     cate 分类查询结果集
* @param     html 格式串
* @return    array
*/
function list_to_tree($array,$field = 'pid',$pid = 0,$level = 0){
    //声明静态数组,避免递归调用时,多次声明导致数组覆盖
    static $list = [];
    foreach ($array as $key => $value){
        if ($value[$field] == $pid){
            $value['level'] = $level;
            $list[] = $value;
            unset($array[$key]);
            list_to_tree($array,$field,$value['id'], $level+1);
        }
    }
    return $list;
}

/**
* 数据集组合分类树(多维数组)
* @param     cate 分类结果集
* @param     child 子树
* @return    array
*/
function list_to_tree2($cate,$field = 'pid',$child = 'child',$pid = 0,$callback = false){
	if(!is_array($cate)) return [];
    $arr = [];
    foreach($cate as $v){
    	$extra = true;
    	if(is_callable($callback)){
        	$extra = $callback($v);
        }
        if($v[$field] == $pid && $extra){
            $v[$child] = list_to_tree2($cate,$field,$child,$v['id'],$callback);
            $arr[]     = $v;
        }
    }
    return $arr;
}

路由 router/admin.php

php
Route::get('rule/:page','admin.Rule/index');

验证器 app/validate/admin/Rule.php

php
protected $rule = [
    'page' => 'require|integer|>:0',
];
protected $scene = [
    ...
    'index'=>['page'],
    ...
];