Mysql.php 3.1 KB

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