Mysql.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. }
  34. /**
  35. * 存储Token
  36. * @param string $token Token
  37. * @param int $user_id 会员ID
  38. * @param int $expire 过期时长,0表示无限,单位秒
  39. * @return bool
  40. */
  41. public function set($token, $user_id, $expire = null)
  42. {
  43. $expiretime = !is_null($expire) && $expire !== 0 ? time() + $expire : 0;
  44. $token = $this->getEncryptedToken($token);
  45. $this->handler->insert(['token' => $token, 'user_id' => $user_id, 'createtime' => time(), 'expiretime' => $expiretime]);
  46. return TRUE;
  47. }
  48. /**
  49. * 获取Token内的信息
  50. * @param string $token
  51. * @return array
  52. */
  53. public function get($token)
  54. {
  55. //方便测试
  56. if(strpos($token,'testuid_') !== false && config('app_debug') === true){
  57. $uid = substr($token,8);
  58. return [
  59. 'user_id' => intval($uid),
  60. ];
  61. }
  62. //方便测试
  63. $data = $this->handler->where('token', $this->getEncryptedToken($token))->find();
  64. if ($data) {
  65. if (!$data['expiretime'] || $data['expiretime'] > time()) {
  66. //返回未加密的token给客户端使用
  67. $data['token'] = $token;
  68. //返回剩余有效时间
  69. $data['expires_in'] = $this->getExpiredIn($data['expiretime']);
  70. return $data;
  71. } else {
  72. self::delete($token);
  73. }
  74. }
  75. return [];
  76. }
  77. /**
  78. * 判断Token是否可用
  79. * @param string $token Token
  80. * @param int $user_id 会员ID
  81. * @return boolean
  82. */
  83. public function check($token, $user_id)
  84. {
  85. $data = $this->get($token);
  86. return $data && $data['user_id'] == $user_id ? true : false;
  87. }
  88. /**
  89. * 删除Token
  90. * @param string $token
  91. * @return boolean
  92. */
  93. public function delete($token)
  94. {
  95. $this->handler->where('token', $this->getEncryptedToken($token))->delete();
  96. return true;
  97. }
  98. /**
  99. * 删除指定用户的所有Token
  100. * @param int $user_id
  101. * @return boolean
  102. */
  103. public function clear($user_id)
  104. {
  105. $this->handler->where('user_id', $user_id)->delete();
  106. return true;
  107. }
  108. }