UserTokenCache.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\user\User;
  16. use app\common\model\user\UserSession;
  17. class UserTokenCache extends BaseCache
  18. {
  19. private $prefix = 'token_user_';
  20. /**
  21. * @notes 通过token获取缓存用户信息
  22. * @param $token
  23. * @return array|false|mixed
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\DbException
  26. * @throws \think\db\exception\ModelNotFoundException
  27. * @author 段誉
  28. * @date 2022/9/16 10:11
  29. */
  30. public function getUserInfo($token)
  31. {
  32. //直接从缓存获取
  33. $userInfo = $this->get($this->prefix . $token);
  34. if ($userInfo) {
  35. return $userInfo;
  36. }
  37. //从数据获取信息被设置缓存(可能后台清除缓存)
  38. $userInfo = $this->setUserInfo($token);
  39. if ($userInfo) {
  40. return $userInfo;
  41. }
  42. return false;
  43. }
  44. /**
  45. * @notes 通过有效token设置用户信息缓存
  46. * @param $token
  47. * @return array|false|mixed
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. * @author 段誉
  52. * @date 2022/9/16 10:11
  53. */
  54. public function setUserInfo($token)
  55. {
  56. $userSession = UserSession::where([['token', '=', $token], ['expire_time', '>', time()]])->find();
  57. if (empty($userSession)) {
  58. return [];
  59. }
  60. $user = User::where('id', '=', $userSession->user_id)
  61. ->find();
  62. $userInfo = [
  63. 'user_id' => $user->id,
  64. 'nickname' => $user->nickname,
  65. 'token' => $token,
  66. 'sn' => $user->sn,
  67. 'mobile' => $user->mobile,
  68. 'avatar' => $user->avatar,
  69. 'terminal' => $userSession->terminal,
  70. 'expire_time' => $userSession->expire_time,
  71. ];
  72. $ttl = new \DateTime(Date('Y-m-d H:i:s', $userSession->expire_time));
  73. $this->set($this->prefix . $token, $userInfo, $ttl);
  74. return $this->getUserInfo($token);
  75. }
  76. /**
  77. * @notes 删除缓存
  78. * @param $token
  79. * @return bool
  80. * @author 段誉
  81. * @date 2022/9/16 10:13
  82. */
  83. public function deleteUserInfo($token)
  84. {
  85. return $this->delete($this->prefix . $token);
  86. }
  87. }