UserTokenService.php 3.9 KB

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