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