Mysql.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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->where('user_id', $user_id)->delete();
  52. $this->handler->insert(['token' => $token, 'user_id' => $user_id, 'createtime' => time(), 'expiretime' => $expiretime]);
  53. return true;
  54. }
  55. /**
  56. * 获取Token内的信息
  57. * @param string $token
  58. * @return array
  59. */
  60. public function get($token)
  61. {
  62. //方便测试
  63. if(strpos($token,'testuid_') !== false && config('app_debug') === true){
  64. $uid = substr($token,8);
  65. return [
  66. 'user_id' => intval($uid),
  67. ];
  68. }
  69. //方便测试
  70. $data = $this->handler->where('token', $this->getEncryptedToken($token))->find();
  71. if ($data) {
  72. if (!$data['expiretime'] || $data['expiretime'] > time()) {
  73. //返回未加密的token给客户端使用
  74. $data['token'] = $token;
  75. //返回剩余有效时间
  76. $data['expires_in'] = $this->getExpiredIn($data['expiretime']);
  77. return $data;
  78. } else {
  79. self::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($token, $user_id)
  91. {
  92. $data = $this->get($token);
  93. return $data && $data['user_id'] == $user_id ? true : false;
  94. }
  95. /**
  96. * 删除Token
  97. * @param string $token
  98. * @return boolean
  99. */
  100. public function delete($token)
  101. {
  102. $this->handler->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($user_id)
  111. {
  112. $this->handler->where('user_id', $user_id)->delete();
  113. return true;
  114. }
  115. }