User.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace app\common;
  3. use think\facade\Cache;
  4. class User {
  5. protected static $instance;
  6. protected $logined = false;
  7. protected $error='';
  8. protected $errorCode="0";
  9. protected $token = '';
  10. protected $userinfo=null;
  11. protected $keeptime=3600;
  12. //获取用户信息
  13. public function __get($name){
  14. return $this->userinfo ? $this->userinfo[$name] : null;
  15. }
  16. public function getErroCode()
  17. {
  18. return $this->errorCode ?: 1;
  19. }
  20. public function getError()
  21. {
  22. return $this->error ?: '';
  23. }
  24. public function setError($error): User
  25. {
  26. $this->error = $error;
  27. return $this;
  28. }
  29. /**
  30. * 获取会员Token
  31. * @return string
  32. */
  33. public function getToken(): string
  34. {
  35. return $this->token;
  36. }
  37. public function getUser()
  38. {
  39. return $this->userinfo;
  40. }
  41. public function init($token): bool
  42. {
  43. if($token==''){
  44. $this->setError('token参数不能为空');
  45. $this->setErrorCode('102');
  46. return false;
  47. }
  48. $tokenData = Cache::get('user:info:'.$token);
  49. if (!$tokenData) {
  50. $this->token= $token;
  51. $this->setError('用户信息已失效');
  52. $this->setErrorCode('102');
  53. return false;
  54. }
  55. if ($this->error) {
  56. return false;
  57. }
  58. $userId = intval($tokenData['id']);
  59. if ($userId > 0) {
  60. $this->userinfo = $tokenData?:null;
  61. return $this->loginSuccessful();
  62. }
  63. $this->setError('登录失败');
  64. return false;
  65. }
  66. public function checkToken(){
  67. return false;
  68. }
  69. public function setErrorCode(int $code):User
  70. {
  71. $this->errorCode=$code;
  72. return $this;
  73. }
  74. /**
  75. * 登录失败
  76. * @return bool
  77. */
  78. public function loginFailed(): bool
  79. {
  80. if (!$this->userinfo) {
  81. return false;
  82. }
  83. $this->token = '';
  84. $this->userinfo = null;
  85. $this->logined = false;
  86. return true;
  87. }
  88. public function checkUser(){
  89. if(!$this->userinfo) return false;
  90. return true;
  91. }
  92. public function LoginUserInfo($userInfo,$token="",$keepTime){
  93. $this->keeptime= $keepTime;
  94. $this->userinfo =$userInfo;
  95. $this->token = $token;
  96. return $this->loginSuccessful();
  97. }
  98. /** 刷新token有效期
  99. * 登录成功
  100. * @return bool
  101. */
  102. public function loginSuccessful(): bool
  103. {
  104. if (!$this->userinfo) {
  105. return false;
  106. }
  107. $this->logined = true;
  108. if ($this->token!='') {
  109. Cache::set("user:info:{$this->token}",$this->userinfo,$this->keeptime);
  110. }
  111. return true;
  112. }
  113. public static function instance(): User
  114. {
  115. if (is_null(self::$instance)) {
  116. self::$instance = new static();
  117. }
  118. return self::$instance;
  119. }
  120. //是否已经登录标识
  121. public function isLogin(): bool
  122. {
  123. return $this->logined;
  124. }
  125. }