// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\index\controller; use app\index\service\passport\MailCaptcha; use think\response\Json; use app\index\service\passport\{Captcha as CaptchaService, MailCaptcha as MailCaptchaService, SmsCaptcha as SmsCaptchaService}; use cores\exception\BaseException; /** * 验证码管理 * Class Captcha * @package app\api\controller */ class Captcha extends Controller { /** * 图形验证码 * @return Json */ public function image(): Json { $CaptchaService = new CaptchaService; return $this->renderSuccess($CaptchaService->create()); } /** * 发送短信验证码 * @return Json * @throws BaseException */ public function sendSmsCaptcha(): Json { $SmsCaptchaService = new SmsCaptchaService; if ($SmsCaptchaService->handle($this->postForm())) { return $this->renderSuccess('发送成功,请注意查收'); } return $this->renderError($SmsCaptchaService->getError() ?: '短信发送失败'); } /** * 发送邮箱验证码 * @return Json * @throws BaseException */ public function sendEmailCaptcha(): Json { //todo return $this->renderSuccess('Sent Successful!Please check your new mails.'); $MailCaptchaService = new MailCaptchaService; if ($MailCaptchaService->handle($this->postForm())) { return $this->renderSuccess('Sent Successful!Please check your new mails.'); } return $this->renderError($MailCaptchaService->getError() ?: 'Failed to send code. Please try again later'); } public function checkEmailCaptcha(): Json { $code = $this->request->param('smsCode'); $mobile = $this->request->param('mobile'); $mailCaptcha = new MailCaptcha(); $flag = $mailCaptcha->checkCaptcha($code, $mobile); if ($flag) { return $this->renderSuccess('Successful.'); } return $this->renderError('not match'); } }