Captcha.php 3.9 KB

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