[游戏管理]用户管理-优化
This commit is contained in:
@@ -4,6 +4,7 @@ namespace app\admin\controller\game;
|
||||
|
||||
use Throwable;
|
||||
use app\common\controller\Backend;
|
||||
use support\think\Db;
|
||||
use support\Response;
|
||||
use Webman\Http\Request as WebmanRequest;
|
||||
|
||||
@@ -23,7 +24,7 @@ class User extends Backend
|
||||
|
||||
protected array $withJoinTable = ['channel', 'admin'];
|
||||
|
||||
protected string|array $quickSearchField = ['id', 'username', 'phone'];
|
||||
protected string|array $quickSearchField = ['id', 'username', 'phone', 'email', 'register_invite_code'];
|
||||
|
||||
protected function initController(WebmanRequest $request): ?Response
|
||||
{
|
||||
@@ -205,6 +206,135 @@ class User extends Backend
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 角色组 → 管理员树(仅当前账号可管理的角色组及其下管理员;用于游戏用户归属)
|
||||
* 同一管理员若属于多个组,只挂在 id 最小的所属组下,避免树中重复 value
|
||||
*/
|
||||
public function adminScopeTree(WebmanRequest $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$groupIds = $this->getManageableAdminGroupIds();
|
||||
if ($groupIds === []) {
|
||||
return $this->success('', ['list' => []]);
|
||||
}
|
||||
|
||||
$groups = Db::name('admin_group')
|
||||
->where('id', 'in', $groupIds)
|
||||
->where('status', 1)
|
||||
->field(['id', 'pid', 'name'])
|
||||
->order('id', 'asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$accessRows = Db::name('admin_group_access')->alias('aga')
|
||||
->join('admin a', 'aga.uid = a.id')
|
||||
->where('aga.group_id', 'in', $groupIds)
|
||||
->field(['a.id', 'a.username', 'a.channel_id', 'a.invite_code', 'aga.group_id'])
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$adminPrimary = [];
|
||||
foreach ($accessRows as $row) {
|
||||
$uid = intval((string)$row['id']);
|
||||
$gid = intval((string)$row['group_id']);
|
||||
if (!isset($adminPrimary[$uid]) || $gid < $adminPrimary[$uid]['gid']) {
|
||||
$adminPrimary[$uid] = [
|
||||
'gid' => $gid,
|
||||
'user' => $row,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$adminsByGroup = [];
|
||||
foreach ($adminPrimary as $item) {
|
||||
$row = $item['user'];
|
||||
$gid = $item['gid'];
|
||||
$invite = $row['invite_code'] ?? '';
|
||||
$invite = is_string($invite) ? $invite : '';
|
||||
$channelId = $row['channel_id'] ?? null;
|
||||
$adminsByGroup[$gid][] = [
|
||||
'value' => (string)$row['id'],
|
||||
'label' => (string)$row['username'],
|
||||
'is_leaf' => true,
|
||||
'channel_id' => $channelId === null || $channelId === '' ? null : intval((string)$channelId),
|
||||
'invite_code' => $invite,
|
||||
];
|
||||
}
|
||||
|
||||
$groupMap = [];
|
||||
foreach ($groups as $g) {
|
||||
$groupMap[intval((string)$g['id'])] = $g;
|
||||
}
|
||||
|
||||
$childGroupIdsByPid = [];
|
||||
foreach ($groups as $g) {
|
||||
$id = intval((string)$g['id']);
|
||||
$pid = intval((string)($g['pid'] ?? 0));
|
||||
$childGroupIdsByPid[$pid][] = $id;
|
||||
}
|
||||
|
||||
$buildNode = null;
|
||||
$buildNode = function (int $groupId) use (&$buildNode, $groupMap, $childGroupIdsByPid, $adminsByGroup): array {
|
||||
if (!isset($groupMap[$groupId])) {
|
||||
return [];
|
||||
}
|
||||
$g = $groupMap[$groupId];
|
||||
$children = [];
|
||||
foreach ($childGroupIdsByPid[$groupId] ?? [] as $childId) {
|
||||
$children[] = $buildNode($childId);
|
||||
}
|
||||
foreach ($adminsByGroup[$groupId] ?? [] as $leaf) {
|
||||
$children[] = $leaf;
|
||||
}
|
||||
|
||||
return [
|
||||
'value' => 'group_' . $groupId,
|
||||
'label' => (string)$g['name'],
|
||||
'disabled' => true,
|
||||
'children' => $children,
|
||||
];
|
||||
};
|
||||
|
||||
$groupIdSet = array_fill_keys(array_keys($groupMap), true);
|
||||
$roots = [];
|
||||
foreach ($groups as $g) {
|
||||
$id = intval((string)$g['id']);
|
||||
$pid = intval((string)($g['pid'] ?? 0));
|
||||
if ($pid === 0 || !isset($groupIdSet[$pid])) {
|
||||
$roots[] = $id;
|
||||
}
|
||||
}
|
||||
$roots = array_values(array_unique($roots));
|
||||
sort($roots);
|
||||
|
||||
$tree = [];
|
||||
foreach ($roots as $rid) {
|
||||
$tree[] = $buildNode($rid);
|
||||
}
|
||||
|
||||
return $this->success('', [
|
||||
'list' => $tree,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int[]
|
||||
*/
|
||||
private function getManageableAdminGroupIds(): array
|
||||
{
|
||||
if ($this->auth->isSuperAdmin()) {
|
||||
return Db::name('admin_group')->where('status', 1)->column('id');
|
||||
}
|
||||
$own = Db::name('admin_group_access')->where('uid', $this->auth->id)->column('group_id');
|
||||
$own = array_map('intval', $own);
|
||||
$children = array_map('intval', $this->auth->getAdminChildGroups());
|
||||
return array_values(array_unique(array_merge($own, $children)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 若需重写查看、编辑、删除等方法,请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user