123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace App\Services\Common;
- use Gt\GeetestLib;
- use Illuminate\Support\Facades\Cache;
- /**
- * 极验服务
- * Class GeetestService
- * @package App\Services\Common
- * Auth Zhong
- * Date 2018/11/6
- */
- class GeetestService
- {
- protected $gtClient;
- protected $viewKey;
- /**
- * GtService constructor.
- */
- public function __construct()
- {
- $this->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;
- }
- }
|