ResetPasswordController.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Http\Controllers\Mobile\Auth;
  3. use Aix\Sms\Contracts\Smser;
  4. use App\Exceptions\ResponseException;
  5. use App\Http\Controllers\Mobile\MobileBaseController;
  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 MobileBaseController
  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('mobile.app.auth.password', ['wap_title'=>'重置密码']);
  39. }
  40. public function passwordRequestMobile($utype)
  41. {
  42. return view('mobile.app.auth.password_mobile', ['utype'=>$utype, 'wap_title'=>'重置密码']);
  43. }
  44. public function passwordRequestEmail($utype)
  45. {
  46. return view('mobile.app.auth.password_email', ['utype'=>$utype, 'wap_title'=>'重置密码']);
  47. }
  48. public function passwordRequestPost(ResetPasswordValidatorRequest $request)
  49. {
  50. if ($request->type == 'mobile') {
  51. if (!$this->smsService->checkAuthSms($request->mobile, Smser::TEMPLATE_AUTH_CHECK, $request->mobile_vcode)) {
  52. return $this->sendErrorResponse("短信验证码不对");
  53. }
  54. $token=$this->authService->resetPasswordToken($request->all());
  55. return $this->sendSuccessResponse(['url'=>route('mobile.password.reset', ['token'=>$token])]);
  56. } elseif ($request->type == 'email') {
  57. $token=$this->authService->resetPasswordToken($request->all());
  58. $this->emailService->setCallback('App\Services\Auth\AuthService', 'sendEmailHook', [$request->email, $token])
  59. ->setCheckRoute("mobile.email.check")
  60. ->sendAuthMail($request->email, EmailService::TEMPLATE_PASSWORD_RESET);
  61. return $this->sendSuccessResponse(['url'=>route('mobile.password.reset', ['token'=>$request->email])]);
  62. }
  63. }
  64. public function passwordReset($token)
  65. {
  66. $data['token']=$token;
  67. $data['wap_title']="重置密码";
  68. if (validator_check($token, 'email')) {
  69. return view('mobile.app.auth.password_reset_email', $data);
  70. }
  71. if (!$data=$this->authService->checkResetPasswordToken($token)) {
  72. throw new ResponseException("who are you?", [], 404);
  73. }
  74. $data['token']=$token;
  75. $data['wap_title']="重置密码";
  76. return view('mobile.app.auth.password_reset', $data);
  77. }
  78. public function passwordResetPost(ResetPasswordValidatorRequest $request, $token)
  79. {
  80. if (!$data=$this->authService->checkResetPasswordToken($token)) {
  81. throw new ResponseException("who are you?", [], 404);
  82. }
  83. $utype=$this->authService->resetPassword($token, $request->password);
  84. $login_url =$utype==1?route('mobile.login.company'):route('mobile.login');
  85. return view('mobile.app.auth.password_reset_success', ['wap_title'=>'重置密码', 'login_url'=>$login_url]);
  86. }
  87. }