Mysql.php 2.9 KB

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