处理jc流量显示的问题
This commit is contained in:
@@ -5,22 +5,55 @@ namespace App\Http\Controllers\V1\User;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\TrafficLogResource;
|
||||
use App\Models\StatUser;
|
||||
use App\Services\StatisticalService;
|
||||
use App\Services\UserOnlineService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class StatController extends Controller
|
||||
{
|
||||
public function getTrafficLog(Request $request)
|
||||
{
|
||||
$startDate = now()->startOfMonth()->timestamp;
|
||||
$userId = (int) $request->user()->id;
|
||||
|
||||
$records = StatUser::query()
|
||||
->where('user_id', $request->user()->id)
|
||||
->with(['server:id,name'])
|
||||
->where('user_id', $userId)
|
||||
->where('record_at', '>=', $startDate)
|
||||
->orderBy('record_at', 'DESC')
|
||||
->get();
|
||||
|
||||
$deviceMap = $this->buildNodeDeviceMap($userId);
|
||||
$records->each(function (StatUser $record) use ($deviceMap): void {
|
||||
$serverType = strtolower((string) $record->server_type);
|
||||
$serverId = (int) $record->server_id;
|
||||
$nodeKey = $serverType !== '' && $serverId > 0 ? "{$serverType}{$serverId}" : null;
|
||||
$deviceIps = $nodeKey ? ($deviceMap[$nodeKey] ?? []) : [];
|
||||
|
||||
$record->setAttribute('server_name', $record->server?->name);
|
||||
$record->setAttribute('node_name', $record->server?->name);
|
||||
$record->setAttribute('device_ips', $deviceIps);
|
||||
$record->setAttribute('device_count', count($deviceIps));
|
||||
$record->setAttribute('device_name', $deviceIps[0] ?? 'Unknown');
|
||||
});
|
||||
|
||||
$data = TrafficLogResource::collection(collect($records));
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
private function buildNodeDeviceMap(int $userId): array
|
||||
{
|
||||
$devices = UserOnlineService::getUserDevices($userId);
|
||||
$deviceList = data_get($devices, 'devices', []);
|
||||
|
||||
return collect($deviceList)
|
||||
->filter(fn($item): bool => is_array($item) && !empty($item['ip']) && !empty($item['node_type']))
|
||||
->groupBy(fn(array $item): string => strtolower((string) $item['node_type']))
|
||||
->map(fn($items): array => collect($items)
|
||||
->pluck('ip')
|
||||
->filter()
|
||||
->unique()
|
||||
->values()
|
||||
->all())
|
||||
->all();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers\V2\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\TrafficLogResource;
|
||||
use App\Models\CommissionLog;
|
||||
use App\Models\Order;
|
||||
use App\Models\Server;
|
||||
@@ -12,6 +13,7 @@ use App\Models\StatUser;
|
||||
use App\Models\Ticket;
|
||||
use App\Models\User;
|
||||
use App\Services\StatisticalService;
|
||||
use App\Services\UserOnlineService;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class StatController extends Controller
|
||||
@@ -234,17 +236,54 @@ class StatController extends Controller
|
||||
]);
|
||||
|
||||
$pageSize = $request->input('pageSize', 10);
|
||||
$records = StatUser::orderBy('record_at', 'DESC')
|
||||
->where('user_id', $request->input('user_id'))
|
||||
$userId = (int) $request->input('user_id');
|
||||
$records = StatUser::query()
|
||||
->with(['server:id,name'])
|
||||
->orderBy('record_at', 'DESC')
|
||||
->where('user_id', $userId)
|
||||
->paginate($pageSize);
|
||||
|
||||
$data = $records->items();
|
||||
$deviceMap = $this->buildNodeDeviceMap($userId);
|
||||
$data = collect($records->items())
|
||||
->map(function (StatUser $record) use ($deviceMap, $request): array {
|
||||
$serverType = strtolower((string) $record->server_type);
|
||||
$serverId = (int) $record->server_id;
|
||||
$nodeKey = $serverType !== '' && $serverId > 0 ? "{$serverType}{$serverId}" : null;
|
||||
$deviceIps = $nodeKey ? ($deviceMap[$nodeKey] ?? []) : [];
|
||||
|
||||
$record->setAttribute('server_name', $record->server?->name);
|
||||
$record->setAttribute('node_name', $record->server?->name);
|
||||
$record->setAttribute('device_ips', $deviceIps);
|
||||
$record->setAttribute('device_count', count($deviceIps));
|
||||
$record->setAttribute('device_name', $deviceIps[0] ?? 'Unknown');
|
||||
|
||||
return (new TrafficLogResource($record))->toArray($request);
|
||||
})
|
||||
->all();
|
||||
|
||||
return [
|
||||
'data' => $data,
|
||||
'total' => $records->total(),
|
||||
];
|
||||
}
|
||||
|
||||
private function buildNodeDeviceMap(int $userId): array
|
||||
{
|
||||
$devices = UserOnlineService::getUserDevices($userId);
|
||||
$deviceList = data_get($devices, 'devices', []);
|
||||
|
||||
return collect($deviceList)
|
||||
->filter(fn($item): bool => is_array($item) && !empty($item['ip']) && !empty($item['node_type']))
|
||||
->groupBy(fn(array $item): string => strtolower((string) $item['node_type']))
|
||||
->map(fn($items): array => collect($items)
|
||||
->pluck('ip')
|
||||
->filter()
|
||||
->unique()
|
||||
->values()
|
||||
->all())
|
||||
->all();
|
||||
}
|
||||
|
||||
public function getStatRecord(Request $request)
|
||||
{
|
||||
return [
|
||||
|
||||
@@ -14,13 +14,49 @@ class TrafficLogResource extends JsonResource
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
$serverId = (int) data_get($this->resource, 'server_id', 0);
|
||||
$serverType = strtolower((string) data_get($this->resource, 'server_type', ''));
|
||||
$serverName = data_get($this->resource, 'server_name')
|
||||
?: data_get($this->resource, 'node_name')
|
||||
?: data_get($this->resource, 'server.name');
|
||||
|
||||
if (!$serverName && $serverId > 0) {
|
||||
$serverName = "Node #{$serverId}";
|
||||
}
|
||||
|
||||
$deviceIps = data_get($this->resource, 'device_ips', []);
|
||||
if (!is_array($deviceIps)) {
|
||||
$deviceIps = [];
|
||||
}
|
||||
|
||||
$deviceName = data_get($this->resource, 'device_name');
|
||||
if (!$deviceName) {
|
||||
$deviceName = $deviceIps[0] ?? 'Unknown';
|
||||
}
|
||||
|
||||
$data = [
|
||||
"d" => $this['d'],
|
||||
"u" => $this['u'],
|
||||
"record_at" => $this['record_at'],
|
||||
"server_rate" => $this['server_rate'],
|
||||
'id' => data_get($this->resource, 'id'),
|
||||
'd' => (int) data_get($this->resource, 'd', 0),
|
||||
'u' => (int) data_get($this->resource, 'u', 0),
|
||||
'record_at' => (int) data_get($this->resource, 'record_at', 0),
|
||||
'record_type' => data_get($this->resource, 'record_type'),
|
||||
'server_rate' => (float) data_get($this->resource, 'server_rate', 1),
|
||||
'server_id' => $serverId > 0 ? $serverId : null,
|
||||
'server_type' => $serverType !== '' ? $serverType : null,
|
||||
'server_name' => $serverName,
|
||||
'node_name' => $serverName,
|
||||
'node_key' => $serverId > 0 && $serverType !== '' ? "{$serverType}{$serverId}" : null,
|
||||
'device_name' => $deviceName,
|
||||
'device_ips' => $deviceIps,
|
||||
'device_count' => (int) data_get($this->resource, 'device_count', count($deviceIps)),
|
||||
'created_at' => data_get($this->resource, 'created_at'),
|
||||
'updated_at' => data_get($this->resource, 'updated_at'),
|
||||
];
|
||||
if(!config('hidden_features.enable_exposed_user_count_fix')) $data['user_id']= $this['user_id'];
|
||||
|
||||
if (!config('hidden_features.enable_exposed_user_count_fix')) {
|
||||
$data['user_id'] = (int) data_get($this->resource, 'user_id', 0);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user