Mysql.php 3.1 KB

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