添加激活nft功能

This commit is contained in:
aozhiwei 2022-04-25 10:27:10 +08:00
parent 80ca38f784
commit 9117ee6910
8 changed files with 290 additions and 4 deletions

View File

@ -141,6 +141,42 @@ class Market(object):
['!nfts', [_common.NftDetail()], '获得的nft列表'],
]
},
{
'name': 'activateNft',
'desc': '激活nft',
'group': 'Market',
'url': 'webapp/index.php?c=Market&a=activateNft',
'params': [
['account', 0, '钱包账号'],
['token', '', 'token'],
['token_id', '', 'token_id'],
['net_id', '', '网络id'],
],
'response': [
_common.RspHead(),
['new_token_id', '', '新nft tokenid'],
['nonce', '', 'nonce'],
['signature', '', 'signature'],
]
},
{
'name': 'queryActivateResult',
'desc': '查询开宝箱信息',
'group': 'Market',
'url': 'webapp/index.php?c=Market&a=queryActivateResult',
'params': [
['account', 0, '钱包账号'],
['token', '', 'token'],
['token_id', '', '宝箱token_id'],
['txhash', '', 'txhash'],
['net_id', '', '网络id'],
],
'response': [
_common.RspHead(),
['state', '', '0:激活中 1:激活成功'],
['nft', _common.NftDetail(), '新nft信息'],
]
},
{
'name': 'queryOrder',
'desc': '查询订单状态',

View File

@ -209,6 +209,55 @@ CREATE TABLE `t_lucky_box` (
) ENGINE=InnoDB AUTO_INCREMENT=10001 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `t_activate_nft`
--
DROP TABLE IF EXISTS `t_activate_nft`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `t_activate_nft` (
`idx` bigint NOT NULL AUTO_INCREMENT COMMENT '自增id',
`account` varchar(60) NOT NULL DEFAULT '' COMMENT 'account',
`old_token_id` varchar(60) NOT NULL DEFAULT '' COMMENT 'box_token_id',
`old_token_type` varchar(60) NOT NULL DEFAULT '' COMMENT 'old_token_type',
`new_token_id` varchar(60) NOT NULL DEFAULT '' COMMENT 'new_token_id',
`nonce` varchar(60) NOT NULL DEFAULT '' COMMENT 'nonce',
`signature` varchar(255) NOT NULL DEFAULT '' COMMENT '签名',
`createtime` int(11) NOT NULL DEFAULT '0' COMMENT '创建时间',
`modifytime` int(11) NOT NULL DEFAULT '0' COMMENT '修改时间',
PRIMARY KEY (`idx`),
UNIQUE KEY `old_token_id` (`old_token_id`)
) ENGINE=InnoDB AUTO_INCREMENT=10001 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `t_activate_event`
--
DROP TABLE IF EXISTS `t_activate_event`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `t_activate_event` (
`idx` bigint NOT NULL AUTO_INCREMENT COMMENT '自增id',
`old_token_id` varchar(60) NOT NULL DEFAULT '' COMMENT 'old_token_id',
`new_token_id` varchar(60) NOT NULL DEFAULT '' COMMENT 'new_token_id',
`old_token_type` int(11) NOT NULL DEFAULT '0' COMMENT 'old_token_type',
`txhash` varchar(255) NOT NULL DEFAULT '' COMMENT 'txhash',
`block_number` bigint NOT NULL DEFAULT '0' COMMENT 'block_number',
`log_index` bigint NOT NULL DEFAULT '0' COMMENT 'log_index',
`_to` varchar(60) NOT NULL DEFAULT '' COMMENT '_to',
`raw_data` mediumblob COMMENT 'raw_data',
`createtime` int(11) NOT NULL DEFAULT '0' COMMENT '创建时间',
`modifytime` int(11) NOT NULL DEFAULT '0' COMMENT '修改时间',
PRIMARY KEY (`idx`),
KEY `old_token_id` (`old_token_id`),
KEY `new_token_id` (`new_token_id`),
KEY `txhash` (`txhash`),
KEY `block_number` (`block_number`)
) ENGINE=InnoDB AUTO_INCREMENT=10001 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `t_boxopened_event`
--

@ -1 +1 @@
Subproject commit 066f9dc70f7ab15c1277fa3e84d3197fb87705da
Subproject commit 56e3e7871953b97a0af9c47dbb1ca54d0f807407

View File

@ -14,6 +14,7 @@ require_once('models/BuyRecord.php');
require_once('services/MarketService.php');
require_once('services/LuckyBoxService.php');
require_once('services/ActivateNftService.php');
require_once('phpcommon/bchelper.php');
@ -23,6 +24,7 @@ use models\Nft;
use models\BuyRecord;
use services\MarketService;
use services\LuckyBoxService;
use services\ActivateNftService;
class MarketController extends BaseController {
@ -308,7 +310,7 @@ class MarketController extends BaseController {
)
),
'handle' => function ($row) use(&$nftDbList) {
error_log(json_encode($row));
#error_log(json_encode($row));
array_push($nftDbList, $row);
}
),
@ -457,4 +459,32 @@ class MarketController extends BaseController {
LuckyBoxService::queryResult($account, $tokenId, $netId);
}
public function activateNft()
{
$token = getReqVal('token', '');
$account = getReqVal('account', '');
$tokenId = getReqVal('token_id', '');
$netId = getReqVal('net_id', '');
$account = strtolower(getReqVal('account', ''));
if (!MarketService::isValidToken($account, $token)) {
myself()->_rspErr(1, 'invalid token');
return;
}
ActivateNftService::activate($account, $tokenId, $netId);
}
public function queryActivateResult()
{
$token = getReqVal('token', '');
$account = getReqVal('account', '');
$tokenId = getReqVal('token_id', '');
$netId = getReqVal('net_id', '');
$account = strtolower(getReqVal('account', ''));
if (!MarketService::isValidToken($account, $token)) {
myself()->_rspErr(1, 'invalid token');
return;
}
ActivateNftService::queryResult($account, $tokenId, $netId);
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace models;
use mt;
use phpcommon\SqlHelper;
class ActivateNft extends BaseModel {
public static function find($oldTokenId)
{
$row = SqlHelper::ormSelectOne(
myself()->_getMarketMysql(),
't_activate_nft',
array(
'old_token_id' => $oldTokenId,
)
);
return $row;
}
public static function add($oldTokenId, $account, $newTokenId)
{
SqlHelper::insert(
myself()->_getMarketMysql(),
't_activate_nft',
array(
'account' => $account,
'old_token_id' => $oldTokenId,
'new_token_id' => $newTokenId,
)
);
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace models;
use mt;
use phpcommon\SqlHelper;
class ActivateNftEvent extends BaseModel {
public static function find($oldTokenId)
{
$row = SqlHelper::ormSelectOne(
myself()->_getMarketMysql(),
't_activate_event',
array(
'old_token_id' => $oldTokenId,
)
);
return $row;
}
}

View File

@ -157,10 +157,10 @@ class Nft extends BaseModel {
case Nft::EQUIP_TYPE:
{
$equipMeta = mt\Equip::get($itemMeta['relationship']);
error_log(json_encode(array(
/*error_log(json_encode(array(
'equpMeta' => $equipMeta,
'nftDb' => $nftDb
)));
)));*/
$nft['full_image'] = $nft['image'];
$nft['info']['level'] = 1;
$nft['info']['quality'] = 1;

View File

@ -0,0 +1,114 @@
<?php
namespace services;
require_once('phpcommon/bchelper.php');
require_once('models/Nft.php');
require_once('models/ActivateNft.php');
require_once('models/BuyRecord.php');
require_once('models/ActivateNftEvent.php');
use phpcommon;
use models\Nft;
use models\ActivateNft;
use models\BuyRecord;
use models\ActivateNftEvent;
class ActivateNftService {
public static function activate($account, $tokenId, $netId)
{
if (!phpcommon\isValidOldOrderId($tokenId)) {
myself()->_rspErr(2, 'invalid token_id');
return;
}
$nftDb = Nft::getNft($tokenId);
if (!$nftDb) {
myself()->_rspErr(2, 'nft not found');
return;
}
$actDb = ActivateNft::find($tokenId);
if (!$actDb) {
$newTokenId = phpcommon\oldOrderIdToNew($tokenId);
error_log('oldTokenId:' . $tokenId . ' newTokenId:' . $newTokenId);
ActivateNft::add($tokenId, $account, $newTokenId);
}
$actDb = ActivateNft::find($tokenId);
if (!$actDb) {
myself()->_rspErr(2, 'server internal error3');
return;
}
{
$params = array(
'c' => 'BcService',
'a' => 'activateNftSignature',
'account' => $account,
'old_token_id' => $tokenId,
'old_token_type' => $nftDb['token_type'],
'new_token_id' => $actDb['new_token_id'],
);
$url = self::getWeb3ServiceUrl();
$response = '';
if (!phpcommon\HttpClient::get
($url,
$params,
$response)) {
phpcommon\sendError(500, 'server internal error');
die();
return;
}
error_log(json_encode(array(
'_REQUEST' => $_REQUEST,
'params' => $params,
'response' => $response
)));
$json = json_decode($response, true);
myself()->_rspData(array(
'nonce' => $json['nonce'],
'signature' => $json['signature'],
'new_token_id' => $actDb['new_token_id'],
));
}
}
public static function queryResult($account, $tokenId, $netId)
{
$eventDb = ActivateNftEvent::find($tokenId);
if (!$eventDb) {
myself()->_rspData(array(
'state' => 0,
'nft'=> array()
));
return;
}
$oldNftDb = Nft::getNft($eventDb['old_token_id']);
if (!$oldNftDb || phpcommon\isSameAddress($oldNftDb['owner_address'],
$account)) {
myself()->_rspData(array(
'state' => 0,
'nft'=> array()
));
return;
}
$nftDb = Nft::getNft($eventDb['new_token_id']);
if (!$nftDb) {
myself()->_rspData(array(
'state' => 0,
'nft'=> array()
));
return;
}
myself()->_rspData(array(
'state' => 1,
'nft'=> Nft::toDto($nftDb)
));
}
private static function getWeb3ServiceUrl()
{
$web3ServiceCluster = require_once('../config/web3service.cluster.php');
return $web3ServiceCluster[rand() % count($web3ServiceCluster)];
}
}