Driver.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\library\tokencompany;
  12. /**
  13. * Token基础类
  14. */
  15. abstract class Driver
  16. {
  17. protected $handler = null;
  18. protected $options = [];
  19. /**
  20. * 存储Token
  21. * @param string $token Token
  22. * @param int $user_id 会员ID
  23. * @param int $expire 过期时长,0表示无限,单位秒
  24. * @return bool
  25. */
  26. abstract function set($token, $user_id, $expire = 0);
  27. /**
  28. * 获取Token内的信息
  29. * @param string $token
  30. * @return array
  31. */
  32. abstract function get($token);
  33. /**
  34. * 判断Token是否可用
  35. * @param string $token Token
  36. * @param int $user_id 会员ID
  37. * @return boolean
  38. */
  39. abstract function check($token, $user_id);
  40. /**
  41. * 删除Token
  42. * @param string $token
  43. * @return boolean
  44. */
  45. abstract function delete($token);
  46. /**
  47. * 删除指定用户的所有Token
  48. * @param int $user_id
  49. * @return boolean
  50. */
  51. abstract function clear($user_id);
  52. /**
  53. * 返回句柄对象,可执行其它高级方法
  54. *
  55. * @access public
  56. * @return object
  57. */
  58. public function handler()
  59. {
  60. return $this->handler;
  61. }
  62. /**
  63. * 获取加密后的Token
  64. * @param string $token Token标识
  65. * @return string
  66. */
  67. protected function getEncryptedToken($token)
  68. {
  69. $config = \think\Config::get('token');
  70. return hash_hmac($config['hashalgo'], $token, $config['key']);
  71. }
  72. /**
  73. * 获取过期剩余时长
  74. * @param $expiretime
  75. * @return float|int|mixed
  76. */
  77. protected function getExpiredIn($expiretime)
  78. {
  79. return $expiretime ? max(0, $expiretime - time()) : 365 * 86400;
  80. }
  81. }