SystemMenus.php 6.9 KB

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