From 51a1f207f2e6fe1e9a9d25e68d80db7b9cb58e93 Mon Sep 17 00:00:00 2001 From: songliang Date: Tue, 20 Jun 2023 12:49:43 +0800 Subject: [PATCH] ... --- webapp/bootstrap/constant.php | 3 + webapp/controller/ShopController.class.php | 91 +++++++++++++++---- webapp/models/Transaction.php | 1 + webapp/services/BlockChainService.php | 2 + .../callback/BuyShopGoodsCbService.php | 45 +++++++++ 5 files changed, 125 insertions(+), 17 deletions(-) diff --git a/webapp/bootstrap/constant.php b/webapp/bootstrap/constant.php index e38173bc..3d74705b 100644 --- a/webapp/bootstrap/constant.php +++ b/webapp/bootstrap/constant.php @@ -59,6 +59,9 @@ define('TN_WEEKLY_RECHARGE_UPGRADE_TIMES', 10004); define('TN_WEEKLY_SHARE_GAMES', 10005); define('TN_WEEKLY_END', 10005); +define('SHOP_BUY_MODE_NORMAL', 0); +define('SHOP_BUY_MODE_DAILY_SELECTION', 1); + const kHAT_Begin = 0; const kHAT_Hp = 1; const kHAT_HPRecover = 2; diff --git a/webapp/controller/ShopController.class.php b/webapp/controller/ShopController.class.php index 912318f3..3889e38e 100644 --- a/webapp/controller/ShopController.class.php +++ b/webapp/controller/ShopController.class.php @@ -17,10 +17,16 @@ require_once('models/Gun.php'); require_once('models/GunSkin.php'); require_once('models/ShopBuyRecord.php'); require_once('models/Chip.php'); +require_once('models/BcOrder.php'); +require_once('models/Transaction.php'); require_once('services/AwardService.php'); require_once('services/PropertyChgService.php'); +require_once('services/BlockChainService.php'); +require_once('phpcommon/bignumber.php'); + +use phpcommon; use phpcommon\HttpClient; use phpcommon\SqlHelper; use models\User; @@ -34,6 +40,8 @@ use models\Chip; use mt\Shop; use mt\PayMethod; use mt\Dailyselection; +use models\Transaction; +use models\BcOrder; class ShopController extends BaseAuthedController { @@ -813,33 +821,73 @@ class ShopController extends BaseAuthedController $grid = getReqVal('grid', 0); $count = getReqVal('count', 0); - $address = $this->_getAddress(); - // $chk = $this->decDailySelectionItem($idx, $grid, $count); - // if (!$chk) { - // $this->_rspErr(1, 'goods not enough'); - // return; - // } + $conn = $this->_getMysql(''); - $url = "192.168.100.39:7672/webapp/index.php"; - $params = array( - 'c' => 'GameItemMall', - 'a' => 'buy', - 'address' => $address, - 'price' => 100, + $row = SqlHelper::selectOne( + $conn, + 't_shop_dailyselection', + array( + 'idx', + 'address', + 'grid_' . $grid, + 'count_' . $grid, + ), + array('idx' => $idx) ); - if (!phpcommon\HttpClient::get($url, $params, $response)) { - $this->_rspErr(500, 'GameItemMall buy failed'); + + if (!$row) { + $this->_rspErr(2, 'idx is invalid'); return; } - $response = ''; - error_log($response); + + if ($row['address'] != $address) { + $this->_rspErr(2, 'address is invalid'); + return; + } + + if ($row['grid_' . $grid] == 0) { + $this->_rspErr(2, 'grid is invalid'); + return; + } + + if ($row['count_' . $grid] < $count) { + $this->_rspErr(2, 'count is invalid'); + return; + } + + $sel_id = $row['grid_' . $grid]; + + $goods = mt\Dailyselection::get($sel_id); + + $price = $this->normalizeWeb3Price($goods['price'] * $count); + $item_id = $goods['goods_id']; + $item_count = $goods['goods_num'] * $count; + + $response = services\BlockChainService::gameItemMallBuy( + Transaction::BUY_GOODS_ACTION_TYPE, + $price, + $item_id, + $item_count, + ); + + BcOrder::upsert( $response['trans_id'], array( + 'item_id' => $item_id, + 'item_num' => $item_count, + 'order_type' => 1, + 'ext_data' => json_encode(array( + 'mode' => SHOP_BUY_MODE_DAILY_SELECTION, + 'idx' => $idx, + 'grid' => $grid, + 'count' => $count, + )), + )); $this->_rspData( array( 'idx' => $idx, 'grid' => $grid, 'count' => $count, - 'response' => $response, + 'trans' => $response, ) ); } @@ -1258,4 +1306,13 @@ class ShopController extends BaseAuthedController $row = $conn->execQueryOne('SELECT LAST_INSERT_ID() as lastId;', array()); return $row['lastId']; } + + private function normalizeWeb3Price($price) + { + $bn1 = phpcommon\bnInit($price * pow(10, 8)); + $bn2 = phpcommon\bnInit('1000000000000000000'); + $price = phpcommon\bnDiv(phpcommon\bnMul($bn1, $bn2), pow(10, 8)); + + return $price; + } } diff --git a/webapp/models/Transaction.php b/webapp/models/Transaction.php index 357a2349..b82cbaa4 100644 --- a/webapp/models/Transaction.php +++ b/webapp/models/Transaction.php @@ -20,6 +20,7 @@ class Transaction extends BaseModel { const BUY_EXP_ACTION_TYPE = 101; const BUY_PASS_ACTION_TYPE = 102; + const BUY_GOODS_ACTION_TYPE = 103; const BUY_END_ACTION_TYPE = 200; diff --git a/webapp/services/BlockChainService.php b/webapp/services/BlockChainService.php index 77f673ab..68bcefde 100644 --- a/webapp/services/BlockChainService.php +++ b/webapp/services/BlockChainService.php @@ -5,6 +5,7 @@ namespace services; require_once('models/Transaction.php'); use models\Transaction; +use phpcommon; class BlockChainService { @@ -83,6 +84,7 @@ class BlockChainService { 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'; } diff --git a/webapp/services/callback/BuyShopGoodsCbService.php b/webapp/services/callback/BuyShopGoodsCbService.php index def4688d..05c2de70 100644 --- a/webapp/services/callback/BuyShopGoodsCbService.php +++ b/webapp/services/callback/BuyShopGoodsCbService.php @@ -3,10 +3,55 @@ namespace services; +define('V_ORDER_TYPE_BUY_SHOP_GOODS', 1); + +use phpcommon\SqlHelper; class BuyShopGoodsCbService { public function process($order){ + switch($order['order_type']){ + case V_ORDER_TYPE_BUY_SHOP_GOODS : { + $ext_data = json_decode($order['ext_data'], true); + switch($ext_data['mode']) + { + case SHOP_BUY_MODE_NORMAL: + $this->_buyNormal($order, $ext_data); + break; + case SHOP_BUY_MODE_DAILY_SELECTION: + $this->_buyDailySelection($order, $ext_data); + break; + } + } + break; + default : { + + } + } + } + + private function _buyNormal($order, $ext_data){ + $ext_data = $order['ext_data']; + $goods_id = $ext_data['goods_id']; + $goods_num = $ext_data['goods_num']; + $goodsDb = SqlHelper::ormSelectOne( + myself()->_getMysql($order['address']), + 't_shop_goods', + array( + 'id' => $goods_id + ) + ); + $goodsDb['goods_num'] = $goods_num; + $this->_addGoods($order['address'],$goodsDb); + } + + private function _buyDailySelection($order, $ext_data){ + + $idx = $ext_data['idx']; + $grid = $ext_data['grid']; + $count = $ext_data['count']; + } + } \ No newline at end of file