diff --git a/doc/Market.py b/doc/Market.py index fb72cd8a..bf9eb6ab 100644 --- a/doc/Market.py +++ b/doc/Market.py @@ -409,6 +409,7 @@ class Market(object): 'group': 'Market', 'url': 'webapp/index.php?c=Market&a=sell', 'params': [ + _common.ReqHead(), ['account', '', '账号id'], ['token', '', 'token'], ['nft_token', '', 'nft_token'], @@ -424,6 +425,49 @@ class Market(object): _common.RspHead() ] }, + { + 'name': 'sellCancel', + 'desc': '下架物品', + 'group': 'Market', + 'url': 'webapp/index.php?c=Market&a=sellCancel', + 'params': [ + _common.ReqHead(), + ['account', '', '账号id'], + ['idx', '', '出售的idx'], + ], + 'response': [ + _common.RspHead() + ] + }, + { + 'name': 'sellUpdatePrice', + 'desc': '修改价格', + 'group': 'Market', + 'url': 'webapp/index.php?c=Market&a=sellUpdatePrice', + 'params':[ + _common.ReqHead(), + ['account', '', '账号id'], + ['idx', '', '出售的idx'], + ['s_price', '', '出售价格USDT'], + ], + 'response': [ + _common.RspHead() + ] + }, + { + 'name': 'buy', + 'desc': '购买商品 (金币购买)', + 'group': 'Market', + 'url': 'webapp/index.php?c=Market&a=buy', + 'params': [ + _common.ReqHead(), + ['account', '', '账号id'], + ['idx', '', '出售的idx'], + ], + 'response': [ + _common.RspHead() + ] + }, { 'name': 'buyNft', 'desc': '购买NFT', diff --git a/webapp/controller/MarketController.class.php b/webapp/controller/MarketController.class.php index b899e5e4..8fc8a6e8 100644 --- a/webapp/controller/MarketController.class.php +++ b/webapp/controller/MarketController.class.php @@ -37,9 +37,21 @@ use services\LuckyBoxService; use services\ActivateNftService; use models\Transaction; -class MarketController extends BaseController +class MarketController extends BaseAuthedController { + public function _handlePre() + { + if ( + getReqVal('a', '') != 'eventSellOrder' && + getReqVal('a', '') != 'eventBuyOrder' && + getReqVal('a', '') != 'eventCancelOrder' && + getReqVal('a', '') != 'eventPriceUpdateOrder' + ) { + parent::_handlePre(); + } + } + public function getPreSaleInfo() { $account = strtolower(getReqVal('account', '')); @@ -913,6 +925,12 @@ class MarketController extends BaseController $order_asc = getReqVal('order_asc', 1); $type = getReqVal('type', 1); $job_filters = getReqVal('job_filters', ''); + + $address = $this->_getAddress(); + if ($address != $account) { + $this->_rspErr(1, 'account not match'); + return; + } if (empty($job_filters)) { $job_filter_array = array(); } else { @@ -1043,7 +1061,88 @@ class MarketController extends BaseController $this->_rspOk(); } - public function buy() { + public function sellCancel() + { + $account = strtolower(getReqVal('account', '')); + $idx = getReqVal('idx', ''); + + $address = $this->_getAddress(); + if ($address != $account) { + $this->_rspErr(1, 'not your goods, idx:' . $idx); + return; + } + + $goods = $this->getGoodsByIdx($idx); + if (!$goods) { + $this->_rspErr(1, 'goods not found, idx:' . $idx); + return; + } + + if ($goods['owner_address'] != $account) { + $this->_rspErr(1, 'not your goods, idx:' . $idx); + return; + } + + if (!$this->addItems($address, $goods['item_id'], $goods['amount'])) { + $this->_rspErr(1, "cancel failed, add item failed, idx:" . $idx); + return; + } + + $conn = $this->_getMarketMysql(''); + $r = SqlHelper::update( + $conn, + 't_market_store', + array( + 'idx' => $idx, + ), + array( + 'status' => 1, + 'modifytime' => $this->_getNowTime(), + ) + ); + $this->_rspOk(); + } + + public function sellUpdatePrice() + { + $account = strtolower(getReqVal('account', '')); + $idx = getReqVal('idx', ''); + $s_price = getReqVal('s_price', ''); + + $address = $this->_getAddress(); + if ($address != $account) { + $this->_rspErr(1, 'not your goods, idx:' . $idx); + return; + } + + $goods = $this->getGoodsByIdx($idx); + if (!$goods) { + $this->_rspErr(1, 'goods not found, idx:' . $idx); + return; + } + + if ($goods['owner_address'] != $account) { + $this->_rspErr(1, 'not your goods, idx:' . $idx); + return; + } + + $conn = $this->_getMarketMysql(''); + $r = SqlHelper::update( + $conn, + 't_market_store', + array( + 'idx' => $idx, + ), + array( + 's_price' => $s_price, + 'modifytime' => $this->_getNowTime(), + ) + ); + $this->_rspOk(); + } + + public function buy() + { $account = strtolower(getReqVal('account', '')); $idx = getReqVal('idx', ''); @@ -1053,7 +1152,7 @@ class MarketController extends BaseController return; } - $response = services\BlockChainService::gameItemMallBuyWithAddress($account, + $response = services\BlockChainService::gameItemMallBuy( Transaction::BUY_GOODS_FROM_MARKET_ACTION_TYPE, $goods['s_price'], $goods['item_id'], @@ -1068,7 +1167,6 @@ class MarketController extends BaseController $this->_rspData(array( 'block_chain' => $response, )); - } private function sellMyNft() { @@ -1539,6 +1637,21 @@ class MarketController extends BaseController return $rows; } + private function addItems($address, $item_id, $amount) + { + $self = myself(); + if (!$self) { + return false; + } + + $r = $this->addItem($address, $item_id, $amount); + if (!$r) { + return false; + } + + return true; + } + private function decItems($address, $item_id, $amount) { $self = myself(); @@ -1560,6 +1673,24 @@ class MarketController extends BaseController return true; } + private function addItem($address, $item_id, $amount) + { + $self = myself(); + if (!$self) { + return false; + } + switch ($item_id) { + case V_ITEM_GOLD: { + $r = $this->addGold($address, $amount); + if (!$r) { + return false; + } + } + break; + } + return true; + } + private function decItem($address, $item_id, $amount) { $self = myself(); @@ -1578,6 +1709,26 @@ class MarketController extends BaseController return true; } + private function addGold($address, $amount) + { + $self = myself(); + if (!$self) { + return false; + } + + $r = $this->updateUserInfo($address, array( + 'gold' => function () use ($amount) { + return "gold + ${amount}"; + } + )); + + if (!$r) { + return false; + } + + return true; + } + private function decGold($address, $amount) { $self = myself(); @@ -1590,7 +1741,7 @@ class MarketController extends BaseController if ($count < $amount) { return false; } - + $r = $this->updateUserInfo($address, array( 'gold' => function () use ($amount) { return "GREATEST(0, gold - ${amount})"; @@ -1710,7 +1861,8 @@ class MarketController extends BaseController return phpcommon\bnToStr($ret_price); } - private function getGoodsByIdx($idx) { + private function getGoodsByIdx($idx) + { $self = myself(); if (!$self) { return null; @@ -1719,11 +1871,15 @@ class MarketController extends BaseController $row = SqlHelper::selectOne( $self->_getMarketMysql(''), 't_market_store', - array('item_id', 'amount', 's_price'), + array('item_id', 'amount', 's_price', 'owner_address'), array( - 'idx' => $idx + 'idx' => $idx, + 'status' => 0, ) ); + if ($row['item_id'] == null) { + return null; + } return $row; } } diff --git a/webapp/services/BlockChainService.php b/webapp/services/BlockChainService.php index e09769f7..7a7e3b83 100644 --- a/webapp/services/BlockChainService.php +++ b/webapp/services/BlockChainService.php @@ -83,65 +83,6 @@ class BlockChainService { } } - public static function gameItemMallBuyWithAddress($address, $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 1'); - die(); - return; - } - $account = $address; - if (empty($account)) { - error_log('gameItemMallBuy address is emtpy:' . myself()->_getAccountId()); - myself()->_rspErr(500, 'server internal error 2'); - 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 3, url:' . $url); - die(); - return; - } - error_log("gameItemMallBuy:" . $response . "url:" . $url); - $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 4' . json_encode($rspObj)); - die(); - return; - } - } - } - /* $price 是一个小数精确到小数点后5位 */ @@ -156,7 +97,7 @@ class BlockChainService { private static function getWeb3ServiceUrl() { if (SERVER_ENV == _DEBUG) { - return 'http://login-test.kingsome.cn/webapp/index.php'; + return 'https://login-test.kingsome.cn/webapp/index.php'; } if (SERVER_ENV == _TEST) { return 'http://127.0.0.1:7672/webapp/index.php';