场景获客支持拉群

This commit is contained in:
wong
2026-01-08 10:45:41 +08:00
parent b2e84a2259
commit e469537ac5
13 changed files with 1089 additions and 10564 deletions

View File

@@ -378,32 +378,39 @@ class Adapter implements WeChatServiceInterface
}
if ($passedWeChatId && !empty($task_info['msgConf'])) {
// 更新状态为4已通过并已发消息
Db::name('task_customer')
->where('id', $task['id'])
->update(['status' => 4,'passTime' => time(), 'updateTime' => time()]);
// 记录添加好友奖励如果之前没有记录过status从其他状态变为4时
// 注意如果status已经是2说明已经记录过奖励这里不再重复记录
if ($task['status'] != 2 && !empty($task['channelId'])) {
try {
DistributionRewardService::recordAddFriendReward(
$task['task_id'],
$task['id'],
$task['phone'],
intval($task['channelId'])
);
} catch (\Exception $e) {
// 记录错误但不影响主流程
Log::error('记录添加好友奖励失败:' . $e->getMessage());
}
}
if ($passedWeChatId) {
// 获取好友记录(用于发消息 & 拉群)
$wechatFriendRecord = $this->getWeChatAccoutIdAndFriendIdByWeChatIdAndFriendPhone($passedWeChatId, $task['phone']);
$msgConf = is_string($task_info['msgConf']) ? json_decode($task_info['msgConf'], 1) : $task_info['msgConf'];
$wechatFriendRecord && $this->sendMsgToFriend($wechatFriendRecord['id'], $wechatFriendRecord['wechatAccountId'], $msgConf);
if ($wechatFriendRecord) {
// 1. 如配置了消息则先发送消息并将状态置为4已通过并已发消息
if (!empty($task_info['msgConf'])) {
Db::name('task_customer')
->where('id', $task['id'])
->update(['status' => 4,'passTime' => time(), 'updateTime' => time()]);
// 记录添加好友奖励如果之前没有记录过status从其他状态变为4时
if ($task['status'] != 2 && !empty($task['channelId'])) {
try {
DistributionRewardService::recordAddFriendReward(
$task['task_id'],
$task['id'],
$task['phone'],
intval($task['channelId'])
);
} catch (\Exception $e) {
// 记录错误但不影响主流程
Log::error('记录添加好友奖励失败:' . $e->getMessage());
}
}
$msgConf = is_string($task_info['msgConf']) ? json_decode($task_info['msgConf'], 1) : $task_info['msgConf'];
$this->sendMsgToFriend($wechatFriendRecord['id'], $wechatFriendRecord['wechatAccountId'], $msgConf);
}
// 2. 好友通过后,如配置了拉群,则建群并拉人:通过的好友 + 固定成员
$this->createGroupAfterFriendPass($task_info, $wechatFriendRecord['id'], $wechatFriendRecord['wechatAccountId']);
// 如果没有 msgConf则保持之前更新的状态5已通过未发消息不变
}
} else {
@@ -685,10 +692,90 @@ class Adapter implements WeChatServiceInterface
$task_info['reqConf'] = json_decode($task_info['reqConf'], true);
$task_info['msgConf'] = json_decode($task_info['msgConf'], true);
$task_info['tagConf'] = json_decode($task_info['tagConf'], true);
// 处理拉群固定成员配置JSON 字段)
if (!empty($task_info['groupFixedMembers'])) {
$fixedMembers = json_decode($task_info['groupFixedMembers'], true);
$task_info['groupFixedMembers'] = is_array($fixedMembers) ? $fixedMembers : [];
} else {
$task_info['groupFixedMembers'] = [];
}
}
return $task_info;
}
/**
* 好友通过后,根据任务配置建群并拉人(通过好友 + 固定成员)
* @param array $taskInfo customer_acquisition_task 记录(含 groupInviteEnabled/groupName/groupFixedMembers
* @param int $passedFriendId 通过的好友IDs2_wechat_friend.id
* @param int $wechatAccountId 微信账号ID建群账号
*/
protected function createGroupAfterFriendPass(array $taskInfo, int $passedFriendId, int $wechatAccountId): void
{
// 1. 校验拉群开关与基础配置
if (empty($taskInfo['groupInviteEnabled'])) {
return;
}
$groupName = $taskInfo['groupName'] ?? '';
if ($groupName === '') {
return;
}
$fixedMembers = $taskInfo['groupFixedMembers'] ?? [];
if (!is_array($fixedMembers)) {
$fixedMembers = [];
}
// 2. 过滤出有效的固定成员好友ID数字ID
$fixedFriendIds = [];
foreach ($fixedMembers as $member) {
if (is_numeric($member)) {
$fixedFriendIds[] = intval($member);
}
}
// 包含通过的好友
$friendIds = array_unique(array_merge([$passedFriendId], $fixedFriendIds));
if (empty($friendIds)) {
return;
}
try {
// 3. 初始化 WebSocket参考 sendMsgToFriend / Workbench 群创建逻辑)
$toAccountId = '';
$username = Env::get('api.username', '');
$password = Env::get('api.password', '');
if (!empty($username) || !empty($password)) {
$toAccountId = Db::name('users')->where('account', $username)->value('s2_accountId');
}
if (empty($toAccountId)) {
Log::warning('createGroupAfterFriendPass: toAccountId 为空,跳过建群');
return;
}
$webSocket = new WebSocketController(['userName' => $username, 'password' => $password, 'accountId' => $toAccountId]);
// 4. 调用建群接口:群名 = 配置的 groupName成员 = 通过好友 + 固定好友
$createResult = $webSocket->CmdChatroomCreate([
'chatroomName' => $groupName,
'wechatFriendIds' => $friendIds,
'wechatAccountId' => $wechatAccountId,
]);
$createResultData = json_decode($createResult, true);
if (empty($createResultData) || !isset($createResultData['code']) || $createResultData['code'] != 200) {
Log::warning('createGroupAfterFriendPass: 建群失败', [
'taskId' => $taskInfo['id'] ?? 0,
'wechatAccountId' => $wechatAccountId,
'friendIds' => $friendIds,
'result' => $createResult,
]);
}
} catch (\Exception $e) {
Log::error('createGroupAfterFriendPass 异常: ' . $e->getMessage());
}
}
// 检查是否是好友关系
public function checkIfIsWeChatFriendByPhone($wxId = '', $phone = '', $siteTags = '')