ApiAuthService.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 login(string $username, string $password, int $terminal = 0)
  13. {
  14. $token=uuid();
  15. $user=User::where('username',$username)->find();
  16. if(!$user){
  17. throw new \Exception('账号或密码错误');
  18. }
  19. if($user->password!=md5(md5($password.$user->salt))){
  20. throw new \Exception('账号或密码错误');
  21. }
  22. if($user->status!='normal'){
  23. throw new \Exception('账号已经被禁用');
  24. }
  25. if($terminal==1 && strpos($user->role, "3") === false){
  26. throw new \Exception('账号已经被禁用');
  27. }
  28. //刷新token
  29. $token = MysqlAdapter::login($token, $user);
  30. $user->loginfailure = 0;
  31. $user->logintime = time();
  32. $user->loginip = request()->ip();
  33. $user->save();
  34. Cache::store('redis')->set('user_info_'.$user->id, $user->toArray(), Config::get('app.user_login.keepalive_time'));
  35. return ['userinfo'=>$user,'token'=>$token];
  36. }
  37. public function updateToken(int $uid, array $arr)
  38. {
  39. return UserToken::where('user_id', $uid)->update($arr);
  40. }
  41. }