MasterWokerTokenService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | whitef快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/whitef
  8. // | github下载:https://github.com/likeshop-github/whitef
  9. // | 访问官网:https://www.whitef.cn
  10. // | whitef团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: whitefTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\workerapi\service;
  15. use app\common\cache\MasterWokerTokenCache;
  16. use app\common\model\master_worker\MasterWorkerSession;
  17. use think\facade\Config;
  18. class MasterWokerTokenService
  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,$type = 1)
  32. {
  33. $time = time();
  34. $userSession = MasterWorkerSession::where([['user_id', '=', $userId], ['terminal', '=', $terminal], ['type','=',$type]])->find();
  35. //获取token延长过期的时间
  36. $expireTime = $time + Config::get('project.user_token.expire_duration');
  37. $MasterWokerTokenCache = new MasterWokerTokenCache();
  38. //token处理
  39. if ($userSession) {
  40. //清空缓存
  41. $MasterWokerTokenCache->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 = MasterWorkerSession::create([
  50. 'user_id' => $userId,
  51. 'terminal' => $terminal,
  52. 'type' => $type,
  53. 'token' => create_token($userId),
  54. 'expire_time' => $expireTime
  55. ]);
  56. }
  57. return $MasterWokerTokenCache->setUserInfo($userSession->token);
  58. }
  59. /**
  60. * @notes 延长token过期时间
  61. * @param $token
  62. * @return array|false|mixed
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. * @author 段誉
  67. * @date 2022/9/16 10:10
  68. */
  69. public static function overtimeToken($token)
  70. {
  71. $time = time();
  72. $userSession = MasterWorkerSession::where('token', '=', $token)->findOrEmpty();
  73. if ($userSession->isEmpty()) {
  74. return false;
  75. }
  76. //延长token过期时间
  77. $userSession->expire_time = $time + Config::get('project.user_token.expire_duration');
  78. $userSession->update_time = $time;
  79. $userSession->save();
  80. return (new MasterWokerTokenCache())->setUserInfo($userSession->token);
  81. }
  82. /**
  83. * @notes 设置token为过期
  84. * @param $token
  85. * @return bool
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\DbException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. * @author 段誉
  90. * @date 2022/9/16 10:10
  91. */
  92. public static function expireToken($token)
  93. {
  94. $userSession = MasterWorkerSession::where('token', '=', $token)
  95. ->find();
  96. if (empty($userSession)) {
  97. return false;
  98. }
  99. $time = time();
  100. $userSession->expire_time = $time;
  101. $userSession->update_time = $time;
  102. $userSession->save();
  103. return (new MasterWokerTokenCache())->deleteUserInfo($token);
  104. }
  105. }