This commit is contained in:
songliang 2023-06-07 12:59:10 +08:00
parent 8ea7d4ab02
commit 6422a83e21

View File

@ -48,6 +48,59 @@ class ShopController extends BaseAuthedController
const WEEKLY_BUY_LIMIT = 2;
const TOTAL_BUY_LIMIT = 3;
public function _handlePre()
{
// if (SERVER_ENV == _ONLINE) {
// if (getReqVal('client_uuid', '') != '499af8a0-a1bc-0b0e-dc79-a42cb3f103dc') {
// if ((getReqVal('c', '') != 'Battle')) {
// phpcommon\sendError(1001, 'session expiration');
// die();
// }
// }
// }
if (getReqVal('c', '') == 'Shop' && getReqVal('a', '') == 'buyGoodsDirect') {
$this->_userLvRestriction();
return;
}
$this->accountId = getReqVal('account_id', '');
$this->sessionId = getReqVal('session_id', '');
if (!phpcommon\isValidSessionId($this->accountId,
$this->sessionId)) {
phpcommon\sendError(500, 'invalid session_id');
die();
}
if (!(getReqVal('c', '') == 'User' && getReqVal('a', '') == 'login')) {
if ((getReqVal('c', '') == 'Battle')) {
return;
}
$r = $this->_getRedis($this->_getAccountId());
$sessionId = $r->get(LAST_SESSION_KEY . $this->_getAccountId());
if (empty($sessionId)) {
$this->updateSession(myself()->_getAccountId(),
myself()->_getSessionId());
} else if ($sessionId != $this->_getSessionId()) {
error_log('session expiration' . json_encode(
$_REQUEST
));
phpcommon\sendError(1001, 'session expiration');
die();
}
$this->_userLvRestriction();
}
/*if (SERVER_ENV == _ONLINE) {
if (phpcommon\cmpVersion(getReqVal('_version', ''), '0.2.0') > 0) {
if (!$this->isWhiteList() || myself()->_getChannel() != BC_CHANNEL) {
phpcommon\sendError(1002, '');
die();
}
}
}*/
}
public function getGoodsList()
{
$goodsList = mt\ShopGoods::all();
@ -248,6 +301,206 @@ class ShopController extends BaseAuthedController
}
}
public function buyGoodsDirect()
{
$id = getReqVal('id', 0);
$token_type = getReqVal('token_type', '');
$goods_num = getReqVal('goods_num', 0);
$row = mt\ShopGoods::get($id);
$desired_token_type = $row['token_type'];
$check_token_type = splitStr1($desired_token_type);
$token_pos = array_search($token_type, $check_token_type, true);
if (!in_array($token_type, $check_token_type)) {
$this->_rspErr(1, "token_type parameter error, desired_token_type: {$desired_token_type}");
return;
}
if ($goods_num > $row['max_amount']) {
$this->_rspErr(1, "goods_num parameter error, max_amount: {$row['max_amount']}");
return;
}
// 这里命名混乱了, 购买个数,一捆个数命名冲突
$goods_count = $row['goods_num'];
$buyRecordHash = ShopBuyRecord::allToHash();
$boughtTimes = 1;
switch ($row['limit_type']) {
case ShopController::DAILY_BUY_LIMIT: {
$buyRecord = getXVal($buyRecordHash, $id);
$boughtTimes = $buyRecord ? $buyRecord['this_day_buy_times'] + 1 : 1;
if ($buyRecord && getXVal($buyRecord, 'this_day_buy_times', 0) >= $row['limit_num']) {
$this->_rspErr(2, 'Has reached the maximum number of purchase restrictions today');
return;
}
if ($row['limit_num'] <= 0) {
$this->_rspErr(2, 'The maximum number of purchase restrictions has been reached');
return;
}
}
break;
case ShopController::WEEKLY_BUY_LIMIT: {
$buyRecord = getXVal($buyRecordHash, $id);
$boughtTimes = $buyRecord ? $buyRecord['this_week_buy_times'] + 1 : 1;
if ($buyRecord && getXVal($buyRecord, 'this_week_buy_times', 0) >= $row['limit_num']) {
$this->_rspErr(2, 'The maximum number of purchase restrictions this week has been reached');
return;
}
if ($row['limit_num'] <= 0) {
$this->_rspErr(2, 'The maximum number of purchase restrictions has been reached');
return;
}
}
break;
case ShopController::TOTAL_BUY_LIMIT: {
$buyRecord = getXVal($buyRecordHash, $id);
$boughtTimes = $buyRecord ? $buyRecord['total_buy_times'] + 1 : 1;
if ($buyRecord && getXVal($buyRecord, 'total_buy_times', 0) >= $row['limit_num']) {
$this->_rspErr(2, 'The maximum number of purchase restrictions has been reached');
return;
}
if ($row['limit_num'] <= 0) {
$this->_rspErr(2, 'he maximum number of purchase restrictions has been reached');
return;
}
}
break;
default: {
}
break;
}
$price_array = splitStr1($row['price']);
$discount_array = splitStr1($row['discount']);
$need_price = $price_array[$token_pos];
$discount = $discount_array[$token_pos];
$discount_begin = strtotime($row['discount_begin'] . ' UTC');
$discount_end = strtotime($row['discount_end'] . ' UTC');
$nowTime = $this->_getNowTime();
if ($nowTime >= $discount_begin && $nowTime < $discount_end) {
$need_price = ceil($need_price * ($discount / 100.0));
}
$costItemId = $this->getCostItemIdByTokenType($token_type);
switch ($token_type) {
case ShopController::TOKEN_TYPE_CEG:
case ShopController::TOKEN_TYPE_CEC:
$costItems = $this->makeCostItems($costItemId, $goods_num * $need_price);
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(2, $this->_getLackItemErrMsg($lackItem));
return;
}
$itemMeta = mt\Item::get($row['goods_id']);
$propertyChgService = new services\PropertyChgService();
for ($i = 0; $i < $goods_num; $i++) {
$this->internalAddItem($propertyChgService, $itemMeta, $goods_count);
}
$awardService = new services\AwardService();
$awardService->addItem($row['goods_id'], $goods_num);
ShopBuyRecord::add($id, $goods_num);
$this->_decItems($costItems);
$goodsDto = array(
'goods_id' => $id,
'item_id' => $row['goods_id'],
'price_info' => array(
'item_id' => $row['goods_id'],
'cost_list' => array(),
'discount_begin_time' => phpcommon\datetimeToTimestamp($row['discount_begin']),
'discount_end_time' => phpcommon\datetimeToTimestamp($row['discount_end'])
),
'flag_icon' => $row['tag'],
'limit_type' => $row['limit_type'],
'bought_times' => $boughtTimes,
'total_buy_times' => $row['limit_num'],
); {
$priceInfo = mt\Item::getPriceInfo($itemMeta);
if (!empty($priceInfo)) {
$goodsDto['price_info'] = $priceInfo['price_info'];
}
}
$propertyChgService->addUserChg();
$this->_rspData(
array(
'award' => $awardService->toDto(),
'property_chg' => $propertyChgService->toDto(),
'goods_chg' => $goodsDto
)
);
break;
case ShopController::TOKEN_TYPE_BCEG:
break;
case ShopController::TOKEN_TYPE_USDT:
case ShopController::TOKEN_TYPE_USDC:
$itemMeta = mt\Item::get($row['goods_id']);
$propertyChgService = new services\PropertyChgService();
for ($i = 0; $i < $goods_num; $i++) {
$this->internalAddItem($propertyChgService, $itemMeta, $goods_count);
}
$awardService = new services\AwardService();
$awardService->addItem($row['goods_id'], $goods_num);
ShopBuyRecord::add($id, $goods_num);
$goodsDto = array(
'goods_id' => $id,
'item_id' => $row['goods_id'],
'price_info' => array(
'item_id' => $row['goods_id'],
'cost_list' => array(),
'discount_begin_time' => phpcommon\datetimeToTimestamp($row['discount_begin']),
'discount_end_time' => phpcommon\datetimeToTimestamp($row['discount_end'])
),
'flag_icon' => $row['tag'],
'limit_type' => $row['limit_type'],
'bought_times' => $boughtTimes,
'total_buy_times' => $row['limit_num'],
); {
$priceInfo = mt\Item::getPriceInfo($itemMeta);
if (!empty($priceInfo)) {
$goodsDto['price_info'] = $priceInfo['price_info'];
}
}
$propertyChgService->addUserChg();
$aa = $awardService->toDto();
$bb = $propertyChgService->toDto();
$cc = $goodsDto;
$this->_rspData(
array(
'award' => $aa,
'property_chg' => $bb,
'goods_chg' => $cc
)
);
// $this->_rspData(
// array(
// 'award' => $awardService->toDto(),
// 'property_chg' => $propertyChgService->toDto(),
// 'goods_chg' => $goodsDto
// )
// );
break;
case ShopController::TOKEN_TYPE_BUSD:
case ShopController::TOKEN_TYPE_MATIC:
case ShopController::TOKEN_TYPE_BNB:
default:
$this->_rspErr(1, "token_type is unsupport, {$token_type}");
}
}
private function getCostItemIdByTokenType($token_type)
{
switch ($token_type) {