修复错误
This commit is contained in:
@@ -7,6 +7,7 @@ use App\Models\User;
|
||||
use App\Services\DeviceStateService;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
|
||||
@@ -46,11 +47,46 @@ class UserOnlineDevicesController extends PluginController
|
||||
]);
|
||||
}
|
||||
|
||||
public function userStatus(): View
|
||||
public function adminUsersPage(): View
|
||||
{
|
||||
abort_unless($this->isPluginEnabled(), 404);
|
||||
|
||||
return view('UserOnlineDevices::userstatus');
|
||||
$securePath = admin_setting('secure_path', admin_setting('frontend_admin_path', hash('crc32b', config('app.key'))));
|
||||
|
||||
return view('UserOnlineDevices::admin-users', [
|
||||
'apiEndpoint' => url('/api/v2/' . $securePath . '/user-online-devices/users'),
|
||||
'adminHomeUrl' => url('/' . $securePath),
|
||||
]);
|
||||
}
|
||||
|
||||
public function adminUsers(Request $request)
|
||||
{
|
||||
if ($error = $this->beforePluginAction()) {
|
||||
return $this->fail($error);
|
||||
}
|
||||
|
||||
$current = max(1, (int) $request->input('current', 1));
|
||||
$pageSize = min(100, max(10, (int) $request->input('pageSize', 20)));
|
||||
$keyword = trim((string) $request->input('keyword', ''));
|
||||
|
||||
$query = User::query()
|
||||
->select(['id', 'email', 'last_online_at', 'created_at']);
|
||||
|
||||
if ($keyword !== '') {
|
||||
$query->where(function ($builder) use ($keyword) {
|
||||
$builder->where('email', 'like', '%' . $keyword . '%');
|
||||
if (is_numeric($keyword)) {
|
||||
$builder->orWhere('id', (int) $keyword);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$users = $query
|
||||
->orderByDesc('last_online_at')
|
||||
->orderByDesc('id')
|
||||
->paginate($pageSize, ['*'], 'page', $current);
|
||||
|
||||
return $this->success($this->buildAdminUsersPayload($users));
|
||||
}
|
||||
|
||||
public function panel(Request $request, int $user): View
|
||||
@@ -111,6 +147,43 @@ class UserOnlineDevicesController extends PluginController
|
||||
];
|
||||
}
|
||||
|
||||
private function buildAdminUsersPayload(LengthAwarePaginator $users): array
|
||||
{
|
||||
$items = $users->getCollection();
|
||||
$devicesByUser = $this->deviceStateService->getUsersDevices($items->pluck('id')->all());
|
||||
|
||||
$users->setCollection($items->map(function (User $user) use ($devicesByUser) {
|
||||
$ips = collect($devicesByUser[$user->id] ?? [])
|
||||
->filter(fn ($ip) => is_string($ip) && $ip !== '')
|
||||
->unique()
|
||||
->values();
|
||||
|
||||
return [
|
||||
'id' => $user->id,
|
||||
'email' => $user->email,
|
||||
'online_count' => $ips->count(),
|
||||
'online_devices' => $this->formatOnlineDevices($ips),
|
||||
'last_online_at' => $user->last_online_at?->timestamp,
|
||||
'created_at' => $user->created_at?->timestamp,
|
||||
];
|
||||
}));
|
||||
|
||||
return [
|
||||
'list' => $users->items(),
|
||||
'pagination' => [
|
||||
'current' => $users->currentPage(),
|
||||
'pageSize' => $users->perPage(),
|
||||
'total' => $users->total(),
|
||||
'lastPage' => $users->lastPage(),
|
||||
],
|
||||
'meta' => [
|
||||
'generated_at' => time(),
|
||||
'refresh_interval_seconds' => max(5, (int) $this->getConfig('refresh_interval_seconds', 15)),
|
||||
'mask_ip' => (bool) $this->getConfig('mask_ip', false),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function formatOnlineDevices(Collection $ips): array
|
||||
{
|
||||
return $ips->values()->map(function (string $ip, int $index) {
|
||||
|
||||
Reference in New Issue
Block a user