From 9117ee69107d58d524c6cfc579e58b34aa50a355 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Mon, 25 Apr 2022 10:27:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=BF=80=E6=B4=BBnft?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/Market.py | 36 ++++++ sql/marketdb.sql | 49 ++++++++ third_party/phpcommon | 2 +- webapp/controller/MarketController.class.php | 32 +++++- webapp/models/ActivateNft.php | 35 ++++++ webapp/models/ActivateNftEvent.php | 22 ++++ webapp/models/Nft.php | 4 +- webapp/services/ActivateNftService.php | 114 +++++++++++++++++++ 8 files changed, 290 insertions(+), 4 deletions(-) create mode 100644 webapp/models/ActivateNft.php create mode 100644 webapp/models/ActivateNftEvent.php create mode 100644 webapp/services/ActivateNftService.php diff --git a/doc/Market.py b/doc/Market.py index 75256719..bc1e676b 100644 --- a/doc/Market.py +++ b/doc/Market.py @@ -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': '查询订单状态', diff --git a/sql/marketdb.sql b/sql/marketdb.sql index 262266fd..fb5f757c 100644 --- a/sql/marketdb.sql +++ b/sql/marketdb.sql @@ -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` -- diff --git a/third_party/phpcommon b/third_party/phpcommon index 066f9dc7..56e3e787 160000 --- a/third_party/phpcommon +++ b/third_party/phpcommon @@ -1 +1 @@ -Subproject commit 066f9dc70f7ab15c1277fa3e84d3197fb87705da +Subproject commit 56e3e7871953b97a0af9c47dbb1ca54d0f807407 diff --git a/webapp/controller/MarketController.class.php b/webapp/controller/MarketController.class.php index 4b47a0a0..2cef90ef 100644 --- a/webapp/controller/MarketController.class.php +++ b/webapp/controller/MarketController.class.php @@ -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); + } + } diff --git a/webapp/models/ActivateNft.php b/webapp/models/ActivateNft.php new file mode 100644 index 00000000..f08fa553 --- /dev/null +++ b/webapp/models/ActivateNft.php @@ -0,0 +1,35 @@ +_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, + ) + ); + } + +} diff --git a/webapp/models/ActivateNftEvent.php b/webapp/models/ActivateNftEvent.php new file mode 100644 index 00000000..a7906b3d --- /dev/null +++ b/webapp/models/ActivateNftEvent.php @@ -0,0 +1,22 @@ +_getMarketMysql(), + 't_activate_event', + array( + 'old_token_id' => $oldTokenId, + ) + ); + return $row; + } + +} diff --git a/webapp/models/Nft.php b/webapp/models/Nft.php index 9f5630f1..6c33bb9f 100644 --- a/webapp/models/Nft.php +++ b/webapp/models/Nft.php @@ -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; diff --git a/webapp/services/ActivateNftService.php b/webapp/services/ActivateNftService.php new file mode 100644 index 00000000..6f623717 --- /dev/null +++ b/webapp/services/ActivateNftService.php @@ -0,0 +1,114 @@ +_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)]; + } + +}