69 lines
2.2 KiB
PHP
69 lines
2.2 KiB
PHP
<?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\api\util\ReturnCode;
|
||
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 | 10(1次/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', ReturnCode::MISSING_TOKEN);
|
||
}
|
||
$userId = UserLogic::getUserIdFromToken($token);
|
||
if ($userId === null) {
|
||
return $this->fail('user-token 无效或已过期', ReturnCode::TOKEN_TIMEOUT);
|
||
}
|
||
|
||
$count = (int) $request->post('count', 0);
|
||
if (!in_array($count, [1, 5, 10], true)) {
|
||
return $this->fail('购买抽奖券错误', ReturnCode::EMPTY_PARAMS);
|
||
}
|
||
|
||
try {
|
||
$logic = new GameLogic();
|
||
$data = $logic->buyLotteryTickets($userId, $count);
|
||
return $this->success($data);
|
||
} catch (\plugin\saiadmin\exception\ApiException $e) {
|
||
return $this->fail($e->getMessage(), ReturnCode::EMPTY_PARAMS);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取彩金池(中奖配置表)
|
||
* 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);
|
||
}
|
||
}
|