game2006api/webapp/services/BlockChainService.php
songliang 51a1f207f2 ...
2023-06-20 12:49:48 +08:00

96 lines
2.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace services;
require_once('models/Transaction.php');
use models\Transaction;
use phpcommon;
class BlockChainService {
/*
注意!!!
调用方调用前需要校验actionType和myself()->_getAddress,
非法的参数,或者签名服挂了,该函数直接中断请求,不会运行到调用方后续逻辑
trans_id订单id
gameItemMallBuy
setv
a
b
c
*/
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();
if (empty($account)) {
error_log('gameItemMallBuy address is emtpy:' . myself()->_getAccountId());
myself()->_rspErr(500, 'server internal error');
die();
return;
}
$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()
{
return 'http://192.168.100.39:7672/webapp/index.php';
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)];
}
}