SystemMenus.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/02
  6. */
  7. namespace app\admin\model\system;
  8. use traits\ModelTrait;
  9. use basic\ModelBasic;
  10. use think\Url;
  11. /**
  12. * 菜单 model
  13. * Class SystemMenus
  14. * @package app\admin\model\system
  15. */
  16. class SystemMenus extends ModelBasic
  17. {
  18. use ModelTrait;
  19. public static $isShowStatus = [1=>'显示',0=>'不显示'];
  20. public static $accessStatus = [1=>'管理员可用',0=>'管理员不可用'];
  21. public static function legalWhere($where = [])
  22. {
  23. $where['is_show'] = 1;
  24. }
  25. public function setParamsAttr($value)
  26. {
  27. $value = $value ? explode('/',$value) : [];
  28. $params = array_chunk($value,2);
  29. $data = [];
  30. foreach ($params as $param){
  31. if(isset($param[0]) && isset($param[1])) $data[$param[0]] = $param[1];
  32. }
  33. return json_encode($data);
  34. }
  35. protected function setControllerAttr($value)
  36. {
  37. return lcfirst($value);
  38. }
  39. public function getParamsAttr($_value)
  40. {
  41. return json_decode($_value,true);
  42. }
  43. public function getPidAttr($value)
  44. {
  45. return !$value ? '顶级' : self::get($value)['menu_name'];
  46. }
  47. public static function getParentMenu($field='*',$filter=false)
  48. {
  49. $where = ['pid'=>0];
  50. $query = self::field($field);
  51. $query = $filter ? $query->where(self::legalWhere($where)) : $query->where($where);
  52. return $query->order('sort DESC')->select();
  53. }
  54. public static function menuList()
  55. {
  56. $menusList = self::where('is_show','1')->where('access','1')->order('sort DESC')->select();
  57. return self::tidyMenuTier(true,$menusList);
  58. }
  59. public static function ruleList()
  60. {
  61. $ruleList = self::order('sort DESC')->select();
  62. return self::tidyMenuTier(false,$ruleList);
  63. }
  64. public static function rolesByRuleList($rules)
  65. {
  66. $res = SystemRole::where('id','IN',$rules)->field('GROUP_CONCAT(rules) as ids')->find();
  67. $ruleList = self::where('id','IN',$res['ids'])->whereOr('pid',0)->order('sort DESC')->select();
  68. return self::tidyMenuTier(false,$ruleList);
  69. }
  70. public static function getAuthName($action,$controller,$module,$route)
  71. {
  72. return strtolower($module.'/'.$controller.'/'.$action.'/'.SystemMenus::paramStr($route));
  73. }
  74. public static function tidyMenuTier($adminFilter = false,$menusList,$pid = 0,$navList = [])
  75. {
  76. static $allAuth = null;
  77. static $adminAuth = null;
  78. if($allAuth === null) $allAuth = $adminFilter == true ? SystemRole::getAllAuth() : [];//所有的菜单
  79. if($adminAuth === null) $adminAuth = $adminFilter == true ? SystemAdmin::activeAdminAuthOrFail() : [];//当前登录用户的菜单
  80. foreach ($menusList as $k=>$menu){
  81. $menu = $menu->getData();
  82. if($menu['pid'] == $pid){
  83. unset($menusList[$k]);
  84. $params = json_decode($menu['params'],true);//获取参数
  85. $authName = self::getAuthName($menu['action'],$menu['controller'],$menu['module'],$params);// 按钮链接
  86. if($pid != 0 && $adminFilter && in_array($authName,$allAuth) && !in_array($authName,$adminAuth)) continue;
  87. $menu['child'] = self::tidyMenuTier($adminFilter,$menusList,$menu['id']);
  88. if($pid != 0 && !count($menu['child']) && !$menu['controller'] && !$menu['action']) continue;
  89. $menu['url'] = !count($menu['child']) ? Url::build($menu['module'].'/'.$menu['controller'].'/'.$menu['action'],$params) : 'javascript:void(0);';
  90. if($pid == 0 && !count($menu['child'])) continue;
  91. $navList[] = $menu;
  92. }
  93. }
  94. return $navList;
  95. }
  96. public static function delMenu($id)
  97. {
  98. if(self::where('pid',$id)->count())
  99. return self::setErrorInfo('请先删除改菜单下的子菜单!');
  100. return self::del($id);
  101. }
  102. public static function getAdminPage($params)
  103. {
  104. $model = new self;
  105. if($params['is_show'] !== '') $model = $model->where('is_show',$params['is_show']);
  106. // if($params['access'] !== '') $model = $model->where('access',$params['access']);//子管理员是否可用
  107. if($params['pid'] !== ''&& !$params['keyword'] ) $model = $model->where('pid',$params['pid']);
  108. if($params['keyword'] !== '') $model = $model->where('menu_name|id|pid','LIKE',"%$params[keyword]%");
  109. $model = $model->order('sort DESC,id ASC');
  110. return self::page($model,$params);
  111. }
  112. public static function paramStr($params)
  113. {
  114. if(!is_array($params)) $params = json_decode($params,true)?:[];
  115. $p = [];
  116. foreach ($params as $key => $param){
  117. $p[] = $key;
  118. $p[] = $param;
  119. }
  120. return implode('/',$p);
  121. }
  122. public static function getVisitName($action,$controller,$module,array $route = [])
  123. {
  124. $params = json_encode($route);
  125. return self::where('action',$action)
  126. ->where('controller',lcfirst($controller))
  127. ->where('module',lcfirst($module))
  128. ->where('params',['=',$params],['=','[]'],'or')
  129. ->order('id DESC')
  130. ->value('menu_name');
  131. }
  132. }