PHP无限极菜单
程序员文章站
2022-05-18 20:10:28
权限表结构 相关代码 打印结果 ......
权限表结构
create table `blog_auth` ( `id` int(11) unsigned not null auto_increment comment '序号', `pid` int(11) not null comment '父级id', `auth_name` varchar(191) not null comment '权限名字', `auth_title` varchar(191) not null comment '权限标题', `auth_icon` varchar(191) default null comment '权限图标', `auth_rule` varchar(191) not null comment '权限路径', `menu` tinyint(1) not null comment '菜单 1=>显示菜单,0=>不显示菜单', `debug` tinyint(1) not null comment '调试模式 1-->仅调试模式显示,0-->任何时候都显示', `create_by` int(11) not null comment '创建人', `update_by` int(11) not null comment '更新人', `create_time` int(11) not null comment '创建时间', `update_time` int(11) not null comment '更新时间', `delete_time` int(11) not null comment '删除时间', `sort` int(11) not null comment '排序字段', `status` tinyint(1) not null comment '状态', primary key (`id`) using btree ) engine=myisam auto_increment=23 default charset=utf8 row_format=dynamic;
相关代码
/** * @title 菜单列表 * @param int $uid * @return array * @throws \think\db\exception\datanotfoundexception * @throws \think\db\exception\modelnotfoundexception * @throws \think\exception\dbexception */ public function getmenulist($uid = 0) {//展示只有菜单类型的权限列表 $auth_list_info = db::name('auth')->field(['id','pid','auth_title','auth_rule'])->select(); if ($auth_list_info === null){return [];} $auth_list_info = is_array($auth_list_info) ? $auth_list_info : $auth_list_info->toarray(); return $this->getsonsinfo($auth_list_info); } /** * @title 菜单树生成 * @param array $items - 数据 * @param int $pid - 父级id的值 * @param int $deep - 深度 * @return array */ protected function getsonsinfo(array $items,$pid=0,$deep=0) { $lists = []; foreach ($items as $item){ if ($item['pid'] === $pid){ $item['deep'] = $deep; $item['son'] = $this->getsonsinfo($items,$item['id'],$deep+1); $lists[] = $item; } } return $lists; }
打印结果
array ( [0] => array ( [id] => 1 [pid] => 0 [auth_title] => 博客后台人员账户 [auth_rule] => backend/account/default [deep] => 0 [son] => array ( [0] => array ( [id] => 2 [pid] => 1 [auth_title] => 博客后台人员账户列表页 [auth_rule] => backend/account/index [deep] => 1 [son] => array ( ) ) ) ) [1] => array ( [id] => 9 [pid] => 0 [auth_title] => 博客后台首页 [auth_rule] => backend/index/default [deep] => 0 [son] => array ( [0] => array ( [id] => 10 [pid] => 9 [auth_title] => 博客后台首页 [auth_rule] => backend/index/index [deep] => 1 [son] => array ( ) ) ) ) [2] => array ( [id] => 19 [pid] => 0 [auth_title] => 博客后台文章管理 [auth_rule] => backend/article/default [deep] => 0 [son] => array ( [0] => array ( [id] => 20 [pid] => 19 [auth_title] => 博客后台文章列表页 [auth_rule] => backend/article/index [deep] => 1 [son] => array ( ) ) ) ) )