Mysql.php 3.0 KB

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