User.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\api\controller;
  4. use app\common\model\Qrcode;
  5. class User extends Api
  6. {
  7. protected $allowFields = ['id', 'nickname', 'mobile', 'avatar', 'balance', 'score'];
  8. private Adapter $adapter;
  9. public function userinfo(bool $allinfo = false)
  10. {
  11. $user=$this->adapter->userinfo();
  12. if(!$user){
  13. return false;
  14. }
  15. if($allinfo){
  16. return $user;
  17. }else{
  18. return array_intersect_key($user,array_flip($this->allowFields));
  19. }
  20. }
  21. // public function userinfo()
  22. // {
  23. // $user=$this->auth->userinfo();
  24. // $this->success('',$user);
  25. // }
  26. public function logout()
  27. {
  28. $this->adapter->logout();
  29. }
  30. public function getToken()
  31. {
  32. $usertoken=$this->adapter->getUserToken();
  33. return $usertoken->token;
  34. }
  35. public function login(string $username, string $password)
  36. {
  37. $token=uuid();
  38. $user=User::where('username',$username)->find();
  39. if(!$user){
  40. throw new \Exception('账号或密码错误');
  41. }
  42. if($user->password!=md5(md5($password.$user->salt))){
  43. throw new \Exception('账号或密码错误');
  44. }
  45. if($user->status!='normal'){
  46. throw new \Exception('账号已经被禁用');
  47. }
  48. $this->adapter->login($token,$user);
  49. $this->login_user=$this->adapter->userinfo();
  50. }
  51. }