优化代理逻辑agent_id

This commit is contained in:
2026-03-19 15:52:06 +08:00
parent 333e85f7d9
commit 3dbd68829a
2 changed files with 84 additions and 3 deletions

View File

@@ -43,11 +43,13 @@ class GameController extends BaseController
} }
$adminId = null; $adminId = null;
$adminIdsInTopDept = null;
$agentId = trim((string) ($request->agent_id ?? '')); $agentId = trim((string) ($request->agent_id ?? ''));
if ($agentId !== '') { if ($agentId !== '') {
$systemUser = SystemUser::where('agent_id', $agentId)->find(); $systemUser = SystemUser::where('agent_id', $agentId)->find();
if ($systemUser) { if ($systemUser) {
$adminId = (int) $systemUser->id; $adminId = (int) $systemUser->id;
$adminIdsInTopDept = UserLogic::getAdminIdsByAgentIdTopDept($agentId);
} }
} }
@@ -56,7 +58,7 @@ class GameController extends BaseController
try { try {
$logic = new UserLogic(); $logic = new UserLogic();
$result = $logic->loginByUsername($username, $password, $lang === 'en' ? 'en' : 'chs', 0.0, $time, $adminId); $result = $logic->loginByUsername($username, $password, $lang === 'en' ? 'en' : 'chs', 0.0, $time, $adminId, $adminIdsInTopDept);
} catch (\plugin\saiadmin\exception\ApiException $e) { } catch (\plugin\saiadmin\exception\ApiException $e) {
return $this->fail($e->getMessage(), ReturnCode::PARAMS_ERROR); return $this->fail($e->getMessage(), ReturnCode::PARAMS_ERROR);
} }

View File

@@ -5,6 +5,8 @@ namespace app\api\logic;
use app\dice\model\player\DicePlayer; use app\dice\model\player\DicePlayer;
use app\api\cache\UserCache; use app\api\cache\UserCache;
use plugin\saiadmin\app\model\system\SystemDept;
use plugin\saiadmin\app\model\system\SystemUser;
use plugin\saiadmin\exception\ApiException; use plugin\saiadmin\exception\ApiException;
use Tinywan\Jwt\JwtToken; use Tinywan\Jwt\JwtToken;
@@ -41,21 +43,98 @@ class UserLogic
return md5(self::PASSWORD_SALT . $password); return md5(self::PASSWORD_SALT . $password);
} }
/**
* 根据 parent_id 向上遍历找到顶级部门parent_id=0
*/
private static function getTopDeptIdByParentId(int $deptId): ?int
{
$currentId = $deptId;
$visited = [];
while ($currentId > 0 && !isset($visited[$currentId])) {
$visited[$currentId] = true;
$dept = SystemDept::find($currentId);
if (!$dept) {
return null;
}
$parentId = (int) ($dept->parent_id ?? 0);
if ($parentId === 0) {
return $currentId;
}
$currentId = $parentId;
}
return $currentId > 0 ? $currentId : null;
}
/**
* 根据顶级部门 id递归获取其下所有部门 id含自身仅用 id 和 parent_id
*/
private static function getAllDeptIdsUnderTop(int $topId): array
{
$deptIds = [$topId];
$prevCount = 0;
while (count($deptIds) > $prevCount) {
$prevCount = count($deptIds);
$children = SystemDept::whereIn('parent_id', $deptIds)->column('id');
$deptIds = array_unique(array_merge($deptIds, array_map('intval', $children)));
}
return array_values($deptIds);
}
/**
* 根据 agent_id 获取当前管理员所在顶级部门下的所有管理员 ID 列表
* 使用 SystemDept 的 id 和 parent_id 字段遍历:先向上找顶级部门(parent_id=0),再向下收集所有子部门
* 用于 getGameUrl 接口判断 DicePlayer 是否属于该部门,同顶级部门下不重复创建玩家
*
* @param string $agentId 代理标识sa_system_user.agent_id
* @return int[] 管理员 ID 列表,空数组表示未找到或无法解析
*/
public static function getAdminIdsByAgentIdTopDept(string $agentId): array
{
$agentId = trim($agentId);
if ($agentId === '') {
return [];
}
$admin = SystemUser::where('agent_id', $agentId)->find();
if (!$admin) {
return [];
}
$deptId = $admin->dept_id ?? null;
if ($deptId === null || $deptId === '') {
return [(int) $admin->id];
}
$deptId = (int) $deptId;
$topId = self::getTopDeptIdByParentId($deptId);
if ($topId === null) {
return [(int) $admin->id];
}
$deptIds = self::getAllDeptIdsUnderTop($topId);
if (empty($deptIds)) {
$deptIds = [$deptId];
}
$adminIds = SystemUser::whereIn('dept_id', $deptIds)->column('id');
return array_map('intval', $adminIds);
}
/** /**
* 登录JSONusername, password, lang, coin, time * 登录JSONusername, password, lang, coin, time
* 存在则校验密码并更新 coin累加不存在则创建用户并写入 coin。 * 存在则校验密码并更新 coin累加不存在则创建用户并写入 coin。
* 将会话写入 Redis返回 token 与前端连接地址。 * 将会话写入 Redis返回 token 与前端连接地址。
* *
* @param int|null $adminId 创建新用户时关联的后台管理员IDsa_system_user.id可选 * @param int|null $adminId 创建新用户时关联的后台管理员IDsa_system_user.id可选
* @param int[]|null $adminIdsInTopDept 当前管理员顶级部门下的所有管理员ID用于按部门范围查找玩家为空时退化为仅按 username 查找
*/ */
public function loginByUsername(string $username, string $password, string $lang, float $coin, string $time, ?int $adminId = null): array public function loginByUsername(string $username, string $password, string $lang, float $coin, string $time, ?int $adminId = null, ?array $adminIdsInTopDept = null): array
{ {
$username = trim($username); $username = trim($username);
if ($username === '') { if ($username === '') {
throw new ApiException('username is required'); throw new ApiException('username is required');
} }
$player = DicePlayer::where('username', $username)->find(); $query = DicePlayer::where('username', $username);
if ($adminIdsInTopDept !== null && !empty($adminIdsInTopDept)) {
$query->whereIn('admin_id', $adminIdsInTopDept);
}
$player = $query->find();
if ($player) { if ($player) {
if ((int) ($player->status ?? 1) === 0) { if ((int) ($player->status ?? 1) === 0) {
throw new ApiException('Account is disabled and cannot log in'); throw new ApiException('Account is disabled and cannot log in');