fix: statisc

This commit is contained in:
hashbro
2026-08-10 03:53:10 +08:00
parent ad30453922
commit aad44bf52e
33 changed files with 1207 additions and 28 deletions
@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Concerns\PortalAware;
use App\Http\Controllers\Controller;
use App\Models\Device;
use App\Models\PageVisit;
use App\Models\User;
use App\Support\AgentScope;
use Carbon\Carbon;
@@ -49,6 +50,49 @@ class DashboardController extends Controller
$newCount = (clone $base)->whereBetween('created_at', [$from, $to])->count();
$activeCount = (clone $base)->whereBetween('updated_at', [$from, $to])->count();
$visits = PageVisit::query();
AgentScope::applyChannelIdScope($visits, $this->agent());
if (! $this->isAgentPortal() && $agentUserId > 0) {
AgentScope::applyChannelIdAgentUserFilter($visits, $agentUserId);
}
if ($channelId !== '') {
$visits->where('channel_id', 'like', '%'.$channelId.'%');
}
$visits->whereBetween('created_at', [$from, $to]);
$pv = (clone $visits)->count();
$uv = (int) (clone $visits)->selectRaw('COUNT(DISTINCT client_uid) as aggregate')->value('aggregate');
$byOs = (clone $visits)
->select('os')
->selectRaw('COUNT(*) as pv')
->selectRaw('COUNT(DISTINCT client_uid) as uv')
->groupBy('os')
->orderByDesc('pv')
->limit(10)
->get()
->map(static fn ($r) => [
'label' => ((string) ($r->os ?? '')) !== '' ? (string) $r->os : 'Unknown',
'pv' => (int) $r->pv,
'uv' => (int) $r->uv,
])
->values();
$byBrowser = (clone $visits)
->select('browser')
->selectRaw('COUNT(*) as pv')
->selectRaw('COUNT(DISTINCT client_uid) as uv')
->groupBy('browser')
->orderByDesc('pv')
->limit(10)
->get()
->map(static fn ($r) => [
'label' => ((string) ($r->browser ?? '')) !== '' ? (string) $r->browser : 'Unknown',
'pv' => (int) $r->pv,
'uv' => (int) $r->uv,
])
->values();
return response()->json([
'code' => 0,
'msg' => '',
@@ -56,6 +100,10 @@ class DashboardController extends Controller
'total' => $total,
'new_count' => $newCount,
'active_count' => $activeCount,
'pv' => $pv,
'uv' => $uv,
'by_os' => $byOs,
'by_browser' => $byBrowser,
'range' => $range,
'from' => $from->toDateTimeString(),
'to' => $to->toDateTimeString(),