Captcha.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\utils;
  8. use support\think\Cache;
  9. use Ramsey\Uuid\Uuid;
  10. use Webman\Captcha\CaptchaBuilder;
  11. use Webman\Captcha\PhraseBuilder;
  12. use plugin\saiadmin\exception\ApiException;
  13. /**
  14. * 验证码工具类
  15. */
  16. class Captcha
  17. {
  18. /**
  19. * 图形验证码
  20. * @return array
  21. */
  22. public static function imageCaptcha(): array
  23. {
  24. $builder = new PhraseBuilder(4, 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ');
  25. $captcha = new CaptchaBuilder(null, $builder);
  26. $captcha->setBackgroundColor(242, 243, 245);
  27. $captcha->build(120, 36);
  28. $uuid = Uuid::uuid4();
  29. $key = $uuid->toString();
  30. $mode = config('plugin.saiadmin.saithink.captcha.mode', 'session');
  31. $expire = config('plugin.saiadmin.saithink.captcha.expire', 300);
  32. $code = strtolower($captcha->getPhrase());
  33. if ($mode === 'cache') {
  34. try {
  35. Cache::set($key, $code, $expire);
  36. } catch (\Exception $e) {
  37. return [
  38. 'result' => -1,
  39. 'message' => '验证码获取失败,请检查缓存配置'
  40. ];
  41. }
  42. } else {
  43. $request = request();
  44. if ($request) {
  45. $request->session()->set($key, $code);
  46. }
  47. }
  48. $img_content = $captcha->get();
  49. return [
  50. 'result' => 1,
  51. 'uuid' => $key,
  52. // CaptchaBuilder::get() outputs JPEG data via imagejpeg().
  53. 'image' => 'data:image/jpeg;base64,' . base64_encode($img_content)
  54. ];
  55. }
  56. /**
  57. * 数字验证码
  58. * @param string $key
  59. * @param int $length
  60. * @return array
  61. */
  62. public static function numberCaptcha(string $key, int $length = 4): array
  63. {
  64. $code = str_pad(rand(0, 999999), $length, '0', STR_PAD_LEFT);
  65. $mode = config('plugin.saiadmin.saithink.captcha.mode', 'session');
  66. $expire = config('plugin.saiadmin.saithink.captcha.expire', 300);
  67. if ($mode === 'cache') {
  68. try {
  69. Cache::set($key, $code, $expire);
  70. } catch (\Exception $e) {
  71. return [
  72. 'result' => -1,
  73. 'message' => '验证码获取失败,请检查缓存配置'
  74. ];
  75. }
  76. } else {
  77. $request = request();
  78. if ($request) {
  79. $request->session()->set($key, $code);
  80. }
  81. }
  82. return [
  83. 'result' => 1,
  84. 'uuid' => $key,
  85. 'code' => $code,
  86. ];
  87. }
  88. /**
  89. * 验证码验证
  90. * @param string $uuid
  91. * @param string|int $captcha
  92. * @return bool
  93. */
  94. public static function checkCaptcha(string $uuid, string|int $captcha): bool
  95. {
  96. $mode = config('plugin.saiadmin.saithink.captcha.mode', 'session');
  97. if ($mode === 'cache') {
  98. try {
  99. $code = Cache::get($uuid);
  100. Cache::delete($uuid);
  101. } catch (\Exception $e) {
  102. throw new ApiException($e->getMessage());
  103. }
  104. } else {
  105. try {
  106. $code = session($uuid);
  107. session()->forget($uuid);
  108. } catch (\Exception $e) {
  109. throw new ApiException($e->getMessage());
  110. }
  111. }
  112. if (strtolower($captcha) !== $code) {
  113. return false;
  114. }
  115. return true;
  116. }
  117. }