game2006api/webapp/services/BlockChainService.php
aozhiwei c105d7bcf8 1
2023-06-19 13:57:22 +08:00

70 lines
2.1 KiB
PHP

<?php
namespace services;
class BlockChainService {
public static function gameItemMallBuy($actionType, $price, $itemId, $itemNum)
{
if (!($actionType > Transaction::BUY_BEGIN_ACTION_TYPE &&
$actionType < Transaction::BUY_END_ACTION_TYPE)) {
error_log('gameItemMallBuy action_type error:' . $actionType);
myself()->_rspErr(500, 'server internal error');
die();
return;
}
$account = myself()->_getAddress();
$params = array(
'c' => 'GameItemMall',
'a' => 'buy',
'account' => $account,
'price' => $price,
);
{
$url = self::getWeb3ServiceUrl();
$response = '';
if (!phpcommon\HttpClient::get
($url,
$params,
$response)) {
myself()->_rspErr(500, 'server internal error');
die();
return;
}
error_log($response);
$rspObj = json_decode($response, true);
if ($rspObj['errcode'] == 0) {
$transId = $rspObj['trans_id'];
Transaction::add(
$transId,
$actionType,
'', //$tokenId,
'', //$tokenType,
0, //$itemUniId,
$itemId, //$itemId,
$itemNum,
1
);
return array(
'trans_id' => $transId,
'params' => $rspObj['params']
);
} else {
myself()->_rspErr(500, 'server internal error');
die();
return;
}
}
}
private static function getWeb3ServiceUrl()
{
if (SERVER_ENV == _TEST) {
return 'http://127.0.0.1:7672/webapp/index.php';
}
$web3ServiceCluster = require_once('../config/web3service.cluster.php');
return $web3ServiceCluster[rand() % count($web3ServiceCluster)];
}
}