| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- namespace App\Services;
- use App\Authority;
- use App\Traits\ServiceAppAop;
- use App\Menu;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Support\Facades\Cache;
- class MenuService
- {
- use ServiceAppAop;
- protected $modelClass=Menu::class;
- public function getMenu()
- {
- if (!Cache::has("menus")){
- if (Cache::lock("menus",2)){
- $this->setMenu();
- Cache::lock("menus")->release();
- }else{
- sleep(1);
- return Cache::get("menus") ?? [];
- }
- }
- return Cache::get("menus");
- }
- public function setMenu()
- {
- //如果需要热更新 为所有用户个人菜单锁定缓存 标记TAGS 在此清除 Cache::tag("tags")->flush()
- Cache::forever("menus",Menu::query()
- ->select("id","name","parent_id","route","font","font_style")
- ->orderByRaw("level DESC,sequence")->get());
- }
- public function appendMenu(Menu $menu)
- {
- $menus = $this->getMenu();
- if ($menus){
- $menus->add($menu);
- Cache::forever("menus",$menus);
- }
- }
- //菜单与权限关联依据为 name与parent_id字段 当两个表中任意此字段发生变更 必须刷掉缓存
- public function getVisibleFunctionList($authorities = null,&$mapping = [])
- {
- /** @var Collection $authorities */
- if (!$authorities)$authorities = app("AuthorityService")->getUserAuthority();
- $authMap = app("AuthorityService")->format($authorities);
- $menus = $this->format($this->getMenu());
- return array_values($this->formatMenu($menus,$authMap,$mapping));
- }
- /**
- * 格式化为树状结构
- *
- * @param Collection $menus
- * @return array
- */
- public function format($menus)
- {
- $menuMap = [];
- foreach ($menus as $menu){
- $menuMap[$menu->id] = [
- "id" => $menu->id,
- "child" => [],
- "name" => $menu->name,
- "font" => $menu->font,
- "fontStyle" => $menu->font_style,
- "route" => $menu->route,
- ];
- }
- foreach ($menus as $menu){
- if ($menu->parent_id){
- $menuMap[$menu->parent_id]["child"][] = $menuMap[$menu->id];
- unset($menuMap[$menu->id]);
- }
- }
- return $menuMap;
- }
- /**
- * 递归格式化菜单组
- *
- * @param array $menus
- * @param array $authorities
- * @param array $mapping
- *
- * @return array|bool
- */
- 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;
- $authorities[$j]["mark"] = true;
- if ($authority["child"])$menus[$index]["child"] = array_values($this->formatMenu($menu["child"],$authority["child"],$mapping));
- }
- }
- if (!$mark)unset($menus[$index]);
- }
- foreach ($authorities as $authority){
- if (!isset($authority["mark"])){
- $menus[] = [
- "name" => $authority["name"],
- "route" => "",
- ];
- }
- }
- return $menus;
- }
- /**
- * 获取菜单与权限映射
- */
- public function getMenuAndAuthorityMapping()
- {
- $this->getVisibleFunctionList(Authority::query()->get(),$mapping);
- return $mapping;
- }
- /**
- * 获取唯一名称
- */
- public function getUniqueName(Menu $menu)
- {
- if (!$menu->parent_id)return $menu->name;
- $menus = $this->getMenu();
- $menuMap = [];
- foreach ($menus as $m)$menuMap[$m->id] = $m;
- $names = [];
- while ($menu){
- $names[] = $menu->name;
- if ($menu->parent_id)$menu = $menuMap[$menu->parent_id] ?? null;
- else $menu = null;
- }
- $name = "";
- for ($i=count($names)-1;$i>=0;$i--){
- $name .= $names[$i];
- if ($i!=0)$name .= "-";
- }
- return $name;
- }
- }
|