SystemMenus.php 5.7 KB

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