UserTokenService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\shopapi\service;
  20. use app\common\cache\UserTokenCache;
  21. use app\common\model\UserSession;
  22. use think\facade\Config;
  23. class UserTokenService
  24. {
  25. /**
  26. * @notes 设置或更新用户token
  27. * @param $userId
  28. * @param $terminal
  29. * @param int $multipointLogin
  30. * @return array|false|mixed
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. * @author 令狐冲
  35. * @date 2021/7/13 23:07
  36. */
  37. public static function setToken($userId, $terminal)
  38. {
  39. $time = time();
  40. $userSession = UserSession::where([['user_id', '=', $userId], ['terminal', '=', $terminal]])->find();
  41. //获取token延长过期的时间
  42. $expireTime = $time + Config::get('project.user_token.expire_duration');
  43. $userTokenCache = new UserTokenCache();
  44. //token处理
  45. if ($userSession) {
  46. //清空缓存
  47. $userTokenCache->deleteUserInfo($userSession->token);
  48. //重新获取token
  49. $userSession->token = create_token($userId);
  50. $userSession->expire_time = $expireTime;
  51. $userSession->update_time = $time;
  52. $userSession->save();
  53. } else {
  54. //找不到在该终端的token记录,创建token记录
  55. $userSession = UserSession::create([
  56. 'user_id' => $userId,
  57. 'terminal' => $terminal,
  58. 'token' => create_token($userId),
  59. 'expire_time' => $expireTime
  60. ]);
  61. }
  62. return $userTokenCache->setUserInfo($userSession->token);
  63. }
  64. /**
  65. * @notes 延长token过期时间
  66. * @param $token
  67. * @return array|false|mixed
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. * @author 令狐冲
  72. * @date 2021/7/5 14:25
  73. */
  74. public static function overtimeToken($token)
  75. {
  76. $time = time();
  77. $adminSession = UserSession::where('token', '=', $token)->find();
  78. if (empty($adminSession['id'])) {
  79. return false;
  80. }
  81. //延长token过期时间
  82. $adminSession->expire_time = $time + Config::get('project.user_token.expire_duration');
  83. $adminSession->update_time = $time;
  84. $adminSession->save();
  85. return (new UserTokenCache())->setUserInfo($adminSession->token);
  86. }
  87. /**
  88. * @notes 设置token为过期
  89. * @param $token
  90. * @return bool
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\DbException
  93. * @throws \think\db\exception\ModelNotFoundException
  94. * @author 令狐冲
  95. * @date 2021/7/5 14:31
  96. */
  97. public static function expireToken($token)
  98. {
  99. $userSession = UserSession::where('token', '=', $token)
  100. ->find();
  101. if (empty($userSession)) {
  102. return false;
  103. }
  104. $time = time();
  105. $userSession->expire_time = $time;
  106. $userSession->update_time = $time;
  107. $userSession->save();
  108. return (new UserTokenCache())->deleteUserInfo($token);
  109. }
  110. }