Menu.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model;
  12. use app\user\model\Role as RoleModel;
  13. use think\Model;
  14. use think\Exception;
  15. use util\Tree;
  16. /**
  17. * 节点模型
  18. * @package app\admin\model
  19. */
  20. class Menu extends Model
  21. {
  22. // 设置当前模型对应的完整数据表名称
  23. protected $table = '__ADMIN_MENU__';
  24. // 自动写入时间戳
  25. protected $autoWriteTimestamp = true;
  26. // 将节点url转为小写
  27. public function setUrlValueAttr($value)
  28. {
  29. return strtolower(trim($value));
  30. }
  31. /**
  32. * 递归修改所属模型
  33. * @param int $id 父级节点id
  34. * @param string $module 模型名称
  35. * @author 蔡伟明 <314013107@qq.com>
  36. * @return bool
  37. */
  38. public static function changeModule($id = 0, $module = '')
  39. {
  40. if ($id > 0) {
  41. $ids = self::where('pid', $id)->column('id');
  42. if ($ids) {
  43. foreach ($ids as $id) {
  44. self::where('id', $id)->setField('module', $module);
  45. self::changeModule($id, $module);
  46. }
  47. }
  48. }
  49. return true;
  50. }
  51. /**
  52. * 获取树形节点
  53. * @param int $id 需要隐藏的节点id
  54. * @param string $default 默认第一个节点项,默认为“顶级节点”,如果为false则不显示,也可传入其他名称
  55. * @param string $module 模型名
  56. * @author 蔡伟明 <314013107@qq.com>
  57. * @return mixed
  58. */
  59. public static function getMenuTree($id = 0, $default = '', $module = '')
  60. {
  61. $result[0] = '顶级节点';
  62. $where = [
  63. ['status', 'egt', 0]
  64. ];
  65. if ($module != '') {
  66. $where[] = ['module', '=', $module];
  67. }
  68. // 排除指定节点及其子节点
  69. if ($id !== 0) {
  70. $hide_ids = array_merge([$id], self::getChildsId($id));
  71. $where[] = ['id', 'not in', $hide_ids];
  72. }
  73. // 获取节点
  74. $menus = Tree::toList(self::where($where)->order('pid,id')->column('id,pid,title'));
  75. foreach ($menus as $menu) {
  76. $result[$menu['id']] = $menu['title_display'];
  77. }
  78. // 设置默认节点项标题
  79. if ($default != '') {
  80. $result[0] = $default;
  81. }
  82. // 隐藏默认节点项
  83. if ($default === false) {
  84. unset($result[0]);
  85. }
  86. return $result;
  87. }
  88. /**
  89. * 获取顶部节点
  90. * @param string $max 最多返回多少个
  91. * @param string $cache_tag 缓存标签
  92. * @author 蔡伟明 <314013107@qq.com>
  93. * @return array
  94. */
  95. public static function getTopMenu($max = '', $cache_tag = '')
  96. {
  97. $cache_tag .= '_role_'.session('user_auth.role');
  98. $menus = cache($cache_tag);
  99. if (!$menus) {
  100. // 非开发模式,只显示可以显示的菜单
  101. if (config('develop_mode') == 0) {
  102. $map['online_hide'] = 0;
  103. }
  104. $map['status'] = 1;
  105. $map['pid'] = 0;
  106. $list_menu = self::where($map)->order('sort,id')->column('id,pid,module,title,url_value,url_type,url_target,icon,params');
  107. $i = 0;
  108. $menus = [];
  109. foreach ($list_menu as $key => &$menu) {
  110. if ($max != '' && $i >= $max) {
  111. break;
  112. }
  113. // 没有访问权限的节点不显示
  114. if (!RoleModel::checkAuth($menu['id'])) {
  115. continue;
  116. }
  117. if ($menu['url_value'] != '' && ($menu['url_type'] == 'module_admin' || $menu['url_type'] == 'module_home')) {
  118. $url = explode('/', $menu['url_value']);
  119. $menu['controller'] = $url[1];
  120. $menu['action'] = $url[2];
  121. $menu['url_value'] = $menu['url_type'] == 'module_admin' ? admin_url($menu['url_value'], $menu['params']) : home_url($menu['url_value'], $menu['params']);
  122. }
  123. $menus[$key] = $menu;
  124. $i++;
  125. }
  126. // 非开发模式,缓存菜单
  127. if (config('develop_mode') == 0) {
  128. cache($cache_tag, $menus);
  129. }
  130. }
  131. return $menus;
  132. }
  133. /**
  134. * 获取侧栏节点
  135. * @param string $id 模块id
  136. * @param string $module 模块名
  137. * @param string $controller 控制器名
  138. * @author 蔡伟明 <314013107@qq.com>
  139. * @return array|mixed
  140. */
  141. public static function getSidebarMenu($id = '', $module = '', $controller = '')
  142. {
  143. $module = $module == '' ? request()->module() : $module;
  144. $controller = $controller == '' ? request()->controller() : $controller;
  145. $cache_tag = strtolower('_sidebar_menus_' . $module . '_' . $controller).'_role_'.session('user_auth.role');
  146. $menus = cache($cache_tag);
  147. if (!$menus) {
  148. // 获取当前节点地址
  149. $location = self::getLocation($id);
  150. // 当前顶级节点id
  151. $top_id = $location[0]['id'];
  152. // 获取顶级节点下的所有节点
  153. $map = [
  154. 'status' => 1
  155. ];
  156. // 非开发模式,只显示可以显示的菜单
  157. if (config('develop_mode') == 0) {
  158. $map['online_hide'] = 0;
  159. }
  160. $menus = self::where($map)->order('sort,id')->column('id,pid,module,title,url_value,url_type,url_target,icon,params');
  161. // 解析模块链接
  162. foreach ($menus as $key => &$menu) {
  163. // 没有访问权限的节点不显示
  164. if (!RoleModel::checkAuth($menu['id'])) {
  165. unset($menus[$key]);
  166. continue;
  167. }
  168. if ($menu['url_value'] != '' && ($menu['url_type'] == 'module_admin' || $menu['url_type'] == 'module_home')) {
  169. $menu['url_value'] = $menu['url_type'] == 'module_admin' ? admin_url($menu['url_value'], $menu['params']) : home_url($menu['url_value'], $menu['params']);
  170. }
  171. }
  172. $menus = Tree::toLayer($menus, $top_id, 2);
  173. // 非开发模式,缓存菜单
  174. if (config('develop_mode') == 0) {
  175. cache($cache_tag, $menus);
  176. }
  177. }
  178. return $menus;
  179. }
  180. /**
  181. * 获取指定节点ID的位置
  182. * @param string $id 节点id,如果没有指定,则取当前节点id
  183. * @param bool $del_last_url 是否删除最后一个节点的url地址
  184. * @param bool $check 检查节点是否存在,不存在则抛出错误
  185. * @author 蔡伟明 <314013107@qq.com>
  186. * @return array
  187. * @throws \think\Exception
  188. */
  189. public static function getLocation($id = '', $del_last_url = false, $check = true)
  190. {
  191. $model = request()->module();
  192. $controller = request()->controller();
  193. $action = request()->action();
  194. if ($id != '') {
  195. $cache_name = 'location_menu_'.$id;
  196. } else {
  197. $cache_name = 'location_'.$model.'_'.$controller.'_'.$action;
  198. }
  199. $location = cache($cache_name);
  200. if (!$location) {
  201. $map = [
  202. ['pid', '<>', 0],
  203. ['url_value', '=', strtolower($model.'/'.trim(preg_replace("/[A-Z]/", "_\\0", $controller), "_").'/'.$action)]
  204. ];
  205. // 当前操作对应的节点ID
  206. $curr_id = $id == '' ? self::where($map)->value('id') : $id;
  207. // 获取节点ID是所有父级节点
  208. $location = Tree::getParents(self::column('id,pid,title,url_value,params'), $curr_id);
  209. if ($check && empty($location)) {
  210. throw new Exception('获取不到当前节点地址,可能未添加节点', 9001);
  211. }
  212. // 剔除最后一个节点url
  213. if ($del_last_url) {
  214. $location[count($location) - 1]['url_value'] = '';
  215. }
  216. // 非开发模式,缓存菜单
  217. if (config('develop_mode') == 0) {
  218. cache($cache_name, $location);
  219. }
  220. }
  221. return $location;
  222. }
  223. /**
  224. * 根据分组获取节点
  225. * @param string $group 分组名称
  226. * @param bool|string $fields 要返回的字段
  227. * @param array $map 查找条件
  228. * @author 蔡伟明 <314013107@qq.com>
  229. * @return array
  230. */
  231. public static function getMenusByGroup($group = '', $fields = true, $map = [])
  232. {
  233. $map['module'] = $group;
  234. return self::where($map)->order('sort,id')->column($fields, 'id');
  235. }
  236. /**
  237. * 获取节点分组
  238. * @author 蔡伟明 <314013107@qq.com>
  239. * @return array
  240. */
  241. public static function getGroup()
  242. {
  243. $map['status'] = 1;
  244. $map['pid'] = 0;
  245. $menus = self::where($map)->order('id,sort')->column('module,title');
  246. return $menus;
  247. }
  248. /**
  249. * 获取所有子节点id
  250. * @param int $pid 父级id
  251. * @author 蔡伟明 <314013107@qq.com>
  252. * @return array
  253. */
  254. public static function getChildsId($pid = 0)
  255. {
  256. $ids = self::where('pid', $pid)->column('id');
  257. foreach ($ids as $value) {
  258. $ids = array_merge($ids, self::getChildsId($value));
  259. }
  260. return $ids;
  261. }
  262. /**
  263. * 获取所有父节点id
  264. * @param int $id 节点id
  265. * @author 蔡伟明 <314013107@qq.com>
  266. * @return array
  267. */
  268. public static function getParentsId($id = 0)
  269. {
  270. $pid = self::where('id', $id)->value('pid');
  271. $pids = [];
  272. if ($pid != 0) {
  273. $pids[] = $pid;
  274. $pids = array_merge($pids, self::getParentsId($pid));
  275. }
  276. return $pids;
  277. }
  278. /**
  279. * 根据节点id获取上下级的所有id
  280. * @param int $id 节点id
  281. * @author 蔡伟明 <314013107@qq.com>
  282. * @return array
  283. */
  284. public static function getLinkIds($id = 0)
  285. {
  286. $childs = self::getChildsId($id);
  287. $parents = self::getParentsId($id);
  288. return array_merge((array)(int)$id, $childs, $parents);
  289. }
  290. }