UserTokenCache.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\cache;
  20. use app\common\model\User;
  21. use app\common\model\UserSession;
  22. class UserTokenCache extends BaseCache
  23. {
  24. private $prefix = 'token_user_';
  25. /**
  26. * @notes 通过token获取缓存用户信息
  27. * @param $token
  28. * @return false|mixed
  29. * @author 令狐冲
  30. * @date 2021/6/30 16:57
  31. */
  32. public function getUserInfo($token)
  33. {
  34. if(empty($token) || $token === 'null'){
  35. return false;
  36. }
  37. //直接从缓存获取
  38. $userInfo = $this->get($this->prefix . $token);
  39. if ($userInfo) {
  40. return $userInfo;
  41. }
  42. //从数据获取信息被设置缓存(可能后台清除缓存)
  43. $userInfo = $this->setUserInfo($token);
  44. if ($userInfo) {
  45. return $userInfo;
  46. }
  47. return false;
  48. }
  49. /**
  50. * @notes 通过有效token设置用户信息缓存
  51. * @param $token
  52. * @return array|false|mixed
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. * @author 令狐冲
  57. * @date 2021/7/5 12:12
  58. */
  59. public function setUserInfo($token)
  60. {
  61. $userSession = UserSession::where([['token', '=', $token], ['expire_time', '>', time()]])->find();
  62. if (empty($userSession)) {
  63. return [];
  64. }
  65. $user = User::where('id', '=', $userSession->user_id)
  66. ->find();
  67. $userInfo = [
  68. 'user_id' => $user->id,
  69. 'nickname' => $user->nickname,
  70. 'token' => $token,
  71. 'sn' => $user->sn,
  72. 'mobile' => $user->mobile,
  73. 'avatar' => $user->avatar,
  74. 'terminal' => $userSession->terminal,
  75. 'expire_time' => $userSession->expire_time,
  76. ];
  77. $this->set($this->prefix . $token, $userInfo, new \DateTime(Date('Y-m-d H:i:s', $userSession->expire_time)));
  78. return $this->getUserInfo($token);
  79. }
  80. /**
  81. * @notes 删除缓存
  82. * @param $token
  83. * @return bool
  84. * @author 令狐冲
  85. * @date 2021/7/3 16:57
  86. */
  87. public function deleteUserInfo($token)
  88. {
  89. return $this->delete($this->prefix . $token);
  90. }
  91. }