'阶段更新'

This commit is contained in:
2021-06-01 11:15:59 +08:00
parent 7e96e3f4f9
commit e15a06e589
7 changed files with 328 additions and 0 deletions

1
README.md Normal file
View File

@@ -0,0 +1 @@
# 平安skd

29
composer.json Normal file
View File

@@ -0,0 +1,29 @@
{
"name": "xuanchen/pingansdk",
"description": "平安sdk",
"license": "MIT",
"homepage": "http://git.yuzhankeji.cn/xuanchen/pinganSdk.git",
"authors": [
{
"name": "xuanchen",
"email": "122383162@qq.com"
}
],
"require": {
"php": ">=7.1.3",
"guzzlehttp/guzzle": "^7.0.1",
"laravel/framework": "*"
},
"autoload": {
"psr-4": {
"XuanChen\\PingAnSdk\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"XuanChen\\PingAnSdk\\ServiceProvider"
]
}
}
}

9
config/config.php Normal file
View File

@@ -0,0 +1,9 @@
<?php
return [
/**
* 平安洗车券
*/
];

203
src/Action/Init.php Normal file
View File

@@ -0,0 +1,203 @@
<?php
namespace XuanChen\PingAnSdk\Action;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
/**
* 超市购物券
*/
class Init
{
protected $this_type;
protected $baseUri;
protected $tokenUri;
protected $client_id;
protected $grant_type;
protected $client_secret;
protected $access_token;
protected $userName;
protected $error;
protected $aes_code; //aes 密钥
protected $log;//日志
public function __construct()
{
$this->this_type = config('pingan.this_type');
$pingan = config('pingan.' . $this->this_type);
$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
* @return void [type] [description]
*/
public function getToken()
{
//从数据库里找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();
}
} else {
$this->getAjaxToken();
}
}
/**
* 获取毫秒级别的时间戳
*/
public function getMsecTime()
{
[$msec, $sec] = explode(' ', microtime());
$msectime = (float) sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
$msectime = explode('.', $msectime);
return $msectime[0];
}
/**
* 请求平台 access_token
* @return void [type] [description]
*/
public function getAjaxToken()
{
$params = [
'client_id' => $this->client_id,
'grant_type' => $this->grant_type,
'client_secret' => $this->client_secret,
];
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]); //更新日志
}
}
/**
* 通用获取数据接口
* @param [type] $url 请求地址
* @param array $query 传递参数
* @param array $json 需要传的json数据
* @param string $method 方式
* @return array|mixed [type] [description]
*/
public function getPingAnData($url, $query = [], $json = [], $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);
$body = $response->getBody();
$content = $body->getContents();
$result = json_decode($content, true);
if ($result['ret'] > 0) {
$retData = $result['msg'];
} 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);
}
}

43
src/Action/Query.php Normal file
View File

@@ -0,0 +1,43 @@
<?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();
}
}
}

13
src/PingAn.php Normal file
View File

@@ -0,0 +1,13 @@
<?php
namespace XuanChen\PingAnSdk;
use Carbon\Carbon;
/**
* 平安SDK
*/
class PingAn
{
}

30
src/ServiceProvider.php Normal file
View File

@@ -0,0 +1,30 @@
<?php
namespace XuanChen\PingAnSdk;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
class ServiceProvider extends LaravelServiceProvider
{
/**
* Register services.
* @return void
*/
public function register()
{
if ($this->app->runningInConsole()) {
$this->publishes([__DIR__ . '/../config/config.php' => config_path('pingan.php')]);
}
}
/**
* Bootstrap services.
* @return void
*/
public function boot()
{
$this->mergeConfigFrom(__DIR__ . '/../config/pingan.php', 'pingan');
}
}