<?php
namespace App\Http\Controllers\Home;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;
class GyController extends Controller
{
private $auth_url = "https://openapi-gy.getui.com/v1/gy/auth_sign";
private $reqid_url = "https://openapi-gy.getui.com/v1/gy/verify_query";
private $token;
/*
* 获取authtoken
*/
public function authToken($type)
{
$path = public_path();
if($type==1){//ios的个验验证
$data = json_decode(file_get_contents($path.'/gytoken_ios.txt'));
$gy_secret = 'ios的密钥';
$gy_key = 'ios的key';
$gy_id = 'ios的id';
}else{// 安卓的个验验证
$data = json_decode(file_get_contents($path.'/gytoken_and.txt'));
$gy_secret = '安卓';
$gy_key = '安卓的';
$gy_id = '安卓的';
}
// 验证token,因为个验对token有限制访问频次
if(Redis::get('authToken')){
$token = Redis::get('authToken');
return $token;
}else{
$time = time()*1000;
$url = $this->auth_url;
$data =array(
'timestamp' => $time,
'appId' => $gy_id,
'sign' => hash('sha256',$gy_key.$time.$gy_secret)
);
$headers[] = 'Content-Type: application/json; charset=utf-8';
$rs = json_decode($this->curl2($url, $data,$headers), true);
if($rs['data']['result']==20000){
$token = $rs['data']['data']['authToken'];
$arr['access_token'] = $token;
$arr['expire'] = (time()+3600)*1000;
if($type==1){
file_put_contents($path.'/gytoken_ios.txt', json_encode($arr));
}else{
file_put_contents($path.'/gytoken_and.txt', json_encode($arr));
}
Redis::set('authToken',$token);
Redis::expire('authToken',3600);
return $token;
}else{
return false;
}
}
// }
}
/*
* 云验证是否成功
*/
public function check_status($gyuid, $reqId,$type)
{
$authToken = $this -> authToken($type);
if(!$authToken){
return false;
}
$url = $this->reqid_url;
if($type==1){
$gy_id = 'ios 的id';
}else{
$gy_id = '安卓的id';
}
$data = array(
'appId'=>$gy_id,
'gyuid'=> $gyuid,
'reqId'=> $reqId,
'authToken'=>$authToken
);
$headers[] = 'Content-Type: application/json; charset=utf-8';
$result = json_decode($this -> curl2($url,$data,$headers));
$success = array(20001,20002,20003,21001,22001,22002);
if($result->data->result==20000){
if(in_array($result->data->data->verify_result,$success)){
return true;
}else{
return false;
}
}else{
return false;
}
}
public function curl2($url,$data,$headers){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 60); //设置超时
if(0 === strpos(strtolower($url), 'https')) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //从证书中检查SSL加密算法是否存在
}
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$rtn = curl_exec($ch);//CURLOPT_RETURNTRANSFER 不设置 curl_exec返回TRUE 设置 curl_exec返回json(此处) 失败都返回FALSE
curl_close($ch);
return $rtn;
}
}
官网