AdminTokenCache.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\common\model\auth\Admin;
  16. use app\common\model\auth\AdminSession;
  17. use app\common\model\auth\SystemRole;
  18. /**
  19. * 管理员token缓存
  20. * Class AdminTokenCache
  21. * @package app\common\cache
  22. */
  23. class AdminTokenCache extends BaseCache
  24. {
  25. private $prefix = 'token_admin_';
  26. /**
  27. * @notes 通过token获取缓存管理员信息
  28. * @param $token
  29. * @return false|mixed
  30. * @author 令狐冲
  31. * @date 2021/6/30 16:57
  32. */
  33. public function getAdminInfo($token)
  34. {
  35. //直接从缓存获取
  36. $adminInfo = $this->get($this->prefix . $token);
  37. if ($adminInfo) {
  38. return $adminInfo;
  39. }
  40. //从数据获取信息被设置缓存(可能后台清除缓存)
  41. $adminInfo = $this->setAdminInfo($token);
  42. if ($adminInfo) {
  43. return $adminInfo;
  44. }
  45. return false;
  46. }
  47. /**
  48. * @notes 通过有效token设置管理信息缓存
  49. * @param $token
  50. * @return array|false|mixed
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. * @author 令狐冲
  55. * @date 2021/7/5 12:12
  56. */
  57. public function setAdminInfo($token)
  58. {
  59. $adminSession = AdminSession::where([['token', '=', $token], ['expire_time', '>', time()]])
  60. ->find();
  61. if (empty($adminSession)) {
  62. return [];
  63. }
  64. $admin = Admin::where('id', '=', $adminSession->admin_id)
  65. ->append(['role_id'])
  66. ->find();
  67. $roleName = '';
  68. $roleLists = SystemRole::column('name', 'id');
  69. if ($admin['root'] == 1) {
  70. $roleName = '系统管理员';
  71. } else {
  72. foreach ($admin['role_id'] as $roleId) {
  73. $roleName .= $roleLists[$roleId] ?? '';
  74. $roleName .= '/';
  75. }
  76. $roleName = trim($roleName, '/');
  77. }
  78. $adminInfo = [
  79. 'admin_id' => $admin->id,
  80. 'root' => $admin->root,
  81. 'name' => $admin->name,
  82. 'account' => $admin->account,
  83. 'role_name' => $roleName,
  84. 'role_id' => $admin->role_id,
  85. 'token' => $token,
  86. 'terminal' => $adminSession->terminal,
  87. 'expire_time' => $adminSession->expire_time,
  88. 'login_ip' => request()->ip(),
  89. ];
  90. $this->set($this->prefix . $token, $adminInfo, new \DateTime(Date('Y-m-d H:i:s', $adminSession->expire_time)));
  91. return $this->getAdminInfo($token);
  92. }
  93. /**
  94. * @notes 删除缓存
  95. * @param $token
  96. * @return bool
  97. * @author 令狐冲
  98. * @date 2021/7/3 16:57
  99. */
  100. public function deleteAdminInfo($token)
  101. {
  102. return $this->delete($this->prefix . $token);
  103. }
  104. }