ResetPasswordController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Http\Controllers\Web\Auth;
  3. use Aix\Sms\Contracts\Smser;
  4. use App\Exceptions\ResponseException;
  5. use App\Http\Controllers\Web\WebBaseController;
  6. use App\Services\Auth\AuthService;
  7. use App\Services\Common\EmailService;
  8. use App\Services\Common\SmsService;
  9. use App\Services\Common\TencentCaptchaService;
  10. use App\Validators\ResetPasswordValidatorRequest;
  11. class ResetPasswordController extends WebBaseController
  12. {
  13. /**
  14. * @var SmsService
  15. */
  16. private $smsService;
  17. /**
  18. * @var AuthService
  19. */
  20. private $authService;
  21. /**
  22. * @var EmailService
  23. */
  24. private $emailService;
  25. private $tencentCaptchaService;
  26. /**
  27. * ResetPasswordController constructor.
  28. * @param SmsService $smsService
  29. * @param AuthService $authService
  30. * @param EmailService $emailService
  31. */
  32. public function __construct(SmsService $smsService, AuthService $authService, EmailService $emailService, TencentCaptchaService $tencentCaptchaService)
  33. {
  34. $this->smsService = $smsService;
  35. $this->authService = $authService;
  36. $this->emailService = $emailService;
  37. $this->tencentCaptchaService = $tencentCaptchaService;
  38. }
  39. public function passwordRequest()
  40. {
  41. return view('app.auth.password_request', ['title'=>'重置密码']);
  42. }
  43. public function passwordRequestPost(ResetPasswordValidatorRequest $request)
  44. {
  45. if ($request->type == 'mobile') {
  46. if (!$this->smsService->checkAuthSms($request->mobile, Smser::TEMPLATE_AUTH_CHECK, $request->mobile_vcode)) {
  47. return $this->sendErrorResponse("短信验证码不对");
  48. }
  49. $token=$this->authService->resetPasswordToken($request->all());
  50. return $this->sendSuccessResponse(['url'=>route('password.reset', ['token'=>$token])]);
  51. } elseif ($request->type == 'email') {
  52. if(!$this->tencentCaptchaService->check(request()->input('randstr'),request()->input('ticket'),request()->ip())){
  53. return $this->sendErrorResponse("验证码不通过,请重新验证");
  54. }
  55. $token=$this->authService->resetPasswordToken($request->all());
  56. $this->emailService->setCallback('App\Services\Auth\AuthService', 'sendEmailHook', [$request->email, $token])
  57. ->sendAuthMail($request->email, EmailService::TEMPLATE_PASSWORD_RESET);
  58. return $this->sendSuccessResponse(['url'=>route('password.reset', ['token'=>$request->email])]);
  59. }
  60. }
  61. public function passwordReset($token)
  62. {
  63. $data['token']=$token;
  64. $data['title']="重置密码";
  65. if (validator_check($token, 'email')) {
  66. return view('app.auth.password_reset_email', $data);
  67. }
  68. if (!$data=$this->authService->checkResetPasswordToken($token)) {
  69. throw new ResponseException("who are you?", [], 404);
  70. }
  71. $data['token']=$token;
  72. $data['title']="重置密码";
  73. return view('app.auth.password_reset', $data);
  74. }
  75. public function passwordResetPost(ResetPasswordValidatorRequest $request, $token)
  76. {
  77. if (!$data=$this->authService->checkResetPasswordToken($token)) {
  78. throw new ResponseException("who are you?", [], 404);
  79. }
  80. $utype=$this->authService->resetPassword($token, $request->password);
  81. $login_url =$utype==1?route('login.company'):route('login');
  82. return view('app.auth.password_reset_success', ['login_url'=>$login_url]);
  83. }
  84. }