AuthorityService.php 989 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace App\Services;
  3. use App\Authority;
  4. use App\Role;
  5. use Exception;
  6. use Illuminate\Support\Facades\Cache;
  7. class AuthorityService
  8. {
  9. /**
  10. * @return Authority[]
  11. */
  12. function getAll(){
  13. $authorities = Cache::get('authorities');
  14. if(!$authorities){
  15. $authorities = Authority::with('roles')->get();
  16. Cache::put('authorities', $authorities, config('cache.expirations.authorities'));
  17. }
  18. return $authorities;
  19. }
  20. /**
  21. * @param Authority $authority
  22. * @return Role[]
  23. * @throws Exception
  24. */
  25. function getRoles(Authority $authority){
  26. if(!$authority['id']??'') throw new Exception('User对象或id不能为空');
  27. $roles = Cache::get('authorityGetRoles'.$authority['id']);
  28. if(!$roles){
  29. $roles = Authority::with('roles')->get();
  30. Cache::put('authorities', $roles, config('cache.expirations.authorities'));
  31. }
  32. return $roles;
  33. }
  34. }