feat(platform): 从开通线索创建订单时自动回写线索为已转化
This commit is contained in:
@@ -82,6 +82,8 @@ class PlatformOrderController extends Controller
|
|||||||
'payment_channel' => ['nullable', 'string', 'max:30'],
|
'payment_channel' => ['nullable', 'string', 'max:30'],
|
||||||
'remark' => ['nullable', 'string', 'max:2000'],
|
'remark' => ['nullable', 'string', 'max:2000'],
|
||||||
'back' => ['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']);
|
$plan = Plan::query()->findOrFail((int) $data['plan_id']);
|
||||||
@@ -131,10 +133,23 @@ class PlatformOrderController extends Controller
|
|||||||
],
|
],
|
||||||
'meta' => [
|
'meta' => [
|
||||||
'created_from' => 'manual_form',
|
'created_from' => 'manual_form',
|
||||||
|
// 轻量联动:从开通线索创建订单时记录 lead_id(用于后续转化漏斗与追溯)
|
||||||
|
'platform_lead_id' => (int) ($data['lead_id'] ?? 0) ?: null,
|
||||||
],
|
],
|
||||||
'remark' => $data['remark'] ?? 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 = (string) ($data['back'] ?? '');
|
||||||
// back 需为站内相对路径,并拒绝引号/尖括号,避免在后续页面以 `{!! !!}` 原样输出时引入 XSS 风险。
|
// back 需为站内相对路径,并拒绝引号/尖括号,避免在后续页面以 `{!! !!}` 原样输出时引入 XSS 风险。
|
||||||
$safeBack = (str_starts_with($back, '/')
|
$safeBack = (str_starts_with($back, '/')
|
||||||
|
|||||||
@@ -22,12 +22,16 @@
|
|||||||
$l->mobile,
|
$l->mobile,
|
||||||
$l->company,
|
$l->company,
|
||||||
$l->email,
|
$l->email,
|
||||||
|
// 后续可用于“从线索追溯来源页/入口”
|
||||||
|
$l->source ? ('src:' . $l->source) : '',
|
||||||
], fn ($v) => (string) $v !== '');
|
], fn ($v) => (string) $v !== '');
|
||||||
|
|
||||||
$q = [
|
$q = [
|
||||||
'order_type' => 'new_purchase',
|
'order_type' => 'new_purchase',
|
||||||
'back' => $selfWithoutBack,
|
'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) {
|
if ((int) $l->plan_id > 0) {
|
||||||
|
|||||||
@@ -0,0 +1,119 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\Models\Merchant;
|
||||||
|
use App\Models\Plan;
|
||||||
|
use App\Models\PlatformLead;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class AdminPlatformOrderStoreFromLeadShouldMarkLeadConvertedTest 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_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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user