1.优化查询用户信息接口/api/v1/getPlayerInfo,如果没有用户则创建

This commit is contained in:
2026-05-19 17:18:51 +08:00
parent 37c0035bfc
commit 9a43e1d8f2
3 changed files with 80 additions and 3 deletions

View File

@@ -68,6 +68,54 @@ class UserLogic
return array_map('intval', $adminIds ?: [(int) $admin->id]);
}
/**
* 按用户名查找玩家;不存在则创建并绑定渠道/管理员(供 getPlayerInfo 等接口)
*
* @param int|null $adminId 关联后台管理员 IDsa_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;
}
/**
* 登录JSONusername, password, lang, coin, time
* 存在则校验密码并更新 coin累加不存在则创建用户并写入 coin。