Mysql.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\common\library\token\driver;
  3. use Throwable;
  4. use think\Response;
  5. use think\facade\Db;
  6. use think\facade\Cache;
  7. use app\common\library\token\Driver;
  8. use think\exception\HttpResponseException;
  9. /**
  10. * @see Driver
  11. */
  12. class Mysql extends Driver
  13. {
  14. /**
  15. * 默认配置
  16. * @var array
  17. */
  18. protected array $options = [];
  19. /**
  20. * 构造函数
  21. * @access public
  22. * @param array $options 参数
  23. */
  24. public function __construct(array $options = [])
  25. {
  26. if (!empty($options)) {
  27. $this->options = array_merge($this->options, $options);
  28. }
  29. if ($this->options['name']) {
  30. $this->handler = Db::connect($this->options['name'])->name($this->options['table']);
  31. } else {
  32. $this->handler = Db::name($this->options['table']);
  33. }
  34. }
  35. /**
  36. * @throws Throwable
  37. */
  38. public function set(string $token, string $type, int $user_id, int $expire = null): bool
  39. {
  40. if (is_null($expire)) {
  41. $expire = $this->options['expire'];
  42. }
  43. $expireTime = $expire !== 0 ? time() + $expire : 0;
  44. $token = $this->getEncryptedToken($token);
  45. $this->handler->insert(['token' => $token, 'type' => $type, 'user_id' => $user_id, 'create_time' => time(), 'expire_time' => $expireTime]);
  46. // 每隔48小时清理一次过期缓存
  47. $time = time();
  48. $lastCacheCleanupTime = Cache::get('last_cache_cleanup_time');
  49. if (!$lastCacheCleanupTime || $lastCacheCleanupTime < $time - 172800) {
  50. Cache::set('last_cache_cleanup_time', $time);
  51. $this->handler->where('expire_time', '<', time())->where('expire_time', '>', 0)->delete();
  52. }
  53. return true;
  54. }
  55. /**
  56. * @throws Throwable
  57. */
  58. public function get(string $token, bool $expirationException = true): array
  59. {
  60. $data = $this->handler->where('token', $this->getEncryptedToken($token))->find();
  61. if (!$data) {
  62. return [];
  63. }
  64. // 返回未加密的token给客户端使用
  65. $data['token'] = $token;
  66. // 返回剩余有效时间
  67. $data['expires_in'] = $this->getExpiredIn($data['expire_time'] ?? 0);
  68. if ($data['expire_time'] && $data['expire_time'] <= time() && $expirationException) {
  69. // token过期-触发前端刷新token
  70. $response = Response::create(['code' => 409, 'msg' => __('Token expiration'), 'data' => $data], 'json');
  71. throw new HttpResponseException($response);
  72. }
  73. return $data;
  74. }
  75. /**
  76. * @throws Throwable
  77. */
  78. public function check(string $token, string $type, int $user_id, bool $expirationException = true): bool
  79. {
  80. $data = $this->get($token, $expirationException);
  81. if (!$data || (!$expirationException && $data['expire_time'] && $data['expire_time'] <= time())) return false;
  82. return $data['type'] == $type && $data['user_id'] == $user_id;
  83. }
  84. /**
  85. * @throws Throwable
  86. */
  87. public function delete(string $token): bool
  88. {
  89. $this->handler->where('token', $this->getEncryptedToken($token))->delete();
  90. return true;
  91. }
  92. /**
  93. * @throws Throwable
  94. */
  95. public function clear(string $type, int $user_id): bool
  96. {
  97. $this->handler->where('type', $type)->where('user_id', $user_id)->delete();
  98. return true;
  99. }
  100. }