GeetestService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Services\Common;
  3. use Gt\GeetestLib;
  4. use Illuminate\Support\Facades\Cache;
  5. /**
  6. * 极验服务
  7. * Class GeetestService
  8. * @package App\Services\Common
  9. * Auth Zhong
  10. * Date 2018/11/6
  11. */
  12. class GeetestService
  13. {
  14. protected $gtClient;
  15. protected $viewKey;
  16. /**
  17. * GtService constructor.
  18. */
  19. public function __construct()
  20. {
  21. $this->gtClient = new GeetestLib(
  22. config('aix.system.site_safety.site_vo_code.captcha_id'),
  23. config('aix.system.site_safety.site_vo_code.captcha_key')
  24. );
  25. }
  26. public function init($ip, $user_id)
  27. {
  28. $data=[
  29. "user_id" => $user_id, # 网站用户id
  30. "client_type" => "web", #web:电脑上的浏览器;h5:手机上的浏览器,包括移动应用内完全内置的web_view;native:通过原生SDK植入APP应用的方式
  31. "ip_address" => $ip # 请在此处传输用户请求验证时所携带的IP
  32. ];
  33. $status = $this->gtClient->pre_process($data, 1);
  34. Cache::put($user_id."_gtserver", $status, 10);
  35. return json_decode($this->gtClient->get_response_str(), true);
  36. }
  37. public function check($ip, $user_id, array $geetestData)
  38. {
  39. if (!array_has($geetestData, ['geetest_challenge', 'geetest_validate', 'geetest_seccode'])) {
  40. return 0;
  41. }
  42. if (Cache::has($user_id."_gtserver")&&Cache::get($user_id."_gtserver")==1) {
  43. $data=[
  44. "user_id" => $user_id, # 网站用户id
  45. "client_type" => "web", #web:电脑上的浏览器;h5:手机上的浏览器,包括移动应用内完全内置的web_view;native:通过原生SDK植入APP应用的方式
  46. "ip_address" => $ip # 请在此处传输用户请求验证时所携带的IP
  47. ];
  48. return $this->gtClient->success_validate(
  49. $geetestData['geetest_challenge'],
  50. $geetestData['geetest_validate'],
  51. $geetestData['geetest_seccode'],
  52. $data
  53. );
  54. } else {
  55. return $this->gtClient->fail_validate(
  56. $geetestData['geetest_challenge'],
  57. $geetestData['geetest_validate'],
  58. $geetestData['geetest_seccode']
  59. );
  60. }
  61. }
  62. public function checkGeetest(int $maxTime = 0)
  63. {
  64. if ($this->getViewTime()<$maxTime) {
  65. return true;
  66. }
  67. return $this->check(request()->ip(), request()->session()->getId(), request()->all())==1?true:false;
  68. }
  69. public function isNeedGeetest(int $maxTime)
  70. {
  71. if ($this->addViewTime()>=$maxTime) {
  72. return 1;
  73. }
  74. return 0;
  75. }
  76. private function getViewTime()
  77. {
  78. return Cache::get($this->getViewKey(), 0);
  79. }
  80. private function addViewTime()
  81. {
  82. $time=$this->getViewTime()+1;
  83. Cache::put($this->getViewKey(), $time, 10);
  84. return $time;
  85. }
  86. private function getViewKey()
  87. {
  88. if (!is_null($this->viewKey)) {
  89. return $this->viewKey;
  90. }
  91. return md5(request()->route()->getAction('controller').request()->session()->getId());
  92. }
  93. public function setViewKey($key)
  94. {
  95. $this->viewKey=$key;
  96. }
  97. }