525 lines
16 KiB
PHP
525 lines
16 KiB
PHP
<?php
|
|
|
|
require_once('mt/Shop.php');
|
|
require_once('mt/ShopGoods.php');
|
|
require_once('mt/Hero.php');
|
|
require_once('mt/Item.php');
|
|
require_once('mt/Parameter.php');
|
|
require_once('mt/Drop.php');
|
|
require_once('mt/ShopChest.php');
|
|
|
|
require_once('models/User.php');
|
|
require_once('models/Hero.php');
|
|
require_once('models/Bag.php');
|
|
require_once('models/HeroSkin.php');
|
|
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('services/ShopService.php');
|
|
|
|
require_once('phpcommon/bignumber.php');
|
|
|
|
require_once('services/LogService.php');
|
|
require_once('services/callback/ShopAddItemService.php');
|
|
|
|
use models\User;
|
|
use models\Bag;
|
|
use models\Hero;
|
|
use models\HeroSkin;
|
|
use models\Gun;
|
|
use models\GunSkin;
|
|
use models\ShopBuyRecord;
|
|
use models\Chip;
|
|
use mt\Shop;
|
|
use mt\ShopChest;
|
|
use mt\ShopGoods;
|
|
use models\Transaction;
|
|
use models\BcOrder;
|
|
use services\LogService;
|
|
use services\ShopAddItemService;
|
|
use services\ShopService;
|
|
|
|
class ShopController extends BaseAuthedController {
|
|
|
|
public function getGoodsList()
|
|
{
|
|
$shopId = getReqVal('shop_id', 0);
|
|
$goodsList = ShopService::getGoodsList($shopId);
|
|
$this->_rspData(
|
|
array(
|
|
'goods_list' => $goodsList,
|
|
)
|
|
);
|
|
}
|
|
|
|
public function buyGoods()
|
|
{
|
|
$goodsUuid = getReqVal('goods_uuid', 0);
|
|
$tokenType = getReqVal('token_type', '');
|
|
$goodsNum = getReqVal('goods_num', 0);
|
|
|
|
if ($goodsNum < 1) {
|
|
$this->_rspErr(1, "goods_num parameter error, goods_num: {$goodsNum}");
|
|
return;
|
|
}
|
|
|
|
$goodsMeta = mt\ShopGoods::getByGoodsUuid($goodsUuid);
|
|
if (!$goodsMeta) {
|
|
$this->_rspErr(1, 'goods not found');
|
|
return;
|
|
}
|
|
if ($goodsNum > $goodsMeta['max_amount']) {
|
|
$this->_rspErr(1, "goods_num parameter error, max_amount: {$goodsMeta['max_amount']}");
|
|
return;
|
|
}
|
|
$itemMeta = mt\Item::get($goodsMeta['item_id']);
|
|
if (!$itemMeta) {
|
|
$this->_rspErr(1, 'goods not found, goods_id: ' . $goodsMeta['goods_id']);
|
|
return;
|
|
}
|
|
$errCode = 0;
|
|
$errMsg = '';
|
|
if ($itemMeta['type'] == mt\Item::HERO_SKIN_TYPE) {
|
|
if (!$this->canBuy($itemMeta, $errCode, $errMsg)) {
|
|
$this->_rspErr($errCode, $errMsg);
|
|
return;
|
|
}
|
|
}
|
|
if (!ShopService::buyLimitCheck($goodsMeta, $errCode, $errMsg)) {
|
|
$this->_rspErr($errCode, $errMsg);
|
|
return;
|
|
}
|
|
|
|
$desiredTokenType = $goodsMeta['token_type'];
|
|
$checkTokenType = splitStr1($desiredTokenType);
|
|
$tokenPos = array_search($tokenType, $checkTokenType, true);
|
|
$isFreeBuy = false;
|
|
if (!empty($goodsMeta['free_type'])) {
|
|
$count = $this->countFreeBuyTimes($goodsMeta['free_type'], $goodsMeta['id'], $goodsMeta['item_id']);
|
|
if ($count < $goodsMeta['free_num']) {
|
|
$isFreeBuy = true;
|
|
}
|
|
}
|
|
$propertyChgService = new services\PropertyChgService();
|
|
$awardService = new services\AwardService();
|
|
if ($isFreeBuy) {
|
|
$awardService->addItem($itemMeta['id'], $goodsNum);
|
|
$this->_rspData(
|
|
array(
|
|
'award' => $awardService->toDto(),
|
|
'property_chg' => $propertyChgService->toDto(),
|
|
//'goods_chg' => $goodsDto
|
|
)
|
|
);
|
|
return;
|
|
} else {
|
|
if (!in_array($tokenType, $checkTokenType)) {
|
|
$this->_rspErr(1, "token_type parameter error, desired_token_type: {$desiredTokenType}");
|
|
return;
|
|
}
|
|
}
|
|
|
|
$priceArray = splitStr1($goodsMeta['price']);
|
|
$discountArray = splitStr1($goodsMeta['discount']);
|
|
|
|
$needPrice = $priceArray[$tokenPos];
|
|
$discount = $discountArray[$tokenPos];
|
|
|
|
$discountBegin = strtotime($goodsMeta['discount_begin']);
|
|
$discountEnd = strtotime($goodsMeta['discount_end']);
|
|
$nowTime = $this->_getNowTime();
|
|
|
|
if ($nowTime >= $discountBegin && $nowTime < $discountEnd) {
|
|
$needPrice = ceil($needPrice * ($discount / 100.0));
|
|
}
|
|
|
|
$costItemId = $this->getCostItemIdByTokenType($tokenType);
|
|
$costItems = array(
|
|
'item_id' => $costItemId,
|
|
'item_num' => $goodsNum * $needPrice
|
|
);
|
|
if (!in_array(
|
|
$tokenType,
|
|
array(
|
|
mt\Shop::TOKEN_TYPE_GOLD,
|
|
mt\Shop::TOKEN_TYPE_DIAMOND
|
|
)
|
|
)) {
|
|
$this->_rspErr(1, "token_type is unsupport, {$tokenType}");
|
|
return;
|
|
}
|
|
$lackItem = null;
|
|
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
|
|
$this->_rspErr(2, $this->_getLackItemErrMsg($lackItem));
|
|
return;
|
|
}
|
|
ShopBuyRecord::add($id, $goodsNum);
|
|
$this->_decItems($costItems);
|
|
for ($i = 0; $i < $goodsNum; $i++) {
|
|
$this->internalAddItem($awardService,
|
|
$propertyChgService,
|
|
$itemMeta,
|
|
$goodsMeta['goods_num'],
|
|
0);
|
|
}
|
|
$propertyChgService->addUserChg();
|
|
$this->_rspData(
|
|
array(
|
|
'award' => $awardService->toDto(),
|
|
'property_chg' => $propertyChgService->toDto(),
|
|
//'goods_chg' => $goodsDto
|
|
)
|
|
);
|
|
}
|
|
|
|
public function outappPurchase()
|
|
{
|
|
$id = getReqVal('id', 0);
|
|
$tokenType = getReqVal('token_type', '');
|
|
$goodsNum = getReqVal('goods_num', 1);
|
|
|
|
if ($goodsNum <= 0) {
|
|
$this->_rspErr(1, 'goods_num is invalid');
|
|
return;
|
|
}
|
|
|
|
$goods = mt\ShopGoods::get($id);
|
|
if (!$goods) {
|
|
$this->_rspErr(1, "id is invalid. {$id}");
|
|
return;
|
|
}
|
|
|
|
if ($goods['shop_id'] == 9 && $goodsNum > 1) {
|
|
$this->_rspErr(1, 'goods_num is invalid');
|
|
return;
|
|
}
|
|
|
|
$conn = myself()->_getSelfMysql();
|
|
|
|
$address = myself()->_getAddress();
|
|
if (!$address) {
|
|
$this->_rspErr(1, 'address is empty');
|
|
return;
|
|
}
|
|
|
|
$chk = SqlHelper::insert(
|
|
$conn,
|
|
't_shop_buy_order',
|
|
array(
|
|
'address' => $address,
|
|
'createtime' => myself()->_getNowTime(),
|
|
'id' => $id,
|
|
'item_id' => $goods['goods_id'] ? $goods['goods_id'] : 0,
|
|
'goods_num' => $goodsNum,
|
|
'status' => 0, // 0-客户端申请了订单 1-订单完成 2-订单失败
|
|
)
|
|
);
|
|
if ($chk) {
|
|
$lastId = $this->lastInsertId($conn);
|
|
$order_id = $this->genOrderId($lastId);
|
|
SqlHelper::update(
|
|
$conn,
|
|
't_shop_buy_order',
|
|
array(
|
|
'idx' => $lastId,
|
|
),
|
|
array(
|
|
'order_id' => $order_id,
|
|
)
|
|
);
|
|
$this->_rspData(
|
|
array(
|
|
'order_id' => $order_id,
|
|
)
|
|
);
|
|
} else {
|
|
$this->_rspErr(1, "insert error, id: {$id}, token_type: {$tokenType}, goods_num: {$goodsNum}");
|
|
}
|
|
}
|
|
|
|
public function inappPurchase()
|
|
{
|
|
$self = myself();
|
|
if (!$self) {
|
|
$this->_rspErr(1, "start purchase failed");
|
|
return;
|
|
}
|
|
|
|
$id = getReqVal('id', 0);
|
|
$goods = mt\ShopGoods::get($id);
|
|
if (!$goods) {
|
|
$this->_rspErr(2, "start purchase failed");
|
|
return;
|
|
}
|
|
if ($goods['shop_id'] != 9) {
|
|
$this->_rspErr(3, "start purchase failed");
|
|
return;
|
|
}
|
|
|
|
$goodsNum = getReqVal('goods_num', 1);
|
|
|
|
$account_id = $self->_getAccountId();
|
|
$address = $self->_getAddress();
|
|
if (empty($address)) {
|
|
$this->_rspErr(4, "start purchase failed");
|
|
return;
|
|
}
|
|
|
|
$item_id = $goods['goods_id'];
|
|
$item_num = $goods['goods_num'] * $goodsNum;
|
|
|
|
$conn = $self->_getMysql('');
|
|
|
|
$chk = SqlHelper::insert($conn, 't_web2_order', array(
|
|
'status' => 0,
|
|
'createtime' => $self->_getNowTime(),
|
|
'account_id' => $account_id,
|
|
'address' => $address,
|
|
'item_id' => $item_id,
|
|
'item_num' => $item_num,
|
|
'id' => $id,
|
|
'goods_num' => $goodsNum,
|
|
'price' => $goods['price'],
|
|
));
|
|
|
|
if (!$chk) {
|
|
$this->_rspErr(5, "start purchase failed");
|
|
return;
|
|
}
|
|
$lastId = $this->lastInsertId($conn);
|
|
$order_id = $this->genOrderId($lastId);
|
|
|
|
$test = SqlHelper::update($conn, 't_web2_order', array('idx' => $lastId), array('order_id' => $order_id));
|
|
if (!$test) {
|
|
$this->_rspErr(6, "start purchase failed");
|
|
return;
|
|
}
|
|
$this->_rspData(array(
|
|
'order_id' => $order_id,
|
|
));
|
|
}
|
|
|
|
private function genOrderId($id)
|
|
{
|
|
$order_id_base = date('YmdHis') . "10000000";
|
|
$divIdx = phpcommon\bnToStr(gmp_mod($id, 9999999));
|
|
$order_id = phpcommon\bnAdd_s($order_id_base, $divIdx);
|
|
return $order_id;
|
|
}
|
|
|
|
public function queryInappPurchase()
|
|
{
|
|
$order_id = getReqVal('order_id', '');
|
|
$conn = myself()->_getMysql('');
|
|
$order = SqlHelper::selectOne($conn, 't_web2_order', array('item_id', 'item_num', 'status'), array('order_id' => $order_id));
|
|
if (!$order) {
|
|
$this->_rspErr(1, "order not found");
|
|
return;
|
|
}
|
|
$this->_rspData($order);
|
|
}
|
|
|
|
public function boxPreview()
|
|
{
|
|
$id = getReqVal('id', 0);
|
|
$goods = mt\ShopGoods::get($id);
|
|
$goodsId = $goods['goods_id'];
|
|
$shopId = $goods['shop_id'];
|
|
|
|
$meta = mt\Item::get($goodsId);
|
|
if ($meta['type'] != mt\Item::CHEST_BOX_TYPE) {
|
|
$this->_rspErr(2, 'goods_id is invalid');
|
|
return;
|
|
}
|
|
$chestType = $meta['sub_type'];
|
|
$itemStore = mt\ShopChest::getRandomItemListByChestType($chestType);
|
|
if (!$itemStore) {
|
|
$this->_rspErr(2, 'goods_id is invalid');
|
|
return;
|
|
}
|
|
$record = array();
|
|
foreach ($itemStore as $key => $value) {
|
|
foreach ($value as $k => $v) {
|
|
if (empty($record[$v['item_id']])) {
|
|
$record[$v['item_id']] = 0;
|
|
}
|
|
$record[$v['item_id']] += 1;
|
|
}
|
|
}
|
|
|
|
if (!empty($goods['free_type'])) {
|
|
$count = $this->countFreeBuyTimes($goods['free_type'], $goods['id'], $goods['goods_id']);
|
|
$goods['free_num'] = $goods['free_num'] - $count;
|
|
} else {
|
|
$goods['free_num'] = 0;
|
|
}
|
|
|
|
$free_num = $goods['free_num'];
|
|
$this->_rspData(
|
|
array(
|
|
'items' => array_keys($record),
|
|
'free_num' => $free_num,
|
|
'pending' => 0,
|
|
)
|
|
);
|
|
}
|
|
|
|
private function getCostItemIdByTokenType($tokenType)
|
|
{
|
|
switch ($tokenType) {
|
|
case ShopController::TOKEN_TYPE_GOLD:
|
|
return V_ITEM_GOLD;
|
|
break;
|
|
|
|
case ShopController::TOKEN_TYPE_DIAMOND:
|
|
return V_ITEM_DIAMOND;
|
|
break;
|
|
|
|
case ShopController::TOKEN_TYPE_CEG:
|
|
case ShopController::TOKEN_TYPE_BCEG:
|
|
case ShopController::TOKEN_TYPE_USDT:
|
|
case ShopController::TOKEN_TYPE_USDC:
|
|
case ShopController::TOKEN_TYPE_BUSD:
|
|
case ShopController::TOKEN_TYPE_MATIC:
|
|
case ShopController::TOKEN_TYPE_BNB:
|
|
default:
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
private function internalAddItem($awardService,
|
|
$propertyChgService,
|
|
$itemMeta,
|
|
$count,
|
|
$grade = null)
|
|
{
|
|
switch ($itemMeta['type']) {
|
|
case mt\Item::HERO_TYPE:
|
|
{
|
|
if (empty($grade)) {
|
|
$grade = 0;
|
|
}
|
|
switch ($grade) {
|
|
case 1:
|
|
{
|
|
Hero::addHero1($itemMeta);
|
|
}
|
|
break;
|
|
case 2:
|
|
{
|
|
Hero::addHero2($itemMeta);
|
|
}
|
|
break;
|
|
case 3:
|
|
{
|
|
Hero::addHero3($itemMeta);
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
Hero::addHero($itemMeta);
|
|
}
|
|
break;
|
|
}
|
|
$propertyChgService->addHeroChg();
|
|
$propertyChgService->addUserChg();
|
|
}
|
|
break;
|
|
default:
|
|
{
|
|
myself()->_addItems(
|
|
array(
|
|
'item_id' => $itemMeta['id'],
|
|
'item_num' => $count
|
|
),
|
|
$awardService,
|
|
$propertyChgService);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
private function beginFirstTupop($address = null)
|
|
{
|
|
if (!$address) {
|
|
$address = myself()->_getAddress();
|
|
}
|
|
$conn = myself()->_getMysql('');
|
|
|
|
$exist = SqlHelper::selectOne(
|
|
$conn,
|
|
't_first_topup',
|
|
array('address'),
|
|
array('address' => $address)
|
|
);
|
|
if ($exist) {
|
|
return;
|
|
}
|
|
|
|
// 开始首充奖励活动进程
|
|
$chk = SqlHelper::insert(
|
|
$conn,
|
|
't_first_topup',
|
|
array(
|
|
'address' => $address,
|
|
'createtime' => myself()->_getNowTime(),
|
|
'status1' => 0,
|
|
'status2' => 0,
|
|
'status3' => 0,
|
|
)
|
|
);
|
|
if (!$chk) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
private function lastInsertId($conn)
|
|
{
|
|
$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');
|
|
$ret_price = phpcommon\bnDiv(phpcommon\bnMul($bn1, $bn2), pow(10, 8));
|
|
return phpcommon\bnToStr($ret_price);
|
|
}
|
|
|
|
private function addFreeBuyRecord($goods)
|
|
{
|
|
$conn = myself()->_getMysql('');
|
|
$account = myself()->_getAccountId();
|
|
|
|
switch ($goods['free_type']) {
|
|
case 1: {
|
|
$dayTime = myself()->_getNowTime();
|
|
SqlHelper::insert(
|
|
$conn,
|
|
't_shop_free_record',
|
|
array(
|
|
'account_id' => $account,
|
|
'shop_id' => $goods['shop_id'],
|
|
'id' => $goods['id'],
|
|
'goods_id' => $goods['goods_id'],
|
|
'goods_num' => $goods['goods_num'],
|
|
'free_type' => $goods['free_type'],
|
|
'free_num' => $goods['free_num'],
|
|
'createtime' => $dayTime,
|
|
)
|
|
);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|