From 23476b0ac36cd306b848bc0923c801c303b3ebac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=9D=E5=8D=9C?= Date: Tue, 17 Mar 2026 19:11:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor(admin):=20AdminFlash=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?apply=E5=B9=B6=E5=9C=A8=E6=89=B9=E9=87=8F=E5=8A=A8=E4=BD=9C?= =?UTF-8?q?=E6=88=90=E5=8A=9F=E6=8F=90=E7=A4=BA=E5=A4=8D=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Admin/PlatformOrderController.php | 27 ++----------- app/Support/AdminFlash.php | 35 +++++++++++++++++ tests/Unit/AdminFlashApplyTest.php | 39 +++++++++++++++++++ 3 files changed, 77 insertions(+), 24 deletions(-) create mode 100644 tests/Unit/AdminFlashApplyTest.php diff --git a/app/Http/Controllers/Admin/PlatformOrderController.php b/app/Http/Controllers/Admin/PlatformOrderController.php index 311f46b..33f5779 100644 --- a/app/Http/Controllers/Admin/PlatformOrderController.php +++ b/app/Http/Controllers/Admin/PlatformOrderController.php @@ -1662,14 +1662,7 @@ class PlatformOrderController extends Controller '进入批次复盘', ); - $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; + return \App\Support\AdminFlash::apply(redirect()->back(), $flash); } public function batchMarkPaidAndActivate(Request $request, SubscriptionActivationService $service): RedirectResponse @@ -1825,14 +1818,7 @@ class PlatformOrderController extends Controller '进入批次复盘', ); - $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; + return \App\Support\AdminFlash::apply(redirect()->back(), $flash); } public function batchMarkActivated(Request $request): RedirectResponse @@ -2030,14 +2016,7 @@ class PlatformOrderController extends Controller '查看本次批量结果', ); - $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; + return \App\Support\AdminFlash::apply(redirect()->back(), $flash); } public function clearSyncError(Request $request, PlatformOrder $order): RedirectResponse diff --git a/app/Support/AdminFlash.php b/app/Support/AdminFlash.php index b514fbd..ca2020a 100644 --- a/app/Support/AdminFlash.php +++ b/app/Support/AdminFlash.php @@ -57,4 +57,39 @@ class AdminFlash return $payload; } + + /** + * 把 payload 批量 apply 到 redirect response(统一 session key 处理口径)。 + * + * @param object $redirect 需提供 ->with($key,$value) + */ + public static function apply(object $redirect, array $payload): object + { + $alwaysKeys = ['success', 'warning', 'error']; + + foreach ($payload as $k => $v) { + $k = (string) $k; + + if (in_array($k, $alwaysKeys, true)) { + $redirect = $redirect->with($k, $v); + continue; + } + + if ($v === null) { + continue; + } + + if (is_string($v)) { + if (trim($v) === '') { + continue; + } + $redirect = $redirect->with($k, $v); + continue; + } + + $redirect = $redirect->with($k, $v); + } + + return $redirect; + } } diff --git a/tests/Unit/AdminFlashApplyTest.php b/tests/Unit/AdminFlashApplyTest.php new file mode 100644 index 0000000..687d5ec --- /dev/null +++ b/tests/Unit/AdminFlashApplyTest.php @@ -0,0 +1,39 @@ +calls[] = [$k, $v]; + return $this; + } + }; + + $payload = [ + 'success' => 'ok', + 'success_link_href' => '/admin/x', + 'success_link_label' => '查看', + 'warning_copy_text' => '', // 空字符串应跳过 + ]; + + AdminFlash::apply($redirect, $payload); + + $this->assertNotEmpty($redirect->calls); + $keys = array_map(fn ($x) => (string) $x[0], $redirect->calls); + + $this->assertContains('success', $keys); + $this->assertContains('success_link_href', $keys); + $this->assertContains('success_link_label', $keys); + $this->assertNotContains('warning_copy_text', $keys); + } +}