Mysql.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | mysql驱动类
  13. // +----------------------------------------------------------------------
  14. namespace app\common\library\token\driver;
  15. use app\common\library\token\Driver;
  16. use think\facade\Db;
  17. class Mysql extends Driver
  18. {
  19. /**
  20. * 默认配置
  21. * @var array
  22. */
  23. protected $options = [
  24. 'table' => 'user_token',
  25. 'expire' => 2592000,
  26. 'connection' => [],
  27. ];
  28. /**
  29. * 构造函数
  30. * @param array $options 参数
  31. * @throws \think\db\exception\DbException
  32. */
  33. public function __construct(array $options = [])
  34. {
  35. if (!empty($options)) {
  36. $this->options = array_merge($this->options, $options);
  37. }
  38. if ($this->options['connection']) {
  39. $this->handler = Db::connect($this->options['connection'])->name($this->options['table']);
  40. } else {
  41. $this->handler = Db::name($this->options['table']);
  42. }
  43. $time = time();
  44. $tokentime = cache('tokentime');
  45. if (!$tokentime || $tokentime < $time - 86400) {
  46. cache('tokentime', $time);
  47. $this->handler->where('expire_time', '<', $time)->where('expire_time', '>', 0)->delete();
  48. }
  49. }
  50. /**
  51. * 存储Token
  52. * @param string $token Token
  53. * @param int $user_id 会员ID
  54. * @param int $expire 过期时长,0表示无限,单位秒
  55. * @return true
  56. */
  57. public function set(string $token, int $user_id, $expire = null): bool
  58. {
  59. $expiretime = !is_null($expire) && $expire !== 0 ? time() + $expire : 0;
  60. $token = $this->getEncryptedToken($token);
  61. $this->handler->removeOption('where');
  62. $this->handler->insert(['token' => $token, 'user_id' => $user_id, 'create_time' => time(), 'expire_time' => $expiretime]);
  63. return true;
  64. }
  65. /**
  66. * 获取Token内的信息
  67. * @param string $token
  68. * @return mixed
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public function get(string $token): mixed
  74. {
  75. $this->handler->removeOption('where');
  76. $data = $this->handler->where('token', $this->getEncryptedToken($token))->find();
  77. if ($data) {
  78. if (!$data['expire_time'] || $data['expire_time'] > time()) {
  79. //返回未加密的token给客户端使用
  80. $data['token'] = $token;
  81. //返回剩余有效时间
  82. $data['expires_in'] = $this->getExpiredIn($data['expire_time']);
  83. return $data;
  84. } else {
  85. self::delete($token);
  86. }
  87. }
  88. return [];
  89. }
  90. /**
  91. * 判断Token是否可用
  92. * @param string $token Token
  93. * @param int $user_id 会员ID
  94. * @return bool
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. */
  99. public function check(string $token, int $user_id): bool
  100. {
  101. $data = $this->get($token);
  102. return $data && $data['user_id'] == $user_id;
  103. }
  104. /**
  105. * 删除Token
  106. * @param string $token
  107. * @return true
  108. * @throws \think\db\exception\DbException
  109. */
  110. public function delete(string $token): bool
  111. {
  112. $this->handler->removeOption('where');
  113. $this->handler->where('token', $this->getEncryptedToken($token))->delete();
  114. return true;
  115. }
  116. /**
  117. * 删除指定用户的所有Token
  118. * @param int $user_id
  119. * @return true
  120. * @throws \think\db\exception\DbException
  121. */
  122. public function clear(int $user_id): bool
  123. {
  124. $this->handler->removeOption('where');
  125. $this->handler->where('user_id', $user_id)->delete();
  126. return true;
  127. }
  128. }