Captcha.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\utils;
  8. use support\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()->session()->set($key, $code);
  44. }
  45. $img_content = $captcha->get();
  46. return [
  47. 'result' => 1,
  48. 'uuid' => $key,
  49. 'image' => 'data:image/png;base64,' . base64_encode($img_content)
  50. ];
  51. }
  52. /**
  53. * 数字验证码
  54. * @param string $key
  55. * @param int $length
  56. * @return array
  57. */
  58. public static function numberCaptcha(string $key, int $length = 4): array
  59. {
  60. $code = str_pad(rand(0, 999999), $length, '0', STR_PAD_LEFT);
  61. $mode = config('plugin.saiadmin.saithink.captcha.mode', 'session');
  62. $expire = config('plugin.saiadmin.saithink.captcha.expire', 300);
  63. if ($mode === 'cache') {
  64. try {
  65. Cache::set($key, $code, $expire);
  66. } catch (\Exception $e) {
  67. return [
  68. 'result' => -1,
  69. 'message' => '验证码获取失败,请检查缓存配置'
  70. ];
  71. }
  72. } else {
  73. request()->session()->set($key, $code);
  74. }
  75. return [
  76. 'result' => 1,
  77. 'uuid' => $key,
  78. 'code' => $code,
  79. ];
  80. }
  81. /**
  82. * 验证码验证
  83. * @param string $uuid
  84. * @param string|int $captcha
  85. * @return bool
  86. */
  87. public static function checkCaptcha(string $uuid, string|int $captcha): bool
  88. {
  89. $mode = config('plugin.saiadmin.saithink.captcha.mode', 'session');
  90. if ($mode === 'cache') {
  91. try {
  92. $code = Cache::get($uuid);
  93. Cache::delete($uuid);
  94. } catch (\Exception $e) {
  95. throw new ApiException($e->getMessage());
  96. }
  97. } else {
  98. try {
  99. $code = session($uuid);
  100. session()->forget($uuid);
  101. } catch (\Exception $e) {
  102. throw new ApiException($e->getMessage());
  103. }
  104. }
  105. if (strtolower($captcha) !== $code) {
  106. return false;
  107. }
  108. return true;
  109. }
  110. }