MenuService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace app\common\service;
  3. use app\common\constants\MenuConstant;
  4. use think\facade\Db;
  5. use utils\RedisCache;
  6. /**
  7. * 菜单服务 by wes
  8. * Class MenuService
  9. * @package app\common\service
  10. */
  11. class MenuService
  12. {
  13. /**
  14. * 管理员ID
  15. * @var integer
  16. */
  17. protected $adminId;
  18. public function __construct($adminId)
  19. {
  20. $this->adminId = $adminId;
  21. return $this;
  22. }
  23. /**
  24. * 获取首页信息
  25. * @return array|\think\Model|null
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\DbException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. */
  30. public function getHomeInfo()
  31. {
  32. $data = Db::name('system_menu')
  33. ->field('title,icon,href')
  34. ->where("delete_time is null")
  35. ->where('pid', MenuConstant::HOME_PID)
  36. ->find();
  37. !empty($data) && $data['href'] = __url($data['href']);
  38. return $data;
  39. }
  40. /**
  41. * 获取后台菜单树信息
  42. * @return mixed
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. */
  47. public function getMenuTree()
  48. {
  49. /** @var AuthService $authService */
  50. $authServer = app(AuthService::class, ['adminId' => $this->adminId]);
  51. return $this->buildMenuChild(0, $this->getMenuData(),$authServer);
  52. }
  53. /**
  54. * 获取子菜单
  55. * @param $pid
  56. * @param $menuList
  57. * @param AuthService $authServer
  58. * @return array
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. private function buildMenuChild($pid, $menuList, AuthService $authServer)
  64. {
  65. $treeList = [];
  66. foreach ($menuList as &$v) {
  67. $check = empty($v['href']) ? true : $authServer->checkNode($v['href']);
  68. !empty($v['href']) && $v['href'] = __url($v['href']);
  69. if ($pid == $v['pid'] && $check) {
  70. $node = $v;
  71. $child = $this->buildMenuChild($v['id'], $menuList, $authServer);
  72. if (!empty($child)) {
  73. $node['child'] = $child;
  74. }
  75. if (!empty($v['href']) || !empty($child)) {
  76. $treeList[] = $node;
  77. }
  78. }
  79. }
  80. return $treeList;
  81. }
  82. /**
  83. * 获取所有菜单数据
  84. * @param bool $cache
  85. * @return array|mixed|\think\Collection|Db[]
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\DbException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. */
  90. protected function getMenuData($cache=true)
  91. {
  92. $cacheKey = "caches:temp:menus:all";
  93. $data = RedisCache::get($cacheKey);
  94. if($data && $cache){
  95. return $data;
  96. }
  97. $menuData = Db::name('system_menu')
  98. ->field('id,pid,title,icon,href,target')
  99. ->where("delete_time is null")
  100. ->where([
  101. ['status', '=', '1'],
  102. ['pid', '<>', MenuConstant::HOME_PID],
  103. ])
  104. ->order([
  105. 'sort' => 'desc',
  106. 'id' => 'asc',
  107. ])
  108. ->select();
  109. if($menuData && $cache){
  110. $menuData = $menuData->toArray();
  111. RedisCache::set($cacheKey, $menuData, 7200);
  112. }
  113. return $menuData;
  114. }
  115. }