| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- <?php
- namespace App\Services;
- use App\Authority;
- use App\Role;
- use Exception;
- use Illuminate\Support\Facades\Cache;
- class AuthorityService
- {
- /**
- * @return Authority[]
- */
- function getAll(){
- $authorities = Cache::get('authorities');
- if(!$authorities){
- $authorities = Authority::with('roles')->get();
- Cache::put('authorities', $authorities, config('cache.expirations.authorities'));
- }
- return $authorities;
- }
- /**
- * @param Authority $authority
- * @return Role[]
- * @throws Exception
- */
- function getRoles(Authority $authority){
- if(!$authority['id']??'') throw new Exception('User对象或id不能为空');
- $roles = Cache::get('authorityGetRoles'.$authority['id']);
- if(!$roles){
- $roles = Authority::with('roles')->get();
- Cache::put('authorities', $roles, config('cache.expirations.authorities'));
- }
- return $roles;
- }
- }
|