diff --git a/resources/messages_resource.py b/resources/messages_resource.py index 98f2341..99fea07 100644 --- a/resources/messages_resource.py +++ b/resources/messages_resource.py @@ -724,19 +724,22 @@ def handle_mod_contacts(token_id,app_id,wxid,msg_data): # 更新好友信息 # 检查好友关系,不是好友则删除 - ret,msg,check_relation=gewe_chat.wxchat.check_relation(token_id, app_id,[contact_wxid]) - first_item = check_relation[0] - check_relation_status=first_item.get('relation') - logger.info(f'{wxid} 好友 {contact_wxid} 关系检查:{check_relation_status}') - if check_relation_status != 0: - gewe_chat.wxchat.delete_contacts_brief_from_cache(wxid, [contact_wxid]) - logger.info(f'好友关系异常:{check_relation_status},删除好友 {contact_wxid} 信息') - - # ret,msg,contacts_list = gewe_chat.wxchat.fetch_contacts_list(token_id, app_id) - # friend_wxids = contacts_list['friends'][3:] # 可以调整截取范围 - # print(friend_wxids) - # #friend_wxids.remove('weixin') - #gewe_chat.wxchat.save_contacts_brief_to_cache(token_id, app_id, wxid, friend_wxids) + # ret,msg,check_relation=gewe_chat.wxchat.check_relation(token_id, app_id,[contact_wxid]) + # first_item = check_relation[0] + # check_relation_status=first_item.get('relation') + # logger.info(f'{wxid} 好友 {contact_wxid} 关系检查:{check_relation_status}') + # if check_relation_status != 0: + # gewe_chat.wxchat.delete_contacts_brief_from_cache(wxid, [contact_wxid]) + # logger.info(f'好友关系异常:{check_relation_status},删除好友 {contact_wxid} 信息') + # else: + # gewe_chat.wxchat.save_contacts_brief_to_cache(token_id, app_id, wxid, [contact_wxid]) + + + ret,msg,contacts_list = gewe_chat.wxchat.fetch_contacts_list(token_id, app_id) + friend_wxids = contacts_list['friends'][3:] # 可以调整截取范围 + print(friend_wxids) + #friend_wxids.remove('weixin') + gewe_chat.wxchat.save_contacts_brief_to_cache(token_id, app_id, wxid, friend_wxids) else: logger.info('群聊好友通过验证及好友资料变更的通知消息') diff --git a/wechat/gewe_chat.py b/wechat/gewe_chat.py index a87b512..24ad09c 100644 --- a/wechat/gewe_chat.py +++ b/wechat/gewe_chat.py @@ -764,26 +764,43 @@ class GeWeChatCom: hash_key = f"__AI_OPS_WX__:CONTACTS_BRIEF:{wxid}" # 获取缓存中的数据 - # cache_str = redis_helper.redis_helper.get_hash_field(hash_key, "data") - # cache = json.loads(cache_str) if cache_str else [] - cache=[] - - # 缓存为空,分批处理 contacts_wxids - batch_size = 100 - for i in range(0, len(contacts_wxids), batch_size): - batch = contacts_wxids[i:i + batch_size] - friends_brief = self.get_brief_info(token_id, app_id, batch) + cache_str = redis_helper.redis_helper.get_hash_field(hash_key, "data") + cache = json.loads(cache_str) if cache_str else [] + # 回调处理 + if len(contacts_wxids) == 1: + cache_wxids = [f['userName'] for f in cache] + friends_brief = self.get_brief_info(token_id, app_id, contacts_wxids) + if contacts_wxids[0] in cache_wxids: + # 替换已经存在的数据 + for i in range(len(cache)): + if cache[i]['userName'] == contacts_wxids[0]: + cache[i] = friends_brief[0] + else: + cache.extend(f for f in friends_brief if f["nickName"]) + friends_no_brief_wxid = [f['userName'] for f in friends_brief if not f["nickName"]] + if friends_no_brief_wxid: + detailed_info = self.get_detail_info(token_id, app_id, friends_no_brief_wxid) + cache.extend(detailed_info) - friends_no_brief_wxid = [f['userName'] for f in friends_brief if not f["nickName"]] - cache.extend(f for f in friends_brief if f["nickName"]) + # 分批处理 + else: + cache=[] + # 缓存为空,分批处理 contacts_wxids + batch_size = 100 + for i in range(0, len(contacts_wxids), batch_size): + batch = contacts_wxids[i:i + batch_size] + friends_brief = self.get_brief_info(token_id, app_id, batch) + + cache.extend(f for f in friends_brief if f["nickName"]) + + friends_no_brief_wxid = [f['userName'] for f in friends_brief if not f["nickName"]] + if friends_no_brief_wxid: + detailed_info = self.get_detail_info(token_id, app_id, friends_no_brief_wxid) + cache.extend(detailed_info) - if friends_no_brief_wxid: - detailed_info = self.get_detail_info(token_id, app_id, friends_no_brief_wxid) - cache.extend(detailed_info) + # 更新缓存 - # 更新缓存 redis_helper.redis_helper.update_hash_field(hash_key, "data", json.dumps(cache, ensure_ascii=False)) - return cache def delete_contacts_brief_from_cache(self, wxid, contacts_wxids: list):