MenuService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | EasyAdmin
  4. // +----------------------------------------------------------------------
  5. // | PHP交流群: 763822524
  6. // +----------------------------------------------------------------------
  7. // | 开源协议 https://mit-license.org
  8. // +----------------------------------------------------------------------
  9. // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
  10. // +----------------------------------------------------------------------
  11. namespace app\common\service;
  12. use app\common\constants\MenuConstant;
  13. use think\facade\Db;
  14. class MenuService
  15. {
  16. /**
  17. * 管理员ID
  18. * @var integer
  19. */
  20. protected $adminId;
  21. public function __construct($adminId)
  22. {
  23. $this->adminId = $adminId;
  24. return $this;
  25. }
  26. /**
  27. * 获取首页信息
  28. * @return array|\think\Model|null
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\DbException
  31. * @throws \think\db\exception\ModelNotFoundException
  32. */
  33. public function getHomeInfo()
  34. {
  35. $data = Db::name('system_menu')
  36. ->field('title,icon,href')
  37. ->where("delete_time is null")
  38. ->where('pid', MenuConstant::HOME_PID)
  39. ->find();
  40. !empty($data) && $data['href'] = __url($data['href']);
  41. return $data;
  42. }
  43. /**
  44. * 获取后台菜单树信息
  45. * @return mixed
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public function getMenuTree()
  51. {
  52. /** @var AuthService $authService */
  53. $authServer = app(AuthService::class, ['adminId' => $this->adminId]);
  54. return $this->buildMenuChild(0, $this->getMenuData(),$authServer);
  55. }
  56. private function buildMenuChild($pid, $menuList, AuthService $authServer)
  57. {
  58. $treeList = [];
  59. foreach ($menuList as &$v) {
  60. $check = empty($v['href']) ? true : $authServer->checkNode($v['href']);
  61. !empty($v['href']) && $v['href'] = __url($v['href']);
  62. if ($pid == $v['pid'] && $check) {
  63. $node = $v;
  64. $child = $this->buildMenuChild($v['id'], $menuList, $authServer);
  65. if (!empty($child)) {
  66. $node['child'] = $child;
  67. }
  68. if (!empty($v['href']) || !empty($child)) {
  69. $treeList[] = $node;
  70. }
  71. }
  72. }
  73. return $treeList;
  74. }
  75. /**
  76. * 获取所有菜单数据
  77. * @return \think\Collection
  78. * @throws \think\db\exception\DataNotFoundException
  79. * @throws \think\db\exception\DbException
  80. * @throws \think\db\exception\ModelNotFoundException
  81. */
  82. protected function getMenuData()
  83. {
  84. $menuData = Db::name('system_menu')
  85. ->field('id,pid,title,icon,href,target')
  86. ->where("delete_time is null")
  87. ->where([
  88. ['status', '=', '1'],
  89. ['pid', '<>', MenuConstant::HOME_PID],
  90. ])
  91. ->order([
  92. 'sort' => 'desc',
  93. 'id' => 'asc',
  94. ])
  95. ->select();
  96. return $menuData;
  97. }
  98. }