1.优化查询用户信息接口/api/v1/getPlayerInfo,如果没有用户则创建
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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。
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user