Mysql.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Utils\Encrypt\Token\Driver;
  3. use App\Master\Enum\RedisKeyEnum;
  4. use App\Utils\Encrypt\Token\Driver;
  5. use App\Utils\RedisUtil;
  6. use Hyperf\DbConnection\Db;
  7. use function Hyperf\Config\config;
  8. /**
  9. * Token操作类
  10. */
  11. class Mysql extends Driver
  12. {
  13. /**
  14. * 默认配置
  15. * @var array
  16. */
  17. protected array $options = [
  18. 'table' => 'user_token',
  19. 'expire' => 2592000
  20. ];
  21. /**
  22. * 构造函数
  23. * @param array $options 参数
  24. * @access public
  25. * @throws \Exception
  26. */
  27. public function __construct(array $options = [])
  28. {
  29. if (!empty($options)) {
  30. $this->options = array_merge($this->options, $options);
  31. }
  32. $time = time();
  33. $token_time = RedisUtil::getInstance(RedisKeyEnum::TOKEN_TIME)->get();
  34. if (!$token_time || $token_time < $time - 86400) {
  35. RedisUtil::getInstance(RedisKeyEnum::TOKEN_TIME)->setex($time, $this->options['expire']);
  36. Db::table($this->options['table'])->whereBetween('expiretime', [0, $time])->delete();
  37. }
  38. }
  39. /**
  40. * 存储Token
  41. * @param string $token Token
  42. * @param int $user_id 会员ID
  43. * @param int|null $expire 过期时长,0表示无限,单位秒
  44. * @return bool
  45. */
  46. public function set(string $token, int $user_id, int $expire = null): bool
  47. {
  48. $expire_time = !is_null($expire) && $expire !== 0 ? time() + $expire : 0;
  49. $token = $this->getEncryptedToken($token);
  50. Db::table($this->options['table'])->where('user_id', $user_id)->delete();
  51. Db::table($this->options['table'])->insert(['token' => $token, 'user_id' => $user_id, 'createtime' => time(), 'expiretime' => $expire_time]);
  52. return true;
  53. }
  54. /**
  55. * 获取Token内的信息
  56. * @param string $token
  57. * @return array
  58. */
  59. public function get(string $token): array
  60. {
  61. //方便测试
  62. if (strpos($token, 'testuid_') !== false && config('app_debug',false)) {
  63. $uid = substr($token, 8);
  64. return [
  65. 'user_id' => intval($uid),
  66. ];
  67. }
  68. //方便测试
  69. $data = Db::table($this->options['table'])->where('token', $this->getEncryptedToken($token))->first();
  70. if ($data) {
  71. $data = json_decode(json_encode($data),true);
  72. if (!$data['expiretime'] || $data['expiretime'] > time()) {
  73. //返回未加密的token给客户端使用
  74. $data['token'] = $token;
  75. //返回剩余有效时间
  76. $data['expires_in'] = $this->getExpiredIn((int)$data['expiretime']);
  77. return $data;
  78. } else {
  79. $this->delete($token);
  80. }
  81. }
  82. return [];
  83. }
  84. /**
  85. * 判断Token是否可用
  86. * @param string $token Token
  87. * @param int $user_id 会员ID
  88. * @return boolean
  89. */
  90. public function check(string $token, int $user_id): bool
  91. {
  92. $data = $this->get($token);
  93. return $data && $data['user_id'] == $user_id;
  94. }
  95. /**
  96. * 删除Token
  97. * @param string $token
  98. * @return boolean
  99. */
  100. public function delete(string $token): bool
  101. {
  102. Db::table($this->options['table'])->where('token', $this->getEncryptedToken($token))->delete();
  103. return true;
  104. }
  105. /**
  106. * 删除指定用户的所有Token
  107. * @param int $user_id
  108. * @return boolean
  109. */
  110. public function clear(int $user_id): bool
  111. {
  112. Db::table($this->options['table'])->where('user_id', $user_id)->delete();
  113. return true;
  114. }
  115. }