refactor(admin): AdminFlash增加apply并在批量动作成功提示复用
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
39
tests/Unit/AdminFlashApplyTest.php
Normal file
39
tests/Unit/AdminFlashApplyTest.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Support\AdminFlash;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AdminFlashApplyTest extends TestCase
|
||||
{
|
||||
public function test_apply_should_call_with_for_non_empty_values(): void
|
||||
{
|
||||
$redirect = new class {
|
||||
public array $calls = [];
|
||||
|
||||
public function with($k, $v)
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user