SystemMenus.php 7.0 KB

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