MenuServer.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Servers\Common;
  3. use App\Models\SysModels\Menu;
  4. use App\Models\UserModels\Role;
  5. /**
  6. * 后台菜单路由
  7. */
  8. class MenuServer
  9. {
  10. /**
  11. * 单列对象
  12. * @var
  13. */
  14. private static $server;
  15. private function __construct()
  16. {
  17. }
  18. /**
  19. * 创建对象
  20. * @return MenuServer
  21. */
  22. static function creatServer()
  23. {
  24. if (empty(self::$server)) {
  25. self::$server = new MenuServer();
  26. }
  27. return self::$server;
  28. }
  29. /**
  30. * 获取后台菜单
  31. * @return \Illuminate\Http\JsonResponse
  32. */
  33. function getMenuList($role_id = ''){
  34. if($role_id){
  35. //获取该角色的权限节点
  36. $permission_ids = Role::where('id', $role_id)->value('permission_ids');
  37. $url_ids = explode(',', $permission_ids);
  38. }else{
  39. $url_ids = [];
  40. }
  41. //获取数据
  42. $list = Menu::where([['p_id',0], ['level',1]])
  43. ->select(['id as value','menu_name as label'])
  44. ->orderBy('sort','asc')
  45. ->get();
  46. foreach ($list as $value){
  47. $value['is_check'] = in_array($value['value'],$url_ids) ? 1 : 0;
  48. $value['children'] = Menu::where([['level',2], ['p_id',$value['value']]])->orderBy('sort', 'asc')->select(['id as value','menu_name as label'])->get();
  49. foreach ($value['children'] as $item){
  50. $item['is_check'] = in_array($item['value'],$url_ids) ? 1 : 0;
  51. $item['children'] = Menu::where([['level',3], ['p_id',$item['value']]])->orderBy('sort', 'asc')->select(['id as value','menu_name as label'])->get();
  52. foreach ($item['children'] as $three){
  53. $three['is_check'] = in_array($three['value'],$url_ids) ? 1 : 0;
  54. }
  55. }
  56. }
  57. return $list;
  58. }
  59. /**
  60. * 获取系统后台角色对应权限节点
  61. * @param $role_id
  62. * @return false|mixed|string[]
  63. */
  64. function getRole($role_id){
  65. //获取该角色的权限节点
  66. $key_name = 'gw_role_ids_' . $role_id;
  67. $url_ids = RedisDataServer::creatServer()->getData($key_name,'json');
  68. if(empty($url_ids)){
  69. $permission_ids = Role::where('id', $role_id)->value('permission_ids');
  70. $url_ids = explode(',', $permission_ids);
  71. RedisDataServer::creatServer()->setData($key_name,$url_ids,'json',7200);
  72. }
  73. if (empty($url_ids)) {
  74. return false;
  75. }
  76. return $url_ids;
  77. }
  78. }