SystemMenus.php 7.1 KB

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