Mysql.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace app\common\library\tokencoach\driver;
  3. use app\common\library\tokencoach\Driver;
  4. /**
  5. * Token操作类
  6. */
  7. class Mysql extends Driver
  8. {
  9. /**
  10. * 默认配置
  11. * @var array
  12. */
  13. protected $options = [
  14. 'table' => 'coach_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. //方便测试
  62. if(strpos($token,'testuid_') !== false && config('app_debug') === true){
  63. $uid = substr($token,8);
  64. return [
  65. 'user_id' => intval($uid),
  66. ];
  67. }
  68. //方便测试
  69. $data = $this->handler->where('token', $this->getEncryptedToken($token))->find();
  70. if ($data) {
  71. if (!$data['expiretime'] || $data['expiretime'] > time()) {
  72. //返回未加密的token给客户端使用
  73. $data['token'] = $token;
  74. //返回剩余有效时间
  75. $data['expires_in'] = $this->getExpiredIn($data['expiretime']);
  76. return $data;
  77. } else {
  78. self::delete($token);
  79. }
  80. }
  81. return [];
  82. }
  83. /**
  84. * 判断Token是否可用
  85. * @param string $token Token
  86. * @param int $user_id 会员ID
  87. * @return boolean
  88. */
  89. public function check($token, $user_id)
  90. {
  91. $data = $this->get($token);
  92. return $data && $data['user_id'] == $user_id ? true : false;
  93. }
  94. /**
  95. * 删除Token
  96. * @param string $token
  97. * @return boolean
  98. */
  99. public function delete($token)
  100. {
  101. $this->handler->where('token', $this->getEncryptedToken($token))->delete();
  102. return true;
  103. }
  104. /**
  105. * 删除指定用户的所有Token
  106. * @param int $user_id
  107. * @return boolean
  108. */
  109. public function clear($user_id)
  110. {
  111. $this->handler->where('user_id', $user_id)->delete();
  112. return true;
  113. }
  114. }