新版流量池服务端提交

This commit is contained in:
wong
2026-02-04 10:58:56 +08:00
parent f956fc949b
commit 7534dd79fa
19 changed files with 1101 additions and 147 deletions

View File

@@ -10,6 +10,8 @@ use think\facade\Env;
// use EasyWeChat\Kernel\Exceptions\DecryptException;
use EasyWeChat\Kernel\Http\StreamResponse;
use think\Db;
use app\cunkebao\service\TrafficPoolService;
use app\common\model\TrafficPoolSource;
class PosterWeChatMiniProgram extends Controller
{
@@ -112,16 +114,18 @@ class PosterWeChatMiniProgram extends Controller
if ($result['errcode'] == 0 && isset($result['phone_info']['phoneNumber'])) {
// ========== 旧版流量池代码(已废弃,保留用于兼容) ==========
// TODO 拿到手机号之后的后续操作:
// 1. 先写入 ck_traffic_pool_v1 表 identifier mobile 都是 用 phone字段的值
$trafficPool = Db::name('traffic_pool_v1')->where('identifier', $result['phone_info']['phoneNumber'])->find();
if (!$trafficPool) {
Db::name('traffic_pool_v1')->insert([
'identifier' => $result['phone_info']['phoneNumber'],
'mobile' => $result['phone_info']['phoneNumber'],
'createTime' => time()
]);
}
// $trafficPool = Db::name('traffic_pool_v1')->where('identifier', $result['phone_info']['phoneNumber'])->find();
// if (!$trafficPool) {
// Db::name('traffic_pool_v1')->insert([
// 'identifier' => $result['phone_info']['phoneNumber'],
// 'mobile' => $result['phone_info']['phoneNumber'],
// 'createTime' => time()
// ]);
// }
// ========== 旧版流量池代码结束已迁移到V2实时同步 ==========
// 2. 写入 ck_task_customer: 以 task_id ~~identifier~~ phone 为条件如果存在则忽略使用类似laravel的firstOrcreate但我不知道thinkphp5.1里的写法)
// $taskCustomer = Db::name('task_customer')->where('task_id', $taskId)->where('identifier', $result['phone_info']['phoneNumber'])->find();
$taskCustomer = Db::name('task_customer')
@@ -165,6 +169,50 @@ class PosterWeChatMiniProgram extends Controller
'siteTags' => json_encode([]),
]);
// 实时同步到 V2 流量池系统(异步处理,不影响主流程)
if ($customerId) {
try {
$poolService = new TrafficPoolService();
$identifier = $result['phone_info']['phoneNumber'];
// 准备流量池数据
$poolData = [
'identifierType' => 2, // 手机号
'mobile' => $identifier,
];
// 准备公司流量数据
$companyData = [
'phone' => $identifier,
];
// 准备来源数据
$sourceData = [
'sourceName' => $task['name'] ?? '海报获客',
'extra' => json_encode([
'planId' => $taskId,
'planName' => $task['name'] ?? '',
'channelId' => $finalChannelId,
'customerId' => $customerId,
'source' => 'poster_miniprogram',
], JSON_UNESCAPED_UNICODE),
];
// 同步到 V2 流量池
$poolService->enterPool(
$identifier,
$task['companyId'],
TrafficPoolSource::SOURCE_TYPE_POSTER, // 海报获客
$poolData,
$companyData,
$sourceData
);
} catch (\Exception $e) {
// 记录错误但不影响主流程
\think\facade\Log::error('同步到V2流量池失败' . $e->getMessage());
}
}
// 记录获客奖励(异步处理,不影响主流程)
if ($customerId) {
try {
@@ -259,31 +307,33 @@ class PosterWeChatMiniProgram extends Controller
continue;
}
$isPhone = preg_match('/^\+?\d{6,}$/', $identifier);
$trafficPool = Db::name('traffic_pool_v1')->where('identifier', $identifier)->find();
if (!$trafficPool) {
$insertData = [
'identifier' => $identifier,
'createTime' => time()
];
if ($isPhone) {
$insertData['mobile'] = $identifier;
} else {
$insertData['wechatId'] = $identifier;
}
Db::name('traffic_pool_v1')->insert($insertData);
} else {
$updates = [];
if ($isPhone && empty($trafficPool['mobile'])) {
$updates['mobile'] = $identifier;
}
if (!$isPhone && empty($trafficPool['wechatId'])) {
$updates['wechatId'] = $identifier;
}
if (!empty($updates)) {
$updates['updateTime'] = time();
Db::name('traffic_pool_v1')->where('id', $trafficPool['id'])->update($updates);
}
}
// ========== 旧版流量池代码(已废弃,保留用于兼容) ==========
// $trafficPool = Db::name('traffic_pool_v1')->where('identifier', $identifier)->find();
// if (!$trafficPool) {
// $insertData = [
// 'identifier' => $identifier,
// 'createTime' => time()
// ];
// if ($isPhone) {
// $insertData['mobile'] = $identifier;
// } else {
// $insertData['wechatId'] = $identifier;
// }
// Db::name('traffic_pool_v1')->insert($insertData);
// } else {
// $updates = [];
// if ($isPhone && empty($trafficPool['mobile'])) {
// $updates['mobile'] = $identifier;
// }
// if (!$isPhone && empty($trafficPool['wechatId'])) {
// $updates['wechatId'] = $identifier;
// }
// if (!empty($updates)) {
// $updates['updateTime'] = time();
// Db::name('traffic_pool_v1')->where('id', $trafficPool['id'])->update($updates);
// }
// }
// ========== 旧版流量池代码结束已迁移到V2实时同步 ==========
$taskCustomer = Db::name('task_customer')
->where('task_id', $taskId)
@@ -305,6 +355,55 @@ class PosterWeChatMiniProgram extends Controller
// 使用 insertGetId 以便在需要时记录获客奖励
$customerId = Db::name('task_customer')->insertGetId($insertCustomer);
// 实时同步到 V2 流量池系统(异步处理,不影响主流程)
if (!empty($customerId)) {
try {
$poolService = new TrafficPoolService();
// 判断 identifier 类型
$identifierType = $isPhone ? 2 : 1; // 2=手机号, 1=微信号
// 准备流量池数据
$poolData = [
'identifierType' => $identifierType,
'mobile' => $isPhone ? $identifier : '',
'wechatId' => !$isPhone ? $identifier : '',
];
// 准备公司流量数据
$companyData = [
'phone' => $isPhone ? $identifier : '',
'remark' => $remark,
];
// 准备来源数据
$sourceData = [
'sourceName' => $task['name'] ?? '海报获客',
'remark' => $remark,
'extra' => json_encode([
'planId' => $taskId,
'planName' => $task['name'] ?? '',
'channelId' => $finalChannelId,
'customerId' => $customerId,
'source' => 'poster_batch_import',
], JSON_UNESCAPED_UNICODE),
];
// 同步到 V2 流量池
$poolService->enterPool(
$identifier,
$task['companyId'],
TrafficPoolSource::SOURCE_TYPE_POSTER, // 海报获客
$poolData,
$companyData,
$sourceData
);
} catch (\Exception $e) {
// 记录错误但不影响主流程
\think\facade\Log::error('同步到V2流量池失败' . $e->getMessage());
}
}
// 表单录入成功即视为一次获客:
// 仅在存在有效渠道ID时记录获客奖励谁的cid谁获客
if (!empty($customerId) && $finalChannelId > 0) {