From bc37f23ea2d4101b5e2c29f5f107c6f32e95d906 Mon Sep 17 00:00:00 2001 From: BrettonYe <867057410@qq.com> Date: Fri, 30 Jan 2026 09:41:48 +0800 Subject: [PATCH] Unify Form Value Persistence on Reload --- .gitignore | 4 ++ .../Controllers/Admin/ReportController.php | 66 ++++++------------- public/assets/js/config/admin.js | 25 +++++++ .../admin/report/nodeDataAnalysis.blade.php | 14 ++-- .../admin/report/siteDataAnalysis.blade.php | 2 +- .../admin/report/userDataAnalysis.blade.php | 15 ++--- resources/views/admin/table_layouts.blade.php | 27 ++------ .../components/admin/filter/input.blade.php | 2 +- 8 files changed, 67 insertions(+), 88 deletions(-) diff --git a/.gitignore b/.gitignore index 039152c1..c10eca46 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,7 @@ _ide_helper_models.php composer.lock .prettierrc package-lock.json +/.junie +/public/build +/.lingma +.tongyiignore diff --git a/app/Http/Controllers/Admin/ReportController.php b/app/Http/Controllers/Admin/ReportController.php index 5cdabd67..6b51505a 100644 --- a/app/Http/Controllers/Admin/ReportController.php +++ b/app/Http/Controllers/Admin/ReportController.php @@ -79,59 +79,35 @@ class ReportController extends Controller // 处理今天的数据 if ($hourlyDate->isToday() || $endDate->isToday()) { - $todayHoursFlow = $user->hourlyDataFlows() - ->whereNotNull('node_id') - ->whereDate('created_at', $currentTime) - ->with('node:id,name') - ->selectRaw('node_id, HOUR(created_at) as hour, u + d as total') - ->get(); + $todayHoursFlow = $user->hourlyDataFlows()->whereNotNull('node_id')->whereDate('created_at', $currentTime)->with('node:id,name')->selectRaw('node_id, HOUR(created_at) as hour, u + d as total')->get(); - $currentHourFlow = $user->dataFlowLogs() - ->where('log_time', '>=', $currentTime->startOfHour()->timestamp) - ->with('node:id,name') - ->groupBy('node_id') - ->selectRaw('node_id, ? as hour, sum(u + d) as total', [$currentTime->hour]) - ->get(); + $currentHourFlow = $user->dataFlowLogs()->where('log_time', '>=', $currentTime->startOfHour()->timestamp)->with('node:id,name')->groupBy('node_id')->selectRaw('node_id, ? as hour, sum(u + d) as total', [$currentTime->hour])->get(); - $todayData = $todayHoursFlow->concat($currentHourFlow) - ->groupBy('node_id') - ->map(function ($items) use ($mapFlow) { - $hourlyData = $items->mapWithKeys(fn ($item) => [$item->hour => $mapFlow($item)]); - $totalFlow = $items->sum('total'); + $todayData = $todayHoursFlow->concat($currentHourFlow)->groupBy('node_id')->map(function ($items) use ($mapFlow) { + $hourlyData = $items->mapWithKeys(fn ($item) => [$item->hour => $mapFlow($item)]); + $totalFlow = $items->sum('total'); - return [ - 'hourly' => $hourlyData, - 'daily' => $mapFlow((object) [ - 'node_id' => $items->first()->node_id, - 'node' => $items->first()->node, - 'created_at' => Carbon::today(), - 'total' => $totalFlow, - ], 'date'), - ]; - }); + return [ + 'hourly' => $hourlyData, + 'daily' => $mapFlow((object) [ + 'node_id' => $items->first()->node_id, + 'node' => $items->first()->node, + 'created_at' => Carbon::today(), + 'total' => $totalFlow, + ], 'date'), + ]; + }); } // 处理小时数据 if ($todayData && $hourlyDate->isToday()) { $hourlyFlows = $todayData->flatMap(fn ($item) => $item['hourly'])->values(); } else { - $hourlyFlows = $user->hourlyDataFlows() - ->whereNotNull('node_id') - ->whereDate('created_at', $hourlyDate) - ->with('node:id,name') - ->selectRaw('node_id, HOUR(created_at) as hour, u + d as total') - ->get() - ->map(fn ($item) => $mapFlow($item)); + $hourlyFlows = $user->hourlyDataFlows()->whereNotNull('node_id')->whereDate('created_at', $hourlyDate)->with('node:id,name')->selectRaw('node_id, HOUR(created_at) as hour, u + d as total')->get()->map(fn ($item) => $mapFlow($item)); } // 处理每日数据 - $dailyFlows = $user->dailyDataFlows() - ->whereNotNull('node_id') - ->whereBetween('created_at', [$startDate, $endDate]) - ->with('node:id,name') - ->selectRaw('node_id, DATE_FORMAT(created_at, "%m-%d") as date, u + d as total') - ->get() - ->map(fn ($item) => $mapFlow($item, 'date')); + $dailyFlows = $user->dailyDataFlows()->whereNotNull('node_id')->whereBetween('created_at', [$startDate, $endDate])->with('node:id,name')->selectRaw('node_id, DATE_FORMAT(created_at, "%m-%d") as date, u + d as total')->get()->map(fn ($item) => $mapFlow($item, 'date')); if ($todayData && $endDate->isToday()) { $dailyFlows = $dailyFlows->concat($todayData->map(fn ($item) => $item['daily'])); @@ -143,10 +119,7 @@ class ReportController extends Controller 'nodes' => $hourlyFlows->concat($dailyFlows)->pluck('name', 'id')->unique()->toArray(), 'hourlyFlows' => $hourlyFlows->toArray(), 'dailyFlows' => $dailyFlows->toArray(), - 'hour_dates' => UserHourlyDataFlow::selectRaw('DISTINCT DATE(created_at) as date') - ->orderByDesc('date') - ->pluck('date') - ->toArray(), + 'hour_dates' => UserHourlyDataFlow::selectRaw('DISTINCT DATE(created_at) as date')->orderByDesc('date')->pluck('date')->toArray(), ]; } @@ -258,7 +231,7 @@ class ReportController extends Controller $nodeId = $request->input('node_id'); $nodes = Node::orderBy('name')->pluck('name', 'id'); - // Fetch flows + // 获取流量数据 $flows = NodeDailyDataFlow::whereNodeId($nodeId)->selectRaw('DATE(created_at) as date, sum(u + d) as total')->groupBy('date')->get()->keyBy('date'); $dailyFlows = $flows->filter(fn ($flow) => $flow->date >= now()->subMonthNoOverflow()->startOfMonth()->toDateString())->pluck('total', 'date'); @@ -276,7 +249,6 @@ class ReportController extends Controller $trafficData = NodeDailyDataFlow::where('node_id', $nodeId)->where('created_at', '>=', $thirtyDaysAgo)->selectRaw('SUM(u + d) as total, COUNT(*) as dataCounts')->first(); $total30Days = $trafficData->total ?? 0; - $daysWithData = max($trafficData->dataCounts ?? 0, 1); $months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; diff --git a/public/assets/js/config/admin.js b/public/assets/js/config/admin.js index 9cef42a8..88bb313a 100644 --- a/public/assets/js/config/admin.js +++ b/public/assets/js/config/admin.js @@ -290,3 +290,28 @@ function collectFormData(formSelector, options = {}) { }; })(jQuery); +/** + * 从URL查询参数中提取数据并填充表单 + * @param {Object} options - 配置选项 + * @param {string} options.formSelector - 表单选择器,默认为 'form' + * @param {Array} options.skipFields - 跳过的字段名 + */ +function populateFormFromQueryParams(options = {}) { + // 将URL查询参数转换为对象 + const urlParams = new URLSearchParams(window.location.search); + const data = {}; + + for (const [key, value] of urlParams.entries()) { + if (key.endsWith('[]')) { + if (!data.hasOwnProperty(key)) { + data[key] = []; + } + data[key].push(value); + } else { + data[key] = value; + } + } + + // 使用现有的 autoPopulateForm 函数填充表单 + autoPopulateForm(data, options); +} diff --git a/resources/views/admin/report/nodeDataAnalysis.blade.php b/resources/views/admin/report/nodeDataAnalysis.blade.php index 117e4c16..314d55c4 100644 --- a/resources/views/admin/report/nodeDataAnalysis.blade.php +++ b/resources/views/admin/report/nodeDataAnalysis.blade.php @@ -341,13 +341,7 @@ window.location.href = `${window.location.pathname}?${urlParams.toString()}`; }; - const resetSearchForm = () => { - window.location.href = window.location.href.split("?")[0]; - }; - document.addEventListener("DOMContentLoaded", () => { - initCharts(); - const hourDateSelect = document.getElementById("hour_date"); if (hourDateSelect) { hourDateSelect.addEventListener("change", (event) => handleFormSubmit(event, event.target.form)); @@ -356,8 +350,14 @@ $(".input-daterange").datepicker({ startDate: nodeData.start_date, - endDate: new Date() + endDate: new Date(), + language: document.documentElement.lang || 'en', + autoclose: true, + todayHighlight: true }); + + populateFormFromQueryParams(); + initCharts(); }); @endsection diff --git a/resources/views/admin/report/siteDataAnalysis.blade.php b/resources/views/admin/report/siteDataAnalysis.blade.php index 493fd1fd..791471a1 100644 --- a/resources/views/admin/report/siteDataAnalysis.blade.php +++ b/resources/views/admin/report/siteDataAnalysis.blade.php @@ -201,7 +201,7 @@ }); $(document).ready(function() { - $('#node_id').val({{ Request::query('node_id') }}); + populateFormFromQueryParams(); }); @endsection diff --git a/resources/views/admin/report/userDataAnalysis.blade.php b/resources/views/admin/report/userDataAnalysis.blade.php index c0f765d5..e143fc3c 100644 --- a/resources/views/admin/report/userDataAnalysis.blade.php +++ b/resources/views/admin/report/userDataAnalysis.blade.php @@ -8,7 +8,7 @@