Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 12f5f71817 |
@@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace XuanChen\PingAnSdk\Action;
|
|
||||||
|
|
||||||
class Info extends Init
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
176
Action/Init.php
176
Action/Init.php
@@ -1,176 +0,0 @@
|
|||||||
<?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()];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace XuanChen\PingAnSdk\Action;
|
|
||||||
|
|
||||||
class Info extends Conr
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"name": "xuanchen/pingan_washcar",
|
"name": "xuanchen/washcar",
|
||||||
"description": "平安sdk",
|
"description": "平安sdk",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"homepage": "http://git.yuzhankeji.cn/xuanchen/PingAnWashCar.git",
|
"homepage": "http://git.yuzhankeji.cn/xuanchen/PingAnWashCar.git",
|
||||||
@@ -16,13 +16,13 @@
|
|||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"XuanChen\\PingAnSdk\\": "src/"
|
"XuanChen\\WashCar\\": "src/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"extra": {
|
"extra": {
|
||||||
"laravel": {
|
"laravel": {
|
||||||
"providers": [
|
"providers": [
|
||||||
"XuanChen\\PingAnSdk\\ServiceProvider"
|
"XuanChen\\WashCar\\ServiceProvider"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace XuanChen\PingAnSdk\Action;
|
namespace XuanChen\WashCar\Action;
|
||||||
|
|
||||||
class BaseAction extends Init
|
class BaseAction extends Init
|
||||||
{
|
{
|
||||||
|
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
re
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,79 +1,118 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace XuanChen\PingAnSdk\Action;
|
namespace XuanChen\WashCar\Action;
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
use GuzzleHttp\Exception\RequestException;
|
use GuzzleHttp\Exception\RequestException;
|
||||||
|
|
||||||
/**
|
|
||||||
* 超市购物券
|
|
||||||
*/
|
|
||||||
class Init
|
class Init
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $this_type;
|
protected $appId;
|
||||||
|
|
||||||
protected $baseUri;
|
protected $appSecret;
|
||||||
|
|
||||||
protected $tokenUri;
|
protected $apiVersion = 'v2';
|
||||||
|
|
||||||
protected $client_id;
|
protected $params;
|
||||||
|
|
||||||
protected $grant_type;
|
protected $sign;
|
||||||
|
|
||||||
protected $client_secret;
|
protected $url;
|
||||||
|
|
||||||
protected $access_token;
|
|
||||||
|
|
||||||
protected $userName;
|
|
||||||
|
|
||||||
protected $error;
|
|
||||||
|
|
||||||
protected $aes_code; //aes 密钥
|
|
||||||
|
|
||||||
protected $log;//日志
|
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$appId = config('pingan.appId');
|
$this->appId = config('pingan.appId');
|
||||||
|
$this->appSecret = config('pingan.appSecret');
|
||||||
$this->baseUri = $pingan['Uri'];
|
|
||||||
$this->tokenUri = $pingan['tokenUri'];
|
|
||||||
$this->client_id = $pingan['client_id'];
|
|
||||||
$this->grant_type = $pingan['grant_type'];
|
|
||||||
$this->userName = $pingan['userName'];
|
|
||||||
$this->client_secret = $pingan['client_secret'];
|
|
||||||
$this->aes_code = $pingan['AES_CODE'];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取access_token
|
* Notes: 设置数据
|
||||||
* @return void [type] [description]
|
* @Author: 玄尘
|
||||||
|
* @Date : 2021/6/1 14:37
|
||||||
|
* @param $data
|
||||||
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function getToken()
|
public function setParams($data)
|
||||||
{
|
{
|
||||||
//从数据库里找token
|
//判断是否有sign,有的话是传来的值
|
||||||
$token = PinganToken::where('type', $this->this_type)->orderBy('id', 'desc')->first();
|
if (isset($data['sign'])) {
|
||||||
|
$this->sign = $data['sign'];
|
||||||
if ($token) {
|
|
||||||
$access_token = $token->access_token;
|
|
||||||
$expires_in = $token->expires_in;
|
|
||||||
$get_token_time = $token->get_token_time;
|
|
||||||
$diffMinutes = $get_token_time->diffInMinutes(now(), false);
|
|
||||||
if ($diffMinutes < $expires_in) {
|
|
||||||
$this->access_token = $access_token;
|
|
||||||
} else {
|
|
||||||
$this->getAjaxToken();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
$this->getAjaxToken();
|
$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 getMsecTime()
|
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());
|
[$msec, $sec] = explode(' ', microtime());
|
||||||
$msectime = (float) sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
|
$msectime = (float) sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
|
||||||
@@ -83,48 +122,25 @@ class Init
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 请求平台 access_token
|
* Notes: description
|
||||||
* @return void [type] [description]
|
* @Author: 玄尘
|
||||||
|
* @Date : 2021/6/1 14:32
|
||||||
*/
|
*/
|
||||||
public function getAjaxToken()
|
public function getSignString()
|
||||||
{
|
{
|
||||||
$params = [
|
$params = $this->params;
|
||||||
'client_id' => $this->client_id,
|
|
||||||
'grant_type' => $this->grant_type,
|
|
||||||
'client_secret' => $this->client_secret,
|
|
||||||
];
|
|
||||||
|
|
||||||
try {
|
if (empty($params)) {
|
||||||
$log = $this->createLog($this->tokenUri, 'POST', $params, 'pingan');
|
throw new \Exception('获取校验数据失败,缺少数据.');
|
||||||
|
|
||||||
$client = new Client();
|
|
||||||
$response = $client->request('POST', $this->tokenUri, [
|
|
||||||
'form_params' => $params,
|
|
||||||
]);
|
|
||||||
$body = $response->getBody();
|
|
||||||
$content = $body->getContents();
|
|
||||||
$result = json_decode($content, true);
|
|
||||||
|
|
||||||
$this->updateLog($log, $result); //更新日志
|
|
||||||
|
|
||||||
if ($result['ret'] > 0) {
|
|
||||||
$this->error = $result['msg'];
|
|
||||||
} else {
|
|
||||||
$data = $result['data'];
|
|
||||||
PinganToken::create([
|
|
||||||
'type' => $this->this_type,
|
|
||||||
'access_token' => $data['access_token'],
|
|
||||||
'expires_in' => $data['expires_in'],
|
|
||||||
'get_token_time' => now(),
|
|
||||||
]);
|
|
||||||
$this->access_token = $data['access_token'];
|
|
||||||
$this->error = '';
|
|
||||||
}
|
|
||||||
} catch (RequestException $e) {
|
|
||||||
$this->error = $e->getMessage();
|
|
||||||
$this->updateLog($log, [$this->error]); //更新日志
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isset($params['sign'])) {
|
||||||
|
unset($params['sign']);
|
||||||
|
}
|
||||||
|
|
||||||
|
ksort($params);
|
||||||
|
|
||||||
|
return http_build_query($params) . $this->appSecret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -135,32 +151,11 @@ class Init
|
|||||||
* @param string $method 方式
|
* @param string $method 方式
|
||||||
* @return array|mixed [type] [description]
|
* @return array|mixed [type] [description]
|
||||||
*/
|
*/
|
||||||
public function getPingAnData($url, $query = [], $json = [], $method = 'POST')
|
public function http($url, $query, $method = 'POST')
|
||||||
{
|
{
|
||||||
$this->getToken();
|
|
||||||
|
|
||||||
if ($this->error) {
|
|
||||||
return $this->error;
|
|
||||||
}
|
|
||||||
|
|
||||||
$postData = [
|
|
||||||
'query' => array_merge([
|
|
||||||
'access_token' => $this->access_token,
|
|
||||||
'request_id' => $this->getMsecTime(),
|
|
||||||
'userName' => $this->userName,
|
|
||||||
], $query),
|
|
||||||
'json' => $json,
|
|
||||||
'headers' => [
|
|
||||||
'Content-Type' => 'application/json;charset=utf-8',
|
|
||||||
'accept' => 'application/json;charset=utf-8',
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
$log = $this->createLog($url, $method, $postData, 'pingan'); //日志
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$client = new Client();
|
$client = new Client();
|
||||||
$response = $client->request($method, $url, $postData);
|
$response = $client->request($method, $url, $query);
|
||||||
$body = $response->getBody();
|
$body = $response->getBody();
|
||||||
$content = $body->getContents();
|
$content = $body->getContents();
|
||||||
$result = json_decode($content, true);
|
$result = json_decode($content, true);
|
||||||
@@ -170,33 +165,12 @@ class Init
|
|||||||
} else {
|
} else {
|
||||||
$retData = $result['data'];
|
$retData = $result['data'];
|
||||||
}
|
}
|
||||||
$this->updateLog($log, $retData);//更新日志
|
|
||||||
|
|
||||||
return $retData;
|
return $retData;
|
||||||
} catch (RequestException $e) {
|
} catch (RequestException $e) {
|
||||||
$this->updateLog($log, [$e->getMessage()]);//更新日志
|
|
||||||
|
|
||||||
return ['ret' => '99999', $e->getMessage()];
|
return ['ret' => '99999', $e->getMessage()];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//加密
|
|
||||||
public function encrypt($str)
|
|
||||||
{
|
|
||||||
if (is_array($str)) {
|
|
||||||
$str = json_encode($str);
|
|
||||||
}
|
|
||||||
$data = openssl_encrypt($str, 'aes-128-ecb', $this->aes_code, OPENSSL_RAW_DATA);
|
|
||||||
|
|
||||||
return base64_encode($data);
|
|
||||||
}
|
|
||||||
|
|
||||||
//解密
|
|
||||||
public function decrypt($str)
|
|
||||||
{
|
|
||||||
$encrypted = base64_decode($str);
|
|
||||||
|
|
||||||
return openssl_decrypt($encrypted, 'aes-128-ecb', $this->aes_code, OPENSSL_RAW_DATA);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,43 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace XuanChen\PingAnSdk\Action;
|
|
||||||
|
|
||||||
class Query extends Init
|
|
||||||
{
|
|
||||||
|
|
||||||
public function start()
|
|
||||||
{
|
|
||||||
|
|
||||||
try {
|
|
||||||
//查询网点是否存在
|
|
||||||
$outlet = User::where('outlet_id', $this->outletId)->first();
|
|
||||||
|
|
||||||
if (!$outlet) {
|
|
||||||
throw new \Exception('网点编号错误,未查询到网点信息');
|
|
||||||
}
|
|
||||||
|
|
||||||
$url = $this->baseUri . 'partner/v2/coupondetail';
|
|
||||||
$params = [
|
|
||||||
'redemptionCode' => $this->redemptionCode,
|
|
||||||
'outletNo' => $outlet->PaOutletId,
|
|
||||||
'thirdOutletNo' => $outlet->outlet_id,
|
|
||||||
];
|
|
||||||
|
|
||||||
$res = $this->getPingAnData($url, $params);
|
|
||||||
|
|
||||||
if (!is_array($res)) {
|
|
||||||
throw new \Exception($res);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($res['code'] != 200) {
|
|
||||||
throw new \Exception($res['message']);
|
|
||||||
}
|
|
||||||
|
|
||||||
return collect($res['data']);
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
return $e->getMessage();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace XuanChen\PingAnSdk;
|
namespace XuanChen\WashCar;
|
||||||
|
|
||||||
use XuanChen\PingAnSdk\Action\BaseAction;
|
use XuanChen\WashCar\Action\BaseAction;
|
||||||
|
|
||||||
class PingAn
|
class PingAn
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace XuanChen\PingAnSdk;
|
namespace XuanChen\WashCar;
|
||||||
|
|
||||||
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
|
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user