77
88class Api :
99 port : int = 18888
10- db_handle : int = 0
10+ db_handle : Dict [ str , int ] = 0
1111
1212 def IsLoginIn (self , ** params ) -> Dict :
1313 return self .post (WECHAT_IS_LOGIN , IsLoginBody (** params ))
@@ -137,33 +137,39 @@ def GetQrcodeImage(self , **params):
137137 return r .content
138138
139139 #[自定义
140- def GetDBHandle (self ) -> Union [ None , int ] :
141- try :
142- return [ i for i in self .GetDatabaseHandles ()["data" ] if i [ "db_name" ] == "MicroMsg.db" ][ 0 ][ "handle" ]
143- except :
144- return None
140+ def GetDBHandle (self , db_name = "MicroMsg.db" ) -> int :
141+ if not self . db_handle :
142+ self . db_handle = { i [ "db_name" ]: i [ "handle" ] for i in self .GetDatabaseHandles ()["data" ]}
143+
144+ return self . db_handle [ db_name ]
145145
146146 def GetContactListBySql (self ) -> Dict :
147- if not self .db_handle :
148- self .db_handle = self .GetDBHandle ()
149147 sql = "select UserName,Alias,Remark,NickName,Type from Contact" # where type!=4 and type!=0;
150- ContactList = self .QueryDatabase (db_handle = self .db_handle , sql = sql )["data" ]
148+ ContactList = self .QueryDatabase (db_handle = self .GetDBHandle () , sql = sql )["data" ]
151149 contact_data = {} # {wxid : {alias, remark, nickname , type}}
152- for index in range (1 , len (ContactList )):
150+ for index in range (1 , len (ContactList )):
153151 wxid = ContactList [index ][0 ]
154152 contact_data [wxid ] = {}
155153 contact_data [wxid ]['alias' ] = ContactList [index ][1 ]
156154 contact_data [wxid ]['remark' ] = ContactList [index ][2 ]
157155 contact_data [wxid ]['nickname' ] = ContactList [index ][3 ]
158156 contact_data [wxid ]['type' ] = ContactList [index ][4 ]
157+
158+ sql = "select UserName,'' as Alias,Remark,NickName,Type from OpenIMContact" # where type!=4 and type!=0;
159+ OpenIMContactList = self .QueryDatabase (db_handle = self .GetDBHandle ("OpenIMContact.db" ), sql = sql )["data" ]
160+ for index in range (1 , len (OpenIMContactList )):
161+ wxid = OpenIMContactList [index ][0 ]
162+ contact_data [wxid ] = {}
163+ contact_data [wxid ]['alias' ] = ContactList [index ][1 ]
164+ contact_data [wxid ]['remark' ] = OpenIMContactList [index ][2 ]
165+ contact_data [wxid ]['nickname' ] = OpenIMContactList [index ][3 ]
166+ contact_data [wxid ]['type' ] = OpenIMContactList [index ][4 ]
159167 return contact_data
160168
161169 def GetAllGroupMembersBySql (self ) -> Dict :
162170 group_data = {} #{"group_id" : { "wxID" : "displayName"}}
163- if not self .db_handle :
164- self .db_handle = self .GetDBHandle ()
165171 sql = "select ChatRoomName,RoomData from ChatRoom"
166- GroupMemberList = self .QueryDatabase (db_handle = self .db_handle , sql = sql )['data' ]
172+ GroupMemberList = self .QueryDatabase (db_handle = self .GetDBHandle () , sql = sql )['data' ]
167173 chatroom = ChatRoom .ChatRoomData ()
168174 for index in range (1 , len (GroupMemberList )):
169175 group_member = {}
@@ -174,11 +180,13 @@ def GetAllGroupMembersBySql(self) -> Dict:
174180 group_data [GroupMemberList [index ][0 ]] = group_member
175181 return group_data
176182
177- def GetPictureBySql (self , wxid ) -> Dict :
178- if not self .db_handle :
179- self .db_handle = self .GetDBHandle ()
180- sql = f"select usrName,smallHeadImgUrl,bigHeadImgUrl from ContactHeadImgUrl where usrName='{ wxid } ';"
181- result = self .QueryDatabase (db_handle = self .db_handle ,sql = sql )
183+ def GetPictureBySql (self , wxid ) -> Dict :
184+ if not wxid .endswith ("@openim" ):
185+ sql = f"select usrName,smallHeadImgUrl,bigHeadImgUrl from ContactHeadImgUrl where usrName='{ wxid } ';"
186+ result = self .QueryDatabase (db_handle = self .GetDBHandle (),sql = sql )
187+ else :
188+ sql = f"select UserName,SmallHeadImgUrl,BigHeadImgUrl from OpenIMContact where UserName='{ wxid } ';"
189+ result = self .QueryDatabase (db_handle = self .GetDBHandle ("OpenIMContact.db" ),sql = sql )
182190 try :
183191 if result ["data" ][1 ][2 ] != "" :
184192 return result ["data" ][1 ][2 ]
@@ -188,11 +196,13 @@ def GetPictureBySql(self , wxid) -> Dict:
188196 except :
189197 return None
190198
191- def GetContactBySql (self , wxid ):
192- if not self .db_handle :
193- self .db_handle = self .GetDBHandle ()
194- sql = f"select UserName,Alias,Remark,NickName,Type from Contact where UserName='{ wxid } ';"
195- result = self .QueryDatabase (db_handle = self .db_handle ,sql = sql )
199+ def GetContactBySql (self , wxid ):
200+ if not wxid .endswith ("@openim" ):
201+ sql = f"select UserName,Alias,Remark,NickName,Type from Contact where UserName='{ wxid } ';"
202+ result = self .QueryDatabase (db_handle = self .GetDBHandle (),sql = sql )
203+ else :
204+ sql = f"select UserName,'' as Alias,Remark,NickName,Type from OpenIMContact where UserName='{ wxid } ';"
205+ result = self .QueryDatabase (db_handle = self .GetDBHandle ("OpenIMContact.db" ),sql = sql )
196206 if len (result ["data" ]) > 0 :
197207 return result ["data" ][1 ]
198208 else :
@@ -205,4 +215,4 @@ def post(self , type : int, params : Body) -> Dict:
205215 def exec_command (self , item : str ) -> Callable :
206216 return eval (f"self.{ item } " )
207217
208-
218+
0 commit comments