UserAuthCache.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\cache;
  8. use support\Cache;
  9. use plugin\saiadmin\app\logic\system\SystemMenuLogic;
  10. /**
  11. * 用户权限缓存
  12. */
  13. class UserAuthCache
  14. {
  15. private string $prefix = 'user_auth_'; // 缓存前缀
  16. private array $authCodeList = []; // 全部权限列表
  17. private string $cacheMd5Key = ''; // 权限文件MD5的key
  18. private string $cacheAllKey = ''; // 全部权限的key
  19. private string $cacheUrlKey = ''; // 管理员的url缓存key
  20. private string $authMd5 = ''; // 权限文件MD5的值
  21. private string $adminId = ''; // 管理员id
  22. /**
  23. * 初始化
  24. * @param string $adminId
  25. */
  26. public function __construct(string $adminId = '')
  27. {
  28. $this->adminId = $adminId;
  29. // 全部权限
  30. $this->authCodeList = (new SystemMenuLogic())->getAllCode();
  31. // 当前权限配置文件的md5
  32. $this->authMd5 = md5(json_encode($this->authCodeList));
  33. $this->cacheMd5Key = $this->prefix . 'md5';
  34. $this->cacheAllKey = $this->prefix . 'all';
  35. $this->cacheUrlKey = $this->prefix . 'url_' . $this->adminId;
  36. $cacheAuthMd5 = Cache::get($this->cacheMd5Key);
  37. $cacheAuth = Cache::get($this->cacheAllKey);
  38. //权限配置和缓存权限对比,不一样说明权限配置文件已修改,清理缓存
  39. if ($this->authMd5 !== $cacheAuthMd5 || empty($cacheAuth)) {
  40. Cache::deleteMultiple([
  41. $this->cacheMd5Key,
  42. $this->cacheAllKey,
  43. $this->cacheUrlKey
  44. ]);
  45. }
  46. }
  47. /**
  48. * 获取全部权限uri
  49. */
  50. public function getAllUri()
  51. {
  52. // 从缓存获取,直接返回
  53. $cacheAuth = Cache::get($this->cacheAllKey);
  54. if ($cacheAuth) {
  55. return $cacheAuth;
  56. }
  57. // 获取全部权限
  58. $authList = (new SystemMenuLogic)->getAllCode();
  59. // 保存到缓存并读取返回
  60. Cache::set($this->cacheMd5Key, $this->authMd5);
  61. Cache::set($this->cacheAllKey, $authList);
  62. return $authList;
  63. }
  64. /**
  65. * 获取管理权限uri
  66. */
  67. public function getAdminUri()
  68. {
  69. // 从缓存获取,直接返回
  70. $urisAuth = Cache::get($this->cacheUrlKey);
  71. if ($urisAuth) {
  72. return $urisAuth;
  73. }
  74. // 获取角色关联的菜单id(菜单或权限)
  75. $urisAuth = (new SystemMenuLogic)->getAuthByAdminId($this->adminId);
  76. if (empty($urisAuth)) {
  77. return [];
  78. }
  79. // 保存到缓存
  80. Cache::set($this->cacheUrlKey, $urisAuth, 3600);
  81. return $urisAuth;
  82. }
  83. /**
  84. * 清理用户权限缓存
  85. */
  86. public function clearUserCache(): bool
  87. {
  88. Cache::delete($this->cacheUrlKey);
  89. return true;
  90. }
  91. /**
  92. * 清理缓存
  93. */
  94. public function clearAuthCache(): bool
  95. {
  96. Cache::delete($this->cacheAllKey);
  97. return true;
  98. }
  99. }