优化以下代码public function convertedAndSameActiveClue(&$postData, $clueState, $handleSource = Constant::REGISTER) { $result = $this->result; $recentCluePhones = DistributeCommon::getRecentIpClue($postData['phone'], $postData['active_ip']); $specified_channel = TSalesConf::getValue('filter_specified_channel_categories'); foreach ($recentCluePhones as $recentCluePhone) { //过滤指定渠道大类 if ($specified_channel) { $specified_channel = json_decode($specified_channel, true); if (in_array($postData['channel_big_type'], $specified_channel, true)) { event(new SaveOperateLogEvent(OperateLog::OPERATING_CLUE, $postData['phone'] . '已转化线索同IP不下发,过滤指定渠道大类' . json_encode($postData))); break; } } $stateInfo = app(Clue::class)->getClue($recentCluePhone, '+86');//查找当前线索的状态 //排除代理商线索 if ($stateInfo['transform'] && $stateInfo['info']['base_id'] != self::AGENT_BASE_ID) { $result[self::IS_NEXT_CLUE] = true; $result[self::IS_TRIGGER_RULE] = true; $result[self::IS_DISTRIBUTE] = false; $result[self::BASE_ID] = self::WAIT_ACTIVE_BASE_ID; $result[self::POOL_ID] = self::WAIT_ACTIVE_POOL_ID; event(new SaveOperateLogEvent(OperateLog::OPERATING_CLUE, $postData['phone'].' 已转化线索同IP不下发, 与'.$recentCluePhone.'同一IP')); return $result; } } if (!empty($recentCluePhones)) { Log::channel('distribute')->info($postData['phone'] . ' 存在同一IP ' . json_encode($recentCluePhones) . ', 但该线索未转化或不存在与新系统中,或是代理商线索,或过滤指定渠道大类'); event(new SaveOperateLogEvent(OperateLog::OPERATING_CLUE, $postData['phone'] . ' 存在同一IP ' . json_encode($recentCluePhones) . ', 但该线索未转化或不存在与新系统中,或是代理商线索,或过滤指定渠道大类')); } return $result; }
时间: 2024-04-05 18:30:09 浏览: 89
这段代码的作用是将传入的线索信息转化并检查是否存在同IP的线索,若存在则判断是否为代理商线索或指定渠道大类的线索,如果不是则返回一个标记为待激活的线索结果。
以下是一些可以优化的地方:
1. 可以将 `$specified_channel` 的解析和判断移到函数外,避免每次循环时都进行解析和判断。
2. 可以将 `$result` 的初始值移动到函数内部,避免全局变量的使用。
3. 可以考虑使用 `continue` 替代 `break`,这样可以遍历完所有同IP线索并记录日志。
4. 可以将函数名改为更加具体的名称,提高代码的可读性。
下面是优化后的代码示例:
```
public function checkConvertedClueAndSameIP(&$postData, $clueState, $handleSource = Constant::REGISTER)
{
$result = [
self::IS_NEXT_CLUE => false,
self::IS_TRIGGER_RULE => false,
self::IS_DISTRIBUTE => true,
self::BASE_ID => self::WAIT_ACTIVE_BASE_ID,
self::POOL_ID => self::WAIT_ACTIVE_POOL_ID,
];
$recentCluePhones = DistributeCommon::getRecentIpClue($postData['phone'], $postData['active_ip']);
$specifiedChannel = json_decode(TSalesConf::getValue('filter_specified_channel_categories'), true) ?? [];
foreach ($recentCluePhones as $recentCluePhone) {
if (in_array($postData['channel_big_type'], $specifiedChannel, true)) {
event(new SaveOperateLogEvent(OperateLog::OPERATING_CLUE, $postData['phone'] . '已转化线索同IP不下发,过滤指定渠道大类' . json_encode($postData)));
continue;
}
$stateInfo = app(Clue::class)->getClue($recentCluePhone, '+86');
if ($stateInfo['transform'] && $stateInfo['info']['base_id'] != self::AGENT_BASE_ID) {
$result[self::IS_NEXT_CLUE] = true;
$result[self::IS_TRIGGER_RULE] = true;
$result[self::IS_DISTRIBUTE] = false;
event(new SaveOperateLogEvent(OperateLog::OPERATING_CLUE, $postData['phone'].' 已转化线索同IP不下发, 与'.$recentCluePhone.'同一IP'));
}
}
if (!empty($recentCluePhones)) {
Log::channel('distribute')->info($postData['phone'] . ' 存在同一IP ' . json_encode($recentCluePhones) . ', 但该线索未转化或不存在与新系统中,或是代理商线索,或过滤指定渠道大类');
event(new SaveOperateLogEvent(OperateLog::OPERATING_CLUE, $postData['phone'] . ' 存在同一IP ' . json_encode($recentCluePhones) . ', 但该线索未转化或不存在与新系统中,或是代理商线索,或过滤指定渠道大类'));
}
return $result;
}
```
阅读全文