SystemMenus.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. if ($pid == 21) {
  135. dump('s');
  136. }
  137. unset($menusList[$k]);
  138. $params = json_decode($menu['params'], true);//获取参数
  139. if ($pid == 21) {
  140. dump($params);
  141. }
  142. $authName = self::getAuthName($menu['action'], $menu['controller'], $menu['module'], $params);// 按钮链接
  143. if ($pid == 21) {
  144. dump($authName);
  145. }
  146. if ($pid != 0 && $adminFilter && in_array($authName, $allAuth) && (!in_array($authName, $adminAuth) || !array_key_exists($menu['id'], $adminAuth))) continue;
  147. $menu['child'] = self::tidyMenuTier($adminFilter, $menusList, $menu['id']);
  148. if ($pid == 21) {
  149. dump($menu['child']);
  150. }
  151. if ($pid != 0 && !count($menu['child']) && !$menu['controller'] && !$menu['action']) continue;
  152. $menu['url'] = !count($menu['child']) ? Url::buildUrl($menu['module'] . '/' . $menu['controller'] . '/' . $menu['action'], $params) : 'javascript:void(0);';
  153. if ($pid == 21) {
  154. dump($menu['url']);
  155. }
  156. if ($pid == 0 && !count($menu['child'])) continue;
  157. $navList[] = $menu;
  158. }
  159. }
  160. return $navList;
  161. }
  162. /**
  163. * @param $id
  164. * @return bool
  165. */
  166. public static function delMenu($id)
  167. {
  168. if (self::where('pid', $id)->count())
  169. return self::setErrorInfo('请先删除改菜单下的子菜单!');
  170. return self::del($id);
  171. }
  172. /**
  173. * @param $params
  174. * @return array
  175. */
  176. public static function getAdminPage($params)
  177. {
  178. $model = new self;
  179. if ($params['is_show'] !== '') $model = $model->where('is_show', $params['is_show']);
  180. // if($params['access'] !== '') $model = $model->where('access',$params['access']);//子管理员是否可用
  181. if ($params['pid'] !== '' && !$params['keyword']) $model = $model->where('pid', $params['pid']);
  182. if ($params['keyword'] !== '') $model = $model->where('menu_name|id|pid', 'LIKE', "%$params[keyword]%");
  183. $model = $model->order('sort DESC,id ASC');
  184. return self::page($model, $params);
  185. }
  186. /**
  187. * @param $params
  188. * @return string
  189. */
  190. public static function paramStr($params)
  191. {
  192. if (!is_array($params)) $params = json_decode($params, true) ?: [];
  193. $p = [];
  194. foreach ($params as $key => $param) {
  195. $p[] = $key;
  196. if (!is_array($param))
  197. $p[] = $param;
  198. }
  199. return implode('/', $p);
  200. }
  201. /**
  202. * @param $action
  203. * @param $controller
  204. * @param $module
  205. * @param array $route
  206. * @return mixed
  207. */
  208. public static function getVisitName($action, $controller, $module, array $route = [])
  209. {
  210. $params = json_encode($route);
  211. return self::where('action', $action)
  212. ->where('controller', lcfirst($controller))
  213. ->where('module', lcfirst($module))
  214. ->where('params', $params)
  215. ->where("params = '$params' OR params = '[]'")
  216. ->order('id DESC')
  217. ->value('menu_name');
  218. }
  219. }