Token.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace app\common\library;
  3. use think\Cache;
  4. /**
  5. * Token类
  6. */
  7. class Token
  8. {
  9. /**
  10. * 缓存用户的Token及UID
  11. *
  12. * @param string $token
  13. * @param int $uid
  14. * @param int $expire
  15. */
  16. public static function set($token, $uid, $expire)
  17. {
  18. Cache::set($token, $uid, $expire);
  19. }
  20. /**
  21. * 通过Token获取用户的身份标识
  22. *
  23. * @param string $token
  24. * @return string
  25. */
  26. public static function identity($token)
  27. {
  28. $uid = Cache::get($token);
  29. return $uid;
  30. }
  31. /**
  32. * 验证Token是否可用
  33. * @param string $token
  34. */
  35. public static function check($token)
  36. {
  37. return Cache::has($token);
  38. }
  39. /**
  40. * 延长Token的有效期
  41. * key 用户的Token(随机Hash)
  42. */
  43. public static function refresh($token)
  44. {
  45. $uid = Cache::get($token);
  46. Cache::set($token, $uid);
  47. }
  48. /**
  49. * 删除用户的Token缓存
  50. *
  51. * @param string $token
  52. */
  53. public static function delete($token)
  54. {
  55. Cache::rm($token);
  56. }
  57. }