场景获客支持拉群
This commit is contained in:
@@ -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 通过的好友ID(s2_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 = '')
|
||||
|
||||
Reference in New Issue
Block a user