分销功能提交
This commit is contained in:
@@ -113,10 +113,39 @@ class PosterWeChatMiniProgram extends Controller
|
||||
}
|
||||
// 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')->where('task_id', $taskId)->where('phone', $result['phone_info']['phoneNumber'])->find();
|
||||
$taskCustomer = Db::name('task_customer')
|
||||
->where('task_id', $taskId)
|
||||
->where('phone', $result['phone_info']['phoneNumber'])
|
||||
->find();
|
||||
if (!$taskCustomer) {
|
||||
Db::name('task_customer')->insert([
|
||||
// 渠道ID(cid),对应 distribution_channel.id
|
||||
$channelId = intval($this->request->param('cid', 0));
|
||||
|
||||
$finalChannelId = 0;
|
||||
if ($channelId > 0) {
|
||||
// 获取任务信息,解析分销配置
|
||||
$sceneConf = json_decode($task['sceneConf'] ?? '[]', true) ?: [];
|
||||
$distributionConfig = $sceneConf['distribution'] ?? null;
|
||||
$allowedChannelIds = $distributionConfig['channels'] ?? [];
|
||||
if (!empty($distributionConfig) && !empty($distributionConfig['enabled']) && in_array($channelId, $allowedChannelIds)) {
|
||||
// 验证渠道是否存在且正常
|
||||
$channel = Db::name('distribution_channel')
|
||||
->where([
|
||||
['id', '=', $channelId],
|
||||
['companyId', '=', $task['companyId']],
|
||||
['status', '=', 'enabled'],
|
||||
['deleteTime', '=', 0]
|
||||
])
|
||||
->find();
|
||||
if ($channel) {
|
||||
$finalChannelId = $channelId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$customerId = Db::name('task_customer')->insertGetId([
|
||||
'task_id' => $taskId,
|
||||
'channelId' => $finalChannelId,
|
||||
// 'identifier' => $result['phone_info']['phoneNumber'],
|
||||
'phone' => $result['phone_info']['phoneNumber'],
|
||||
'source' => $task['name'],
|
||||
@@ -124,6 +153,23 @@ class PosterWeChatMiniProgram extends Controller
|
||||
'tags' => json_encode([]),
|
||||
'siteTags' => json_encode([]),
|
||||
]);
|
||||
|
||||
// 记录获客奖励(异步处理,不影响主流程)
|
||||
if ($customerId) {
|
||||
try {
|
||||
if ($finalChannelId > 0) {
|
||||
\app\cunkebao\service\DistributionRewardService::recordCustomerReward(
|
||||
$taskId,
|
||||
$customerId,
|
||||
$result['phone_info']['phoneNumber'],
|
||||
$finalChannelId
|
||||
);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// 记录错误但不影响主流程
|
||||
\think\facade\Log::error('记录获客奖励失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
// return $result['phone_info']['phoneNumber'];
|
||||
return json([
|
||||
@@ -149,6 +195,8 @@ class PosterWeChatMiniProgram extends Controller
|
||||
|
||||
$taskId = request()->param('id');
|
||||
$rawInput = trim((string)request()->param('phone', ''));
|
||||
// 渠道ID(cid),对应 distribution_channel.id
|
||||
$channelId = intval(request()->param('cid', 0));
|
||||
if ($rawInput === '') {
|
||||
return json([
|
||||
'code' => 400,
|
||||
@@ -164,6 +212,28 @@ class PosterWeChatMiniProgram extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
// 预先根据任务的分销配置校验渠道是否有效(仅当传入了cid时)
|
||||
$finalChannelId = 0;
|
||||
if ($channelId > 0) {
|
||||
$sceneConf = json_decode($task['sceneConf'] ?? '[]', true) ?: [];
|
||||
$distributionConfig = $sceneConf['distribution'] ?? null;
|
||||
$allowedChannelIds = $distributionConfig['channels'] ?? [];
|
||||
if (!empty($distributionConfig) && !empty($distributionConfig['enabled']) && in_array($channelId, $allowedChannelIds)) {
|
||||
// 验证渠道是否存在且正常
|
||||
$channel = Db::name('distribution_channel')
|
||||
->where([
|
||||
['id', '=', $channelId],
|
||||
['companyId', '=', $task['companyId']],
|
||||
['status', '=', 'enabled'],
|
||||
['deleteTime', '=', 0]
|
||||
])
|
||||
->find();
|
||||
if ($channel) {
|
||||
$finalChannelId = $channelId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$lines = preg_split('/\r\n|\r|\n/', $rawInput);
|
||||
foreach ($lines as $line) {
|
||||
@@ -210,17 +280,35 @@ class PosterWeChatMiniProgram extends Controller
|
||||
->find();
|
||||
if (empty($taskCustomer)) {
|
||||
$insertCustomer = [
|
||||
'task_id' => $taskId,
|
||||
'phone' => $identifier,
|
||||
'source' => $task['name'],
|
||||
'createTime' => time(),
|
||||
'tags' => json_encode([]),
|
||||
'siteTags' => json_encode([]),
|
||||
'task_id' => $taskId,
|
||||
'channelId' => $finalChannelId, // 记录本次导入归属的分销渠道(如有)
|
||||
'phone' => $identifier,
|
||||
'source' => $task['name'],
|
||||
'createTime'=> time(),
|
||||
'tags' => json_encode([]),
|
||||
'siteTags' => json_encode([]),
|
||||
];
|
||||
if ($remark !== '') {
|
||||
$insertCustomer['remark'] = $remark;
|
||||
}
|
||||
Db::name('task_customer')->insert($insertCustomer);
|
||||
// 使用 insertGetId 以便在需要时记录获客奖励
|
||||
$customerId = Db::name('task_customer')->insertGetId($insertCustomer);
|
||||
|
||||
// 表单录入成功即视为一次获客:
|
||||
// 仅在存在有效渠道ID时,记录获客奖励(谁的cid谁获客)
|
||||
if (!empty($customerId) && $finalChannelId > 0) {
|
||||
try {
|
||||
\app\cunkebao\service\DistributionRewardService::recordCustomerReward(
|
||||
$taskId,
|
||||
$customerId,
|
||||
$identifier,
|
||||
$finalChannelId
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
// 记录错误但不影响主流程
|
||||
\think\facade\Log::error('记录获客奖励失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
} elseif ($remark !== '' && $taskCustomer['remark'] !== $remark) {
|
||||
Db::name('task_customer')
|
||||
->where('id', $taskCustomer['id'])
|
||||
|
||||
Reference in New Issue
Block a user