修改名称

This commit is contained in:
2021-06-01 16:28:47 +08:00
parent 2012bfc6b6
commit 12f5f71817
9 changed files with 111 additions and 372 deletions

View File

@@ -1,8 +0,0 @@
<?php
namespace XuanChen\PingAnSdk\Action;
class Info extends Init
{
}

View File

@@ -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()];
}
}
}

View File

@@ -1,8 +0,0 @@
<?php
namespace XuanChen\PingAnSdk\Action;
class Info extends Conr
{
}

View File

@@ -1,5 +1,5 @@
{
"name": "xuanchen/pingan_washcar",
"name": "xuanchen/washcar",
"description": "平安sdk",
"license": "MIT",
"homepage": "http://git.yuzhankeji.cn/xuanchen/PingAnWashCar.git",
@@ -16,13 +16,13 @@
},
"autoload": {
"psr-4": {
"XuanChen\\PingAnSdk\\": "src/"
"XuanChen\\WashCar\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"XuanChen\\PingAnSdk\\ServiceProvider"
"XuanChen\\WashCar\\ServiceProvider"
]
}
}

View File

@@ -1,13 +1,13 @@
<?php
namespace XuanChen\PingAnSdk\Action;
namespace XuanChen\WashCar\Action;
class BaseAction extends Init
{
public function index()
{
re
return 1;
}
}

View File

@@ -1,79 +1,118 @@
<?php
namespace XuanChen\PingAnSdk\Action;
namespace XuanChen\WashCar\Action;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
/**
* 超市购物券
*/
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 $access_token;
protected $userName;
protected $error;
protected $aes_code; //aes 密钥
protected $log;//日志
protected $url;
public function __construct()
{
$appId = config('pingan.appId');
$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'];
$this->appId = config('pingan.appId');
$this->appSecret = config('pingan.appSecret');
}
/**
* 获取access_token
* @return void [type] [description]
* Notes: 设置数据
* @Author: 玄尘
* @Date : 2021/6/1 14:37
* @param $data
* @return $this
*/
public function getToken()
public function setParams($data)
{
//从数据库里找token
$token = PinganToken::where('type', $this->this_type)->orderBy('id', 'desc')->first();
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();
}
//判断是否有sign有的话是传来的值
if (isset($data['sign'])) {
$this->sign = $data['sign'];
} 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());
$msectime = (float) sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
@@ -83,48 +122,25 @@ class Init
}
/**
* 请求平台 access_token
* @return void [type] [description]
* Notes: description
* @Author: 玄尘
* @Date : 2021/6/1 14:32
*/
public function getAjaxToken()
public function getSignString()
{
$params = [
'client_id' => $this->client_id,
'grant_type' => $this->grant_type,
'client_secret' => $this->client_secret,
];
$params = $this->params;
try {
$log = $this->createLog($this->tokenUri, 'POST', $params, 'pingan');
$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 (empty($params)) {
throw new \Exception('获取校验数据失败,缺少数据.');
}
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 方式
* @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 {
$client = new Client();
$response = $client->request($method, $url, $postData);
$response = $client->request($method, $url, $query);
$body = $response->getBody();
$content = $body->getContents();
$result = json_decode($content, true);
@@ -170,33 +165,12 @@ class Init
} else {
$retData = $result['data'];
}
$this->updateLog($log, $retData);//更新日志
return $retData;
} catch (RequestException $e) {
$this->updateLog($log, [$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);
}
}

View File

@@ -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();
}
}
}

View File

@@ -1,8 +1,8 @@
<?php
namespace XuanChen\PingAnSdk;
namespace XuanChen\WashCar;
use XuanChen\PingAnSdk\Action\BaseAction;
use XuanChen\WashCar\Action\BaseAction;
class PingAn
{

View File

@@ -1,6 +1,6 @@
<?php
namespace XuanChen\PingAnSdk;
namespace XuanChen\WashCar;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;