代码提交

This commit is contained in:
wong
2025-10-16 10:59:08 +08:00
parent db22100846
commit 1083822513
3 changed files with 161 additions and 232 deletions

View File

@@ -54,9 +54,11 @@ class WechatChatroomController extends BaseController
// 保存数据到数据库
if (!empty($response['results'])) {
$isUpdate = false;
$updated = $this->saveChatroom($response['results']);
if($updated && $isDel == 0){
$isUpdate = true;
foreach ($response['results'] as $item) {
$updated = $this->saveChatroom($item);
if($updated && $isDel == 0){
$isUpdate = true;
}
}
}
@@ -78,71 +80,53 @@ class WechatChatroomController extends BaseController
* 保存群聊数据到数据库
* @param array $item 群聊数据
*/
private function saveChatroom($data)
private function saveChatroom($item)
{
$sqlData = [];
foreach ($data as $item) {
$sqlData[] = [
'id' => $item['id'],
'wechatAccountId' => $item['wechatAccountId'],
'wechatAccountAlias' => $item['wechatAccountAlias'],
'wechatAccountWechatId' => $item['wechatAccountWechatId'],
'wechatAccountAvatar' => $item['wechatAccountAvatar'],
'wechatAccountNickname' => $item['wechatAccountNickname'],
'chatroomId' => $item['chatroomId'],
'hasMe' => $item['hasMe'],
'chatroomOwnerNickname' => isset($item['chatroomOwnerNickname']) ? $item['chatroomOwnerNickname'] : '',
'chatroomOwnerAvatar' => isset($item['chatroomOwnerAvatar']) ? $item['chatroomOwnerAvatar'] : '',
'conRemark' => isset($item['conRemark']) ? $item['conRemark'] : '',
'nickname' => isset($item['nickname']) ? $item['nickname'] : '',
'pyInitial' => isset($item['pyInitial']) ? $item['pyInitial'] : '',
'quanPin' => isset($item['quanPin']) ? $item['quanPin'] : '',
'chatroomAvatar' => isset($item['chatroomAvatar']) ? $item['chatroomAvatar'] : '',
'members' => is_array($item['members']) ? json_encode($item['members']) : json_encode([]),
'isDeleted' => isset($item['isDeleted']) ? $item['isDeleted'] : 0,
'deleteTime' => !empty($item['isDeleted']) ? strtotime($item['deleteTime']) : 0,
'createTime' => isset($item['createTime']) ? strtotime($item['createTime']) : 0,
'accountId' => isset($item['accountId']) ? $item['accountId'] : 0,
'accountUserName' => isset($item['accountUserName']) ? $item['accountUserName'] : '',
'accountRealName' => isset($item['accountRealName']) ? $item['accountRealName'] : '',
'accountNickname' => isset($item['accountNickname']) ? $item['accountNickname'] : '',
'groupId' => isset($item['groupId']) ? $item['groupId'] : 0,
'updateTime' => time()
];
}
$data = [
'id' => $item['id'],
'wechatAccountId' => $item['wechatAccountId'],
'wechatAccountAlias' => $item['wechatAccountAlias'],
'wechatAccountWechatId' => $item['wechatAccountWechatId'],
'wechatAccountAvatar' => $item['wechatAccountAvatar'],
'wechatAccountNickname' => $item['wechatAccountNickname'],
'chatroomId' => $item['chatroomId'],
'hasMe' => $item['hasMe'],
'chatroomOwnerNickname' => isset($item['chatroomOwnerNickname']) ? $item['chatroomOwnerNickname'] : '',
'chatroomOwnerAvatar' => isset($item['chatroomOwnerAvatar']) ? $item['chatroomOwnerAvatar'] : '',
'conRemark' => isset($item['conRemark']) ? $item['conRemark'] : '',
'nickname' => isset($item['nickname']) ? $item['nickname'] : '',
'pyInitial' => isset($item['pyInitial']) ? $item['pyInitial'] : '',
'quanPin' => isset($item['quanPin']) ? $item['quanPin'] : '',
'chatroomAvatar' => isset($item['chatroomAvatar']) ? $item['chatroomAvatar'] : '',
'members' => is_array($item['members']) ? json_encode($item['members']) : json_encode([]),
'isDeleted' => isset($item['isDeleted']) ? $item['isDeleted'] : 0,
'deleteTime' => !empty($item['isDeleted']) ? strtotime($item['deleteTime']) : 0,
'createTime' => isset($item['createTime']) ? strtotime($item['createTime']) : 0,
'accountId' => isset($item['accountId']) ? $item['accountId'] : 0,
'accountUserName' => isset($item['accountUserName']) ? $item['accountUserName'] : '',
'accountRealName' => isset($item['accountRealName']) ? $item['accountRealName'] : '',
'accountNickname' => isset($item['accountNickname']) ? $item['accountNickname'] : '',
'groupId' => isset($item['groupId']) ? $item['groupId'] : 0,
'updateTime' => time()
];
if (empty($sqlData)) {
// 使用chatroomId和wechatAccountId的组合作为唯一性判断
$chatroom = WechatChatroomModel::where('id',$item['id'])->find();
if ($chatroom) {
$chatroom->save($data);
return true;
} else {
WechatChatroomModel::create($data);
return false;
}
// 先查已存在ID拆分插入与更新两批参考 WechatFriendController
$ids = array_column($sqlData, 'id');
$existingIds = WechatChatroomModel::whereIn('id', $ids)->column('id');
$existingSet = array_flip($existingIds);
$toInsert = [];
$toUpdate = [];
foreach ($sqlData as $row) {
if (isset($existingSet[$row['id']])) {
$toUpdate[] = $row;
} else {
$toInsert[] = $row;
}
}
$isUpdate = false;
if (!empty($toInsert)) {
$m = new WechatChatroomModel();
// 批量插入(允许外部主键)
$m->insertAll($toInsert, true);
$isUpdate = false;
}
if (!empty($toUpdate)) {
$m = new WechatChatroomModel();
$m->saveAll($toUpdate);
$isUpdate = true;
}
return $isUpdate;
// // 同时保存群成员数据
// if (!empty($item['members'])) {
// foreach ($item['members'] as $member) {
// $this->saveChatroomMember($member, $item['chatroomId']);
// }
// }
}
/**
@@ -190,7 +174,9 @@ class WechatChatroomController extends BaseController
// 保存数据到数据库
if (!empty($response)) {
$this->saveChatroomMember($response, $chatroomId);
foreach ($response as $item) {
$this->saveChatroomMember($item, $chatroomId);
}
}
if($isInner){
@@ -212,50 +198,30 @@ class WechatChatroomController extends BaseController
* @param array $item 群成员数据
* @param string $wechatChatroomId 微信群ID
*/
private function saveChatroomMember($data, $wechatChatroomId)
private function saveChatroomMember($item, $wechatChatroomId)
{
$sqlData = [];
foreach ($data as $item) {
$sqlData[] = [
'chatroomId' => $wechatChatroomId,
'wechatId' => isset($item['wechatId']) ? $item['wechatId'] : '',
'nickname' => isset($item['nickname']) ? $item['nickname'] : '',
'avatar' => isset($item['avatar']) ? $item['avatar'] : '',
'conRemark' => isset($item['conRemark']) ? $item['conRemark'] : '',
'alias' => isset($item['alias']) ? $item['alias'] : '',
'friendType' => isset($item['friendType']) ? $item['friendType'] : false,
'updateTime' => time()
];
}
$data = [
'chatroomId' => $wechatChatroomId,
'wechatId' => isset($item['wechatId']) ? $item['wechatId'] : '',
'nickname' => isset($item['nickname']) ? $item['nickname'] : '',
'avatar' => isset($item['avatar']) ? $item['avatar'] : '',
'conRemark' => isset($item['conRemark']) ? $item['conRemark'] : '',
'alias' => isset($item['alias']) ? $item['alias'] : '',
'friendType' => isset($item['friendType']) ? $item['friendType'] : false,
'updateTime' => time()
];
if (empty($sqlData)) {
return false;
}
// 使用chatroomId和wechatId的组合作为唯一性判断
$member = WechatChatroomMemberModel::where([
['chatroomId', '=', $wechatChatroomId],
['wechatId', '=', $item['wechatId']]
])->find();
// 使用 (chatroomId, wechatId) 组合作为唯一性判断,拆分插入与更新
$existing = WechatChatroomMemberModel::where('chatroomId', $wechatChatroomId)
->column('id,wechatId', 'wechatId');
$toInsert = [];
$toUpdate = [];
foreach ($sqlData as $row) {
$wid = $row['wechatId'];
if ($wid !== '' && isset($existing[$wid])) {
// 带上主键以便 saveAll 根据主键更新
$row['id'] = $existing[$wid]['id'] ?? $existing[$wid];
$toUpdate[] = $row;
} else {
$toInsert[] = $row;
}
}
if (!empty($toInsert)) {
$m = new WechatChatroomMemberModel();
$m->insertAll($toInsert, true);
}
if (!empty($toUpdate)) {
$m = new WechatChatroomMemberModel();
$m->saveAll($toUpdate);
if ($member) {
$member->savea($data);
} else {
$data['createTime'] = time();
WechatChatroomMemberModel::create($data);
}
}