效果:
{
"errcode": 1,
"errmsg": "ok",
"result": {
"id": "129365a2-bc2c-11ea-4dd1-000c29547b13",
"company_id": "68753A44-4D6F-1226-9C60-0050E4C00067",
"user_account": "admin",
"user_name": "zha7ngs",
"user_gender": 1,
"user_mobile": "17718327230",
"user_phone": "",
"user_email": "24@qq.com",
"user_avatar": "2",
"privilege_list": [
{
"privilege_id": "678f6dae-c018-11ea-bdd1-000c29547b13",
"privilege_caption": "0a3dsb1w3",
"privilege_type": 2,
"privilege_sort": 10,
"privilege_url": "123456789123456789",
"privilege_icon": "3",
"pid": "5847df16-c018-11ea-9344-000c29547b13",
"submunu": [
{
"privilege_id": "44ff3754-c02e-11ea-bdd1-000c29547b13",
"privilege_caption": "b1w3",
"privilege_type": 2,
"privilege_sort": 10,
"privilege_url": "123456789123456789",
"privilege_icon": "1",
"pid": "678f6dae-c018-11ea-bdd1-000c29547b13",
"submunu": [
{
"privilege_id": "686274b6-c030-11ea-9344-000c29547b13",
"privilege_caption": "b1ws3",
"privilege_type": 2,
"privilege_sort": 10,
"privilege_url": "123456789123456789",
"privilege_icon": "我s223",
"pid": "44ff3754-c02e-11ea-bdd1-000c29547b13"
}
]
},
{
"privilege_id": "73938518-c018-11ea-9344-000c29547b13",
"privilege_caption": "0fdsb1w3",
"privilege_type": 2,
"privilege_sort": 10,
"privilege_url": "123456789123456789",
"privilege_icon": "4",
"pid": "678f6dae-c018-11ea-bdd1-000c29547b13"
}
]
},
{
"privilege_id": "80524c76-c018-11ea-bdd1-000c29547b13",
"privilege_caption": "0ffd1w3",
"privilege_type": 2,
"privilege_sort": 10,
"privilege_url": "123456789123456789",
"privilege_icon": "8",
"pid": "0",
"submunu": [
{
"privilege_id": "8ffd3988-c018-11ea-8680-000c29547b13",
"privilege_caption": "0ffdf1w3",
"privilege_type": 2,
"privilege_sort": 10,
"privilege_url": "123456789123456789",
"privilege_icon": "5",
"pid": "80524c76-c018-11ea-bdd1-000c29547b13"
}
]
}
],
"role_list": "[\"a\", \"b\"]",
"department_list": "[\"1\", \"2\"]",
"create_time": "2020-07-02 14:20:06",
"create_user": "admin",
"is_frozen": 0
}
}
代码:
<?php
namespace app\test\controller;
use think\Controller;
/**
* 临时代码调试专用控制器
*/
class Test extends Controller {
//属性声明
private $model = null;
//方法声明
/**
* 构造方法
* @param \think\Request $request
*/
public function __construct(\think\Request $request = null) {
parent::__construct($request);
}
/**
* 入口测试方法
*/
public function index() {
$data = [
['id' => 1, 'name' => 'n1', 'pid' => 0],
['id' => 4, 'name' => 'n1-1', 'pid' => 1],
['id' => 5, 'name' => 'n1-2', 'pid' => 1],
['id' => 2, 'name' => 'n2', 'pid' => 0],
['id' => 3, 'name' => 'n3', 'pid' => 0],
['id' => 6, 'name' => 'n1-1-1', 'pid' => 4],
['id' => 7, 'name' => 'n1-1-2', 'pid' => 4],
['id' => 8, 'name' => 'n1-1-2-1', 'pid' => 7],
];
$r = $this -> generateTree($data);
p($r);
}
/**
* 利用引用算法获取无限级权限树
* @param type $data
* @return type
*/
public function generateTree($data) {
$items = array();
foreach ($data as $v) {
$items[$v['id']] = $v;
}
$tree = array();
foreach ($items as $k => $item) {
if (isset($items[$item['pid']])) {
$items[$item['pid']]['son'][] = &$items[$k];
} else {
$tree[] = &$items[$k];
}
}
return $tree;
}
}
网友评论