Files
WashCar/Action/Init.php
2021-06-01 16:12:27 +08:00

177 lines
4.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace XuanChen\PingAnSdk\Action;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
class Init
{
protected $appId;
protected $appSecret;
protected $apiVersion = 'v2';
protected $params;
protected $sign;
protected $url;
public function __construct()
{
$this->appId = config('pingan.appId');
$this->appSecret = config('pingan.appSecret');
}
/**
* Notes: 设置数据
* @Author: 玄尘
* @Date : 2021/6/1 14:37
* @param $data
* @return $this
*/
public function setParams($data)
{
//判断是否有sign有的话是传来的值
if (isset($data['sign'])) {
$this->sign = $data['sign'];
} else {
$data = array_merge($data, [
'appId' => $this->appId,
'apiVersion' => $this->apiVersion,
'timestamp' => $this->getMillisecond(),
]);
}
$this->params = $data;
return $this;
}
/**
* Notes: 获取传入值
* @Author: 玄尘
* @Date : 2021/6/1 15:31
* @return mixed
*/
public function getParams()
{
$params = $this->params;
$params['sign'] = $this->getSign();
return $params;
}
/**
* Notes: 获取签名
* @Author: 玄尘
* @Date : 2021/6/1 14:32
*/
public function getSign()
{
$str = $this->getSignString();
return md5($str);
}
/**
* Notes: 检验
* @Author: 玄尘
* @Date : 2021/6/1 15:32
*/
public function checkSign()
{
$sign = $this->getSign();
if ($sign == $this->sign) {
return true;
}
return false;
}
/**
* Notes: 封装跳转链接
* @Author: 玄尘
* @Date : 2021/6/1 15:25
* @return string
*/
public function getUrl()
{
$params = $this->params;
$params['sign'] = $this->getSign();
return $this->url . http_build_query($this->params);
}
/**
* Notes: 获取时间戳
* @Author: 玄尘
* @Date : 2021/6/1 14:32
* @return float
*/
private function getMillisecond()
{
[$msec, $sec] = explode(' ', microtime());
$msectime = (float) sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
$msectime = explode('.', $msectime);
return $msectime[0];
}
/**
* Notes: description
* @Author: 玄尘
* @Date : 2021/6/1 14:32
*/
public function getSignString()
{
$params = $this->params;
if (empty($params)) {
throw new \Exception('获取校验数据失败,缺少数据.');
}
if (isset($params['sign'])) {
unset($params['sign']);
}
ksort($params);
return http_build_query($params) . $this->appSecret;
}
/**
* 通用获取数据接口
* @param [type] $url 请求地址
* @param array $query 传递参数
* @param array $json 需要传的json数据
* @param string $method 方式
* @return array|mixed [type] [description]
*/
public function http($url, $query, $method = 'POST')
{
try {
$client = new Client();
$response = $client->request($method, $url, $query);
$body = $response->getBody();
$content = $body->getContents();
$result = json_decode($content, true);
if ($result['ret'] > 0) {
$retData = $result['msg'];
} else {
$retData = $result['data'];
}
return $retData;
} catch (RequestException $e) {
return ['ret' => '99999', $e->getMessage()];
}
}
}