SystemMenus.php 7.1 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\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. dump($ruleList->toArray());
  105. return self::tidyMenuTier(false, $ruleList);
  106. }
  107. /**
  108. * @param $action
  109. * @param $controller
  110. * @param $module
  111. * @param $route
  112. * @return string
  113. */
  114. public static function getAuthName($action, $controller, $module, $route)
  115. {
  116. return strtolower($module . '/' . $controller . '/' . $action . '/' . SystemMenus::paramStr($route));
  117. }
  118. /**
  119. * @param bool $adminFilter
  120. * @param $menusList
  121. * @param int $pid
  122. * @param array $navList
  123. * @return array
  124. * @throws \Exception
  125. */
  126. public static function tidyMenuTier($adminFilter = false, $menusList, $pid = 0, $navList = [])
  127. {
  128. static $allAuth = null;
  129. static $adminAuth = null;
  130. if ($allAuth === null) $allAuth = $adminFilter == true ? SystemRole::getAllAuth() : [];//所有的菜单
  131. if ($adminAuth === null) $adminAuth = $adminFilter == true ? SystemAdmin::activeAdminAuthOrFail() : [];//当前登录用户的菜单
  132. foreach ($menusList as $k => $menu) {
  133. $menu = $menu->getData();
  134. if ($menu['pid'] == $pid) {
  135. unset($menusList[$k]);
  136. $params = json_decode($menu['params'], true);//获取参数
  137. $authName = self::getAuthName($menu['action'], $menu['controller'], $menu['module'], $params);// 按钮链接
  138. if ($pid != 0 && $adminFilter && in_array($authName, $allAuth) && (!in_array($authName, $adminAuth) || !array_key_exists($menu['id'], $adminAuth))) continue;
  139. $menu['child'] = self::tidyMenuTier($adminFilter, $menusList, $menu['id']);
  140. if ($pid != 0 && !count($menu['child']) && !$menu['controller'] && !$menu['action']) continue;
  141. $menu['url'] = !count($menu['child']) ? Url::buildUrl($menu['module'] . '/' . $menu['controller'] . '/' . $menu['action'], $params) : 'javascript:void(0);';
  142. if ($pid == 0 && !count($menu['child'])) continue;
  143. $navList[] = $menu;
  144. }
  145. }
  146. return $navList;
  147. }
  148. /**
  149. * @param $id
  150. * @return bool
  151. */
  152. public static function delMenu($id)
  153. {
  154. if (self::where('pid', $id)->count())
  155. return self::setErrorInfo('请先删除改菜单下的子菜单!');
  156. return self::del($id);
  157. }
  158. /**
  159. * @param $params
  160. * @return array
  161. */
  162. public static function getAdminPage($params)
  163. {
  164. $model = new self;
  165. if ($params['is_show'] !== '') $model = $model->where('is_show', $params['is_show']);
  166. // if($params['access'] !== '') $model = $model->where('access',$params['access']);//子管理员是否可用
  167. if ($params['pid'] !== '' && !$params['keyword']) $model = $model->where('pid', $params['pid']);
  168. if ($params['keyword'] !== '') $model = $model->where('menu_name|id|pid', 'LIKE', "%$params[keyword]%");
  169. $model = $model->order('sort DESC,id ASC');
  170. return self::page($model, $params);
  171. }
  172. /**
  173. * @param $params
  174. * @return string
  175. */
  176. public static function paramStr($params)
  177. {
  178. if (!is_array($params)) $params = json_decode($params, true) ?: [];
  179. $p = [];
  180. foreach ($params as $key => $param) {
  181. $p[] = $key;
  182. if(!is_array($param))
  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. }