AppCentreCase.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. unit AppCentreCase;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, StdCtrls, AppsLayout, AppBtn, InterfaceAppCentre, AppCentreImport,
  6. LoggerImport, pngimage, DataProviderImport, cefvcl, IdHTTP, superobject,
  7. IdURI;
  8. type
  9. TAppCentreTestCase = class(TForm, IHotAppView)
  10. btnLogin: TButton;
  11. btnOpenAppCentre: TButton;
  12. btnLogout: TButton;
  13. btnIdHttp: TButton;
  14. dlgFind1: TFindDialog;
  15. btn1: TButton;
  16. dlgOpen1: TOpenDialog;
  17. btn2: TButton;
  18. edt1: TEdit;
  19. btnCanlian: TButton;
  20. edt2: TEdit;
  21. lbl1: TLabel;
  22. Label1: TLabel;
  23. btn3: TButton;
  24. procedure btnLoginClick(Sender: TObject);
  25. procedure FormCreate(Sender: TObject);
  26. procedure btnOpenAppCentreClick(Sender: TObject);
  27. procedure btnLogoutClick(Sender: TObject);
  28. procedure FormDestroy(Sender: TObject);
  29. procedure btnIdHttpClick(Sender: TObject);
  30. procedure btn1Click(Sender: TObject);
  31. procedure btn2Click(Sender: TObject);
  32. procedure btnCanlianClick(Sender: TObject);
  33. procedure btn3Click(Sender: TObject);
  34. private
  35. FAppsLayout: TAppsLayout;
  36. FDotPic: TGraphic;
  37. function CreateAppBtn(AHotApp: IHotApp): TAppBtn;
  38. procedure DestroyHotApps;
  39. procedure InitHttpHeader(AIdHttp: TIdHTTP);
  40. procedure SSO(Sender: TObject);
  41. function Authenticate(AAuthURL, AAppSecret: PChar): PChar;
  42. function DecodeAdapter(AData, AKey: string): string;
  43. procedure OnRedirect(Sender: TObject; var dest: string;
  44. var NumRedirect: Integer; var Handled: boolean;
  45. var VMethod: TIdHTTPMethod);
  46. function GetDefaultBrowser: string;
  47. public
  48. function AddHotApp(AHotApp: IHotApp): Boolean; stdcall;
  49. function RemoveHotApp(AHotApp: IHotApp): Boolean; stdcall;
  50. procedure OnASyncAppIconDownloaded(AAppKey: AnsiString; const ABuffer; const ACount: Integer); stdcall;
  51. procedure InitHotApps;
  52. end;
  53. TAuthIdHTTP = class(TIdHTTP)
  54. private
  55. FAccessToken: string;
  56. FClientKey: string;
  57. public
  58. property AccessToken: string read FAccessToken write FAccessToken;
  59. property ClientKey: string read FClientKey write FClientKey;
  60. end;
  61. var
  62. AppCentreTestCase: TAppCentreTestCase;
  63. implementation
  64. uses
  65. StrUtils, XXTEA, System.Win.Registry, Winapi.ShellAPI;
  66. {$R *.dfm}
  67. function TAppCentreTestCase.AddHotApp(AHotApp: IHotApp): Boolean;
  68. var
  69. AAppBtn: TAppBtn;
  70. begin
  71. AAppBtn := CreateAppBtn(AHotApp);
  72. FAppsLayout.AddAppBtn(AAppBtn);
  73. FAppsLayout.Layout;
  74. end;
  75. procedure TAppCentreTestCase.InitHttpHeader(AIdHttp: TIdHTTP);
  76. var
  77. openIDEx: string;
  78. begin
  79. // openIDEx := 'Basic '+ EncodeString('12312');
  80. // AIdHttp.Request.Accept := '';
  81. // AIdHttp.Request.CustomHeaders.Values['Authorization'] := openIDEx;
  82. // AIdHttp.Request.CustomHeaders.Values['Content-Type'] := 'application/x-www-form-urlencoded';
  83. end;
  84. procedure TAppCentreTestCase.btn1Click(Sender: TObject);
  85. begin
  86. if dlgOpen1.Execute(Handle) then
  87. begin
  88. ShowMessage(dlgOpen1.FileName);
  89. end;
  90. end;
  91. function SplitString(const Source,Ch:string):TStringList;
  92. var
  93. Temp: string;
  94. iLoop: Integer;
  95. begin
  96. Result := TStringList.Create;
  97. Temp := Source;
  98. iLoop := Pos(Ch, Source);
  99. while iLoop <> 0 do
  100. begin
  101. Result.Add(copy(temp, 0, iLoop-1));
  102. Delete(temp, 1, iLoop);
  103. iLoop := Pos(Ch, Temp);
  104. end;
  105. Result.Add(temp);
  106. end;
  107. procedure TAppCentreTestCase.OnRedirect(Sender: TObject; var dest: string; var NumRedirect: Integer; var Handled: boolean; var VMethod: TIdHTTPMethod);
  108. const
  109. TOKEN: string = 'access_token=';
  110. CLIENT_KEY: string = 'clientkey=';
  111. var
  112. AParamsStr: string;
  113. AParams: TStrings;
  114. iStart, iEnd, iCount, i: Integer;
  115. begin
  116. if NumRedirect = 3 then
  117. begin
  118. AParamsStr := dest;
  119. AParams := AppCentreCase.SplitString(AParamsStr, '&');
  120. try
  121. for i := 0 to AParams.Count - 1 do
  122. begin
  123. iStart := Pos(TOKEN, AParams[i]);
  124. if iStart > 0 then
  125. begin
  126. Inc(iStart, Length(TOKEN));
  127. (Sender as TAuthIdHTTP).FAccessToken := Copy(AParams[i], iStart);
  128. Break;
  129. end;
  130. end;
  131. for i := 0 to AParams.Count - 1 do
  132. begin
  133. iStart := Pos(CLIENT_KEY, AParams[i]);
  134. if iStart > 0 then
  135. begin
  136. Inc(iStart, Length(CLIENT_KEY));
  137. (Sender as TAuthIdHTTP).FClientKey := Copy(AParams[i], iStart);
  138. Break;
  139. end;
  140. end;
  141. finally
  142. AParams.Free;
  143. end;
  144. end;
  145. end;
  146. function HexToStr(const Str: AnsiString): AnsiString;
  147. asm
  148. push ebx
  149. push edi
  150. push esi
  151. test eax,eax //为空串
  152. jz @@Exit
  153. mov edi,eax
  154. mov esi,edx
  155. mov edx,[eax-4]
  156. test edx,edx
  157. je @@Exit
  158. mov ecx,edx
  159. push ecx
  160. shr edx,1
  161. mov eax,esi //开始构造字符串
  162. {$IFDEF VER210}
  163. movzx ecx, word ptr [edi-12] {需要设置CodePage}
  164. {$ENDIF}
  165. call System.@LStrSetLength //设置新串长度
  166. mov eax,esi //新字符串地址
  167. Call UniqueString //产生一个唯一的新字符串,串位置在eax中
  168. Pop ecx
  169. xor ebx,ebx
  170. xor esi,esi
  171. @@CharFromHex:
  172. xor edx,edx
  173. mov dl, [edi] //Str字符串字符
  174. cmp dl, '0' //查看是否在0到f之间的字符
  175. JB @@Exit //小于0,退出
  176. cmp dl,'9' //小于=9
  177. ja @@DoChar//CompOkNum
  178. sub dl,'0'
  179. jmp @@DoConvert
  180. @@DoChar:
  181. //先转成大写字符
  182. and dl,$DF
  183. cmp dl,'F'
  184. ja @@Exit //大于F退出
  185. add dl,10
  186. sub dl,'A'
  187. @@DoConvert: //转化
  188. inc ebx
  189. cmp ebx,2
  190. je @@Num1
  191. xor esi,esi
  192. shl edx,4
  193. mov esi,edx
  194. jmp @@Num2
  195. @@Num1:
  196. add esi,edx
  197. mov edx,esi
  198. mov [eax],dl
  199. xor ebx,ebx
  200. inc eax
  201. @@Num2:
  202. dec ecx
  203. inc edi
  204. test ecx,ecx
  205. jnz @@CharFromHex
  206. @@Exit:
  207. pop esi
  208. pop edi
  209. pop ebx
  210. end;
  211. function TAppCentreTestCase.DecodeAdapter(AData, AKey: string): string;
  212. const
  213. TICKET_TAG: string = 'ticket=';
  214. EXTEND_TAG: string = 'extend=';
  215. ID_TAG: string = 'id=';
  216. var
  217. AResults: TStrings;
  218. AResultStr, ATicket, AExtend, AID: string;
  219. iStart, iEnd, iCount, i: Integer;
  220. jo: ISuperObject;
  221. begin
  222. AResultStr := (DecryptNoHex(HexToStr(AData), AKey));
  223. AResultStr := UTF8Encode(AResultStr);
  224. if AResultStr = '' then
  225. begin
  226. ShowMessage('错误:解密失败.');
  227. Exit;
  228. end;
  229. AResults := SplitString(AResultStr, '&');
  230. try
  231. for i := 0 to AResults.Count - 1 do
  232. begin
  233. iStart := Pos(TICKET_TAG, AResults[i]);
  234. if iStart > 0 then
  235. begin
  236. Inc(iStart, Length(TICKET_TAG));
  237. ATicket := Copy(AResults[i], iStart);
  238. Break;
  239. end;
  240. end;
  241. for i := 0 to AResults.Count - 1 do
  242. begin
  243. iStart := Pos(EXTEND_TAG, AResults[i]);
  244. if iStart > 0 then
  245. begin
  246. Inc(iStart, Length(EXTEND_TAG));
  247. AExtend := Copy(AResults[i], iStart);
  248. Break;
  249. end;
  250. end;
  251. for i := 0 to AResults.Count - 1 do
  252. begin
  253. iStart := Pos(ID_TAG, AResults[i]);
  254. if iStart > 0 then
  255. begin
  256. Inc(iStart, Length(ID_TAG));
  257. AID := Copy(AResults[i], iStart);
  258. Break;
  259. end;
  260. end;
  261. if (AID = '') or (ATicket = '') then
  262. begin
  263. ShowMessage('错误:解密失败,找不到关键信息.');
  264. Exit;
  265. end;
  266. jo := SO('{}');
  267. jo.I['ticket'] := StrToInt64(ATicket);
  268. jo.S['id'] := AID;
  269. jo.S['extend'] := AExtend;
  270. Result := jo.AsJSon();
  271. finally
  272. AResults.Free;
  273. end;
  274. end;
  275. function TAppCentreTestCase.Authenticate(AAuthURL, AAppSecret: PChar): PChar;
  276. const
  277. GET_OPENKEY: string = 'http://%s:%s/api/oauth/me?access_token=%s';
  278. var
  279. AIdHttp: TAuthIdHTTP;
  280. AURL, AToken, AClientKey,
  281. ASecret,
  282. AOpenKey: string;
  283. AHost: string;
  284. joStr: string;
  285. jo: ISuperObject;
  286. AIDURL: TIdURI;
  287. begin
  288. AURL := string(AAuthURL);
  289. ASecret := string(AAppSecret);
  290. AIdHttp := TAuthIdHTTP.Create(nil);
  291. AIDURL := TIdURI.Create(AURL);
  292. try
  293. AIdHttp.RedirectMaximum := 5;
  294. AIdHttp.HandleRedirects := True;
  295. AIdHttp.OnRedirect := OnRedirect;
  296. AIdHttp.Get(AURL);
  297. if (Length(AIdHttp.FAccessToken) = 0) or (Length(AIdHttp.FAccessToken) <> 32) then
  298. begin
  299. ShowMessage('错误:没有获取到通行证或不是有效的通行证,可能是因为认证链接已经过期.');
  300. Exit;
  301. end;
  302. if (Length(AIdHttp.FClientKey) = 0) then
  303. begin
  304. ShowMessage('错误:您还没有绑定该应用的账号,请联系管理员绑定.');
  305. Exit;
  306. end;
  307. AClientKey := AIdHttp.FClientKey;
  308. AToken := AIdHttp.FAccessToken;
  309. AURL := Format(GET_OPENKEY, [AIDURL.Host, AIDURL.Port, AToken]);
  310. joStr := Utf8ToAnsi(AIdHttp.Get(AURL));
  311. if joStr = '' then
  312. begin
  313. ShowMessage('错误:不能获取OpenKey.');
  314. Exit;
  315. end;
  316. jo := SO(joStr);
  317. if jo = nil then
  318. begin
  319. ShowMessage('错误:OpenKey格式错误.');
  320. Exit;
  321. end;
  322. AOpenKey := jo.S['openkey'];
  323. if AOpenKey = '' then
  324. begin
  325. ShowMessage('错误:OpenKey为null.');
  326. Exit;
  327. end;
  328. try
  329. Result := PChar(DecodeAdapter(AClientKey, (string(ASecret) + AOpenKey)));
  330. except
  331. on Ex: Exception do
  332. begin
  333. ShowMessage('错误:解密异常,' + Ex.Message + '.');
  334. Result := nil;
  335. end;
  336. end;
  337. finally
  338. AIdHttp.Free;
  339. AIDURL.Free;
  340. end;
  341. end;
  342. procedure TAppCentreTestCase.btnIdHttpClick(Sender: TObject);
  343. var
  344. AIdHttp:TIdHTTP;
  345. ResponeStr: String;
  346. begin
  347. AIdHttp := TIdHTTP.Create(nil);
  348. InitHttpHeader(AIdHttp);
  349. try
  350. ResponeStr:=AIdHttp.Get('http://www.baidu.com');
  351. ResponeStr := UTF8Decode(ResponeStr);
  352. // FHotApps := ProcessHotAppJsonStr(ResponeStr);
  353. // Result := FHotApps;
  354. except
  355. on E: Exception do
  356. begin
  357. // FHotApps := nil;
  358. // Result := nil;
  359. Freeandnil(AIdHttp);
  360. end;
  361. end;
  362. Freeandnil(AIdHttp);
  363. end;
  364. procedure TAppCentreTestCase.btnLoginClick(Sender: TObject);
  365. begin
  366. GetDataModule.Install('lqq', ExtractFilePath(ParamStr(0)) + 'Users\Data\');
  367. GetAppCentre.Login('lqq', Self);
  368. InitHotApps;
  369. end;
  370. procedure TAppCentreTestCase.btnLogoutClick(Sender: TObject);
  371. begin
  372. DestroyHotApps;
  373. GetAppCentre.Logout;
  374. GetDataModule.Uninstall;
  375. end;
  376. procedure TAppCentreTestCase.btnOpenAppCentreClick(Sender: TObject);
  377. begin
  378. GetAppCentre.OpenAppCentreFrom;
  379. GetAppCentre.OpenWebDebuggerTool;
  380. end;
  381. function TAppCentreTestCase.CreateAppBtn(AHotApp: IHotApp): TAppBtn;
  382. var
  383. AStream: TMemoryStream;
  384. pngObj: TPNGObject;
  385. ABuffer: TBytes;
  386. begin
  387. Result := TAppBtn.Create(Self);
  388. with Result do
  389. begin
  390. Parent := Self;
  391. BtnStyle := absSmall;
  392. AppModel := AHotApp;
  393. DotPicture.Assign(FDotPic);
  394. Caption := (AHotApp.GetUserApp.GetTitle);
  395. ShowHint := True;
  396. end;
  397. Result.OnClick := SSO;
  398. DataProviderImport.GetAppIconProvider.FindIcon(AHotApp.GetUserApp.GetAppKey, ABuffer);
  399. if Length(ABuffer) = 0 then
  400. Result.AppIcon.LoadFromFile('Images\DefaultApp.png')
  401. else
  402. begin
  403. Debug('获取到ICON'+AHotApp.GetUserApp.GetAppKey, 'TMainFrmFooter.CreateAppBtn');
  404. AStream := TMemoryStream.Create;
  405. AStream.Write(ABuffer[0], Length(ABuffer));
  406. pngObj := TPNGObject.Create;
  407. AStream.Position := 0;
  408. pngObj.LoadFromStream(AStream);
  409. Result.AppIcon.Assign(pngObj);
  410. FreeAndNil(pngObj);
  411. FreeAndNil(AStream);
  412. end;
  413. end;
  414. procedure TAppCentreTestCase.FormCreate(Sender: TObject);
  415. begin
  416. FAppsLayout := TAppsLayout.Create;
  417. FAppsLayout.Col := 8;
  418. FAppsLayout.Row := 1;
  419. FAppsLayout.ClientRect := Rect(30, 2, 238, 32);
  420. FDotPic := TPNGObject.Create;
  421. FDotPic.LoadFromFile('Images\Dot.png');
  422. end;
  423. procedure TAppCentreTestCase.FormDestroy(Sender: TObject);
  424. var
  425. AAppBtn: TAppBtn;
  426. begin
  427. FreeAndNil(FAppsLayout);
  428. FreeAndNil(FDotPic);
  429. end;
  430. procedure TAppCentreTestCase.InitHotApps;
  431. var
  432. AAppBtn: TAppBtn;
  433. AList: IInterfaceList;
  434. iLoop: Integer;
  435. begin
  436. AList := GetAppCentre.GetHotApps;
  437. if AList = nil then
  438. Exit;
  439. for iLoop := 0 to AList.Count - 1 do
  440. begin
  441. AAppBtn := CreateAppBtn((AList[iLoop] as IHotApp));
  442. FAppsLayout.AddAppBtn(AAppBtn);
  443. end;
  444. FAppsLayout.Layout;
  445. end;
  446. procedure TAppCentreTestCase.DestroyHotApps;
  447. var
  448. AAppBtn: TAppBtn;
  449. AList: TStringList;
  450. iLoop: Integer;
  451. begin
  452. AList := FAppsLayout.Apps;
  453. if AList = nil then
  454. Exit;
  455. while AList.Count > 0 do
  456. begin
  457. AAppBtn := AList.Objects[0] as TAppBtn;
  458. AList.Delete(0);
  459. FreeAndNil(AAppBtn);
  460. end;
  461. FAppsLayout.Layout;
  462. end;
  463. procedure TAppCentreTestCase.OnASyncAppIconDownloaded(AAppKey: AnsiString;
  464. const ABuffer; const ACount: Integer);
  465. var
  466. iLoop: Integer;
  467. AAppBtn: TAppBtn;
  468. AHotApp: IHotApp;
  469. pngObj: TPNGObject;
  470. AStream: TStream;
  471. begin
  472. for iLoop := 0 to FAppsLayout.Apps.Count - 1 do
  473. begin
  474. AAppBtn := FAppsLayout.Apps.Objects[iLoop] as TAppBtn;
  475. AHotApp := AAppBtn.AppModel as IHotApp;
  476. if SameText(AHotApp.GetUserApp.GetAppKey, AAppKey) then
  477. begin
  478. AStream := TMemoryStream.Create;
  479. try
  480. pngObj := TPNGObject.Create;
  481. AStream.Position := 0;
  482. AStream.Write(TBytes(ABuffer)[0], ACount);
  483. AStream.Position := 0;
  484. pngObj.LoadFromStream(AStream);
  485. AAppBtn.AppIcon.Assign(pngObj);
  486. AAppBtn.Invalidate;
  487. FreeAndNil(pngObj);
  488. finally
  489. AStream.Free;
  490. end;
  491. Exit;
  492. end;
  493. end;
  494. end;
  495. function TAppCentreTestCase.RemoveHotApp(AHotApp: IHotApp): Boolean;
  496. var
  497. iLoop: Integer;
  498. AAppBtn: TAppBtn;
  499. AOldHotApp: IHotApp;
  500. begin
  501. for iLoop := 0 to FAppsLayout.Apps.Count - 1 do
  502. begin
  503. AAppBtn := FAppsLayout.Apps.Objects[iLoop] as TAppBtn;
  504. AOldHotApp := AAppBtn.AppModel as IHotApp;
  505. if SameText(AOldHotApp.GetUserApp.GetAppKey, AHotApp.GetUserApp.GetAppKey) then
  506. begin
  507. FAppsLayout.RemoveAppBtn(AAppBtn);
  508. FreeAndNil(AAppBtn);
  509. Exit;
  510. end;
  511. end;
  512. end;
  513. procedure TAppCentreTestCase.SSO(Sender: TObject);
  514. var
  515. AHotApp: IHotApp;
  516. begin
  517. if (Sender = nil) or not (Sender is TAppBtn) then
  518. Exit;
  519. AHotApp := (Sender as TAppBtn).AppModel as IHotApp;
  520. GetAppCentre.SSO(AHotApp.GetUserApp.GetAppKey);
  521. end;
  522. procedure TAppCentreTestCase.btn2Click(Sender: TObject);
  523. begin
  524. // Authenticate(PChar(edt1.Text), PChar('B0F0E1308C2111EF92E995795A3DED42'))
  525. ShowMessage(DecryptNoHex(HexToStr(
  526. '48BE536245C22280C1F1899C9C28D0E7646F267CA61C4DFBAD0107A7FF33E22A83A5D2394F8FD405C26FF401'),
  527. 'B0F0E1308c2111EF92E995795A3DED427AE31A14DEB647059A31073F1D641752'));
  528. end;
  529. procedure TAppCentreTestCase.btn3Click(Sender: TObject);
  530. var
  531. AHandle: Cardinal;
  532. ShExecInfo: SHELLEXECUTEINFO;
  533. ADir: PAnsiChar;
  534. ADlgOpen: TOpenDialog;
  535. ADirStr: string;
  536. begin
  537. ShExecInfo.cbSize := SizeOf(SHELLEXECUTEINFO);
  538. ShExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
  539. ShExecInfo.Wnd := 0;
  540. ShExecInfo.lpVerb := nil;
  541. ShExecInfo.lpFile := PChar(GetDefaultBrowser);
  542. ShExecInfo.lpParameters := PChar('http://www.baidu.com');
  543. // ShExecInfo.lpDirectory := PChar(ExtractFilePath(string(ADir)));
  544. ShExecInfo.nShow := SW_SHOW;
  545. ShExecInfo.hInstApp := 0;
  546. ShellExecuteEx(@ShExecInfo);
  547. end;
  548. function TAppCentreTestCase.GetDefaultBrowser: string;
  549. var
  550. reg: TRegistry;
  551. begin
  552. reg := TRegistry.Create;
  553. try
  554. reg.RootKey := HKEY_CLASSES_ROOT;
  555. reg.OpenKey('http\\shell\\open\\command',false);
  556. result:=reg.ReadString('');
  557. result:=Copy(result,Pos('"',result)+1,Length(result)-1);
  558. result:=Copy(result,1,Pos('"',result)-1);
  559. reg.CloseKey;
  560. finally
  561. if (result='') then result:='IEXPLORE.EXE';
  562. reg.Free;
  563. end;
  564. end;
  565. procedure TAppCentreTestCase.btnCanlianClick(Sender: TObject);
  566. begin
  567. if GetAppCentre.AuthFromCanlian(edt1.Text, edt2.Text) then
  568. ShowMessage('login sucessfully!')
  569. else
  570. ShowMessage('Password error!');
  571. end;
  572. end.