gtClient = new GeetestLib( config('aix.system.site_safety.site_vo_code.captcha_id'), config('aix.system.site_safety.site_vo_code.captcha_key') ); } public function init($ip, $user_id) { $data=[ "user_id" => $user_id, # 网站用户id "client_type" => "web", #web:电脑上的浏览器;h5:手机上的浏览器,包括移动应用内完全内置的web_view;native:通过原生SDK植入APP应用的方式 "ip_address" => $ip # 请在此处传输用户请求验证时所携带的IP ]; $status = $this->gtClient->pre_process($data, 1); Cache::put($user_id."_gtserver", $status, 10); return json_decode($this->gtClient->get_response_str(), true); } public function check($ip, $user_id, array $geetestData) { if (!array_has($geetestData, ['geetest_challenge', 'geetest_validate', 'geetest_seccode'])) { return 0; } if (Cache::has($user_id."_gtserver")&&Cache::get($user_id."_gtserver")==1) { $data=[ "user_id" => $user_id, # 网站用户id "client_type" => "web", #web:电脑上的浏览器;h5:手机上的浏览器,包括移动应用内完全内置的web_view;native:通过原生SDK植入APP应用的方式 "ip_address" => $ip # 请在此处传输用户请求验证时所携带的IP ]; return $this->gtClient->success_validate( $geetestData['geetest_challenge'], $geetestData['geetest_validate'], $geetestData['geetest_seccode'], $data ); } else { return $this->gtClient->fail_validate( $geetestData['geetest_challenge'], $geetestData['geetest_validate'], $geetestData['geetest_seccode'] ); } } public function checkGeetest(int $maxTime = 0) { if ($this->getViewTime()<$maxTime) { return true; } return $this->check(request()->ip(), request()->session()->getId(), request()->all())==1?true:false; } public function isNeedGeetest(int $maxTime) { if ($this->addViewTime()>=$maxTime) { return 1; } return 0; } private function getViewTime() { return Cache::get($this->getViewKey(), 0); } private function addViewTime() { $time=$this->getViewTime()+1; Cache::put($this->getViewKey(), $time, 10); return $time; } private function getViewKey() { if (!is_null($this->viewKey)) { return $this->viewKey; } return md5(request()->route()->getAction('controller').request()->session()->getId()); } public function setViewKey($key) { $this->viewKey=$key; } }