AdminTokenCache.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\cache;
  15. use app\adminapi\logic\auth\AdminLogic;
  16. use app\common\model\auth\Admin;
  17. use app\common\model\auth\AdminSession;
  18. use app\common\model\auth\SystemRole;
  19. /**
  20. * 管理员token缓存
  21. * Class AdminTokenCache
  22. * @package app\common\cache
  23. */
  24. class AdminTokenCache extends BaseCache
  25. {
  26. private $prefix = 'token_admin_';
  27. /**
  28. * @notes 通过token获取缓存管理员信息
  29. * @param $token
  30. * @return false|mixed
  31. * @author 令狐冲
  32. * @date 2021/6/30 16:57
  33. */
  34. public function getAdminInfo($token)
  35. {
  36. //直接从缓存获取
  37. $adminInfo = $this->get($this->prefix . $token);
  38. if ($adminInfo) {
  39. return $adminInfo;
  40. }
  41. //从数据获取信息被设置缓存(可能后台清除缓存)
  42. $adminInfo = $this->setAdminInfo($token);
  43. if ($adminInfo) {
  44. return $adminInfo;
  45. }
  46. return false;
  47. }
  48. /**
  49. * @notes 通过有效token设置管理信息缓存
  50. * @param $token
  51. * @return array|false|mixed
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. * @author 令狐冲
  56. * @date 2021/7/5 12:12
  57. */
  58. public function setAdminInfo($token)
  59. {
  60. $adminSession = AdminSession::where([['token', '=', $token], ['expire_time', '>', time()]])
  61. ->find();
  62. if (empty($adminSession)) {
  63. return [];
  64. }
  65. $admin = Admin::where('id', '=', $adminSession->admin_id)
  66. ->append(['role_id'])
  67. ->find();
  68. $roleName = '';
  69. $roleLists = SystemRole::column('name', 'id');
  70. if ($admin['root'] == 1) {
  71. $roleName = '系统管理员';
  72. } else {
  73. foreach ($admin['role_id'] as $roleId) {
  74. $roleName .= $roleLists[$roleId] ?? '';
  75. $roleName .= '/';
  76. }
  77. $roleName = trim($roleName, '/');
  78. }
  79. // 获取数据权限信息
  80. $data_rules = AdminLogic::getDataPermissions($admin->id);
  81. $adminInfo = [
  82. 'admin_id' => $admin->id,
  83. 'root' => $admin->root,
  84. 'name' => $admin->name,
  85. 'account' => $admin->account,
  86. 'role_name' => $roleName,
  87. 'role_id' => $admin->role_id,
  88. 'token' => $token,
  89. 'terminal' => $adminSession->terminal,
  90. 'expire_time' => $adminSession->expire_time,
  91. 'login_ip' => request()->ip(),
  92. 'data_rules' => $data_rules,
  93. ];
  94. $this->set($this->prefix . $token, $adminInfo, new \DateTime(Date('Y-m-d H:i:s', $adminSession->expire_time)));
  95. return $this->getAdminInfo($token);
  96. }
  97. /**
  98. * @notes 删除缓存
  99. * @param $token
  100. * @return bool
  101. * @author 令狐冲
  102. * @date 2021/7/3 16:57
  103. */
  104. public function deleteAdminInfo($token)
  105. {
  106. return $this->delete($this->prefix . $token);
  107. }
  108. }