This commit is contained in:
aozhiwei 2024-07-17 16:59:18 +08:00
parent 54e4a11695
commit fb3873c960
3 changed files with 139 additions and 12 deletions

View File

@ -124,6 +124,8 @@ class BlockChainController extends BaseAuthedController {
switch ($type) { switch ($type) {
case 1: case 1:
{ {
myself()->_rspErr(1, 'server internal error');
return;
$heroDb = Hero::find($uniid); $heroDb = Hero::find($uniid);
if (!$heroDb) { if (!$heroDb) {
myself()->_rspErr(1, 'hero not found'); myself()->_rspErr(1, 'hero not found');
@ -145,7 +147,7 @@ class BlockChainController extends BaseAuthedController {
myself()->_getNowTime(), myself()->_getNowTime(),
myself()->_getAddress() myself()->_getAddress()
); );
Hero::safeUpdateTokenId($heroDb['hero_uniid'], $tokenId); Hero::safeUpdateTokenId(myself()->_getAccountId(), $heroDb['hero_uniid'], $tokenId);
$heroDb = Hero::find($uniid); $heroDb = Hero::find($uniid);
if (!$heroDb) { if (!$heroDb) {
myself()->_rspErr(1, 'hero not found'); myself()->_rspErr(1, 'hero not found');

View File

@ -0,0 +1,110 @@
<?php
use phpcommon\SqlHelper;
require_once('models/Nft.php');
require_once('models/User.php');
require_once('models/Hero.php');
require_once('models/BuyRecord.php');
require_once('services/NftService.php');
require_once('mt/NftDesc.php');
require_once('mt/Hero.php');
require_once('mt/EconomyAttribute.php');
use models\Nft;
use models\User;
use models\Hero;
use models\BuyRecord;
class OutAppMintController extends BaseController {
public function mintHero()
{
error_log(json_encode($_REQUEST));
$accountId = getReqVal('account_id', '');
//$accountAddress = User::findUserAddress($accountId);
//$accountAddress = '0x0c6c7399f1b6b19e96950ef294685bfd0dc46434';
$accountAddress = getReqVal('account_address', '');
$toAddress = getReqVal('to_address', '');
$uniid = getReqVal('uniid', 0);
error_log(json_encode(array(
'accoutn_id' => $accountId,
'uuid' => $uniid
)));
$heroDb = Hero::findByAccountIdEx($accountId, $uniid);
if (!$heroDb) {
myself()->_rspErr(1, 'hero not found');
return;
}
/*
if (!$heroDb['seal_type']) {
myself()->_rspErr(1, 'hero no seal');
return;
}*/
$isMint = true;
$tokenId = $heroDb['token_id'];
if ($heroDb['token_id'] && $heroDb['activate']) {
myself()->_rspErr(1, 'hero already on chain');
return;
$isMint = false;
} else if (empty($tokenId) ){
$tokenId = BuyRecord::genOrderId
(
GAME_ID,
phpcommon\BC_FUNC_CREATION,
myself()->_getNowTime(),
$accountAddress
);
Hero::safeUpdateTokenId($accountId, $heroDb['idx'], $tokenId);
$heroDb = Hero::findByAccountIdEx($accountId, $uniid);
if (!$heroDb) {
myself()->_rspErr(1, 'hero not found');
return;
}
if ($heroDb['token_id'] != $tokenId) {
myself()->_rspErr(500, 'server internal error');
return;
}
}
$params = array(
'c' => 'BcService',
'a' => 'mintHero',
'account_id' => $accountId,
'account_address' => $accountAddress,
'token_ids' => $tokenId,
'nft_address' => NORMAL_HERO_CONTRACT_ADDRESS,
'to_address' => $toAddress,
'net_id' => NET_ID
);
error_log(json_encode($params));
{
$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) {
myself()->_rspData(array(
'trans_id' => '',
'trans_req' => $rspObj['trans_req']
));
} else {
myself()->_rspErr(500, 'server internal error');
return;
}
}
}
private static function getWeb3ServiceUrl()
{
$web3ServiceCluster = require_once('../config/web3service.cluster.php');
return $web3ServiceCluster[rand() % count($web3ServiceCluster)];
}
}

View File

@ -72,6 +72,22 @@ class Hero extends BaseModel {
return self::internalFind($accountId, User::findUserAddress($accountId), $heroUniId); return self::internalFind($accountId, User::findUserAddress($accountId), $heroUniId);
} }
public static function findByAccountIdEx($accountId, $heroUniId)
{
$row = SqlHelper::ormSelectOne(
myself()->_getMysql($accountId),
't_hero',
array(
'idx' => $heroUniId,
'account_id' => $accountId,
)
);
if ($row) {
$row['hero_uniid'] = $row['idx'];
}
return $row;
}
public static function findByTokenId($tokenId) public static function findByTokenId($tokenId)
{ {
$row = SqlHelper::ormSelectOne( $row = SqlHelper::ormSelectOne(
@ -671,18 +687,17 @@ class Hero extends BaseModel {
} }
} }
public static function safeUpdateTokenId($heroUniId, $tokenId) public static function safeUpdateTokenId($accountId, $heroUniId, $tokenId)
{ {
if (self::find($heroUniId)) { myself()->_getMysql($accountId)->execScript(
myself()->_getSelfMysql()->execScript( 'UPDATE t_hero SET token_id=:token_id ' .
'UPDATE t_hero SET token_id=:token_id ' . 'WHERE idx=:idx AND account_id=:account_id AND token_id IS NULL;',
'WHERE idx=:idx AND token_id IS NULL;', array(
array( ':idx' => $heroUniId,
':idx' => $heroUniId, ':account_id' => $accountId,
':token_id' => $tokenId, ':token_id' => $tokenId,
) )
); );
}
} }
public static function updateByTokenId($tokenId, $fieldsKv) public static function updateByTokenId($tokenId, $fieldsKv)