123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace App\Servers;
- use App\Models\Menu;
- use App\Models\Role;
- /**
- * Redis数据缓存类
- */
- class MenuServer
- {
- /**
- * 单列对象
- * @var
- */
- static $server;
- /**
- * 缓存key
- * @var string
- */
- private $key_name = 'menu_list';
- /**
- * 缓存时间
- * @var string
- */
- private $cache_time = '10';
- private function __construct()
- {
- }
- /**
- * 创建对象
- * @return MenuServer
- */
- static function creatServer()
- {
- if (empty(self::$server)) {
- self::$server = new MenuServer();
- }
- return self::$server;
- }
- /**
- * 获取后台菜单信息
- * @param $roles_id
- * @return array|mixed
- */
- function getMenu($roles_id)
- {
- $key_name = $this->key_name . '_' . $roles_id;
- $list = RedisDataServer::creatServer()->getData($key_name, 'json');
- if (empty($list)) {
- if ($roles_id > 0) {
- //获取对应角色授权ID
- $permission_ids = Role::where('id', $roles_id)->value('permission_ids');
- if (empty($permission_ids)) $permission_ids = [0];
- else $permission_ids = explode(',', $permission_ids);
- } else {
- $permission_ids = [];
- }
- $list = Menu::where('p_id', 0)->where('is_del',0)->where('version',1)->where(function ($query) use ($permission_ids) {
- if ($permission_ids) {
- $p_ids= Menu::whereIn('id', $permission_ids)->where('level',1)->pluck('p_id')->toArray();
- if(empty($p_ids))$p_ids=[0];
- $query->whereIn('id', $p_ids);
- }
- })->select(['id', 'menu_name', 'label_name'])->orderBy('sort','desc')->get()->toArray();
- foreach ($list as &$item) {
- $item['items'] = Menu::where('p_id', $item['id'])->where('is_del',0)->where('version',1)->where(function ($query) use ($permission_ids) {
- if ($permission_ids) $query->whereIn('id', $permission_ids);
- })->select(['id', 'menu_name', 'url_name'])->orderBy('sort','desc')->get()->toArray();
- }
- RedisDataServer::creatServer()->setData($key_name, $list, 'json', $this->cache_time);
- }
- return $list;
- }
- /**
- * 根据角色传递菜单信息
- * @param $roles_id
- */
- function sendMenu($roles_id = '')
- {
- if (!request()->ajax()) {
- view()->composer('layouts.admin.menu', function ($view) use ($roles_id) {
- $menu_list = self::creatServer()->getMenu($roles_id);
- $view->with('menu_list', $menu_list);
- });
- }
- }
- function getPermissionsMenu($r_p)
- {
- //当前角色已授权信息
- if (is_string($r_p)) {
- $r_p = explode(',', $r_p);
- }
- $permission=[];
- //权限信息
- $one_p = Menu::where('p_id', 0)->select(['id', 'menu_name as label'])->get();
- foreach ($one_p as $key => $item) {
- $item->{'id'}.='';
- $two_list = Menu::where('p_id', $item['id'])->select(['id', 'menu_name as label'])->get();
- foreach ($two_list as $t_v) {
- $t_v->{'id'}.='';
- $three_list= Menu::where('p_id', $t_v['id'])->select(['id', 'menu_name as label'])->get();
- foreach ($three_list as $three_v){
- $three_v->{'id'}.='';
- }
- $t_v->{'children'}=$three_list->toArray();
- }
- $item->{'children'}=$two_list->toArray();
- $permission[]=[$item->toArray()];
- }
- return $permission;
- }
- /**
- * 获取系统后台角色对应权限节点
- * @param $role_id
- * @return false|mixed|string[]
- */
- function getRole($role_id){
- //获取该角色的权限节点
- $key_name = '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;
- }
- }
|