1.优化接口/api/v1/getPlayerGameRecord
2.增加接口日志记录接口request和response
This commit is contained in:
@@ -23,6 +23,14 @@ use app\api\cache\UserCache;
|
||||
*/
|
||||
class GameController extends BaseController
|
||||
{
|
||||
/** 游戏记录 / 钱包流水 / 中奖券记录等拉取类接口的条数上限(与对接文档一致) */
|
||||
private const PLAYER_PULL_RECORD_MAX_LIMIT = 2000;
|
||||
|
||||
private const PLAYER_PULL_RECORD_DEFAULT_LIMIT = 20;
|
||||
|
||||
/** 拉取类流水仅允许查询「当前时间起向前」的最大天数(含起止) */
|
||||
private const PLAYER_PULL_RECORD_MAX_RANGE_DAYS = 7;
|
||||
|
||||
private const GAME_PUBLIC_FIELDS = [
|
||||
'provider',
|
||||
'provider_code',
|
||||
@@ -144,9 +152,110 @@ class GameController extends BaseController
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析拉取类流水接口的 limit(与 getPlayerGameRecord / getPlayerWalletRecord / getPlayerTicketRecord 共用)
|
||||
*/
|
||||
private function resolvePullRecordLimit(Request $request): int
|
||||
{
|
||||
$limit = (int) $request->post('limit', self::PLAYER_PULL_RECORD_DEFAULT_LIMIT);
|
||||
if ($limit < 1 || $limit > self::PLAYER_PULL_RECORD_MAX_LIMIT) {
|
||||
$limit = self::PLAYER_PULL_RECORD_DEFAULT_LIMIT;
|
||||
}
|
||||
|
||||
return $limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 start_create_time / end_create_time 规范为「最近 7 天内」且跨度不超过 7 天的闭区间。
|
||||
*
|
||||
* @return array{ok: true, start: string, end: string}|array{ok: false, message: string}
|
||||
*/
|
||||
private function resolvePullRecordTimeWindow(string $startRaw, string $endRaw): array
|
||||
{
|
||||
$nowTs = time();
|
||||
$weekAgoTs = strtotime('-' . self::PLAYER_PULL_RECORD_MAX_RANGE_DAYS . ' days', $nowTs);
|
||||
if ($weekAgoTs === false) {
|
||||
$weekAgoTs = $nowTs - self::PLAYER_PULL_RECORD_MAX_RANGE_DAYS * 86400;
|
||||
}
|
||||
$nowStr = date('Y-m-d H:i:s', $nowTs);
|
||||
$weekAgoStr = date('Y-m-d H:i:s', $weekAgoTs);
|
||||
|
||||
if ($startRaw === '' && $endRaw === '') {
|
||||
return ['ok' => true, 'start' => $weekAgoStr, 'end' => $nowStr];
|
||||
}
|
||||
|
||||
$startTs = $startRaw === '' ? null : strtotime($startRaw);
|
||||
$endTs = $endRaw === '' ? null : strtotime($endRaw);
|
||||
|
||||
if ($startRaw !== '' && $startTs === false) {
|
||||
return ['ok' => false, 'message' => 'Invalid start_create_time'];
|
||||
}
|
||||
if ($endRaw !== '' && $endTs === false) {
|
||||
return ['ok' => false, 'message' => 'Invalid end_create_time'];
|
||||
}
|
||||
|
||||
if ($startRaw === '' && $endRaw !== '' && $endTs !== null) {
|
||||
$endTs = min($endTs, $nowTs);
|
||||
if ($endTs < $weekAgoTs) {
|
||||
return ['ok' => false, 'message' => 'end_create_time must be within the last 7 days'];
|
||||
}
|
||||
$startTs = strtotime('-' . self::PLAYER_PULL_RECORD_MAX_RANGE_DAYS . ' days', $endTs);
|
||||
if ($startTs === false) {
|
||||
$startTs = $endTs - self::PLAYER_PULL_RECORD_MAX_RANGE_DAYS * 86400;
|
||||
}
|
||||
if ($startTs < $weekAgoTs) {
|
||||
$startTs = $weekAgoTs;
|
||||
}
|
||||
|
||||
return ['ok' => true, 'start' => date('Y-m-d H:i:s', $startTs), 'end' => date('Y-m-d H:i:s', $endTs)];
|
||||
}
|
||||
|
||||
if ($startRaw !== '' && $startTs !== null && $endRaw === '') {
|
||||
if ($startTs > $nowTs) {
|
||||
return ['ok' => false, 'message' => 'start_create_time cannot be in the future'];
|
||||
}
|
||||
if ($startTs < $weekAgoTs) {
|
||||
return ['ok' => false, 'message' => 'start_create_time cannot be earlier than 7 days ago'];
|
||||
}
|
||||
$endTs = strtotime('+' . self::PLAYER_PULL_RECORD_MAX_RANGE_DAYS . ' days', $startTs);
|
||||
if ($endTs === false) {
|
||||
$endTs = $startTs + self::PLAYER_PULL_RECORD_MAX_RANGE_DAYS * 86400;
|
||||
}
|
||||
$endTs = min($endTs, $nowTs);
|
||||
|
||||
return ['ok' => true, 'start' => date('Y-m-d H:i:s', $startTs), 'end' => date('Y-m-d H:i:s', $endTs)];
|
||||
}
|
||||
|
||||
if ($startTs !== null && $endTs !== null) {
|
||||
if ($endTs > $nowTs) {
|
||||
$endTs = $nowTs;
|
||||
}
|
||||
if ($startTs > $endTs) {
|
||||
return ['ok' => false, 'message' => 'start_create_time cannot be after end_create_time'];
|
||||
}
|
||||
if ($startTs < $weekAgoTs) {
|
||||
return ['ok' => false, 'message' => 'start_create_time cannot be earlier than 7 days ago'];
|
||||
}
|
||||
if ($endTs < $weekAgoTs) {
|
||||
return ['ok' => false, 'message' => 'end_create_time must be within the last 7 days'];
|
||||
}
|
||||
$maxEndForStart = strtotime('+' . self::PLAYER_PULL_RECORD_MAX_RANGE_DAYS . ' days', $startTs);
|
||||
if ($maxEndForStart === false) {
|
||||
$maxEndForStart = $startTs + self::PLAYER_PULL_RECORD_MAX_RANGE_DAYS * 86400;
|
||||
}
|
||||
if ($endTs > $maxEndForStart) {
|
||||
return ['ok' => false, 'message' => 'Time range cannot exceed 7 days'];
|
||||
}
|
||||
|
||||
return ['ok' => true, 'start' => date('Y-m-d H:i:s', $startTs), 'end' => date('Y-m-d H:i:s', $endTs)];
|
||||
}
|
||||
|
||||
return ['ok' => false, 'message' => 'Invalid time parameters'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取游戏记录
|
||||
* POST 参数:username(非必填,没填则不按用户筛选), start_create_time, end_create_time(非必填,没填则不筛选时间)
|
||||
* POST 参数:username(非必填), start_create_time, end_create_time(可选,须在「最近 7 天」内且跨度不超过 7 天;均不传则默认最近 7 天), limit(非必填,返回条数上限)
|
||||
* 返回 DicePlayRecord 中非敏感信息
|
||||
*/
|
||||
public function getPlayerGameRecord(Request $request): Response
|
||||
@@ -154,15 +263,11 @@ class GameController extends BaseController
|
||||
$username = trim((string) ($request->post('username', '')));
|
||||
$startCreateTime = trim((string) ($request->post('start_create_time', '')));
|
||||
$endCreateTime = trim((string) ($request->post('end_create_time', '')));
|
||||
$page = (int) $request->post('page', 1);
|
||||
$limit = (int) $request->post('limit', 20);
|
||||
|
||||
if ($page < 1) {
|
||||
$page = 1;
|
||||
}
|
||||
if ($limit < 1 || $limit > 100) {
|
||||
$limit = 20;
|
||||
$window = $this->resolvePullRecordTimeWindow($startCreateTime, $endCreateTime);
|
||||
if (!$window['ok']) {
|
||||
return $this->fail($window['message'], ReturnCode::PARAMS_ERROR);
|
||||
}
|
||||
$limit = $this->resolvePullRecordLimit($request);
|
||||
|
||||
$query = DicePlayRecord::order('id', 'desc');
|
||||
|
||||
@@ -174,14 +279,10 @@ class GameController extends BaseController
|
||||
$query->where('player_id', (int) $player->id);
|
||||
}
|
||||
|
||||
if ($startCreateTime !== '') {
|
||||
$query->where('create_time', '>=', $startCreateTime);
|
||||
}
|
||||
if ($endCreateTime !== '') {
|
||||
$query->where('create_time', '<=', $endCreateTime);
|
||||
}
|
||||
$query->where('create_time', '>=', $window['start']);
|
||||
$query->where('create_time', '<=', $window['end']);
|
||||
|
||||
$list = $query->page($page, $limit)->select()->toArray();
|
||||
$list = $query->limit($limit)->select()->toArray();
|
||||
$playerIds = array_unique(array_column($list, 'player_id'));
|
||||
if (!empty($playerIds)) {
|
||||
$players = DicePlayer::whereIn('id', $playerIds)->field('id,username,phone')->select()->toArray();
|
||||
@@ -199,7 +300,7 @@ class GameController extends BaseController
|
||||
|
||||
/**
|
||||
* 获取钱包流水
|
||||
* POST 参数:username(非必填,没填则不按用户筛选), start_create_time, end_create_time(非必填,没填则不筛选时间)
|
||||
* POST 参数:username(非必填), start_create_time, end_create_time(可选,规则同 getPlayerGameRecord), limit(非必填)
|
||||
* 返回 DicePlayerWalletRecord 中非敏感信息
|
||||
*/
|
||||
public function getPlayerWalletRecord(Request $request): Response
|
||||
@@ -207,15 +308,11 @@ class GameController extends BaseController
|
||||
$username = trim((string) ($request->post('username', '')));
|
||||
$startCreateTime = trim((string) ($request->post('start_create_time', '')));
|
||||
$endCreateTime = trim((string) ($request->post('end_create_time', '')));
|
||||
$page = (int) $request->post('page', 1);
|
||||
$limit = (int) $request->post('limit', 20);
|
||||
|
||||
if ($page < 1) {
|
||||
$page = 1;
|
||||
}
|
||||
if ($limit < 1 || $limit > 100) {
|
||||
$limit = 20;
|
||||
$window = $this->resolvePullRecordTimeWindow($startCreateTime, $endCreateTime);
|
||||
if (!$window['ok']) {
|
||||
return $this->fail($window['message'], ReturnCode::PARAMS_ERROR);
|
||||
}
|
||||
$limit = $this->resolvePullRecordLimit($request);
|
||||
|
||||
$query = DicePlayerWalletRecord::order('id', 'desc');
|
||||
|
||||
@@ -227,23 +324,19 @@ class GameController extends BaseController
|
||||
$query->where('player_id', (int) $player->id);
|
||||
}
|
||||
|
||||
if ($startCreateTime !== '') {
|
||||
$query->where('create_time', '>=', $startCreateTime);
|
||||
}
|
||||
if ($endCreateTime !== '') {
|
||||
$query->where('create_time', '<=', $endCreateTime);
|
||||
}
|
||||
$query->where('create_time', '>=', $window['start']);
|
||||
$query->where('create_time', '<=', $window['end']);
|
||||
|
||||
$list = $query->with(['dicePlayer' => function ($q) {
|
||||
$q->field('id,username,phone');
|
||||
}])->page($page, $limit)->select()->toArray();
|
||||
}])->limit($limit)->select()->toArray();
|
||||
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户中奖券获取记录
|
||||
* POST 参数:username(非必填,没填则不按用户筛选), start_create_time, end_create_time(非必填,没填则不筛选时间)
|
||||
* POST 参数:username(非必填), start_create_time, end_create_time(可选,规则同 getPlayerGameRecord), limit(非必填)
|
||||
* 返回 DicePlayerTicketRecord 中非敏感信息
|
||||
*/
|
||||
public function getPlayerTicketRecord(Request $request): Response
|
||||
@@ -251,15 +344,11 @@ class GameController extends BaseController
|
||||
$username = trim((string) ($request->post('username', '')));
|
||||
$startCreateTime = trim((string) ($request->post('start_create_time', '')));
|
||||
$endCreateTime = trim((string) ($request->post('end_create_time', '')));
|
||||
$page = (int) $request->post('page', 1);
|
||||
$limit = (int) $request->post('limit', 20);
|
||||
|
||||
if ($page < 1) {
|
||||
$page = 1;
|
||||
}
|
||||
if ($limit < 1 || $limit > 100) {
|
||||
$limit = 20;
|
||||
$window = $this->resolvePullRecordTimeWindow($startCreateTime, $endCreateTime);
|
||||
if (!$window['ok']) {
|
||||
return $this->fail($window['message'], ReturnCode::PARAMS_ERROR);
|
||||
}
|
||||
$limit = $this->resolvePullRecordLimit($request);
|
||||
|
||||
$query = DicePlayerTicketRecord::order('id', 'desc');
|
||||
|
||||
@@ -271,16 +360,12 @@ class GameController extends BaseController
|
||||
$query->where('player_id', (int) $player->id);
|
||||
}
|
||||
|
||||
if ($startCreateTime !== '') {
|
||||
$query->where('create_time', '>=', $startCreateTime);
|
||||
}
|
||||
if ($endCreateTime !== '') {
|
||||
$query->where('create_time', '<=', $endCreateTime);
|
||||
}
|
||||
$query->where('create_time', '>=', $window['start']);
|
||||
$query->where('create_time', '<=', $window['end']);
|
||||
|
||||
$list = $query->with(['dicePlayer' => function ($q) {
|
||||
$q->field('id,username,phone');
|
||||
}])->page($page, $limit)->select()->toArray();
|
||||
}])->limit($limit)->select()->toArray();
|
||||
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user