1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace App\Servers\Common;
- use App\Models\SysModels\Menu;
- use App\Models\UserModels\Role;
- /**
- * 后台菜单路由
- */
- class MenuServer
- {
- /**
- * 单列对象
- * @var
- */
- private static $server;
- private function __construct()
- {
- }
- /**
- * 创建对象
- * @return MenuServer
- */
- static function creatServer()
- {
- if (empty(self::$server)) {
- self::$server = new MenuServer();
- }
- return self::$server;
- }
- /**
- * 获取后台菜单
- * @return \Illuminate\Http\JsonResponse
- */
- function getMenuList($role_id = ''){
- if($role_id){
- //获取该角色的权限节点
- $permission_ids = Role::where('id', $role_id)->value('permission_ids');
- $url_ids = explode(',', $permission_ids);
- }else{
- $url_ids = [];
- }
- //获取数据
- $list = Menu::where([['p_id',0], ['level',1]])
- ->select(['id as value','menu_name as label'])
- ->orderBy('sort','asc')
- ->get();
- foreach ($list as $value){
- $value['is_check'] = in_array($value['value'],$url_ids) ? 1 : 0;
- $value['children'] = Menu::where([['level',2], ['p_id',$value['value']]])->orderBy('sort', 'asc')->select(['id as value','menu_name as label'])->get();
- foreach ($value['children'] as $item){
- $item['is_check'] = in_array($item['value'],$url_ids) ? 1 : 0;
- $item['children'] = Menu::where([['level',3], ['p_id',$item['value']]])->orderBy('sort', 'asc')->select(['id as value','menu_name as label'])->get();
- foreach ($item['children'] as $three){
- $three['is_check'] = in_array($three['value'],$url_ids) ? 1 : 0;
- }
- }
- }
- return $list;
- }
- /**
- * 获取系统后台角色对应权限节点
- * @param $role_id
- * @return false|mixed|string[]
- */
- function getRole($role_id){
- //获取该角色的权限节点
- $key_name = 'gw_role_ids_' . $role_id;
- $url_ids = RedisDataServer::creatServer()->getData($key_name,'json');
- if(empty($url_ids)){
- $permission_ids = Role::where('id', $role_id)->value('permission_ids');
- $url_ids = explode(',', $permission_ids);
- RedisDataServer::creatServer()->setData($key_name,$url_ids,'json',7200);
- }
- if (empty($url_ids)) {
- return false;
- }
- return $url_ids;
- }
- }
|