AuthService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * @Name
  4. * @Description
  5. * @Author 刘学玺
  6. * @Date 2024/8/23 10:16
  7. */
  8. namespace App\Http\Services\Backend\Server\System;
  9. use App\Enums\Common\Status;
  10. use App\Enums\System\Login\LogType;
  11. use App\Exceptions\ApiException;
  12. use App\Http\Requests\Backend\Server\System\AuthRequest;
  13. use App\Http\Services\Service;
  14. use App\Models\System\User;
  15. use BadMethodCallException;
  16. use Exception;
  17. use Illuminate\Support\Facades\Auth;
  18. use Illuminate\Support\Facades\Hash;
  19. use Illuminate\Support\Str;
  20. use Illuminate\Validation\ValidationException;
  21. use Symfony\Component\HttpFoundation\Response;
  22. class AuthService extends Service
  23. {
  24. /**
  25. * @throws ApiException
  26. */
  27. public function login($request)
  28. {
  29. // $params = $request->safe(['username','password']);
  30. // 校验验证码
  31. // validateCaptcha($data)
  32. // $verifyCodeResult = (new SmsService())->verifyCode($mobile, intval($code));
  33. // if(!$verifyCodeResult) return $this->fail('验证码错误!', 400);
  34. // 使用账号密码,进行登录
  35. $user = $this->authenticate($request);
  36. // 创建 Token 令牌,记录登录日志
  37. return $this->createTokenAfterLoginSuccess($user, LogType::LOGIN_USERNAME);
  38. }
  39. /**
  40. * @throws ApiException
  41. * @throws ValidationException
  42. */
  43. protected function authenticate(AuthRequest $request)
  44. {
  45. $request->authenticate();
  46. // 校验是否禁用
  47. $user = $request->user();
  48. if ($user->status === Status::DISABLE) {
  49. //createLoginLog(user.getId(), username, logTypeEnum, LoginResultEnum.USER_DISABLED);
  50. $this->error('AUTH_LOGIN_USER_DISABLED');
  51. }
  52. return $user;
  53. }
  54. /**
  55. * @throws ApiException
  56. */
  57. protected function createTokenAfterLoginSuccess($user, $logType)
  58. {
  59. // $password = Hash::make($user['password']);
  60. // $IsExistUser = User::query()->where('username',$user['name'])->where('password', $password)->exists();
  61. // if(!$IsExistUser){
  62. // //日志
  63. // //登录次数限制
  64. // $this->error('账号密码错误', 401);
  65. // }
  66. // 清理token历史
  67. $user->tokens()->where('tokenable_type', $user::class)->where('name', $user->username)->delete();
  68. $tokenResult = $user->createToken($user->username);
  69. $token = $tokenResult->plainTextToken;
  70. !$token && $this->error('授权错误', 401);
  71. // 设置refreshToken
  72. // $token->refreshToken = Str::random(40);
  73. // $token->save();
  74. // $tokenResult->last_used_at = time();
  75. // $tokenResult->save();
  76. $user->login_date = time();
  77. $user->login_ip = request()->getClientIp();
  78. $user->save();
  79. // id: number // 编号
  80. // accessToken: string // 访问令牌
  81. // refreshToken: string // 刷新令牌
  82. // userId: number // 用户编号
  83. // userType: number //用户类型
  84. // clientId: string //客户端编号
  85. // expiresTime: number //过期时间
  86. return ['userId' => $user->id, 'userType' => 1, 'accessToken' => $token,'refreshToken' => $token];
  87. }
  88. }