platform_orders: add clear single sync_error action from show page

This commit is contained in:
萝卜
2026-03-14 13:58:36 +00:00
parent fa9090f4d8
commit f91be76e08
5 changed files with 189 additions and 1 deletions

View File

@@ -1704,6 +1704,34 @@ class PlatformOrderController extends Controller
return redirect()->back()->with('success', $msg); return redirect()->back()->with('success', $msg);
} }
public function clearSyncError(Request $request, PlatformOrder $order): RedirectResponse
{
$admin = $this->ensurePlatformAdmin($request);
$meta = (array) ($order->meta ?? []);
if (! data_get($meta, 'subscription_activation_error')) {
return redirect()->back()->with('warning', '当前订单暂无同步失败标记,无需清理。');
}
data_forget($meta, 'subscription_activation_error');
// 轻量审计:记录清理动作(不做独立表,先落 meta便于排查
$audit = (array) (data_get($meta, 'audit', []) ?? []);
$audit[] = [
'action' => 'clear_sync_error',
'scope' => 'single',
'at' => now()->toDateTimeString(),
'admin_id' => $admin->id,
'note' => '手动点击订单详情【清除同步失败标记】',
];
data_set($meta, 'audit', $audit);
$order->meta = $meta;
$order->save();
return redirect()->back()->with('success', '已清除该订单的同步失败标记。');
}
public function clearSyncErrors(Request $request): RedirectResponse public function clearSyncErrors(Request $request): RedirectResponse
{ {
$this->ensurePlatformAdmin($request); $this->ensurePlatformAdmin($request);

View File

@@ -565,7 +565,15 @@
</div> </div>
<div class="card mb-20"> <div class="card mb-20">
<div class="flex-between">
<h3>最近一次同步失败</h3> <h3>最近一次同步失败</h3>
@if($activationError)
<form method="post" action="/admin/platform-orders/{{ $order->id }}/clear-sync-error" onsubmit="return confirm('确认清除该订单的同步失败标记?该操作仅清理 meta 标记,不会改变订阅/订单状态。');">
@csrf
<button class="btn btn-danger btn-sm" type="submit">清除失败标记</button>
</form>
@endif
</div>
@if($activationError) @if($activationError)
<table> <table>
<tbody> <tbody>
@@ -574,6 +582,7 @@
<tr><th>操作管理员</th><td>{{ data_get($activationError, 'admin_id') ?: '-' }}</td></tr> <tr><th>操作管理员</th><td>{{ data_get($activationError, 'admin_id') ?: '-' }}</td></tr>
</tbody> </tbody>
</table> </table>
<div class="muted muted-xs mt-6">提示:当你已确认问题已解决,但历史失败标记仍残留时,可先清理标记,再点击上方「重试同步订阅」。</div>
@else @else
<p class="muted">暂无失败记录。</p> <p class="muted">暂无失败记录。</p>
@endif @endif

View File

@@ -128,6 +128,7 @@ Route::prefix('admin')->group(function () {
Route::post('/platform-orders/{order}/mark-partially-refunded', [PlatformOrderController::class, 'markPartiallyRefunded']); Route::post('/platform-orders/{order}/mark-partially-refunded', [PlatformOrderController::class, 'markPartiallyRefunded']);
Route::post('/platform-orders/{order}/mark-paid-status', [PlatformOrderController::class, 'markPaidStatus']); Route::post('/platform-orders/{order}/mark-paid-status', [PlatformOrderController::class, 'markPaidStatus']);
Route::post('/platform-orders/{order}/mark-activated', [PlatformOrderController::class, 'markActivated']); Route::post('/platform-orders/{order}/mark-activated', [PlatformOrderController::class, 'markActivated']);
Route::post('/platform-orders/{order}/clear-sync-error', [PlatformOrderController::class, 'clearSyncError']);
Route::get('/site-subscriptions', [SiteSubscriptionController::class, 'index']); Route::get('/site-subscriptions', [SiteSubscriptionController::class, 'index']);
Route::get('/site-subscriptions/export', [SiteSubscriptionController::class, 'export']); Route::get('/site-subscriptions/export', [SiteSubscriptionController::class, 'export']);

View File

@@ -0,0 +1,79 @@
<?php
namespace Tests\Feature;
use App\Models\Merchant;
use App\Models\Plan;
use App\Models\PlatformOrder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AdminPlatformOrderClearSyncErrorSingleTest extends TestCase
{
use RefreshDatabase;
protected function loginAsPlatformAdmin(): void
{
$this->seed();
$this->post('/admin/login', [
'email' => 'platform.admin@demo.local',
'password' => 'Platform@123456',
])->assertRedirect('/admin');
}
public function test_can_clear_single_order_sync_error_and_append_audit(): void
{
$this->loginAsPlatformAdmin();
$merchant = Merchant::query()->firstOrFail();
$plan = Plan::query()->create([
'code' => 'clear_sync_error_single_test',
'name' => '清理单订单同步失败标记测试',
'billing_cycle' => 'monthly',
'price' => 9,
'list_price' => 9,
'status' => 'active',
'sort' => 10,
'published_at' => now(),
]);
$order = PlatformOrder::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'order_no' => 'PO_CLEAR_SYNC_ERR_0001',
'order_type' => 'new_purchase',
'status' => 'activated',
'payment_status' => 'paid',
'plan_name' => $plan->name,
'billing_cycle' => $plan->billing_cycle,
'period_months' => 1,
'quantity' => 1,
'payable_amount' => 9,
'paid_amount' => 9,
'placed_at' => now(),
'paid_at' => now(),
'activated_at' => now(),
'meta' => [
'subscription_activation_error' => [
'message' => '历史同步失败',
'at' => now()->subMinutes(2)->toDateTimeString(),
'admin_id' => 1,
],
],
]);
$this->post('/admin/platform-orders/' . $order->id . '/clear-sync-error')
->assertRedirect();
$order->refresh();
$this->assertEmpty(data_get($order->meta, 'subscription_activation_error'));
$audit = (array) (data_get($order->meta, 'audit', []) ?? []);
$this->assertNotEmpty($audit);
$last = end($audit);
$this->assertSame('clear_sync_error', data_get($last, 'action'));
$this->assertSame('single', data_get($last, 'scope'));
$this->assertNotEmpty(data_get($last, 'admin_id'));
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace Tests\Feature;
use App\Models\Merchant;
use App\Models\Plan;
use App\Models\PlatformOrder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AdminPlatformOrderShowHasClearSyncErrorButtonTest extends TestCase
{
use RefreshDatabase;
protected function loginAsPlatformAdmin(): void
{
$this->seed();
$this->post('/admin/login', [
'email' => 'platform.admin@demo.local',
'password' => 'Platform@123456',
])->assertRedirect('/admin');
}
public function test_show_page_renders_clear_sync_error_button_when_error_exists(): void
{
$this->loginAsPlatformAdmin();
$merchant = Merchant::query()->firstOrFail();
$plan = Plan::query()->create([
'code' => 'show_clear_sync_error_btn_test',
'name' => '详情页清理同步失败标记按钮测试',
'billing_cycle' => 'monthly',
'price' => 9,
'list_price' => 9,
'status' => 'active',
'sort' => 10,
'published_at' => now(),
]);
$order = PlatformOrder::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'order_no' => 'PO_SHOW_CLEAR_SYNC_ERR_0001',
'order_type' => 'new_purchase',
'status' => 'activated',
'payment_status' => 'paid',
'plan_name' => $plan->name,
'billing_cycle' => $plan->billing_cycle,
'period_months' => 1,
'quantity' => 1,
'payable_amount' => 9,
'paid_amount' => 9,
'placed_at' => now(),
'paid_at' => now(),
'activated_at' => now(),
'meta' => [
'subscription_activation_error' => [
'message' => '历史同步失败',
'at' => now()->subMinutes(2)->toDateTimeString(),
'admin_id' => 1,
],
],
]);
$this->get('/admin/platform-orders/' . $order->id)
->assertOk()
->assertSee('/admin/platform-orders/' . $order->id . '/clear-sync-error', false)
->assertSee('清除失败标记');
}
}