// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\index\controller; use app\common\library\Log; use app\index\service\passport\MailCaptcha; use think\response\Json; use app\index\service\passport\{Captcha as CaptchaService, MailCaptcha as MailCaptchaService, SmsCaptcha as SmsCaptchaService, UMailer }; use cores\exception\BaseException; use yiovo\cache\facade\Cache; /** * 验证码管理 * 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 { $data = $this->postForm(); $email = $data['mobile'] ?? ''; if (empty($email)) { return $this->renderError('Failed to send code. Please try again later'); } $smtp = new UMailer(config('smtp.host'), config('smtp.port'), true, config('smtp.username'), config('smtp.password')); $smtp->debug = false; //是否显示发送的调试信息 $flag = $smtp->sendmail($email, config('smtp.username')); if ($flag) { return $this->renderSuccess('Sent Successful!Please check your new mails.'); } log_record($smtp->getError(), 'error'); return $this->renderError('Failed to send code. Please try again later'); // $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'); } }