SystemMenus.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/02
  5. */
  6. namespace app\models\system;
  7. use crmeb\traits\ModelTrait;
  8. use crmeb\basic\BaseModel;
  9. use crmeb\utils\Arr;
  10. use think\facade\Route as Url;
  11. use think\helper\Str;
  12. /**
  13. * 菜单 model
  14. * Class SystemMenus
  15. * @package app\models\system
  16. */
  17. class SystemMenus extends BaseModel
  18. {
  19. /**
  20. * 数据表主键
  21. * @var string
  22. */
  23. protected $pk = 'id';
  24. /**
  25. * 模型名称
  26. * @var string
  27. */
  28. protected $name = 'system_menus';
  29. use ModelTrait;
  30. public static $isShowStatus = [1 => '显示', 0 => '不显示'];
  31. public static $accessStatus = [1 => '管理员可用', 0 => '管理员不可用'];
  32. public static function legalWhere($where = [])
  33. {
  34. $where['is_show'] = 1;
  35. }
  36. public function setParamsAttr($value)
  37. {
  38. $value = $value ? explode('/', $value) : [];
  39. $params = array_chunk($value, 2);
  40. $data = [];
  41. foreach ($params as $param) {
  42. if (isset($param[0]) && isset($param[1])) $data[$param[0]] = $param[1];
  43. }
  44. return json_encode($data);
  45. }
  46. protected function setControllerAttr($value)
  47. {
  48. return lcfirst($value);
  49. }
  50. public function getParamsAttr($_value)
  51. {
  52. return json_decode($_value, true);
  53. }
  54. public function getPidAttr($value)
  55. {
  56. return !$value ? '顶级' : self::get($value)['menu_name'];
  57. }
  58. /**
  59. * @param string $field
  60. * @param bool $filter
  61. * @return \think\Collection
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. * @throws \think\exception\DbException
  65. */
  66. public static function getParentMenu($field = '*', $filter = false)
  67. {
  68. $where = ['pid' => 0];
  69. $query = self::field($field);
  70. $query = $filter ? $query->where(self::legalWhere($where)) : $query->where($where);
  71. return $query->order('sort DESC')->select();
  72. }
  73. /**
  74. * @return array
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. * @throws \think\exception\DbException
  78. */
  79. public static function menuList()
  80. {
  81. $menusList = self::where('is_show', '1')->where('access', '1')->order('sort DESC')->select();
  82. return self::tidyMenuTier(true, $menusList);
  83. }
  84. /**
  85. * @return array
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. * @throws \think\exception\DbException
  89. */
  90. public static function ruleList()
  91. {
  92. $ruleList = self::order('sort DESC')->select();
  93. return self::tidyMenuTier(false, $ruleList);
  94. }
  95. /**
  96. * @param $rules
  97. * @return array
  98. * @throws \think\db\exception\DataNotFoundException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. * @throws \think\exception\DbException
  101. */
  102. public static function rolesByRuleList($rules)
  103. {
  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. * 截取掉版本号
  121. * @param $controller
  122. * @return string
  123. */
  124. public static function unEditionCode($controller)
  125. {
  126. $nowController = preg_replace('/^v[0-9]{1}+(\.|\\/)/', '', $controller);
  127. return is_null($nowController) ? $controller : $nowController;
  128. }
  129. /**
  130. * @param bool $adminFilter
  131. * @param $menusList
  132. * @param int $pid
  133. * @param array $navList
  134. * @return array
  135. * @throws \Exception
  136. */
  137. public static function tidyMenuTier($adminFilter = false, $menusList, $pid = 0, $navList = [])
  138. {
  139. static $allAuth = null;
  140. static $adminAuth = null;
  141. if ($allAuth === null) $allAuth = $adminFilter == true ? SystemRole::getAllAuth() : [];//所有的菜单
  142. if ($adminAuth === null) $adminAuth = $adminFilter == true ? SystemAdmin::activeAdminAuthOrFail() : [];//当前登录用户的菜单
  143. foreach ($menusList as $k => $menu) {
  144. $menu = $menu->getData();
  145. $menu['title'] = $menu['menu_name'];
  146. unset($menu['menu_name']);
  147. if ($menu['pid'] == $pid) {
  148. unset($menusList[$k]);
  149. $params = json_decode($menu['params'], true);//获取参数
  150. $authName = self::getAuthName($menu['action'], $menu['controller'], $menu['module'], $params);// 按钮链接
  151. if ($menu['auth_type'] == 1 && $pid != 0 && $adminFilter && in_array($authName, $allAuth) && !in_array($authName, $adminAuth)) continue;
  152. $menu['children'] = self::tidyMenuTier($adminFilter, $menusList, $menu['id']);
  153. if ($pid != 0 && !count($menu['children']) && !$menu['controller'] && !$menu['action'] && $menu['auth_type'] == 1) continue;
  154. if ($pid == 0 && !count($menu['children'])) continue;
  155. if ($menu['children']) $menu['expand'] = true;
  156. $navList[] = $menu;
  157. }
  158. }
  159. return $navList;
  160. }
  161. /**
  162. * @param $id
  163. * @return bool
  164. */
  165. public static function delMenu($id)
  166. {
  167. if (self::where('pid', $id)->count())
  168. return self::setErrorInfo('请先删除改菜单下的子菜单!');
  169. return self::del($id);
  170. }
  171. /**
  172. * @param $params
  173. * @return array
  174. */
  175. public static function getAdminList($params)
  176. {
  177. $model = new self;
  178. if ($params['is_show'] !== '') $model = $model->where('is_show', $params['is_show']);
  179. if ($params['pid'] !== '' && !$params['keyword']) $model = $model->where('pid', $params['pid']);
  180. if ($params['keyword'] !== '') $model = $model->where('menu_name|id|pid', 'LIKE', "%$params[keyword]%");
  181. $list = $model->order('sort DESC,id ASC')->select();
  182. return count($list) ? $list->toArray() : [];
  183. }
  184. /**
  185. * @param $params
  186. * @return string
  187. */
  188. public static function paramStr($params)
  189. {
  190. if (!is_array($params)) $params = json_decode($params, true) ?: [];
  191. $p = [];
  192. foreach ($params as $key => $param) {
  193. $p[] = $key;
  194. $p[] = $param;
  195. }
  196. return implode('/', $p);
  197. }
  198. /**
  199. * @param $params
  200. * @return array
  201. */
  202. public static function getAuthList($params)
  203. {
  204. $model = new self;
  205. if ($params['is_show'] !== '') $model = $model->where('is_show', $params['is_show']);
  206. if ($params['pid'] !== '' && !$params['keyword']) $model = $model->where('pid', $params['pid']);
  207. if ($params['keyword'] !== '') $model = $model->where('menu_name|id|pid', 'LIKE', "%$params[keyword]%");
  208. $model = $model->where('is_del', 0);
  209. $list = $model->order('sort DESC,id ASC')->select();
  210. return get_tree_children(self::getMenusArray($list));
  211. }
  212. /**
  213. * @param $list
  214. * @return array
  215. */
  216. public static function getMenusArray($list)
  217. {
  218. $menusValue = [];
  219. foreach ($list as $item) {
  220. $menusValue[] = $item->getData();
  221. }
  222. return $menusValue;
  223. }
  224. /**
  225. * @param $action
  226. * @param $controller
  227. * @param $module
  228. * @param array $route
  229. * @return mixed
  230. */
  231. public static function getVisitName($action, $controller, $module, array $route = [])
  232. {
  233. $params = json_encode($route);
  234. return self::where('action', $action)
  235. ->where('controller', lcfirst($controller))
  236. ->where('module', lcfirst($module))
  237. ->where('params', $params)
  238. ->where("params = '$params' OR params = '[]'")
  239. ->order('id DESC')
  240. ->value('menu_name');
  241. }
  242. /**
  243. * 返回给前端权限唯一标识
  244. * @param $roule_id
  245. * @param $level
  246. * @return array
  247. */
  248. public function getUniqueAuth($roule_id, $level)
  249. {
  250. $rules = (new SystemRole())->getRoleIds($roule_id);
  251. if (!$rules) return [];
  252. $authModel = $this->where('is_del', 0);
  253. if ($level) $authModel = $authModel->whereIn('id', $rules);
  254. return array_values($authModel->column('unique_auth', 'unique_auth'));
  255. }
  256. /**
  257. * 获取admin端用户菜单
  258. * @param $roule_id
  259. * @param $level
  260. * @return array
  261. * @throws \think\db\exception\DataNotFoundException
  262. * @throws \think\db\exception\DbException
  263. * @throws \think\db\exception\ModelNotFoundException
  264. */
  265. public function getRoute($roule_id, $level)
  266. {
  267. $rules = (new SystemRole())->getRoleIds($roule_id);
  268. if (!$rules) return [];
  269. $authModel = $this->where('auth_type', 1)->where('is_show', 1)->where('is_del', 0)->order('sort desc,id desc');
  270. if ($level) $authModel = $authModel->whereIn('id', $rules);
  271. $menus = $authModel->field('id,menu_name,icon,pid,sort,menu_path,is_show,header,is_header,is_show_path')->order('sort desc')->select();
  272. $data = [];
  273. foreach ($menus as $item) {
  274. $data[] = $item->getData();
  275. }
  276. return Arr::toIviewUi(Arr::getTree($data));
  277. }
  278. }