Captcha.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\index\controller;
  13. use app\index\service\passport\MailCaptcha;
  14. use think\response\Json;
  15. use app\api\service\passport\{Captcha as CaptchaService,
  16. MailCaptcha as MailCaptchaService,
  17. SmsCaptcha as SmsCaptchaService};
  18. use cores\exception\BaseException;
  19. /**
  20. * 验证码管理
  21. * Class Captcha
  22. * @package app\api\controller
  23. */
  24. class Captcha extends Controller
  25. {
  26. /**
  27. * 图形验证码
  28. * @return Json
  29. */
  30. public function image(): Json
  31. {
  32. $CaptchaService = new CaptchaService;
  33. return $this->renderSuccess($CaptchaService->create());
  34. }
  35. /**
  36. * 发送短信验证码
  37. * @return Json
  38. * @throws BaseException
  39. */
  40. public function sendSmsCaptcha(): Json
  41. {
  42. $SmsCaptchaService = new SmsCaptchaService;
  43. if ($SmsCaptchaService->handle($this->postForm())) {
  44. return $this->renderSuccess('发送成功,请注意查收');
  45. }
  46. return $this->renderError($SmsCaptchaService->getError() ?: '短信发送失败');
  47. }
  48. /**
  49. * 发送邮箱验证码
  50. * @return Json
  51. * @throws BaseException
  52. */
  53. public function sendEmailCaptcha(): Json
  54. {
  55. $MailCaptchaService = new MailCaptchaService;
  56. if ($MailCaptchaService->handle($this->postForm())) {
  57. return $this->renderSuccess('Sent Successful!Please check your new mails.');
  58. }
  59. return $this->renderError($MailCaptchaService->getError() ?: '短信发送失败');
  60. }
  61. public function checkEmailCaptcha(): Json
  62. {
  63. $code = $this->request->param('smsCode');
  64. $mobile = $this->request->param('mobile');
  65. $mailCaptcha = new MailCaptcha();
  66. $flag = $mailCaptcha->checkCaptcha($code, $mobile);
  67. if ($flag) {
  68. return $this->renderSuccess('Sent Successful!Please check your new mails.');
  69. }
  70. return $this->renderError('not match');
  71. }
  72. }