{"contents":"# -*- coding: utf-8 -*-#\n# -------------------------------------------------------------------------------\n# Name: OpenIMContact.py\n# Description: \n# Author: xaoyaoo\n# Date: 2024/04/16\n# -------------------------------------------------------------------------------\nfrom .db_base import DatabaseBase\nfrom .utils import db_error\n\n\nclass OpenIMContactHandler(DatabaseBase):\n _class_name = \"OpenIMContact\"\n OpenIMContact_required_tables = [\"OpenIMContact\"]\n\n def get_im_user_list(self, word=None, wxids=None):\n \"\"\"\n 获取联系人列表\n [ 注意:如果修改这个函数,要同时修改dbMicro.py中的get_user_list函数 ]\n :param word: 查询关键字,可以是用户名、昵称、备注、描述,允许拼音\n :param wxids: 微信id列表\n :return: 联系人字典\n \"\"\"\n if not self.tables_exist(\"OpenIMContact\"):\n return []\n if not wxids:\n wxids = {}\n if isinstance(wxids, str):\n wxids = [wxids]\n sql = (\"SELECT UserName,NickName,Type,Remark,BigHeadImgUrl,CustomInfoDetail,CustomInfoDetailVisible,\"\n \"AntiSpamTicket,AppId,Sex,DescWordingId,ExtraBuf \"\n \"FROM OpenIMContact WHERE 1==1 ;\")\n if word:\n sql = sql.replace(\";\",\n f\"AND (UserName LIKE '%{word}%' \"\n f\"OR NickName LIKE '%{word}%' \"\n f\"OR Remark LIKE '%{word}%' \"\n f\"OR LOWER(NickNamePYInit) LIKE LOWER('%{word}%') \"\n f\"OR LOWER(NickNameQuanPin) LIKE LOWER('%{word}%') \"\n f\"OR LOWER(RemarkPYInit) LIKE LOWER('%{word}%') \"\n f\"OR LOWER(RemarkQuanPin) LIKE LOWER('%{word}%') \"\n \") ;\")\n if wxids:\n sql = sql.replace(\";\", f\"AND UserName IN ('\" + \"','\".join(wxids) + \"') ;\")\n\n result = self.execute(sql)\n if not result:\n return {}\n\n users = {}\n for row in result:\n # 获取用户名、昵称、备注和聊天记录数量\n (UserName, NickName, Type, Remark, BigHeadImgUrl, CustomInfoDetail, CustomInfoDetailVisible,\n AntiSpamTicket, AppId, Sex, DescWordingId, ExtraBuf) = row\n\n users[UserName] = {\n \"wxid\": UserName, \"nickname\": NickName, \"remark\": Remark, \"account\": UserName,\n \"describe\": '', \"headImgUrl\": BigHeadImgUrl if BigHeadImgUrl else \"\",\n \"ExtraBuf\": None, \"LabelIDList\": tuple(), \"extra\": None}\n return users\n\n\n@db_error\ndef get_ExtraBuf(ExtraBuf: bytes):\n \"\"\"\n 读取ExtraBuf(联系人表)\n :param ExtraBuf:\n :return:\n \"\"\"\n if not ExtraBuf:\n return None\n buf_dict = {\n '74752C06': '性别[1男2女]', '46CF10C4': '个性签名', 'A4D9024A': '国', 'E2EAA8D1': '省', '1D025BBF': '市',\n 'F917BCC0': '公司名称', '759378AD': '手机号', '4EB96D85': '企微属性', '81AE19B4': '朋友圈背景',\n '0E719F13': '备注图片', '945f3190': '备注图片2',\n 'DDF32683': '0', '88E28FCE': '1', '761A1D2D': '2', '0263A0CB': '3', '0451FF12': '4', '228C66A8': '5',\n '4D6C4570': '6', '4335DFDD': '7', 'DE4CDAEB': '8', 'A72BC20A': '9', '069FED52': '10', '9B0F4299': '11',\n '3D641E22': '12', '1249822C': '13', 'B4F73ACB': '14', '0959EB92': '15', '3CF4A315': '16',\n 'C9477AC60201E44CD0E8': '17', 'B7ACF0F5': '18', '57A7B5A8': '19', '695F3170': '20', 'FB083DD9': '21',\n '0240E37F': '22', '315D02A3': '23', '7DEC0BC3': '24', '16791C90': '25'\n }\n rdata = {}\n for buf_name in buf_dict:\n rdata_name = buf_dict[buf_name]\n buf_name = bytes.fromhex(buf_name)\n offset = ExtraBuf.find(buf_name)\n if offset == -1:\n rdata[rdata_name] = \"\"\n continue\n offset += len(buf_name)\n type_id = ExtraBuf[offset: offset + 1]\n offset += 1\n\n if type_id == b\"\\x04\":\n rdata[rdata_name] = int.from_bytes(ExtraBuf[offset: offset + 4], \"little\")\n\n elif type_id == b\"\\x18\":\n length = int.from_bytes(ExtraBuf[offset: offset + 4], \"little\")\n rdata[rdata_name] = ExtraBuf[offset + 4: offset + 4 + length].decode(\"utf-16\").rstrip(\"\\x00\")\n\n elif type_id == b\"\\x17\":\n length = int.from_bytes(ExtraBuf[offset: offset + 4], \"little\")\n rdata[rdata_name] = ExtraBuf[offset + 4: offset + 4 + length].decode(\"utf-8\", errors=\"ignore\").rstrip(\n \"\\x00\")\n elif type_id == b\"\\x05\":\n rdata[rdata_name] = f\"0x{ExtraBuf[offset: offset + 8].hex()}\"\n return rdata\n","is_binary":false,"path":"wxdump_linux/db/db_open_im_contact.py","ref":""}