BaseService.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. unit BaseService;
  2. interface
  3. uses
  4. RealICQContacterTreeView, Classes, SysUtils, superobject, RealICQModel,
  5. Windows, RealICQClient;
  6. type
  7. TBaseService = class
  8. private
  9. protected
  10. public
  11. class function GetServerID(ALoginName: string): string;
  12. class function GetAndClearServerID(var ALoginName: string): string;
  13. class function ClearServerID(ALoginName: string): string; static;
  14. class function FullLoginName(ACenterServerID, AServerID, ALoginName: string): string; static;
  15. class function GetLocalUID(ALoginName: string): string;
  16. class procedure InitUserObject(AUser: TRealICQUser; AJo: ISuperObject); overload;
  17. class procedure InitUserObject(AUser: TRealICQUser; AJson: string); overload;
  18. class function GetSimpleUserJsons(AUsers: TStringList): TInterfaceList;
  19. class function GetUserStatusJsons(AUsers: TStringList): TInterfaceList;
  20. class function GetLoginNamesJsons(ALoginNames: TStrings): TInterfaceList; overload;
  21. class function GetLoginNamesJsons(ALoginNames: array of string): TInterfaceList; overload;
  22. class function ToJsonObject(AUser: TRealICQUser): ISuperObject;
  23. /// <summary>
  24. /// 打包并发送
  25. /// 试用于$D4+总长(2byte)+次协议(1byte)+JSON长(2byte)+JSON字符串的形式
  26. /// </summary>
  27. /// <param name="AProtocol">次协议</param>
  28. /// <param name="AJo">JSON对象</param>
  29. /// <param name="ARealICQClient"></param>
  30. /// <returns>true表示符合包的长度限制,false则表示超出包的长度限制</returns>
  31. function PacketAndSend(AProtocol: Byte; AJo: ISuperObject; ARealICQClient: TRealICQClient): Boolean;
  32. class function GetTreeView(AKey: string): TRealICQContacterTreeView;
  33. procedure FreeStringList(AStringList: TStringList);
  34. procedure ClearStringList(AStringList: TStringList);
  35. end;
  36. implementation
  37. uses
  38. MainFrm, LoggerImport, UsersService, Forms, Dialogs;
  39. class function TBaseService.GetSimpleUserJsons(AUsers: TStringList): TInterfaceList;
  40. var
  41. iLoop, iIndex, jLoop: Integer;
  42. AJo, AjoSimpleUser: ISuperObject;
  43. AJa: TSuperArray;
  44. AUser: TRealICQUser;
  45. AServerID: string;
  46. begin
  47. // AJo := SO('[]');
  48. Result := TInterfaceList.Create;
  49. for iLoop := 0 to AUsers.Count - 1 do
  50. begin
  51. AJo := nil;
  52. AUser := AUsers.Objects[iLoop] as TRealICQUser;
  53. if AUser.IsComparedVer then
  54. Continue;
  55. AUser.IsComparedVer := true;
  56. AServerID := GetServerID(AUser.LoginName);
  57. for jLoop := 0 to Result.Count - 1 do
  58. if SameText(AServerID, (Result[jLoop] as ISuperObject).S['s']) then
  59. begin
  60. AJo := Result[jLoop] as ISuperObject;
  61. if AJo.A['us'].Length > 80 then
  62. begin
  63. AJo := SO();
  64. AJo.S['s'] := AServerID;
  65. AJo.N['us'] := SO('[]');
  66. Result.Insert(0, AJo);
  67. end;
  68. Break;
  69. end;
  70. if AJo = nil then
  71. begin
  72. AJo := SO();
  73. AJo.S['s'] := AServerID;
  74. AJo.N['us'] := SO('[]');
  75. Result.Add(AJo);
  76. end;
  77. AJa := AJo.A['us'];
  78. AjoSimpleUser := SO();
  79. AjoSimpleUser.S['l'] := ClearServerID(AUser.LoginName);
  80. AjoSimpleUser.I['v'] := AUser.CurrentExInfoVersion;
  81. AJa.Add(AjoSimpleUser);
  82. end;
  83. end;
  84. class function TBaseService.GetLoginNamesJsons(ALoginNames: TStrings): TInterfaceList;
  85. var
  86. iLoop, iIndex, jLoop: Integer;
  87. AJo, AjoSimpleUser: ISuperObject;
  88. AJa: TSuperArray;
  89. ALoginName: string;
  90. AServerID: string;
  91. begin
  92. Result := TInterfaceList.Create;
  93. for iLoop := 0 to ALoginNames.Count - 1 do
  94. begin
  95. AJo := nil;
  96. ALoginName := ALoginNames[iLoop];
  97. AServerID := GetServerID(ALoginName);
  98. for jLoop := 0 to Result.Count - 1 do
  99. if SameText(AServerID, (Result[jLoop] as ISuperObject).S['s']) then
  100. begin
  101. AJo := Result[jLoop] as ISuperObject;
  102. if AJo.A['us'].Length > 100 then
  103. begin
  104. AJo := SO();
  105. AJo.S['s'] := AServerID;
  106. AJo.N['us'] := SO('[]');
  107. Result.Insert(0, AJo);
  108. end;
  109. Break;
  110. end;
  111. if AJo = nil then
  112. begin
  113. AJo := SO();
  114. AJo.S['s'] := AServerID;
  115. AJo.N['us'] := SO('[]');
  116. Result.Add(AJo);
  117. end;
  118. AJo.A['us'].Add(ALoginName);
  119. end;
  120. end;
  121. class function TBaseService.GetLoginNamesJsons(
  122. ALoginNames: array of string): TInterfaceList;
  123. var
  124. ALoginNameStrings: TStringList;
  125. iLoop: Integer;
  126. begin
  127. ALoginNameStrings := TStringList.Create;
  128. try
  129. for iLoop := 0 to Length(ALoginnames) - 1 do
  130. ALoginNameStrings.Add(ALoginNames[iLoop]);
  131. Result := GetLoginNamesJsons(ALoginNameStrings);
  132. finally
  133. FreeAndNil(ALoginNameStrings);
  134. end;
  135. end;
  136. procedure TBaseService.ClearStringList(AStringList: TStringList);
  137. var
  138. AObj: TObject;
  139. begin
  140. if AStringList = nil then
  141. Exit;
  142. while AStringList.Count > 0 do
  143. begin
  144. Aobj := AStringList.Objects[0];
  145. AStringList.Delete(0);
  146. FreeAndNil(AObj);
  147. end;
  148. end;
  149. procedure TBaseService.FreeStringList(AStringList: TStringList);
  150. var
  151. AObj: TObject;
  152. begin
  153. if AStringList = nil then
  154. Exit;
  155. while AStringList.Count > 0 do
  156. begin
  157. Aobj := AStringList.Objects[0];
  158. AStringList.Delete(0);
  159. FreeAndNil(AObj);
  160. end;
  161. FreeAndNil(AStringList);
  162. end;
  163. class function TBaseService.GetTreeView(AKey: string): TRealICQContacterTreeView;
  164. var
  165. iIndex: Integer;
  166. begin
  167. Result := nil;
  168. iIndex := MainForm.ContacterTreeViews.IndexOf(AKey);
  169. if iIndex < 0 then
  170. Exit;
  171. Result := MainForm.ContacterTreeViews.Objects[iIndex] as TRealICQContacterTreeView;
  172. end;
  173. class function TBaseService.GetUserStatusJsons(
  174. AUsers: TStringList): TInterfaceList;
  175. var
  176. iLoop, iIndex, jLoop: Integer;
  177. AJo, AjoSimpleUser: ISuperObject;
  178. AJa: TSuperArray;
  179. AUser: TRealICQUser;
  180. AServerID: string;
  181. begin
  182. Result := TInterfaceList.Create;
  183. for iLoop := 0 to AUsers.Count - 1 do
  184. begin
  185. AJo := nil;
  186. AUser := AUsers.Objects[iLoop] as TRealICQUser;
  187. if AUser = nil then
  188. AUser := TUsersService.GetUsersService.GetOrRequestUser(AUsers[iLoop]);
  189. if not AUser.IsNeedRequestUserStatus then
  190. Continue;
  191. AServerID := GetServerID(AUser.LoginName);
  192. for jLoop := 0 to Result.Count - 1 do
  193. if SameText(AServerID, (Result[jLoop] as ISuperObject).S['s']) then
  194. begin
  195. AJo := Result[jLoop] as ISuperObject;
  196. if AJo.A['us'].Length > 200 then
  197. begin
  198. AJo := SO();
  199. AJo.S['s'] := AServerID;
  200. AJo.N['us'] := SO('[]');
  201. Result.Insert(0, AJo);
  202. end;
  203. Break;
  204. end;
  205. if AJo = nil then
  206. begin
  207. AJo := SO();
  208. AJo.S['s'] := AServerID;
  209. AJo.N['us'] := SO('[]');
  210. Result.Add(AJo);
  211. end;
  212. AJa := AJo.A['us'];
  213. AjoSimpleUser := SO();
  214. AjoSimpleUser.S['l'] := ClearServerID(AUser.LoginName);
  215. AjoSimpleUser.I['os'] := Integer(AUser.LoginState);
  216. AJa.Add(AjoSimpleUser);
  217. end;
  218. end;
  219. class procedure TBaseService.InitUserObject(AUser: TRealICQUser;
  220. AJo: ISuperObject);
  221. begin
  222. if (AUser = nil) or (AJo = nil) then
  223. Exit;
  224. AUser.LoginName := AJo.S['l'];
  225. AUser.Sex:= TRealICQSexType(AJo.I['sex']);
  226. AUser.Mobile := AJo.S['mob'];
  227. AUser.BranchID := AJo.S['bid'];
  228. AUser.Position := AJo.D['position'];
  229. AUser.Secret := TRealICQExInfoSecretLevel(AJo.I['secret']);
  230. AUser.CurrentExInfoVersion := AJo.I['v'];
  231. // AUser.Remark := AJo.O['remark'].AsString;
  232. AUser.ShortMobile := AJo.S['sMob'];
  233. AUser.Tel := AJo.S['tel'];
  234. AUser.Watchword := AJo.S['Watchword'];
  235. AUser.Email := AJo.S['email'];
  236. AUser.Branch := AJo.S['branch'];
  237. AUser.DisplayName := AJo.S['name'];
  238. AUser.TrueName := AUser.DisplayName;
  239. AUser.HeadImageFileType := TRealICQHeadImageFileType(AJo.I['iconType']);
  240. AUser.HeadImageHashValue := AJo.S['icon'];
  241. AUser.Duty := AJo.S['duty'];
  242. AUser.Company := AJo.S['comp'];
  243. AUser.OfficeID := AJo.S['officeID'];
  244. end;
  245. class procedure TBaseService.InitUserObject(AUser: TRealICQUser; AJson: string);
  246. var
  247. jo: ISuperObject;
  248. begin
  249. jo := SO(AJson);
  250. if jo = nil then
  251. begin
  252. Error('Json解析错误:' + AJson, 'TBaseService.InitUserObject');
  253. end
  254. else
  255. InitUserObject(AUser, jo);
  256. end;
  257. class function TBaseService.GetServerID(ALoginName: string): string;
  258. var
  259. iStart, iEnd, Len: Integer;
  260. begin
  261. Result := '';
  262. iEnd := AnsiPos('-', ALoginName);
  263. if iEnd < 0 then
  264. Exit;
  265. iStart := AnsiPos('+', ALoginName);
  266. if iStart < 0 then
  267. iStart := 0;
  268. if iEnd <= iStart + 1then
  269. Exit;
  270. Result := Copy(ALoginName, iStart + 1, iEnd - iStart - 1);
  271. end;
  272. class function TBaseService.GetAndClearServerID(var ALoginName: string): string;
  273. var
  274. iStart, iEnd, Len: Integer;
  275. begin
  276. Result := '';
  277. iEnd := AnsiPos('-', ALoginName);
  278. if iEnd < 0 then
  279. Exit;
  280. iStart := AnsiPos('+', ALoginName);
  281. if iStart < 0 then
  282. iStart := 0;
  283. if iEnd <= iStart + 1then
  284. Exit;
  285. Result := Copy(ALoginName, iStart + 1, iEnd - iStart - 1);
  286. ALoginName := Copy(ALoginName, iEnd + 1, Length(ALoginName) - iEnd);
  287. end;
  288. class function TBaseService.ClearServerID(ALoginName: string): string;
  289. var
  290. iStart: Integer;
  291. begin
  292. Result := ALoginName;
  293. iStart := AnsiPos('-', ALoginName);
  294. if iStart < 0 then
  295. Exit;
  296. Result := Copy(ALoginName, iStart + 1, Length(ALoginName) - iStart);
  297. end;
  298. class function TBaseService.GetLocalUID(ALoginName: string): string;
  299. begin
  300. Result := FullLoginName(MainForm.RealICQClient.CenterServerID, MainForm.RealICQClient.ServerID, ALoginName);
  301. end;
  302. class function TBaseService.FullLoginName(ACenterServerID, AServerID, ALoginName: string): string;
  303. var
  304. iStart, iEnd, Len: Integer;
  305. begin
  306. Result := ALoginName;
  307. if AServerID = '' then
  308. Exit;
  309. if AnsiPos('-', Result) <= 0 then
  310. begin
  311. if AnsiPos('+', Result) <= 0 then
  312. Result := AServerID + '-' + Result
  313. else
  314. Insert(AServerID + '-', Result, AnsiPos('+', Result) + 1);
  315. end;
  316. if ACenterServerID = '' then
  317. Exit;
  318. if AnsiPos('+', Result) <= 0 then
  319. Result := ACenterServerID + '+' + Result;
  320. end;
  321. function TBaseService.PacketAndSend(AProtocol: Byte; AJo: ISuperObject;
  322. ARealICQClient: TRealICQClient): Boolean;
  323. var
  324. nIndex,AJsonLength, ALength: Integer;
  325. SendBuffer: array of Byte;
  326. AJson: string;
  327. begin
  328. try
  329. Result := True;
  330. if AJo <> nil then
  331. AJson := AJo.AsJSon();
  332. AJsonLength := Length(AJson);
  333. ALength := 6 + Length(AJson);
  334. if (ALength > 4096) then
  335. begin
  336. Result := False;
  337. Exit;
  338. end;
  339. SetLength(SendBuffer, ALength);
  340. nIndex := 0;
  341. //填充 (1)协议类型(0xD4) 1byte
  342. SendBuffer[nIndex] := $D4;
  343. Inc(nIndex, 1);
  344. //填充 (2)消息总长度 2byte
  345. CopyMemory(@SendBuffer[nIndex], @ALength, 2);
  346. Inc(nIndex, 2);
  347. SendBuffer[nIndex] := AProtocol;
  348. Inc(nIndex, 1);
  349. //填充 (2)JSON总长度 2byte
  350. CopyMemory(@SendBuffer[nIndex], @AJsonLength, 2);
  351. Inc(nIndex, 2);
  352. //填充(4)JSON 动态长度
  353. CopyMemory(@SendBuffer[nIndex], PChar(AJson), AJsonLength);
  354. ARealICQClient.TCPClient.SendBuffer(SendBuffer[0], ALength);
  355. except
  356. on E: Exception do
  357. Log(E.Message, 'TUsersService.InitBuffer('+AJson+')');
  358. end;
  359. end;
  360. class function TBaseService.ToJsonObject(AUser: TRealICQUser): ISuperObject;
  361. var
  362. jo: ISuperObject;
  363. begin
  364. Result := nil;
  365. if (AUser = nil) or (AUser.LoginName = '') then
  366. Exit;
  367. jo := TSuperObject.Create;
  368. jo.S['l'] := AUser.LoginName;
  369. Jo.I['sex'] := Integer(AUser.Sex);
  370. Jo.S['mob'] := AUser.Mobile;
  371. Jo.S['bid'] := AUser.BranchID;
  372. Jo.D['position'] := AUser.Position;
  373. Jo.I['secret'] := Integer(AUser.Secret);
  374. Jo.I['v'] := AUser.CurrentExInfoVersion;
  375. // Jo.S['remark'] := AUser.Remark ;
  376. Jo.S['sMob'] := AUser.ShortMobile;
  377. Jo.S['tel'] := AUser.Tel;
  378. Jo.S['watchword'] := AUser.Watchword;
  379. Jo.S['email'] := AUser.Email;
  380. Jo.S['branch'] := AUser.Branch;
  381. Jo.S['name'] := AUser.DisplayName;
  382. Jo.I['iconType'] := Integer(AUser.HeadImageFileType);
  383. Jo.S['icon'] := AUser.HeadImageHashValue;
  384. Jo.S['duty'] := AUser.Duty;
  385. Jo.S['comp'] := AUser.Company;
  386. Jo.S['officeID'] := AUser.OfficeID;
  387. // jo.B['GettedExInfo'] := AUser.FGettedExInfo;
  388. // jo.B['GettedOffliceAutoResponseSet'] := AUser.GettedOffliceAutoResponseSet;
  389. Result := jo;
  390. end;
  391. end.