From 3f471aa8db901b62ae7896868865f945ef6dd78d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=9D=E5=8D=9C?= Date: Tue, 17 Mar 2026 18:56:14 +0800 Subject: [PATCH] =?UTF-8?q?refactor(admin):=20success=20flash=20payload?= =?UTF-8?q?=E6=8F=90=E7=82=BCAdminFlash::success=E5=B9=B6=E5=A4=8D?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Admin/PlatformOrderController.php | 54 ++++++++++++++----- app/Support/AdminFlash.php | 27 ++++++++++ tests/Unit/AdminFlashSuccessPayloadTest.php | 26 +++++++++ 3 files changed, 95 insertions(+), 12 deletions(-) create mode 100644 app/Support/AdminFlash.php create mode 100644 tests/Unit/AdminFlashSuccessPayloadTest.php diff --git a/app/Http/Controllers/Admin/PlatformOrderController.php b/app/Http/Controllers/Admin/PlatformOrderController.php index 71fba78..311f46b 100644 --- a/app/Http/Controllers/Admin/PlatformOrderController.php +++ b/app/Http/Controllers/Admin/PlatformOrderController.php @@ -1656,10 +1656,20 @@ class PlatformOrderController extends Controller (string) $runId, ); - return redirect()->back() - ->with('success', '批量同步订阅任务已提交到队列:命中 ' . $matchedTotal . ' 条,本次处理 ' . $processed . ' 条(limit=' . $limit . ',run_id=' . $runId . ')。') - ->with('success_link_href', '/admin/platform-batches/show?type=bas&run_id=' . urlencode((string) $runId)) - ->with('success_link_label', '进入批次复盘'); + $flash = \App\Support\AdminFlash::success( + '批量同步订阅任务已提交到队列:命中 ' . $matchedTotal . ' 条,本次处理 ' . $processed . ' 条(limit=' . $limit . ',run_id=' . $runId . ')。', + '/admin/platform-batches/show?type=bas&run_id=' . urlencode((string) $runId), + '进入批次复盘', + ); + + $res = redirect()->back()->with('success', (string) ($flash['success'] ?? '')); + foreach (['success_link_href', 'success_link_label'] as $k) { + if (isset($flash[$k]) && (string) $flash[$k] !== '') { + $res = $res->with($k, (string) $flash[$k]); + } + } + + return $res; } public function batchMarkPaidAndActivate(Request $request, SubscriptionActivationService $service): RedirectResponse @@ -1809,10 +1819,20 @@ class PlatformOrderController extends Controller (string) $runId, ); - return redirect()->back() - ->with('success', '批量 BMPA 任务已提交到队列:命中 ' . $matchedTotal . ' 条,本次处理 ' . $processed . ' 条(limit=' . $limit . ',run_id=' . $runId . ')。') - ->with('success_link_href', '/admin/platform-batches/show?type=bmpa&run_id=' . urlencode((string) $runId)) - ->with('success_link_label', '进入批次复盘'); + $flash = \App\Support\AdminFlash::success( + '批量 BMPA 任务已提交到队列:命中 ' . $matchedTotal . ' 条,本次处理 ' . $processed . ' 条(limit=' . $limit . ',run_id=' . $runId . ')。', + '/admin/platform-batches/show?type=bmpa&run_id=' . urlencode((string) $runId), + '进入批次复盘', + ); + + $res = redirect()->back()->with('success', (string) ($flash['success'] ?? '')); + foreach (['success_link_href', 'success_link_label'] as $k) { + if (isset($flash[$k]) && (string) $flash[$k] !== '') { + $res = $res->with($k, (string) $flash[$k]); + } + } + + return $res; } public function batchMarkActivated(Request $request): RedirectResponse @@ -2003,11 +2023,21 @@ class PlatformOrderController extends Controller $msg .= ';失败原因Top:' . $topText; } - return redirect()->back() - ->with('success', $msg) + $flash = \App\Support\AdminFlash::success( + $msg, // 批量“仅标记为已生效”无 run_id(同步执行),这里提供一个“查看本次批量结果”的快捷入口:最近24小时批量生效集合。 - ->with('success_link_href', '/admin/platform-orders?batch_mark_activated_24h=1#filters') - ->with('success_link_label', '查看本次批量结果'); + '/admin/platform-orders?batch_mark_activated_24h=1#filters', + '查看本次批量结果', + ); + + $res = redirect()->back()->with('success', (string) ($flash['success'] ?? '')); + foreach (['success_link_href', 'success_link_label'] as $k) { + if (isset($flash[$k]) && (string) $flash[$k] !== '') { + $res = $res->with($k, (string) $flash[$k]); + } + } + + return $res; } public function clearSyncError(Request $request, PlatformOrder $order): RedirectResponse diff --git a/app/Support/AdminFlash.php b/app/Support/AdminFlash.php new file mode 100644 index 0000000..7bf758d --- /dev/null +++ b/app/Support/AdminFlash.php @@ -0,0 +1,27 @@ + $message, + ]; + + if ($linkHref !== '') { + $payload['success_link_href'] = $linkHref; + $payload['success_link_label'] = $linkLabel !== '' ? $linkLabel : '查看'; + } + + return $payload; + } +} diff --git a/tests/Unit/AdminFlashSuccessPayloadTest.php b/tests/Unit/AdminFlashSuccessPayloadTest.php new file mode 100644 index 0000000..389fc9a --- /dev/null +++ b/tests/Unit/AdminFlashSuccessPayloadTest.php @@ -0,0 +1,26 @@ +assertSame('ok', (string) ($p['success'] ?? '')); + $this->assertArrayNotHasKey('success_link_href', $p); + } + + public function test_success_should_include_link_when_href_present(): void + { + $p = AdminFlash::success('ok', '/admin/x', '查看'); + + $this->assertSame('ok', (string) ($p['success'] ?? '')); + $this->assertSame('/admin/x', (string) ($p['success_link_href'] ?? '')); + $this->assertSame('查看', (string) ($p['success_link_label'] ?? '')); + } +}