MenuServer.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Servers;
  3. use App\Models\Menu;
  4. use App\Models\Role;
  5. /**
  6. * Redis数据缓存类
  7. */
  8. class MenuServer
  9. {
  10. /**
  11. * 单列对象
  12. * @var
  13. */
  14. static $server;
  15. /**
  16. * 缓存key
  17. * @var string
  18. */
  19. private $key_name = 'menu_list';
  20. /**
  21. * 缓存时间
  22. * @var string
  23. */
  24. private $cache_time = '10';
  25. private function __construct()
  26. {
  27. }
  28. /**
  29. * 创建对象
  30. * @return MenuServer
  31. */
  32. static function creatServer()
  33. {
  34. if (empty(self::$server)) {
  35. self::$server = new MenuServer();
  36. }
  37. return self::$server;
  38. }
  39. /**
  40. * 获取后台菜单信息
  41. * @param $roles_id
  42. * @return array|mixed
  43. */
  44. function getMenu($roles_id)
  45. {
  46. $key_name = $this->key_name . '_' . $roles_id;
  47. $list = RedisDataServer::creatServer()->getData($key_name, 'json');
  48. if (empty($list)) {
  49. if ($roles_id > 0) {
  50. //获取对应角色授权ID
  51. $permission_ids = Role::where('id', $roles_id)->value('permission_ids');
  52. if (empty($permission_ids)) $permission_ids = [0];
  53. else $permission_ids = explode(',', $permission_ids);
  54. } else {
  55. $permission_ids = [];
  56. }
  57. $list = Menu::where('p_id', 0)->where('is_del',0)->where('version',1)->where(function ($query) use ($permission_ids) {
  58. if ($permission_ids) {
  59. $p_ids= Menu::whereIn('id', $permission_ids)->where('level',1)->pluck('p_id')->toArray();
  60. if(empty($p_ids))$p_ids=[0];
  61. $query->whereIn('id', $p_ids);
  62. }
  63. })->select(['id', 'menu_name', 'label_name'])->orderBy('sort','desc')->get()->toArray();
  64. foreach ($list as &$item) {
  65. $item['items'] = Menu::where('p_id', $item['id'])->where('is_del',0)->where('version',1)->where(function ($query) use ($permission_ids) {
  66. if ($permission_ids) $query->whereIn('id', $permission_ids);
  67. })->select(['id', 'menu_name', 'url_name'])->orderBy('sort','desc')->get()->toArray();
  68. }
  69. RedisDataServer::creatServer()->setData($key_name, $list, 'json', $this->cache_time);
  70. }
  71. return $list;
  72. }
  73. /**
  74. * 根据角色传递菜单信息
  75. * @param $roles_id
  76. */
  77. function sendMenu($roles_id = '')
  78. {
  79. if (!request()->ajax()) {
  80. view()->composer('layouts.admin.menu', function ($view) use ($roles_id) {
  81. $menu_list = self::creatServer()->getMenu($roles_id);
  82. $view->with('menu_list', $menu_list);
  83. });
  84. }
  85. }
  86. function getPermissionsMenu($r_p)
  87. {
  88. //当前角色已授权信息
  89. if (is_string($r_p)) {
  90. $r_p = explode(',', $r_p);
  91. }
  92. $permission=[];
  93. //权限信息
  94. $one_p = Menu::where('p_id', 0)->select(['id', 'menu_name as label'])->get();
  95. foreach ($one_p as $key => $item) {
  96. $item->{'id'}.='';
  97. $two_list = Menu::where('p_id', $item['id'])->select(['id', 'menu_name as label'])->get();
  98. foreach ($two_list as $t_v) {
  99. $t_v->{'id'}.='';
  100. $three_list= Menu::where('p_id', $t_v['id'])->select(['id', 'menu_name as label'])->get();
  101. foreach ($three_list as $three_v){
  102. $three_v->{'id'}.='';
  103. }
  104. $t_v->{'children'}=$three_list->toArray();
  105. }
  106. $item->{'children'}=$two_list->toArray();
  107. $permission[]=[$item->toArray()];
  108. }
  109. return $permission;
  110. }
  111. /**
  112. * 获取系统后台角色对应权限节点
  113. * @param $role_id
  114. * @return false|mixed|string[]
  115. */
  116. function getRole($role_id){
  117. //获取该角色的权限节点
  118. $key_name = 'role_ids_' . $role_id;
  119. $url_ids = RedisDataServer::creatServer()->getData($key_name,'json');
  120. if(empty($url_ids)){
  121. $permission_ids = Role::where('id', $role_id)->value('permission_ids');
  122. $url_ids = explode(',', $permission_ids);
  123. RedisDataServer::creatServer()->setData($key_name,$url_ids,'json',7200);
  124. }
  125. if (empty($url_ids)) {
  126. return false;
  127. }
  128. return $url_ids;
  129. }
  130. }