控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Mysql.php 4.0KB

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