Captcha.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\common\library\Log;
  14. use app\index\service\passport\MailCaptcha;
  15. use think\response\Json;
  16. use app\index\service\passport\{Captcha as CaptchaService,
  17. MailCaptcha as MailCaptchaService,
  18. SmsCaptcha as SmsCaptchaService,
  19. UMailer
  20. };
  21. use cores\exception\BaseException;
  22. use yiovo\cache\facade\Cache;
  23. /**
  24. * 验证码管理
  25. * Class Captcha
  26. * @package app\api\controller
  27. */
  28. class Captcha extends Controller
  29. {
  30. /**
  31. * 图形验证码
  32. * @return Json
  33. */
  34. public function image(): Json
  35. {
  36. $CaptchaService = new CaptchaService;
  37. return $this->renderSuccess($CaptchaService->create());
  38. }
  39. /**
  40. * 发送短信验证码
  41. * @return Json
  42. * @throws BaseException
  43. */
  44. public function sendSmsCaptcha(): Json
  45. {
  46. $SmsCaptchaService = new SmsCaptchaService;
  47. if ($SmsCaptchaService->handle($this->postForm())) {
  48. return $this->renderSuccess('发送成功,请注意查收');
  49. }
  50. return $this->renderError($SmsCaptchaService->getError() ?: '短信发送失败');
  51. }
  52. /**
  53. * 发送邮箱验证码
  54. * @return Json
  55. * @throws BaseException
  56. */
  57. public function sendEmailCaptcha(): Json
  58. {
  59. $data = $this->postForm();
  60. $email = $data['mobile'] ?? '';
  61. if (empty($email)) {
  62. return $this->renderError('Failed to send code. Please try again later');
  63. }
  64. $smtp = new UMailer(config('smtp.host'), config('smtp.port'), true,
  65. config('smtp.username'), config('smtp.password'));
  66. $smtp->debug = false; //是否显示发送的调试信息
  67. $flag = $smtp->sendmail($email, config('smtp.username'));
  68. if ($flag) {
  69. return $this->renderSuccess('Sent Successful!Please check your new mails.');
  70. }
  71. log_record($smtp->getError(), 'error');
  72. return $this->renderError('Failed to send code. Please try again later');
  73. // $MailCaptchaService = new MailCaptchaService;
  74. // if ($MailCaptchaService->handle($this->postForm())) {
  75. // return $this->renderSuccess('Sent Successful!Please check your new mails.');
  76. // }
  77. // return $this->renderError($MailCaptchaService->getError() ?: 'Failed to send code. Please try again later');
  78. }
  79. public function checkEmailCaptcha(): Json
  80. {
  81. $code = $this->request->param('smsCode');
  82. $mobile = $this->request->param('mobile');
  83. $mailCaptcha = new MailCaptcha();
  84. $flag = $mailCaptcha->checkCaptcha($code, $mobile);
  85. if ($flag) {
  86. return $this->renderSuccess('Successful.');
  87. }
  88. return $this->renderError('not match');
  89. }
  90. }