platform notify
This commit is contained in:
86
app/Service/HttpService.php
Normal file
86
app/Service/HttpService.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Bean\Service\HttpServiceConfigBean;
|
||||
use App\Bean\Service\HttpServiceReturnLogBean;
|
||||
use App\Tools\Logs;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
|
||||
class HttpService
|
||||
{
|
||||
protected Client $client;
|
||||
|
||||
const REQ_METHOD_GET = 'GET';
|
||||
const REQ_METHOD_POST = 'POST';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->client = new Client();
|
||||
}
|
||||
|
||||
public function getClient(): Client
|
||||
{
|
||||
return $this->client;
|
||||
}
|
||||
|
||||
public function get(string $endpoint, array $data = [], HttpServiceConfigBean $configBean = null)
|
||||
{
|
||||
if ($configBean === null) $configBean = new HttpServiceConfigBean();
|
||||
$oHttpServiceReturnLogBean = $this->request(self::REQ_METHOD_GET, $endpoint, $data, $configBean);
|
||||
if ($configBean->isIsReturnJson() === true) {
|
||||
return json_decode($oHttpServiceReturnLogBean->getResponseBody(), false);
|
||||
}
|
||||
return $oHttpServiceReturnLogBean->getResponseBody();
|
||||
}
|
||||
|
||||
public function post(string $endpoint, array $data = [], HttpServiceConfigBean $configBean = null)
|
||||
{
|
||||
if ($configBean === null) $configBean = new HttpServiceConfigBean();
|
||||
$oHttpServiceReturnLogBean = $this->request(self::REQ_METHOD_POST, $endpoint, $data, $configBean);
|
||||
if ($configBean->isIsReturnJson() === true) {
|
||||
return json_decode($oHttpServiceReturnLogBean->getResponseBody(), false);
|
||||
}
|
||||
return $oHttpServiceReturnLogBean->getResponseBody();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
function request($req_method , string $endpoint, array $data = [], HttpServiceConfigBean $configBean = null): HttpServiceReturnLogBean|bool
|
||||
{
|
||||
try {
|
||||
if(!in_array($req_method, [self::REQ_METHOD_GET, self::REQ_METHOD_POST])) {
|
||||
throw new \Exception('request method not support');
|
||||
}
|
||||
if ($configBean === null) $configBean = new HttpServiceConfigBean();
|
||||
$req_data = [];
|
||||
if($req_method === self::REQ_METHOD_GET) {
|
||||
$req_data = ['query' => $data];
|
||||
}
|
||||
if($req_method === self::REQ_METHOD_POST) {
|
||||
$req_data = ['json' => $data];
|
||||
}
|
||||
$resp = $this->getClient()->request($req_method,$endpoint, $req_data);
|
||||
$status_code = $resp->getStatusCode();
|
||||
$body = $resp->getBody()->getContents();
|
||||
$resp->getBody()->close();
|
||||
|
||||
//记录日志
|
||||
$oHttpServiceReturnLogBean = new HttpServiceReturnLogBean();
|
||||
$oHttpServiceReturnLogBean->setRequestData($data);
|
||||
$oHttpServiceReturnLogBean->setRequestUrl($endpoint);
|
||||
$oHttpServiceReturnLogBean->setResponseStatusCode($status_code);
|
||||
$oHttpServiceReturnLogBean->setResponseBody($body);
|
||||
Logs::InfoLog(__CLASS__ . '-' . __FUNCTION__, $oHttpServiceReturnLogBean->toArrayNotNull());
|
||||
|
||||
return $oHttpServiceReturnLogBean;
|
||||
}catch (\Exception $e) {
|
||||
Logs::ErrLog(__CLASS__ . '-' . __FUNCTION__, $e, func_get_args());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -113,7 +113,7 @@ class UsdtTrx20Service
|
||||
$oWalletTronBlockModel->updateItem($updateItem);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Logs::ErrLog(__FUNCTION__, $e, $oQueueWalletBlockBean->toArray());
|
||||
Logs::ErrLog(__FUNCTION__, $e, $oQueueWalletBlockBean->toArrayNotNull());
|
||||
$oQueueWalletBlockBean->IncrTryTimes();
|
||||
if (!$oQueueWalletBlockBean->checkTryTimes()) { //超出重试次数
|
||||
$updateItem = [
|
||||
@ -137,7 +137,7 @@ class UsdtTrx20Service
|
||||
try {
|
||||
$this->tronBlockTransaction($oEventBean);
|
||||
} catch (\Exception $e) {
|
||||
Logs::ErrLog(__FUNCTION__, $e, $oEventBean->toArray());
|
||||
Logs::ErrLog(__FUNCTION__, $e, $oEventBean->toArrayNotNull());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -158,7 +158,7 @@ class UsdtTrx20Service
|
||||
$is_to = $oTronWalletAddressSetCache->checkKey($to_addr);
|
||||
if (!$is_from && !$is_to) return;
|
||||
//投递到钱包地址账变处理队列
|
||||
$oQueueEventBean = new QueueEventBean($oEventBean->toArray());
|
||||
$oQueueEventBean = new QueueEventBean($oEventBean->toArrayNotNull());
|
||||
WalletAddressTransactionQueue::putToQueue($oQueueEventBean);
|
||||
return;
|
||||
}
|
||||
|
||||
48
app/Service/Wallet/PlatformNotifyService.php
Normal file
48
app/Service/Wallet/PlatformNotifyService.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
namespace App\Service\Wallet;
|
||||
|
||||
use App\Bean\Model\Wallet\Platform\WalletPlatformBean;
|
||||
use App\Bean\Queue\Wallet\QueueNotifyToPlatformBean;
|
||||
use App\Bean\Service\HttpServiceConfigBean;
|
||||
use App\Cache\Table\Wallet\TableWalletPlatformCache;
|
||||
use App\Exceptions\PlatformNotifyException;
|
||||
use App\Jobs\Wallet\WalletNotifyToPlatformQueue;
|
||||
use App\Tools\Logs;
|
||||
|
||||
class PlatformNotifyService
|
||||
{
|
||||
const RETURN_SUCCESS = 'success';
|
||||
function notifyToPlatform(QueueNotifyToPlatformBean $bean): void
|
||||
{
|
||||
try {
|
||||
if(empty($bean->getPlatformId())) return;
|
||||
|
||||
$oTableWalletPlatformCache = new TableWalletPlatformCache();
|
||||
$aWalletPlatform= $oTableWalletPlatformCache->get($bean->getPlatformId());
|
||||
if(empty($aWalletPlatform)) return;
|
||||
|
||||
$oWalletPlatformBean = new WalletPlatformBean($aWalletPlatform);
|
||||
if(empty($oWalletPlatformBean->getNotifyIp())) return;
|
||||
//通知到平台
|
||||
$oHttpService = new \App\Service\HttpService();
|
||||
$HttpServiceConfigBean = new HttpServiceConfigBean();
|
||||
$HttpServiceConfigBean->setIsReturnJson(false); //返回不格式化json
|
||||
$res = $oHttpService->post($oWalletPlatformBean->getNotifyIp(),$bean->toArrayNotNull(),$HttpServiceConfigBean);
|
||||
if($res !== self::RETURN_SUCCESS){ //通知失败
|
||||
throw new PlatformNotifyException('notify to platform failed');
|
||||
}
|
||||
|
||||
Logs::SuccLog(__FUNCTION__, $bean->toArrayNotNull());
|
||||
}catch (\Exception $e) {
|
||||
Logs::ErrLog(__FUNCTION__, $e, $bean->toArrayNotNull());
|
||||
$bean->IncrTryTimes();
|
||||
if (!$bean->checkTryTimes()) { //超出重试次数
|
||||
return;
|
||||
}
|
||||
WalletNotifyToPlatformQueue::putToQueue($bean, $bean->getTryTimes());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user