From 35200e480332475ceec711cd2dc1f8117c37987e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=9D=E5=8D=9C?= Date: Sat, 14 Mar 2026 03:54:30 +0000 Subject: [PATCH] =?UTF-8?q?feat(platform):=20=E4=BB=8E=E5=BC=80=E9=80=9A?= =?UTF-8?q?=E7=BA=BF=E7=B4=A2=E5=88=9B=E5=BB=BA=E8=AE=A2=E5=8D=95=E6=97=B6?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=9B=9E=E5=86=99=E7=BA=BF=E7=B4=A2=E4=B8=BA?= =?UTF-8?q?=E5=B7=B2=E8=BD=AC=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Admin/PlatformOrderController.php | 15 +++ .../admin/platform_leads/index.blade.php | 6 +- ...oreFromLeadShouldMarkLeadConvertedTest.php | 119 ++++++++++++++++++ 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/AdminPlatformOrderStoreFromLeadShouldMarkLeadConvertedTest.php diff --git a/app/Http/Controllers/Admin/PlatformOrderController.php b/app/Http/Controllers/Admin/PlatformOrderController.php index f75ab94..57981bb 100644 --- a/app/Http/Controllers/Admin/PlatformOrderController.php +++ b/app/Http/Controllers/Admin/PlatformOrderController.php @@ -82,6 +82,8 @@ class PlatformOrderController extends Controller 'payment_channel' => ['nullable', 'string', 'max:30'], 'remark' => ['nullable', 'string', 'max:2000'], 'back' => ['nullable', 'string', 'max:2000'], + // 可选:从开通线索进入下单(用于轻量闭环联动) + 'lead_id' => ['nullable', 'integer', 'exists:platform_leads,id'], ]); $plan = Plan::query()->findOrFail((int) $data['plan_id']); @@ -131,10 +133,23 @@ class PlatformOrderController extends Controller ], 'meta' => [ 'created_from' => 'manual_form', + // 轻量联动:从开通线索创建订单时记录 lead_id(用于后续转化漏斗与追溯) + 'platform_lead_id' => (int) ($data['lead_id'] ?? 0) ?: null, ], 'remark' => $data['remark'] ?? null, ]); + // 轻量闭环:从线索创建订单后,自动把线索标记为 converted。 + // 注意:只做“前进”流转,避免误把已关闭/已确认需求的线索回写。 + $leadId = (int) ($data['lead_id'] ?? 0); + if ($leadId > 0) { + $lead = \App\Models\PlatformLead::query()->find($leadId); + if ($lead && !in_array((string) $lead->status, ['converted', 'closed'], true)) { + $lead->status = 'converted'; + $lead->save(); + } + } + $back = (string) ($data['back'] ?? ''); // back 需为站内相对路径,并拒绝引号/尖括号,避免在后续页面以 `{!! !!}` 原样输出时引入 XSS 风险。 $safeBack = (str_starts_with($back, '/') diff --git a/resources/views/admin/platform_leads/index.blade.php b/resources/views/admin/platform_leads/index.blade.php index 9206ad0..014468d 100644 --- a/resources/views/admin/platform_leads/index.blade.php +++ b/resources/views/admin/platform_leads/index.blade.php @@ -22,12 +22,16 @@ $l->mobile, $l->company, $l->email, + // 后续可用于“从线索追溯来源页/入口” + $l->source ? ('src:' . $l->source) : '', ], fn ($v) => (string) $v !== ''); $q = [ 'order_type' => 'new_purchase', 'back' => $selfWithoutBack, - 'remark' => mb_substr(implode(' / ', $remarkParts), 0, 180), + 'remark' => mb_substr(implode(' / ', $remarkParts), 0, 220), + // 轻量联动:从线索创建订单后,把线索标记为已转化(后续可再升级为“创建成功后回写”) + 'lead_id' => (int) $l->id, ]; if ((int) $l->plan_id > 0) { diff --git a/tests/Feature/AdminPlatformOrderStoreFromLeadShouldMarkLeadConvertedTest.php b/tests/Feature/AdminPlatformOrderStoreFromLeadShouldMarkLeadConvertedTest.php new file mode 100644 index 0000000..e5bb83e --- /dev/null +++ b/tests/Feature/AdminPlatformOrderStoreFromLeadShouldMarkLeadConvertedTest.php @@ -0,0 +1,119 @@ +seed(); + + $this->post('/admin/login', [ + 'email' => 'platform.admin@demo.local', + 'password' => 'Platform@123456', + ])->assertRedirect('/admin'); + } + + public function test_store_with_lead_id_should_mark_lead_as_converted_and_record_meta(): void + { + $this->loginAsPlatformAdmin(); + + $merchant = Merchant::query()->firstOrFail(); + + $plan = Plan::query()->create([ + 'code' => 'lead_order_link_plan', + 'name' => '线索联动下单测试套餐', + 'billing_cycle' => 'monthly', + 'price' => 10, + 'list_price' => 10, + 'status' => 'active', + 'sort' => 10, + 'published_at' => now(), + ]); + + $lead = PlatformLead::query()->create([ + 'name' => '联动线索', + 'mobile' => '13800000003', + 'email' => 'lead3@example.com', + 'company' => '联动公司', + 'source' => 'test', + 'status' => 'new', + 'plan_id' => $plan->id, + 'meta' => ['from' => 'test'], + ]); + + $res = $this->post('/admin/platform-orders', [ + 'merchant_id' => $merchant->id, + 'plan_id' => $plan->id, + 'order_type' => 'new_purchase', + 'quantity' => 1, + 'discount_amount' => 0, + 'payment_channel' => 'manual', + 'remark' => 'from lead', + 'lead_id' => $lead->id, + 'back' => '/admin/platform-leads', + ]); + + $res->assertRedirect(); + + $lead->refresh(); + $this->assertSame('converted', $lead->status); + + $order = \App\Models\PlatformOrder::query()->latest('id')->firstOrFail(); + $this->assertSame($merchant->id, $order->merchant_id); + $this->assertSame($plan->id, $order->plan_id); + $this->assertSame($lead->id, (int) data_get($order->meta, 'platform_lead_id')); + } + + public function test_store_with_lead_id_should_not_override_closed_lead_status(): void + { + $this->loginAsPlatformAdmin(); + + $merchant = Merchant::query()->firstOrFail(); + + $plan = Plan::query()->create([ + 'code' => 'lead_order_link_plan2', + 'name' => '线索联动下单测试套餐2', + 'billing_cycle' => 'monthly', + 'price' => 10, + 'list_price' => 10, + 'status' => 'active', + 'sort' => 10, + 'published_at' => now(), + ]); + + $lead = PlatformLead::query()->create([ + 'name' => '已关闭线索', + 'mobile' => '', + 'email' => '', + 'company' => '', + 'source' => 'test', + 'status' => 'closed', + 'plan_id' => $plan->id, + 'meta' => ['from' => 'test'], + ]); + + $res = $this->post('/admin/platform-orders', [ + 'merchant_id' => $merchant->id, + 'plan_id' => $plan->id, + 'order_type' => 'new_purchase', + 'quantity' => 1, + 'discount_amount' => 0, + 'payment_channel' => 'manual', + 'remark' => 'from closed lead', + 'lead_id' => $lead->id, + ]); + + $res->assertRedirect(); + + $this->assertSame('closed', $lead->fresh()->status); + } +}