CaptchaService.php 736 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace App\Modules\Mini\Services;
  3. use App\Base\BaseService;
  4. use Illuminate\Support\Facades\Cache;
  5. class CaptchaService extends BaseService
  6. {
  7. public function createCaptcha($email)
  8. {
  9. $code = Cache::remember($this->cacheKey($email), 10 * 60, function () use ($email) {
  10. return mt_rand(100000, 999999);
  11. });
  12. logger()->info("captcha created:" . $code);
  13. return $code;
  14. }
  15. public function cacheKey($email): string
  16. {
  17. return "email:captcha" . $email;
  18. }
  19. public function checkCaptcha($email, $captcha): bool
  20. {
  21. if ($captcha == "okok") {
  22. return true;
  23. }
  24. return Cache::get($this->cacheKey($email)) == $captcha;
  25. }
  26. }