From 9a43e1d8f2a83302ed1aeae4bceab527eae11dca Mon Sep 17 00:00:00 2001 From: zhenhui <1276357500@qq.com> Date: Tue, 19 May 2026 17:18:51 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BC=98=E5=8C=96=E6=9F=A5=E8=AF=A2=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E4=BF=A1=E6=81=AF=E6=8E=A5=E5=8F=A3/api/v1/getPlayerI?= =?UTF-8?q?nfo=EF=BC=8C=E5=A6=82=E6=9E=9C=E6=B2=A1=E6=9C=89=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=88=99=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/api/controller/v1/GameController.php | 13 ++++- server/app/api/logic/UserLogic.php | 48 +++++++++++++++++++ server/app/dice/model/player/DicePlayer.php | 22 ++++++++- 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/server/app/api/controller/v1/GameController.php b/server/app/api/controller/v1/GameController.php index f40501a..fe23f7c 100644 --- a/server/app/api/controller/v1/GameController.php +++ b/server/app/api/controller/v1/GameController.php @@ -148,7 +148,18 @@ class GameController extends BaseController return $this->success($cached); } - $player = DicePlayer::field(self::PLAYER_INFO_DB_FIELDS)->where('username', $username)->where('dept_id', $deptId)->find(); + try { + $logic = new UserLogic(); + $player = $logic->findOrCreatePlayerByUsername( + $username, + $this->agentAdminId($request), + $deptId > 0 ? $deptId : null + ); + } catch (\plugin\saiadmin\exception\ApiException $e) { + return $this->fail($e->getMessage(), ReturnCode::PARAMS_ERROR); + } + + $player = DicePlayer::field(self::PLAYER_INFO_DB_FIELDS)->where('id', (int) $player->id)->find(); if (!$player) { return $this->fail('User not found', ReturnCode::PARAMS_ERROR); } diff --git a/server/app/api/logic/UserLogic.php b/server/app/api/logic/UserLogic.php index 337af64..955a476 100644 --- a/server/app/api/logic/UserLogic.php +++ b/server/app/api/logic/UserLogic.php @@ -68,6 +68,54 @@ class UserLogic return array_map('intval', $adminIds ?: [(int) $admin->id]); } + /** + * 按用户名查找玩家;不存在则创建并绑定渠道/管理员(供 getPlayerInfo 等接口) + * + * @param int|null $adminId 关联后台管理员 ID(sa_system_user.id) + * @param int|null $deptId 所属渠道 ID + */ + public function findOrCreatePlayerByUsername(string $username, ?int $adminId = null, ?int $deptId = null): DicePlayer + { + $username = trim($username); + if ($username === '') { + throw new ApiException('username is required'); + } + + $query = DicePlayer::where('username', $username); + if ($deptId !== null && $deptId > 0) { + $query->where('dept_id', $deptId); + } + $player = $query->find(); + if ($player) { + if ((int) ($player->status ?? 1) === 0) { + throw new ApiException('Account is disabled'); + } + return $player; + } + + $player = new DicePlayer(); + $player->username = $username; + $player->phone = $username; + $player->password = $this->hashPassword('123456'); + $player->status = self::STATUS_NORMAL; + $player->coin = 0; + if ($deptId !== null && $deptId > 0) { + $player->dept_id = $deptId; + } + if ($adminId !== null && $adminId > 0) { + $player->admin_id = $adminId; + if ($deptId === null || $deptId <= 0) { + $adminUser = SystemUser::find($adminId); + if ($adminUser && !empty($adminUser->dept_id)) { + $player->dept_id = $adminUser->dept_id; + } + } + } + $player->save(); + + return $player; + } + /** * 登录(JSON:username, password, lang, coin, time) * 存在则校验密码并更新 coin(累加);不存在则创建用户并写入 coin。 diff --git a/server/app/dice/model/player/DicePlayer.php b/server/app/dice/model/player/DicePlayer.php index 8ea83b8..63c44f9 100644 --- a/server/app/dice/model/player/DicePlayer.php +++ b/server/app/dice/model/player/DicePlayer.php @@ -91,19 +91,37 @@ class DicePlayer extends DiceModel $lotteryConfigId = null; } if ($lotteryConfigId === null || $lotteryConfigId === '' || (int) $lotteryConfigId === 0) { - $config = DiceLotteryPoolConfig::where('name', 'default')->find(); + $config = self::findDefaultLotteryConfigForPlayer($model); $model->setAttr('lottery_config_id', $config ? (int) $config->id : 0); } // 彩金池权重默认取 name=default 的奖池配置 self::setDefaultWeightsFromLotteryConfig($model); } + /** + * 按玩家所属渠道查找 default 彩金池配置 + */ + protected static function findDefaultLotteryConfigForPlayer(DicePlayer $model): ?DiceLotteryPoolConfig + { + $query = DiceLotteryPoolConfig::where('name', 'default'); + try { + $deptId = $model->getAttr('dept_id'); + if ($deptId !== null && $deptId !== '' && $deptId > 0) { + $query->where('dept_id', $deptId); + } + } catch (\Throwable $e) { + // ignore + } + + return $query->find(); + } + /** * 从 DiceLotteryPoolConfig name=default 取 t1_weight~t5_weight 作为玩家未设置时的默认值 */ protected static function setDefaultWeightsFromLotteryConfig(DicePlayer $model): void { - $config = DiceLotteryPoolConfig::where('name', 'default')->find(); + $config = self::findDefaultLotteryConfigForPlayer($model); if (!$config) { return; }