ResetPasswordController.php 3.0 KB

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