SystemMenus.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace app\admin\model\system;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. use think\facade\Route as Url;
  6. /**
  7. * 菜单 model
  8. * Class SystemMenus
  9. * @package app\admin\model\system
  10. */
  11. class SystemMenus extends BaseModel
  12. {
  13. /**
  14. * 数据表主键
  15. * @var string
  16. */
  17. protected $pk = 'id';
  18. /**
  19. * 模型名称
  20. * @var string
  21. */
  22. protected $name = 'system_menus';
  23. use ModelTrait;
  24. public static $isShowStatus = [1 => '显示', 0 => '不显示'];
  25. public static $accessStatus = [1 => '管理员可用', 0 => '管理员不可用'];
  26. public static function legalWhere($where = [])
  27. {
  28. $where['is_show'] = 1;
  29. }
  30. public function setParamsAttr($value)
  31. {
  32. $value = $value ? explode('/', $value) : [];
  33. $params = array_chunk($value, 2);
  34. $data = [];
  35. foreach ($params as $param) {
  36. if (isset($param[0]) && isset($param[1])) $data[$param[0]] = $param[1];
  37. }
  38. return json_encode($data);
  39. }
  40. protected function setControllerAttr($value)
  41. {
  42. return lcfirst($value);
  43. }
  44. public function getParamsAttr($_value)
  45. {
  46. return json_decode($_value, true);
  47. }
  48. public function getPidAttr($value)
  49. {
  50. return !$value ? '顶级' : self::get($value)['menu_name'];
  51. }
  52. /**
  53. * @param string $field
  54. * @param bool $filter
  55. * @return \think\Collection
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. * @throws \think\exception\DbException
  59. */
  60. public static function getParentMenu($field = '*', $filter = false)
  61. {
  62. $where = ['pid' => 0];
  63. $query = self::field($field);
  64. $query = $filter ? $query->where(self::legalWhere($where)) : $query->where($where);
  65. return $query->order('sort DESC')->select();
  66. }
  67. /**
  68. * @return array
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. * @throws \think\exception\DbException
  72. */
  73. public static function menuList()
  74. {
  75. $menusList = self::where('is_show', '1')->where('access', '1')->order('sort DESC')->select();
  76. return self::tidyMenuTier(true, $menusList);
  77. }
  78. /**
  79. * @return array
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. * @throws \think\exception\DbException
  83. */
  84. public static function ruleList()
  85. {
  86. $ruleList = self::order('sort DESC')->select();
  87. return self::tidyMenuTier(false, $ruleList);
  88. }
  89. /**
  90. * @param $rules
  91. * @return array
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\ModelNotFoundException
  94. * @throws \think\exception\DbException
  95. */
  96. public static function rolesByRuleList($rules)
  97. {
  98. $res = SystemRole::where('id', 'IN', $rules)->field('GROUP_CONCAT(rules) as ids')->find();
  99. $ruleList = self::where('id', 'IN', $res['ids'])->whereOr('pid', 0)->order('sort DESC')->select();
  100. return self::tidyMenuTier(false, $ruleList);
  101. }
  102. /**
  103. * @param $action
  104. * @param $controller
  105. * @param $module
  106. * @param $route
  107. * @return string
  108. */
  109. public static function getAuthName($action, $controller, $module, $route)
  110. {
  111. return strtolower($module . '/' . $controller . '/' . $action . '/' . SystemMenus::paramStr($route));
  112. }
  113. /**
  114. * @param bool $adminFilter
  115. * @param $menusList
  116. * @param int $pid
  117. * @param array $navList
  118. * @return array
  119. * @throws \Exception
  120. */
  121. public static function tidyMenuTier($adminFilter = false, $menusList, $pid = 0, $navList = [])
  122. {
  123. static $allAuth = null;
  124. static $adminAuth = null;
  125. if ($allAuth === null) $allAuth = $adminFilter == true ? SystemRole::getAllAuth() : []; //所有的菜单
  126. if ($adminAuth === null) $adminAuth = $adminFilter == true ? SystemAdmin::activeAdminAuthOrFail() : []; //当前登录用户的菜单
  127. foreach ($menusList as $k => $menu) {
  128. $menu = $menu->getData();
  129. if ($menu['pid'] == $pid) {
  130. unset($menusList[$k]);
  131. $params = json_decode($menu['params'], true); //获取参数
  132. $authName = self::getAuthName($menu['action'], $menu['controller'], $menu['module'], $params); // 按钮链接
  133. if ($pid != 0 && $adminFilter && in_array($authName, $allAuth) && (!in_array($authName, $adminAuth) || !array_key_exists($menu['id'], $adminAuth))) continue;
  134. $menu['child'] = self::tidyMenuTier($adminFilter, $menusList, $menu['id']);
  135. if ($pid != 0 && !count($menu['child']) && !$menu['controller'] && !$menu['action']) continue;
  136. $menu['url'] = !count($menu['child']) ? Url::buildUrl($menu['module'] . '/' . $menu['controller'] . '/' . $menu['action'], $params) : 'javascript:void(0);';
  137. if ($pid == 0 && !count($menu['child'])) continue;
  138. $navList[] = $menu;
  139. }
  140. }
  141. return $navList;
  142. }
  143. /**
  144. * @param $id
  145. * @return bool
  146. */
  147. public static function delMenu($id)
  148. {
  149. if (self::where('pid', $id)->count())
  150. return self::setErrorInfo('请先删除改菜单下的子菜单!');
  151. return self::del($id);
  152. }
  153. /**
  154. * @param $params
  155. * @return array
  156. */
  157. public static function getAdminPage($params)
  158. {
  159. $model = new self;
  160. if ($params['is_show'] !== '') $model = $model->where('is_show', $params['is_show']);
  161. // if($params['access'] !== '') $model = $model->where('access',$params['access']);//子管理员是否可用
  162. if ($params['pid'] !== '' && !$params['keyword']) $model = $model->where('pid', $params['pid']);
  163. if ($params['keyword'] !== '') $model = $model->where('menu_name|id|pid', 'LIKE', "%$params[keyword]%");
  164. $model = $model->order('sort DESC,id ASC');
  165. return self::page($model, $params);
  166. }
  167. /**
  168. * @param $params
  169. * @return string
  170. */
  171. public static function paramStr($params)
  172. {
  173. if (!is_array($params)) $params = json_decode($params, true) ?: [];
  174. $p = [];
  175. foreach ($params as $key => $param) {
  176. $p[] = $key;
  177. $p[] = $param;
  178. }
  179. return implode('/', $p);
  180. }
  181. /**
  182. * @param $action
  183. * @param $controller
  184. * @param $module
  185. * @param array $route
  186. * @return mixed
  187. */
  188. public static function getVisitName($action, $controller, $module, array $route = [])
  189. {
  190. $params = json_encode($route);
  191. return self::where('action', $action)
  192. ->where('controller', lcfirst($controller))
  193. ->where('module', lcfirst($module))
  194. ->where('params', $params)
  195. ->where("params = '$params' OR params = '[]'")
  196. ->order('id DESC')
  197. ->value('menu_name');
  198. }
  199. }