resultRedirectFactory = $redirectFactory; $this->urlModel = $urlFactory; $this->_redirect = $redirect; $this->formKeyValidator = $formKeyValidator; $this->resultJson = $resultJsonFactory->create(); $this->dateTimeFactory = $dateTimeFactory; $this->session = $session; $this->formKey = $formKey; $this->request = $request; $this->jsonResponse = $jsonResponse; $this->aliyunSmsSenderFactory = $aliyunSmsSenderFactory; $this->logger = $logger; } protected function _beforeExecute(RequestInterface $request) { if (!$this->formKeyValidator->validate($request)) { throw new LocalizedException(__('Please refresh current page')); } if ($request->getParam('captcha_fail')) { throw new LocalizedException(__('Incorrect CAPTCHA.')); } return true; } protected function getType() { $type = $this->request->getParam('form_id'); return $type?:CheckRegisterSmsObserver::FORMID; } public function execute() { $aliyunSmsSender = $this->aliyunSmsSenderFactory->create([ 'data' => [ 'type' => $this->getType(), 'mobile' => $this->request->getParam('mobile'), ] ]); try { $this->_beforeExecute($this->request); $response = $aliyunSmsSender->send(); $data = [ 'formKey' => $this->formKey->getFormKey(), 'result' => $response->getIsSuccess() ]; if ($aliyunSmsSender->getAppConfig()->getValue('sms/sms/debug')) { $data['code'] = $aliyunSmsSender->getCode(); } $this->jsonResponse->setErrors(!$response->getIsSuccess())->setData($data)->setMessages([$response->getMessage()]); } catch (LocalizedException $e) { $this->logger->critical('Sms send fail, '.$e->getMessage()); $this->jsonResponse->setErrors(true) ->setMessages([__($e->getMessage())->render()]); // ->setMessages([__('Sms send fail')->render()]); } return $this->resultJson->setData($this->jsonResponse); } public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException { return null; } public function validateForCsrf(RequestInterface $request): ?bool { return true; } }