User.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\common;
  3. use app\common\facade\Token;
  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. $tokenData = Token::get('user:info:'.$token);
  44. if (!$tokenData) {
  45. $this->token= $token;
  46. $this->setError('用户信息已失效');
  47. $this->setErrorCode('102');
  48. return false;
  49. }
  50. if ($this->error) {
  51. return false;
  52. }
  53. $userId = intval($tokenData['user_id']);
  54. if ($tokenData['type'] == 'user' && $userId > 0) {
  55. $this->userinfo = $tokenData['user_info']?:null;
  56. return $this->loginSuccessful();
  57. }
  58. $this->setError('登录失败');
  59. return false;
  60. }
  61. public function checkToken(){
  62. return false;
  63. }
  64. public function setErrorCode(int $code):User
  65. {
  66. $this->errorCode=$code;
  67. return $this;
  68. }
  69. /**
  70. * 登录失败
  71. * @return bool
  72. */
  73. public function loginFailed(): bool
  74. {
  75. if (!$this->userinfo) {
  76. return false;
  77. }
  78. $this->token = '';
  79. $this->userinfo = null;
  80. $this->logined = false;
  81. return true;
  82. }
  83. public function checkUser(){
  84. if(!$this->userinfo) return false;
  85. return true;
  86. }
  87. public function LoginUserInfo($userInfo,$token="",$keepTime){
  88. $this->keeptime= $keepTime;
  89. $this->userinfo =$userInfo;
  90. $this->token = $token;
  91. return $this->loginSuccessful();
  92. }
  93. /** 刷新token有效期
  94. * 登录成功
  95. * @return bool
  96. */
  97. public function loginSuccessful(): bool
  98. {
  99. if (!$this->userinfo) {
  100. return false;
  101. }
  102. $this->logined = true;
  103. if ($this->token!='') {
  104. Token::set("user:info:".$this->token, 'user',$this->userinfo,$this->keeptime);
  105. }
  106. return true;
  107. }
  108. public static function instance(): User
  109. {
  110. if (is_null(self::$instance)) {
  111. self::$instance = new static();
  112. }
  113. return self::$instance;
  114. }
  115. //是否已经登录标识
  116. public function isLogin(): bool
  117. {
  118. return $this->logined;
  119. }
  120. }