AuthService.php 2.9 KB

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