feat(plans): 空库一键初始化默认套餐(带护栏与测试)

This commit is contained in:
萝卜
2026-03-10 21:56:14 +00:00
parent cfbd9a1693
commit b27fd67347
4 changed files with 118 additions and 7 deletions

View File

@@ -228,6 +228,49 @@ class PlanController extends Controller
});
}
public function seedDefaults(Request $request): RedirectResponse
{
$this->ensurePlatformAdmin($request);
// 安全护栏:仅当当前库没有任何套餐时才允许一键初始化
$existingCount = Plan::query()->count();
if ($existingCount > 0) {
return redirect()->back()->with('error', '当前已有套餐(' . $existingCount . ' 条),为避免污染运营数据,已阻止一键初始化。');
}
$now = now();
$defaults = [
[
'code' => 'starter_monthly',
'name' => '基础版(月付)',
'billing_cycle' => 'monthly',
'price' => 99,
'list_price' => 129,
'status' => 'active',
'sort' => 10,
'description' => '适合刚开站的试运营阶段,可用于 Demo 场景。',
'published_at' => $now,
],
[
'code' => 'pro_yearly',
'name' => '专业版(年付)',
'billing_cycle' => 'yearly',
'price' => 1999,
'list_price' => 2599,
'status' => 'active',
'sort' => 20,
'description' => '面向成长型站点,后续搭配授权项配置。',
'published_at' => $now,
],
];
foreach ($defaults as $row) {
Plan::query()->create($row);
}
return redirect('/admin/plans')->with('success', '已初始化默认套餐:' . count($defaults) . ' 条。');
}
protected function statusLabels(): array
{
return [

View File

@@ -11,13 +11,21 @@
<div class="card mb-20">
<h3>工具</h3>
<form method="get" action="/admin/plans/export">
<input type="hidden" name="status" value="{{ $filters['status'] ?? '' }}">
<input type="hidden" name="published" value="{{ $filters['published'] ?? '' }}">
<input type="hidden" name="billing_cycle" value="{{ $filters['billing_cycle'] ?? '' }}">
<input type="hidden" name="keyword" value="{{ $filters['keyword'] ?? '' }}">
<button type="submit">导出当前筛选结果CSV</button>
</form>
<div class="grid-2">
<form method="get" action="/admin/plans/export">
<input type="hidden" name="status" value="{{ $filters['status'] ?? '' }}">
<input type="hidden" name="published" value="{{ $filters['published'] ?? '' }}">
<input type="hidden" name="billing_cycle" value="{{ $filters['billing_cycle'] ?? '' }}">
<input type="hidden" name="keyword" value="{{ $filters['keyword'] ?? '' }}">
<button type="submit">导出当前筛选结果CSV</button>
</form>
<form method="post" action="/admin/plans/seed-defaults" onsubmit="return confirm('仅在当前没有任何套餐时才会初始化。确认执行吗?');">
@csrf
<button type="submit" class="btn">一键初始化默认套餐(空库)</button>
<div class="muted muted-xs" style="margin-top:6px;">安全护栏:当库中已存在套餐时会自动阻止,避免污染运营数据。</div>
</form>
</div>
</div>
<div class="card mb-20">

View File

@@ -121,6 +121,8 @@ Route::prefix('admin')->group(function () {
Route::get('/plans/export', [PlanController::class, 'export']);
Route::get('/plans/create', [PlanController::class, 'create']);
Route::post('/plans', [PlanController::class, 'store']);
// 注意:必须放在 /plans/{plan} 之前,避免被参数路由吞掉导致 404
Route::post('/plans/seed-defaults', [PlanController::class, 'seedDefaults']);
Route::get('/plans/{plan}/edit', [PlanController::class, 'edit']);
Route::post('/plans/{plan}', [PlanController::class, 'update']);
Route::post('/plans/{plan}/set-status', [PlanController::class, 'setStatus']);

View File

@@ -0,0 +1,58 @@
<?php
namespace Tests\Feature;
use App\Models\Plan;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AdminPlanSeedDefaultsTest 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_platform_admin_can_seed_default_plans_when_empty(): void
{
$this->loginAsPlatformAdmin();
// 清空 seed 带来的默认套餐(测试口径:模拟空库)
Plan::query()->delete();
$this->assertSame(0, Plan::query()->count());
$this->post('/admin/plans/seed-defaults')
->assertRedirect('/admin/plans');
$this->assertSame(2, Plan::query()->count());
$this->assertNotNull(Plan::query()->where('code', 'starter_monthly')->first());
$this->assertNotNull(Plan::query()->where('code', 'pro_yearly')->first());
$this->get('/admin/plans')
->assertOk()
->assertSee('基础版(月付)')
->assertSee('专业版(年付)');
}
public function test_seed_default_plans_is_blocked_when_plans_exist(): void
{
$this->loginAsPlatformAdmin();
$this->assertTrue(Plan::query()->count() > 0);
$this->post('/admin/plans/seed-defaults')
->assertRedirect();
// 不应新增到 2 条以上;且仍能打开列表
$this->get('/admin/plans')
->assertOk()
->assertSee('套餐管理');
}
}