UserAuthCache.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. declare(strict_types=1);
  8. namespace plugin\saiadmin\app\cache;
  9. use plugin\saiadmin\app\logic\system\SystemMenuLogic;
  10. use plugin\saiadmin\app\model\system\SystemUserRole;
  11. use support\think\Cache;
  12. /**
  13. * 用户权限缓存
  14. */
  15. class UserAuthCache
  16. {
  17. /**
  18. * 读取缓存配置
  19. * @return array
  20. */
  21. public static function cacheConfig(): array
  22. {
  23. return config('plugin.saiadmin.saithink.button_cache', [
  24. 'prefix' => 'saiadmin:button_cache:user_',
  25. 'expire' => 60 * 60 * 2,
  26. 'all' => 'saiadmin:button_cache:all',
  27. 'role' => 'saiadmin:button_cache:role_',
  28. 'tag' => 'saiadmin:button_cache',
  29. ]);
  30. }
  31. /**
  32. * 获取用户的权限
  33. */
  34. public static function getUserAuth($uid): array
  35. {
  36. if (empty($uid)) {
  37. return [];
  38. }
  39. $cache = static::cacheConfig();
  40. // 直接从缓存获取
  41. $auth = Cache::get($cache['prefix'] . $uid);
  42. if ($auth) {
  43. return $auth;
  44. }
  45. // 设置权限并返回
  46. $auth = static::setUserAuth($uid);
  47. if ($auth) {
  48. return $auth;
  49. }
  50. return [];
  51. }
  52. /**
  53. * 设置用户的权限
  54. */
  55. public static function setUserAuth($uid): array
  56. {
  57. // 从缓存获取,直接返回
  58. $roleIds = SystemUserRole::getRoleIds($uid);
  59. // 获取角色关联的菜单权限
  60. $data = (new SystemMenuLogic())->getAuthByRole($roleIds);
  61. if (empty($data)) {
  62. return [];
  63. }
  64. $cache = static::cacheConfig();
  65. $tag = [];
  66. $tag[] = $cache['tag'];
  67. if (!empty($roleIds)) {
  68. foreach ($roleIds as $role) {
  69. $tag[] = $cache['role'] . $role;
  70. }
  71. }
  72. // 保存到缓存
  73. Cache::tag($tag)->set($cache['prefix'] . $uid, $data, $cache['expire']);
  74. return $data;
  75. }
  76. /**
  77. * 获取全部权限
  78. */
  79. public static function getAllAuth(): array
  80. {
  81. $cache = static::cacheConfig();
  82. // 直接从缓存获取
  83. $auth = Cache::get($cache['all']);
  84. if ($auth) {
  85. return $auth;
  86. }
  87. $all = (new SystemMenuLogic())->getAllAuth();
  88. // 设置权限并返回
  89. Cache::tag($cache['tag'])->set($cache['all'], $all, $cache['expire']);
  90. return $all;
  91. }
  92. /**
  93. * 清理缓存
  94. */
  95. public static function clearUserAuth($uid): bool
  96. {
  97. $cache = static::cacheConfig();
  98. return Cache::delete($cache['prefix'] . $uid);
  99. }
  100. /**
  101. * 清理角色缓存
  102. */
  103. public static function clearUserAuthByRoleId($role_id): bool
  104. {
  105. $cache = static::cacheConfig();
  106. if (is_array($role_id)) {
  107. $tags = [];
  108. foreach ($role_id as $id) {
  109. $tags[] = $cache['role'] . $id;
  110. }
  111. } else {
  112. $tags = $cache['role'] . $role_id;
  113. }
  114. return Cache::tag($tags)->clear();
  115. }
  116. /**
  117. * 清理所有用户缓存
  118. * @return bool
  119. */
  120. public static function clear(): bool
  121. {
  122. $cache = static::cacheConfig();
  123. return Cache::tag($cache['tag'])->clear();
  124. }
  125. }