164 lines
5.2 KiB
PHP
164 lines
5.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @author Any
|
|
* @description KISS
|
|
* @date 2020-11-4
|
|
* @version 1.0.0
|
|
*
|
|
* _____LOG_____
|
|
*
|
|
*/
|
|
namespace app\modules\store\models;
|
|
|
|
|
|
class AdminMenu
|
|
{
|
|
public function getList($ignore_hide = true)
|
|
{
|
|
$menuList = Menu::getMenu();
|
|
|
|
$menuList = $this->resetList($menuList, $ignore_hide);
|
|
$menuList = $this->deleteEmptyList($menuList);
|
|
|
|
return $menuList;
|
|
}
|
|
|
|
|
|
public function resetList($list, $ignore_hide = true)
|
|
{
|
|
foreach ($list as $i => $item) {
|
|
// if (isset($item['is_show']) && $item['is_show'] !== true && $ignore_hide) {
|
|
// unset($list[$i]);
|
|
// continue;
|
|
// }
|
|
|
|
if (isset($item['children']) && is_array($item['children'])) {
|
|
$list[$i]['children'] = $this->resetList($item['children'], $ignore_hide);
|
|
}
|
|
}
|
|
$list = array_values($list);
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* 当父数组下没有没有一个子元素时,因将该父数组删除
|
|
* @param $menuList
|
|
* @return array
|
|
*/
|
|
public function deleteEmptyList($menuList)
|
|
{
|
|
foreach ($menuList as $i => $item) {
|
|
if(!isset($item['children']))
|
|
continue;
|
|
if (is_array($item['children']) && count($item['children']) == 0) {
|
|
if($item['route'] == null){
|
|
unset($menuList[$i]);
|
|
}
|
|
continue;
|
|
}
|
|
}
|
|
$menuList = array_values($menuList);
|
|
|
|
return $menuList;
|
|
}
|
|
|
|
public function getMenuUrls($menus)
|
|
{
|
|
$urls = [];
|
|
foreach ($menus as $index => $item){
|
|
if(isset($item['route']) && !empty($item['route']))
|
|
array_push ($urls, $item['route']);
|
|
if(isset($item['children']) && count($item['children']) != 0){
|
|
$result = $this->getMenuUrls($item['children']);
|
|
$urls = array_merge($urls, $result);
|
|
}
|
|
if(isset($item['action']) && count($item['action']) != 0){
|
|
$result = $this->getMenuUrls($item['action']);
|
|
$urls = array_merge($urls, $result);
|
|
}
|
|
}
|
|
return $urls;
|
|
}
|
|
|
|
/**
|
|
* 获取菜单权限树
|
|
* @param array $permissions 用户已获取的权限路由集
|
|
* retrun array
|
|
*/
|
|
public function getPermissionTree($permissions)
|
|
{
|
|
$menus = $this->getList(false);
|
|
return $this->resetPermissionTree($menus, $permissions);
|
|
}
|
|
|
|
public function resetPermissionTree($menus, $permissions)
|
|
{
|
|
$tree = [];
|
|
foreach ($menus as $index => $item){
|
|
$_item = [];
|
|
$_item['title'] = $item['name'];
|
|
$_item['id'] = !empty($item['route']) ? md5($item['route']) : md5($item['name']);
|
|
$_item['field'] = $item['route'];
|
|
if(isset($item['children']) && count($item['children']) != 0)
|
|
$_item['children'] = $this->resetPermissionTree($item['children'], $permissions);
|
|
$_item['checked'] = in_array($item['route'], $permissions) ? true : false;
|
|
if(isset($item['action']) && count($item['action']) != 0)
|
|
$_item['children'] = $this->resetPermissionTree($item['action'], $permissions);
|
|
if(isset($_item['children']) && !$item['route'] && count($_item['children']) != 0){
|
|
$checked = true;
|
|
foreach ($_item['children'] as $i => $sub_item){
|
|
if(!$sub_item['checked']){
|
|
$checked = false;
|
|
break;
|
|
}
|
|
}
|
|
$_item['checked'] = $checked;
|
|
}
|
|
array_push($tree, $_item);
|
|
}
|
|
return $tree;
|
|
}
|
|
|
|
/**
|
|
* 根据用户权限获取菜单列表
|
|
* @param array $menus 菜单列表
|
|
* @param array $permissions 用户已有权限路由
|
|
* return array
|
|
*/
|
|
public function getUserMenu($menus, $permissions, $is_super_admin = false)
|
|
{
|
|
if($is_super_admin)
|
|
return $menus;
|
|
$menuList = $this->resetUserMenu($menus, $permissions);
|
|
$menuList = $this->resetList($menuList);
|
|
$menuList = $this->deleteEmptyList($menuList);
|
|
return $menuList;
|
|
}
|
|
|
|
public function resetUserMenu($list, $permissions)
|
|
{
|
|
foreach ($list as $index => $item) {
|
|
// if($item['route'] != null){
|
|
// $item['is_show'] = in_array($item['route'], $permissions) ? true : false;
|
|
// }
|
|
if (isset($item['children']) && is_array($item['children'])) {
|
|
$item['children'] = $this->resetUserMenu($item['children'], $permissions);
|
|
$has_menu = false;
|
|
foreach ($item['children'] as $i => $sub_item){
|
|
if($sub_item['is_show']){
|
|
$has_menu = true;
|
|
break;
|
|
}
|
|
}
|
|
if(!$has_menu && $item['route'] == null){
|
|
$item['is_show'] = false;
|
|
}
|
|
}
|
|
$list[$index] = $item;
|
|
}
|
|
$list = array_values($list);
|
|
return $list;
|
|
}
|
|
|
|
} |