Role.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Models\System;
  3. use Illuminate\Support\Facades\DB;
  4. use Spatie\Permission\Models\Role as PermissionRole;
  5. use Spatie\Permission\Traits\HasPermissions;
  6. use Spatie\Permission\Traits\RefreshesPermissionCache;
  7. class Role extends PermissionRole
  8. {
  9. use HasPermissions;
  10. use RefreshesPermissionCache;
  11. public static function getMenus(array $role_ids)
  12. {
  13. $menu_ids = DB::table('system_role_has_menus')->whereIn('role_id',$role_ids)->pluck('menu_id')->all();
  14. return DB::table('system_menus')->whereIn('id',$menu_ids)->where('status', 0)->get();
  15. }
  16. // public function __construct(array $attributes = [])
  17. // {
  18. // parent::__construct($attributes);
  19. // }
  20. //
  21. // /**
  22. // * @throws ApiException
  23. // */
  24. // public static function create(array $attributes = [])
  25. // {
  26. // // 校验角色的唯一字段是否重复
  27. // $role = self::getRoleByParam($attributes);
  28. // $role && throw new ApiException(['code' => Response::HTTP_UNPROCESSABLE_ENTITY, 'message' => 'ROLE_CODE_DUPLICATE']);
  29. // return static::query()->create($attributes);
  30. // }
  31. //
  32. // /**
  33. // * @throws ApiException
  34. // */
  35. // public function update(array $attributes = [], array $options = []): bool|int
  36. // {
  37. // // 校验角色的唯一字段是否重复
  38. // $role = self::getRoleByParam($attributes);
  39. // $role && $role->id !== $attributes['id'] && throw new ApiException(['code' => Response::HTTP_UNPROCESSABLE_ENTITY, 'message' => 'ROLE_CODE_DUPLICATE']);
  40. // // 获取角色的所有权限
  41. // $permissions = $role->getPermissionNames();
  42. // // 存在权限并清空权限缓存
  43. // count($permissions) && Cache::forget(config('permission.cache.key'));
  44. // return static::query()->where('id', $attributes['id'])->update($attributes, $options);
  45. // }
  46. //
  47. // private static function getRoleByParam(array &$attributes = []): PermissionRole|null
  48. // {
  49. // // 校验角色的唯一字段是否重复
  50. // $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
  51. // $params = ['code' => $attributes['code'], 'guard_name' => $attributes['guard_name']];
  52. // if (app(PermissionRegistrar::class)->teams) {
  53. // $teamsKey = app(PermissionRegistrar::class)->teamsKey;
  54. // if (array_key_exists($teamsKey, $attributes)) {
  55. // $params[$teamsKey] = $attributes[$teamsKey];
  56. // } else {
  57. // $attributes[$teamsKey] = getPermissionsTeamId();
  58. // }
  59. // }
  60. // return static::findByParam($params);
  61. // }
  62. }