127 lines
3.5 KiB
PHP
127 lines
3.5 KiB
PHP
<?php
|
||
// app/common/crypto/Idingju.php
|
||
namespace app\common\crypto;
|
||
|
||
class Idingju
|
||
{
|
||
public $orgNo;
|
||
public $appKey;
|
||
public $prefixUrl;
|
||
|
||
public function __construct($company)
|
||
{
|
||
$this->orgNo = $company['orgNo'];
|
||
$this->appKey = $company['appKey'];
|
||
$this->prefixUrl = $company['url'];
|
||
}
|
||
|
||
/**
|
||
获取Token
|
||
*/
|
||
public function getToken($orgNo){
|
||
$filePath = public_path().'temp/'.$orgNo.'.txt';
|
||
$data = [];
|
||
$accessToken = '';
|
||
if(file_exists($filePath)){
|
||
$content = file_get_contents($filePath);
|
||
$data = $content ? json_decode($content,true) : [];
|
||
}
|
||
|
||
if(!empty($data) && $data['expires_time'] > time()){
|
||
$accessToken = $data['access_token'];
|
||
}else{
|
||
$url = $this->prefixUrl.'/api/open/getToken';
|
||
$timestamp = time();
|
||
$params = [
|
||
'appid' => $this->orgNo,
|
||
'appsecret' => $this->appKey,
|
||
];
|
||
$header = [
|
||
'Content-Type: application/json',
|
||
];
|
||
$ret = $this->request($url,json_encode($params),'post',$header);
|
||
$respone = json_decode($ret,true);
|
||
if(isset($respone['code']) && $respone['code'] == 1){
|
||
$data = json_encode($respone['data']);
|
||
$accessToken = $respone['data']['access_token'];
|
||
file_put_contents($filePath,$data);
|
||
}else{
|
||
print_r($url);
|
||
print_r('<br/>');
|
||
print_r($params);
|
||
print_r('<br/>');
|
||
print_r($respone);
|
||
exit;
|
||
}
|
||
}
|
||
if($accessToken == ''){
|
||
echo '获取Token失败';
|
||
return false;
|
||
}
|
||
return $accessToken;
|
||
}
|
||
|
||
/**
|
||
生成签名
|
||
$params : 请求参数数组
|
||
$token : access_token
|
||
$timestamp : 时间戳
|
||
$api : 当前请求接口除去域名的部分,如:/api/gasoline/recharge
|
||
*/
|
||
public function signature($params,$token,$timestamp,$api){
|
||
$params['token'] = $token;
|
||
sort($params, SORT_STRING);
|
||
// $tmpStr = implode($params);
|
||
$tmpStr = implode($params);
|
||
|
||
$str = $api.$token.$tmpStr.$timestamp;
|
||
$sign = sha1($str);
|
||
|
||
return $sign;
|
||
}
|
||
|
||
/**
|
||
网络请求方法
|
||
*/
|
||
public function request($url,$params,$method,$header = [],$gzip = false,$httpcode = false){
|
||
if(!$url) return;
|
||
$ch = curl_init();
|
||
if($method == 'get'){
|
||
$paramsUrl = http_build_query($params);
|
||
if(strpos($url,'?')){
|
||
$url .= '&'.$paramsUrl;
|
||
}else{
|
||
$url .= '?'.$paramsUrl;
|
||
}
|
||
}else{
|
||
curl_setopt($ch, CURLOPT_POST, 1);
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
|
||
}
|
||
|
||
if($header){
|
||
curl_setopt($ch, CURLOPT_HTTPHEADER , $header);
|
||
}
|
||
|
||
|
||
if($gzip) curl_setopt($ch, CURLOPT_ENCODING, "gzip");
|
||
|
||
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||
curl_setopt($ch, CURLOPT_URL , $url);
|
||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , 0);
|
||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST , 0);
|
||
|
||
$cEncode = curl_exec($ch);
|
||
if($httpcode){
|
||
$cEncode = [
|
||
'cEncode'=>$cEncode,
|
||
'httpCode'=>curl_getinfo($ch,CURLINFO_HTTP_CODE)
|
||
];
|
||
}
|
||
curl_close($ch);
|
||
|
||
return $cEncode;
|
||
}
|
||
}
|
||
|