Files
dafuweng-saiadmin6.x/server/app/api/controller/GameController.php

68 lines
2.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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);
}
}