|
|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
+use App\Authority;
|
|
|
use App\Traits\ServiceAppAop;
|
|
|
use App\Menu;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
@@ -43,11 +44,11 @@ class MenuService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public function getVisibleFunctionList()
|
|
|
+ //菜单与权限关联依据为 name与parent_id字段 当两个表中任意此字段发生变更 必须刷掉缓存
|
|
|
+ public function getVisibleFunctionList($authorities = null,&$mapping = [])
|
|
|
{
|
|
|
/** @var Collection $authorities */
|
|
|
- $authorities = app("AuthorityService")->getUserAuthority();
|
|
|
- if (!$authorities)$authorities = [];
|
|
|
+ if (!$authorities)$authorities = app("AuthorityService")->getUserAuthority();
|
|
|
$authMap = [];
|
|
|
foreach ($authorities as $authority){
|
|
|
$item = $authority->toArray();
|
|
|
@@ -65,6 +66,7 @@ class MenuService
|
|
|
$menuMap = [];
|
|
|
foreach ($menus as $menu){
|
|
|
$menuMap[$menu->id] = [
|
|
|
+ "id" => $menu->id,
|
|
|
"child" => [],
|
|
|
"name" => $menu->name,
|
|
|
"font" => $menu->font,
|
|
|
@@ -78,7 +80,7 @@ class MenuService
|
|
|
unset($menuMap[$menu->id]);
|
|
|
}
|
|
|
}
|
|
|
- return array_values($this->formatMenu($menuMap,$authMap));
|
|
|
+ return array_values($this->formatMenu($menuMap,$authMap,$mapping));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -114,17 +116,18 @@ class MenuService
|
|
|
*
|
|
|
* @param array $menus
|
|
|
* @param array $authorities
|
|
|
+ * @param array $mapping
|
|
|
*
|
|
|
* @return array|bool
|
|
|
*/
|
|
|
- private function formatMenu(array $menus,array $authorities)
|
|
|
+ private function formatMenu(array $menus,array $authorities,&$mapping=[])
|
|
|
{
|
|
|
foreach ($menus as $index=>$menu){
|
|
|
$mark = false;
|
|
|
foreach ($authorities as $j=>$authority){
|
|
|
if ($menu["name"] == $authority["name"]){
|
|
|
+ $mapping[$menu["id"]] = $authority["id"];
|
|
|
$mark = true;
|
|
|
- $menus[$index]["authority"] = $j;
|
|
|
$authorities[$j]["mark"] = true;
|
|
|
if ($authority["child"])$menus[$index]["child"] = array_values($this->formatMenu($menu["child"],$authority["child"]));
|
|
|
}
|
|
|
@@ -141,4 +144,13 @@ class MenuService
|
|
|
}
|
|
|
return $menus;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取菜单与权限映射
|
|
|
+ */
|
|
|
+ public function getMenuAndAuthorityMapping()
|
|
|
+ {
|
|
|
+ $this->getVisibleFunctionList(Authority::query()->get(),$mapping);
|
|
|
+ return $mapping;
|
|
|
+ }
|
|
|
}
|