ApiAuthService.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\api\service\auth;
  4. use app\api\service\auth\Adapter;
  5. use think\facade\Cache;
  6. use app\common\model\User;
  7. use app\common\model\UserToken;
  8. use think\facade\Config;
  9. class ApiAuthService
  10. {
  11. protected $allowFields = ['id', 'nickname', 'mobile', 'avatar', 'balance', 'score'];
  12. public function userinfo(bool $allinfo = false)
  13. {
  14. }
  15. public function logout()
  16. {
  17. //$this->adapter->logout();
  18. }
  19. public function getToken()
  20. {
  21. //$usertoken=$this->adapter->getUserToken();
  22. //return $usertoken->token;
  23. }
  24. public function login(string $username, string $password)
  25. {
  26. $token=uuid();
  27. $user=User::where('username',$username)->find();
  28. if(!$user){
  29. throw new \Exception('账号或密码错误');
  30. }
  31. if($user->password!=md5(md5($password.$user->salt))){
  32. throw new \Exception('账号或密码错误');
  33. }
  34. if($user->status!='normal'){
  35. throw new \Exception('账号已经被禁用');
  36. }
  37. //刷新token
  38. $token = MysqlAdapter::login($token, $user);
  39. $user->loginfailure = 0;
  40. $user->logintime = time();
  41. $user->loginip = request()->ip();
  42. $user->save();
  43. Cache::set('user_info_'.$user->id, $user->toArray(), Config::get('app.user_login.keepalive_time'));
  44. return ['userinfo'=>$user,'token'=>$token];
  45. }
  46. public function updateToken(int $uid, array $arr)
  47. {
  48. return UserToken::where('user_id', $uid)->update($arr);
  49. }
  50. }