[接口]新增获取彩金池,购买游戏次数(购买抽奖券)接口

This commit is contained in:
2026-03-04 15:35:30 +08:00
parent dead78a5f3
commit 5d0e2a82ff
3 changed files with 170 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace app\api\controller;
use support\Request;
use support\Response;
use app\api\logic\UserLogic;
use app\api\logic\GameLogic;
use app\dice\model\reward_config\DiceRewardConfig;
use plugin\saiadmin\basic\OpenController;
/**
* 游戏相关接口(购买抽奖券等)
*/
class GameController extends OpenController
{
/**
* 购买抽奖券
* POST /api/game/buyLotteryTickets
* header: auth-token, user-token
* body: count = 1 | 5 | 101次/100coin, 5次/500coin, 10次/1000coin
* 记录钱包流水,并更新缓存中玩家的 total_draw_count、paid_draw_count、free_draw_count、coin
*/
public function buyLotteryTickets(Request $request): Response
{
$token = $request->header('user-token');
if (empty($token)) {
$auth = $request->header('authorization');
if ($auth && stripos($auth, 'Bearer ') === 0) {
$token = trim(substr($auth, 7));
}
}
if (empty($token)) {
return $this->fail('请携带 user-token');
}
$userId = UserLogic::getUserIdFromToken($token);
if ($userId === null) {
return $this->fail('user-token 无效或已过期');
}
$count = (int) $request->post('count', 0);
if (!in_array($count, [1, 5, 10], true)) {
return $this->fail('购买抽奖券错误');
}
try {
$logic = new GameLogic();
$data = $logic->buyLotteryTickets($userId, $count);
return $this->success($data);
} catch (\plugin\saiadmin\exception\ApiException $e) {
return $this->fail($e->getMessage());
}
}
/**
* 获取彩金池(中奖配置表)
* GET /api/game/lotteryPool
* header: auth-token
* 返回 DiceRewardConfig 列表(彩金池/中奖配置)
*/
public function lotteryPool(Request $request): Response
{
$list = DiceRewardConfig::order('id', 'asc')->select()->toArray();
return $this->success($list);
}
}