UAppCentre.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. unit UAppCentre;
  2. interface
  3. uses
  4. mybean.core.objects, Classes, SysUtils, StrUtils, superobject, Dialogs,
  5. DateUtils, InterfaceAppCentre, Graphics,IdHttp,IdHttpEx,EncdDecd,XXTEA,
  6. Forms, IconRequest, TypInfo, UAppCentreConfig, InterfaceUI;
  7. type
  8. TAppCentre = class(TMyBeanInterfacedObject, IAppCentre)
  9. private
  10. FLogined: Boolean;
  11. FLoginName: string;
  12. FOpenID: string;
  13. FAppKey :string;
  14. FAppSecret :string;
  15. FEnable: Boolean;
  16. FApps: IInterfaceList;
  17. FHotApps: IInterfaceList;
  18. FMainApplication: TApplication;
  19. FHotAppView: IHotAppView;
  20. FDownloadThread: TIconRequest;
  21. FHandles: TList;
  22. function URLEncode(const S: string; const InQueryString: Boolean): string;
  23. function ProcessAppJsonStr(AResponeStr: string): IInterfaceList;
  24. function ProcessHotAppJsonStr(AResponeStr: string): IInterfaceList;
  25. function ParseApp(AJsonObject: ISuperObject): IApp;
  26. procedure InitHttpHeader(AIdHttp: TIdHTTP);
  27. procedure GetOpenID(ALoginName: string);
  28. function GetDefaultBrowser: string;
  29. procedure DownloadIcon(AApps: IInterfaceList);
  30. procedure OnDownloadedIcon(AAppKey: string; AStream: TStream);
  31. procedure ExecuteApp(AURL: string; AApp: IApp);
  32. procedure OnRedirect(Sender: TObject; var dest: string;
  33. var NumRedirect: Integer; var Handled: boolean;
  34. var VMethod: string);
  35. function Find(AAppkey: string): IApp;
  36. procedure ExecuteBS(AURL: string);
  37. function GetPublicKey(AApp: IApp): string;
  38. function RSAEncrypt(APublicKey, AData: string): string;
  39. public
  40. destructor Destroy; override;
  41. constructor Create; override;
  42. function Login(ALoginName: WideString; AHotAppView: IHotAppView):WideString; stdcall;
  43. procedure Logout; stdcall;
  44. function GetHotApps: IInterfaceList; stdcall;
  45. function GetAllApps: IInterfaceList; stdcall;
  46. function RemoveHotApp(AAppKey: AnsiString): Boolean; stdcall;
  47. function AddHotApp(AAppKey: AnsiString; AHotApp: IHotApp): Boolean; stdcall;
  48. function GetEnable(): Boolean; stdcall;
  49. function GetAppCentreURL: AnsiString; stdcall;
  50. procedure OpenAppCentreFrom; stdcall;
  51. procedure CloseAppCentreForm; stdcall;
  52. procedure ChangeUIColor(AColor: TColor); stdcall;
  53. procedure Hello; stdcall;
  54. procedure SSO(AAppKey: AnsiString; AUrl: AnsiString = ''); stdcall;
  55. procedure SetMainApplication(AMainApplication: TApplication); stdcall;
  56. procedure OpenWebDebuggerTool; stdcall;
  57. procedure CloseWebDebuggerTool; stdcall;
  58. procedure OpenMessageCentre(ALoginName, APassword: WideString); stdcall;
  59. /// <summary>
  60. /// 残联定制
  61. /// </summary>
  62. function AuthFromCanlian(ALoginName, APassword: WideString): Boolean; stdcall;
  63. end;
  64. {$INCLUDE ..\Client\LXTalk.inc}
  65. implementation
  66. uses
  67. LoggerImport, AppCentreForm, UApp, ShellApi, Registry, Windows, DataProviderImport,
  68. RealICQUtility,
  69. MD5_32, AppCentreImport, AppCentreCom_TLB, Types, IdURI, MessageCentre, UIHandler;
  70. const
  71. APPICON_DIR: string = 'Users\AppIcon';
  72. LOGIN_URL: string = 'http://%s:%d/api/basic/openid?appKey=%s&appSecret=%s&loginName=%s';
  73. GETHOTAPPS_URL : string = 'http://%s:%d/api/service/userfeaturedapps?status=1';
  74. GETAPPS_URL : string = 'http://%s:%d/api/service/applist?key=';
  75. SSO_URL: string = 'http://%s:%d/home/jump?appid=%s&clientuin=%s&clientkey=%s&redirect_uri=%s&toappid=%s&message=%s';
  76. APPCENTRE_LOGIN_URL: string = 'http://%s:%d/Account/Login?appKey=%s&ticket=%s';
  77. CANLIAN_AUTH_URL: string = 'http://%s:%d/api/basic/ValiAccount?Username=%s&Password=%s';
  78. MESSAGE_CENTRE_URL: string = 'http://%s:%d/account/sso?LoginName=%s&Password=%s&URL=/Widgets/home/index';
  79. //xxtea(appsecret,openid=openid&timestamp=14774832748)
  80. PUBLIC_KEY_URL: string = 'http://%s:%d/home/GenerationKey?appid=%s&ticket=%s';
  81. var
  82. config: TAppCentreConfig;
  83. { TAppCentre }
  84. function TAppCentre.AddHotApp(AAppKey: AnsiString; AHotApp: IHotApp): Boolean;
  85. var
  86. WSAppKey: string;
  87. iLoop: Integer;
  88. AApp: IApp;
  89. begin
  90. Result := False;
  91. WSAppKey := string(AAppKey);
  92. if (FApps = nil) or (FHotApps = nil) or (AHotApp = nil) then
  93. Exit;
  94. AApp := nil;
  95. for iLoop := 0 to FHotApps.Count - 1 do
  96. if SameText(string((FHotApps[iLoop] as IHotApp).GetUserApp.GetAppKey), AAppKey) then
  97. begin
  98. AApp := (FHotApps[iLoop] as IHotApp).GetUserApp;
  99. Break;
  100. end;
  101. if AApp <> nil then
  102. Exit;
  103. AApp := nil;
  104. for iLoop := 0 to FApps.Count - 1 do
  105. if SameText(string((FApps[iLoop] as IApp).GetAppKey), AAppKey) then
  106. begin
  107. AApp := (FApps[iLoop] as IApp);
  108. Break;
  109. end;
  110. if AApp = nil then
  111. Exit;
  112. AHotApp.SetUserApp(AApp);
  113. FHotApps.Add(AHotApp);
  114. if FHotAppView <> nil then
  115. FHotAppView.AddHotApp(AHotApp);
  116. Result := True;
  117. end;
  118. function TAppCentre.AuthFromCanlian(ALoginName, APassword: WideString): Boolean;
  119. var
  120. AURL, ARep, WLoginName, WPassword: string;
  121. AConfig: IAppCentreConfig;
  122. AIdHttp: TIdHTTP;
  123. begin
  124. AConfig := GetAppCentreConfig;
  125. WLoginName := string(ALoginName);
  126. WPassword := string(APassword);
  127. AURL := Format(CANLIAN_AUTH_URL, [AConfig.GetIP, AConfig.GetPort, WLoginName, WPassword]);
  128. AIdHttp := TIdHTTP.Create(nil);
  129. Result := False;
  130. AIdHttp.Request.Accept := '';
  131. AIdHttp.Request.ContentType := 'application/json; charset=utf-8';
  132. try
  133. AURL := AIdHttp.URL.ParamsEncode(AURL);
  134. ARep := AIdHttp.Get(AURL);
  135. Result := StrToBoolDef(ARep, False);
  136. finally
  137. Freeandnil(AIdHttp);
  138. end;
  139. end;
  140. procedure TAppCentre.ChangeUIColor(AColor: TColor);
  141. begin
  142. // if GetAppCentreConfig.GetShowType = stOnMainform then
  143. // Exit;
  144. if AppCentreFrm = nil then
  145. begin
  146. AppCentreFrm := TAppCentreForm.Create(nil);
  147. end;
  148. AppCentreFrm.ChangeUIColor(AColor);
  149. end;
  150. procedure TAppCentre.CloseAppCentreForm;
  151. begin
  152. if AppCentreFrm <> nil then
  153. begin
  154. AppCentreFrm.Close();
  155. end;
  156. end;
  157. procedure TAppCentre.CloseWebDebuggerTool;
  158. begin
  159. if AppCentreFrm = nil then
  160. Exit;
  161. end;
  162. constructor TAppCentre.Create;
  163. begin
  164. inherited;
  165. FHandles := TList.Create;
  166. end;
  167. destructor TAppCentre.Destroy;
  168. begin
  169. while FHandles.Count > 0 do
  170. begin
  171. TerminateProcess(Cardinal(FHandles[0]), 0);
  172. FHandles.Delete(0);
  173. end;
  174. FreeAndNil(FHandles);
  175. // if AppCentreFrm <> nil then
  176. // FreeAndNil(AppCentreFrm);
  177. inherited;
  178. end;
  179. procedure TAppCentre.DownloadIcon(AApps: IInterfaceList);
  180. begin
  181. if Assigned(FDownloadThread) then
  182. FreeAndNil(FDownloadThread);
  183. FDownloadThread := TIconRequest.Create(AApps);
  184. FDownloadThread.OnCompleted := OnDownloadedIcon;
  185. end;
  186. procedure TAppCentre.GetOpenID(ALoginName: string);
  187. var
  188. AIdHttp : TIdHTTP;
  189. AOpenID : string;
  190. AConfig: IAppCentreConfig;
  191. AURL: string;
  192. begin
  193. FOpenID := '';
  194. AConfig := GetAppCentreConfig;
  195. FLoginName := ALoginName;
  196. AIdHttp := TIdHTTP.Create(nil);
  197. AIdHttp.Request.Accept := '';
  198. AIdHttp.Request.ContentType := 'application/json; charset=utf-8';
  199. try
  200. AURL := Format(LOGIN_URL, [AConfig.GetIP, AConfig.GetPort, AConfig.GetLxtAppKey, AConfig.GetLxtAppSecret, FloginName]);
  201. AOpenID := AIdHttp.Get(AURL);
  202. AOpenID := UTF8Decode(AOpenID);
  203. AOpenID := Copy(AOpenID,2, Length(AOpenID) - 2);
  204. FOpenID := AOpenID;
  205. except
  206. on E: Exception do
  207. begin
  208. FOpenID := '';
  209. Freeandnil(AIdHttp);
  210. LoggerImport.Error(E.Message, 'GetOpenID');
  211. end;
  212. end;
  213. Freeandnil(AIdHttp);
  214. end;
  215. function TAppCentre.GetAppCentreURL: AnsiString;
  216. const
  217. GET_DATA: string = '{"openID":"%s","timestamp":%d}';
  218. var
  219. AAnsiStrTicket: AnsiString;
  220. AURL: string;
  221. AContent: string;
  222. AConfig: IAppCentreConfig;
  223. //Cardinal
  224. begin
  225. AConfig := GetAppCentreConfig;
  226. AContent := Format(GET_DATA, [FOpenID, (DateTimeToUnix(Now) - 8 * 60 * 60)]);
  227. AAnsiStrTicket := Encrypt(
  228. AContent,
  229. AConfig.GetLxtAppSecret+AConfig.GetLxtAppKey);
  230. AURL := Format(APPCENTRE_LOGIN_URL, [
  231. AConfig.GetClientIP,
  232. AConfig.GetClientPort,
  233. AConfig.GetLxtAppKey,
  234. AAnsiStrTicket
  235. ]);
  236. Result := AnsiString(AURL);
  237. end;
  238. procedure TAppCentre.Hello;
  239. begin
  240. ShowMessage('TAppCentre.Hello');
  241. end;
  242. procedure TAppCentre.InitHttpHeader(AIdHttp: TIdHTTP);
  243. var
  244. openIDEx: string;
  245. begin
  246. openIDEx := 'Basic '+EncodeString(FOpenID);
  247. AIdHttp.Request.Accept := '';
  248. AIdHttp.Request.CustomHeaders.Values['Authorization'] := openIDEx;
  249. AIdHttp.Request.CustomHeaders.Values['Content-Type'] := 'application/x-www-form-urlencoded; charset=utf-8';
  250. end;
  251. function TAppCentre.ParseApp(AJsonObject: ISuperObject): IApp;
  252. var
  253. App: TApp;
  254. joApp: ISuperObject;
  255. begin
  256. App := TApp.Create;
  257. App.ID := AJsonObject.I['ID'];
  258. App.AppID := AJsonObject.I['AppID'];
  259. joApp := AJsonObject.O['Application'];
  260. if joApp = nil then
  261. begin
  262. Result := App;
  263. LoggerImport.Error(Format('AppID:%d', [App.AppID]), 'TAppCentre.ParseApp');
  264. Exit;
  265. end;
  266. App.Icon := (joApp.S['Icon']);
  267. App.Title := (joApp.S['Title']);
  268. App.Code := (joApp.S['Code']);
  269. App.Url := (joApp.S['Uri']);
  270. App.CallBackUri := (joApp.S['CallBackUri']);
  271. App.AppKey := (joApp.S['AppKey']);
  272. App.AppSecret := (joApp.S['AppSecret']);
  273. App.Style := joApp.I['Style'];
  274. App.AppStatus := joApp.I['Status'];
  275. App.UserID := AJsonObject.I['UserID'];
  276. App.AccessToken := (AJsonObject.S['AccessToken']);
  277. App.AccessTokenExpired:= (AJsonObject.S['AccessTokenExpired']);
  278. App.RefreshToken := (AJsonObject.S['RefreshToken']);
  279. App.OpenID := (AJsonObject.S['OpenID']);
  280. App.OpenKey := (AJsonObject.S['OpenKey']);
  281. App.Scope := (AJsonObject.S['Scope']);
  282. App.UserStatus := AJsonObject.I['Status'];
  283. App.IsAccessTokenValid:= AJsonObject.B['IsAccessTokenValid'];
  284. App.Embedded := joApp.B['Embedded'];
  285. Result := App;
  286. end;
  287. function TAppCentre.ProcessAppJsonStr(AResponeStr: string): IInterfaceList;
  288. var
  289. jo: ISuperObject;
  290. ja: TSuperArray;
  291. App :IApp;
  292. iLoop: Integer;
  293. begin
  294. Result := TInterfaceList.Create;
  295. jo := SO(AResponeStr);
  296. if jo = nil then
  297. begin
  298. LoggerImport.Error('JSON解析有问题', 'ProcessAppJsonStr');
  299. Exit;
  300. end;
  301. ja := SO(AResponeStr).AsArray;
  302. for iLoop := 0 to ja.Length - 1 do
  303. begin
  304. App := ParseApp(ja.O[iLoop]);
  305. Result.Add(App);
  306. end;
  307. end;
  308. function TAppCentre.ProcessHotAppJsonStr(AResponeStr: string): IInterfaceList;
  309. var
  310. ja: TSuperArray;
  311. joHotApp: ISuperObject;
  312. AHotApp: THotApp;
  313. iLoop, jLoop: Integer;
  314. begin
  315. ja := SO(AResponeStr).AsArray;
  316. Result := TInterfaceList.Create;
  317. for iLoop := 0 to ja.Length - 1 do
  318. begin
  319. AHotApp := THotApp.Create;
  320. joHotApp := ja.O[iLoop];
  321. AHotApp.ID := joHotApp.I['ID'];
  322. AHotApp.UserAppID := joHotApp.I['UserAppID'];
  323. AHotApp.Position := joHotApp.I['Position'];
  324. AHotApp.Status := joHotApp.I['Status'];
  325. AHotApp.OpenID := joHotApp.S['OpenID'];
  326. AHotApp.CreateDate := joHotApp.S['CreateDate'];
  327. if (joHotApp.O['UserApp'] <> nil) then
  328. AHotApp.UserApp := ParseApp(joHotApp.O['UserApp']);
  329. if AHotApp.UserApp = nil then
  330. begin
  331. for jLoop := 0 to FApps.Count - 1 do
  332. begin
  333. if ((FApps[jLoop] as IApp).GetAppID = AHotApp.UserAppID) then
  334. begin
  335. AHotApp.UserApp := FApps[jLoop] as IApp;
  336. Break;
  337. end;
  338. end;
  339. end;
  340. // else
  341. // begin
  342. // RemoveHotApp()
  343. // Continue;
  344. // end;
  345. // if True then
  346. if AHotApp.UserApp <> nil then
  347. Result.Add(AHotApp);
  348. end;
  349. end;
  350. function TAppCentre.RemoveHotApp(AAppKey: AnsiString): Boolean;
  351. var
  352. WSAppKey: string;
  353. iLoop: Integer;
  354. AApp: IHotApp;
  355. begin
  356. Result := False;
  357. WSAppKey := string(AAppKey);
  358. Debug('移除应用:'+WSAppKey, 'TAppCentre.RemoveHotApp');
  359. if (FApps = nil) or (FHotApps = nil) then
  360. Exit;
  361. AApp := nil;
  362. for iLoop := 0 to FHotApps.Count do
  363. if SameText(string((FHotApps[iLoop] as IHotApp).GetUserApp.GetAppKey), AAppKey) then
  364. begin
  365. AApp := (FHotApps[iLoop] as IHotApp);
  366. Break;
  367. end;
  368. if AApp = nil then
  369. Exit;
  370. FHotApps.Delete(iLoop);
  371. { TODO -olqq -c : 刷新主面板 2015/2/1 21:56:56 }
  372. if AApp.GetUserApp = nil then
  373. Exit;
  374. if FHotAppView <> nil then
  375. FHotAppView.RemoveHotApp(AApp);
  376. Result := True;
  377. end;
  378. function TAppCentre.GetDefaultBrowser: string;
  379. var
  380. reg: TRegistry;
  381. begin
  382. reg := TRegistry.Create;
  383. try
  384. reg.RootKey := HKEY_CLASSES_ROOT;
  385. reg.OpenKey('http\\shell\\open\\command',false);
  386. result:=reg.ReadString('');
  387. result:=Copy(result,Pos('"',result)+1,Length(result)-1);
  388. result:=Copy(result,1,Pos('"',result)-1);
  389. reg.CloseKey;
  390. finally
  391. if (result='') then result:='IEXPLORE.EXE';
  392. reg.Free;
  393. end;
  394. end;
  395. procedure TAppCentre.SetMainApplication(AMainApplication: TApplication);
  396. begin
  397. FMainApplication := AMainApplication;
  398. end;
  399. procedure TAppCentre.OnRedirect(Sender: TObject; var dest: string; var NumRedirect: Integer; var Handled: boolean; var VMethod: string);
  400. const
  401. TOKEN: string = 'access_token=';
  402. var
  403. AToken, AParamsStr, AAppKey: string;
  404. AParams: TStrings;
  405. iStart, iEnd, iCount, i: Integer;
  406. AApp: IApp;
  407. begin
  408. if NumRedirect = 2 then
  409. begin
  410. AParamsStr := dest;
  411. AAppKey := (Sender as TIdHTTP).Name;
  412. AParams := TRealICQUtility.SplitString(AParamsStr, '&');
  413. try
  414. for i := 0 to AParams.Count - 1 do
  415. begin
  416. iStart := Pos(TOKEN, AParams[i]);
  417. if iStart > 0 then
  418. begin
  419. Inc(iStart, Length(TOKEN));
  420. // iCount := Length(AParams[i]) - iStart;
  421. AToken := Copy(AParams[i], iStart);
  422. Break;
  423. end;
  424. end;
  425. finally
  426. AParams.Free;
  427. end;
  428. if Length(AToken) = 0 then
  429. begin
  430. LoggerImport.Error('没有获取到Token.', 'TAppCentre.OnRedirect');
  431. Exit;
  432. end;
  433. AApp := Find(AAppKey);
  434. if AApp = nil then
  435. Exit;
  436. ExecuteApp(PChar(AToken), AApp);
  437. // iStart := Pos(TOKEN
  438. // AToken := Copy((Sender as TIdhttp).URL.Params,
  439. end;
  440. // AURL := (Sender as TIdhttp).URL.Params;
  441. // AURL := dest;
  442. end;
  443. function TAppCentre.Find(AAppkey: string): IApp;
  444. var
  445. AFound: Boolean;
  446. iLoop: Integer;
  447. begin
  448. Result := nil;
  449. if FApps = nil then
  450. Exit;
  451. AFound := False;
  452. for iLoop := 0 to FApps.Count - 1 do
  453. begin
  454. Result := FApps[iLoop] as IApp;
  455. Debug(Result.GetAppKey + ' == ' + AAppKey, 'TAppCentre.Find');
  456. AFound := SameText(string(Result.GetAppKey), string(AAppKey));
  457. if AFound then
  458. Break
  459. end;
  460. if not AFound then
  461. begin
  462. Result := nil;
  463. end;
  464. end;
  465. function TAppCentre.GetPublicKey(AApp: IApp): string;
  466. var
  467. AURL: string;
  468. AMsg,
  469. ATicket,
  470. ARep: string;
  471. AConfig: IAppCentreConfig;
  472. AIdHttp: TIdHTTP;
  473. AJo: ISuperObject;
  474. begin
  475. AConfig := GetAppCentreConfig;
  476. AMsg := 'openid='+FOpenID+'&timestamp='+IntToStr((DateTimeToUnix(Now) - 8 * 60 * 60)*1000);
  477. ATicket := Encrypt(AnsiString(AMsg), AApp.GetAppSecret);
  478. AURL := Format(PUBLIC_KEY_URL, [
  479. AConfig.GetIP,
  480. AConfig.GetPort,
  481. AApp.GetAppKey,
  482. ATicket]);
  483. AIdHttp := TIdHTTP.Create(nil);
  484. try
  485. ARep := AIdHttp.Get(AURL);
  486. except
  487. on E: Exception do
  488. begin
  489. FreeAndNil(AIdHttp);
  490. ShowMessage(E.Message);
  491. end;
  492. end;
  493. AJo := SO(ARep);
  494. if AJo.I['ret'] = 0 then
  495. begin
  496. ShowMessage(AJo.S['msg']);
  497. Exit;
  498. end;
  499. Result := AJo.S['publicKey'];
  500. end;
  501. function TAppCentre.RSAEncrypt(APublicKey, AData: string): string;
  502. var
  503. ADateTamp: string;
  504. AIdHttp: TIdHTTP;
  505. Test: string;
  506. begin
  507. // ADateTamp := 'ticket='+IntToStr((DateTimeToUnix(Now) - 8 * 60 * 60))+'&';
  508. Result := Encrypt(AData, UpperCase(APublicKey));
  509. // AIdHttp := TIdHttp.Create(nil);
  510. // Test := AIdHttp.Get('http://120.26.136.253:51005/home/testKey?key='+UpperCase(APublicKey)+'&value='+ ADateTamp);
  511. end;
  512. procedure TAppCentre.SSO(AAppKey: AnsiString; AUrl: AnsiString = '');
  513. const
  514. TOKEN: string = 'access_token';
  515. DATA_FORMAT: string = 'timestamp=%s&secret=%s';
  516. var
  517. AToken: string;
  518. ADateTamp: string;
  519. AClientKey: AnsiString;
  520. ASSOURL: string;
  521. iLoop: Integer;
  522. AApp: IApp;
  523. AFound: Boolean;
  524. ARredirect_uri: string;
  525. AConfig: IAppCentreConfig;
  526. AHandle: THandle;
  527. AKey, Test: string;
  528. AIdHttp: TIdHTTP;
  529. AForm: IUIForm;
  530. AViewManager: IViewManager;
  531. begin
  532. Debug(AAppKey, 'TAppCentre.SSO');
  533. AApp := Find(AAppKey);
  534. if AApp = nil then
  535. begin
  536. LoggerImport.Error('没有找到对应应用的AppKey', 'TAppCentre.SSO');
  537. Exit;
  538. end;
  539. AKey := GetPublicKey(AApp);
  540. if AKey = '' then
  541. begin
  542. LoggerImport.Error('PublicKey是空的', 'TAppCentre.SSO');
  543. Exit;
  544. end;
  545. AConfig := GetAppCentreConfig;
  546. ADateTamp := 'ticket='+IntToStr((DateTimeToUnix(Now) - 8 * 60 * 60));
  547. AClientKey := Encrypt(AnsiString(ADateTamp), AConfig.GetLxtAppSecret+AApp.GetOpenKey);
  548. // AIdHttp := TIdHttp.Create(nil);
  549. // Test := AIdHttp.Get('http://120.26.136.253:51005/home/testKey?key='+AConfig.GetLxtAppSecret+AApp.GetOpenKey+'&value='+ AnsiString(ADateTamp));
  550. //'http://oa.wswin.cn:8989/home/jump?appid='+AppKey+'&clientuin='+openID+'&clientkey='+clientkey+'&redirect_uri='+redirect_uri+'&toappid='+AppKey;
  551. if AUrl = '' then
  552. AUrl := AApp.GetUrl;
  553. ASSOURL := Format(SSO_URL, [
  554. AConfig.GetIP,
  555. AConfig.GetPort,
  556. AConfig.GetLxtAppKey,
  557. FOpenID,
  558. AClientKey,
  559. AUrl,
  560. AApp.GetAppKey,
  561. RSAEncrypt(AKey, Format(DATA_FORMAT,[IntToStr((DateTimeToUnix(Now) - 8 * 60 * 60)*1000), AApp.GetAppSecret]))
  562. ]);
  563. ///http://120.26.136.253:51005/home/testKey?key=a&value=b
  564. Debug(ASSOURL, 'TAppCentre.SSO');
  565. ASSOURL := URLEncode(ASSOURL, True);
  566. if AApp.IsBSApp then
  567. begin
  568. if FMainApplication <> nil then
  569. AHandle := FMainApplication.Handle
  570. else
  571. AHandle := 0;
  572. AViewManager := GetAppCentreUIHandler.GetViewManager;
  573. if (AApp.GetEmbedded) and (AViewManager <> nil) then
  574. begin
  575. AForm := AViewManager.GetView('TEmbeddedViewForm');
  576. if AForm = nil then
  577. ExecuteBS(ASSOURL)
  578. else
  579. begin
  580. AForm.SetFormInfo(Format('{"url":"%s","center":true}', [ASSOURL]));
  581. AForm.Show;
  582. end;
  583. end
  584. else
  585. ExecuteBS(ASSOURL);
  586. // ShellExecute(AHandle, 'open' , PChar(GetDefaultBrowser), PChar(AURL), '',SW_SHOWDEFAULT);
  587. end
  588. else
  589. begin
  590. ExecuteApp(ASSOURL, AApp);
  591. // AIdHttp := TIdHTTP.Create(nil);
  592. // AIdHttp.Name := AApp.GetAppKey;
  593. // try
  594. // AIdHttp.RedirectMaximum := 3;
  595. // AIdHttp.HandleRedirects := True;
  596. // AIdHttp.OnRedirect := OnRedirect;
  597. // AIdHttp.Get(AURL);
  598. // finally
  599. // AIdHttp.Free;
  600. // end;
  601. end;
  602. end;
  603. procedure TAppCentre.ExecuteBS(AURL: string);
  604. var
  605. AHandle: Cardinal;
  606. ShExecInfo: SHELLEXECUTEINFO;
  607. ADir: PAnsiChar;
  608. AppCentreCom: ILXTAppCentreCom;
  609. ADlgOpen: TOpenDialog;
  610. ADirStr: string;
  611. begin
  612. ADirStr := GetDefaultBrowser;
  613. ShExecInfo.cbSize := SizeOf(SHELLEXECUTEINFO);
  614. ShExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
  615. ShExecInfo.Wnd := 0;
  616. ShExecInfo.lpVerb := PChar('open');
  617. ShExecInfo.lpFile := PChar(ADirStr);
  618. ShExecInfo.lpParameters := PChar(AURL);
  619. ShExecInfo.lpDirectory := PChar(ExtractFilePath(ADirStr));
  620. ShExecInfo.nShow := SW_SHOWMAXIMIZED;
  621. ShExecInfo.hInstApp := 0;
  622. if ShellExecuteEx(@ShExecInfo) then
  623. begin
  624. FHandles.Add(Pointer(ShExecInfo.hProcess));
  625. end
  626. else
  627. ShowMessage(SysErrorMessage(GetLastError));
  628. end;
  629. procedure TAppCentre.ExecuteApp(AURL: string; AApp: IApp);
  630. var
  631. AHandle: Cardinal;
  632. ShExecInfo: SHELLEXECUTEINFO;
  633. ADir: PAnsiChar;
  634. AppCentreCom: ILXTAppCentreCom;
  635. ADlgOpen: TOpenDialog;
  636. ADirStr: string;
  637. begin
  638. try
  639. AppCentreCom := CoLXTAppCentreCom.Create;
  640. if AppCentreCom = nil then
  641. begin
  642. LoggerImport.Error(SysErrorMessage(GetLastError), 'TAppCentre.ExecuteApp');
  643. ShowMessage('您电脑上没有安装AppCentreCom.dll组件.');
  644. LoggerImport.Error('您电脑上没有安装AppCentreCom.dll组件.', 'TAppCentre.ExecuteApp');
  645. end;
  646. ADir := AppCentreCom.GetInstallationDirectory(PAnsiChar(AApp.GetAppKey));
  647. if (ADir = nil) or not FileExists(string(ADir)) then
  648. ADir := PAnsiChar(AApp.GetUrl());
  649. except
  650. on Ex: Exception do
  651. begin
  652. LoggerImport.Error(SysErrorMessage(GetLastError), 'TAppCentre.ExecuteApp');
  653. Exit;
  654. end;
  655. end;
  656. if (ADir = nil) or not FileExists(string(ADir)) then
  657. begin
  658. if not FileExists(string(ADir)) then
  659. ShowMessage('程序目录可能已经更换,请重新指定.');
  660. ADlgOpen := TOpenDialog.Create(Application);
  661. try
  662. if ADlgOpen.Execute(Application.Handle) then
  663. ADirStr := ADlgOpen.FileName;
  664. if ADirStr = '' then
  665. Exit;
  666. ADir := PAnsiChar(AnsiString(ADirStr));
  667. AppCentreCom.RegisterInstallationDirectory(PAnsiChar(AApp.GetAppKey), ADir);
  668. finally
  669. ADlgOpen.Free;
  670. end;
  671. end;
  672. ShExecInfo.cbSize := SizeOf(SHELLEXECUTEINFO);
  673. ShExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
  674. ShExecInfo.Wnd := 0;
  675. ShExecInfo.lpVerb := nil;
  676. ShExecInfo.lpFile := PChar(string(ADir));
  677. ShExecInfo.lpParameters := PChar(AURL);
  678. ShExecInfo.lpDirectory := PChar(ExtractFilePath(string(ADir)));
  679. ShExecInfo.nShow := SW_SHOW;
  680. ShExecInfo.hInstApp := 0;
  681. if ShellExecuteEx(@ShExecInfo) then
  682. FHandles.Add(Pointer(ShExecInfo.hProcess))
  683. else
  684. ShowMessage(SysErrorMessage(GetLastError));
  685. end;
  686. //获取所有应用列表
  687. function TAppCentre.GetAllApps: IInterfaceList;
  688. var
  689. AIdHttp:TIdHTTP;
  690. ResponeStr,openkey, AURL: String;
  691. begin
  692. if FOpenID = '' then
  693. begin
  694. Result := nil;
  695. Exit;
  696. end;
  697. if FApps <> nil then
  698. begin
  699. Result := FApps;
  700. Exit;
  701. end;
  702. Result := nil;
  703. AIdHttp := TIdHTTP.Create(nil);
  704. InitHttpHeader(AIdHttp);
  705. try
  706. AURL := Format(GETAPPS_URL,[GetAppCentreConfig.GetIP, GetAppCentreConfig.GetPort]);
  707. ResponeStr:=AIdHttp.Get(AURL);
  708. ResponeStr := Utf8ToAnsi(ResponeStr);
  709. FApps := ProcessAppJsonStr(ResponeStr);
  710. Debug(Format('Apps Count:%d', [FApps.Count]), 'TAppCentre.GetAllApps');
  711. Result := FApps;
  712. DownloadIcon(FApps);
  713. except
  714. on E: Exception do
  715. begin
  716. FApps := nil;
  717. Result := nil;
  718. LoggerImport.Error(E.Message, 'TAppCentre.GetAllApps');
  719. Freeandnil(AIdHttp);
  720. end;
  721. end;
  722. Freeandnil(AIdHttp);
  723. end;
  724. //获取用户常用的应用列表
  725. function TAppCentre.GetHotApps: IInterfaceList;
  726. var
  727. AIdHttp:TIdHTTP;
  728. ResponeStr: String;
  729. AURL: string;
  730. begin
  731. if FOpenID = '' then
  732. begin
  733. Result := nil;
  734. Exit;
  735. end;
  736. if FHotApps <> nil then
  737. begin
  738. Result := FHotApps;
  739. Exit;
  740. end;
  741. Result := nil;
  742. AIdHttp := TIdHTTP.Create(nil);
  743. InitHttpHeader(AIdHttp);
  744. try
  745. AURL := Format(GETHOTAPPS_URL,[GetAppCentreConfig.GetIP, GetAppCentreConfig.GetPort]);
  746. ResponeStr:=AIdHttp.Get(AURL);
  747. ResponeStr := Utf8ToAnsi(ResponeStr);
  748. FHotApps := ProcessHotAppJsonStr(ResponeStr);
  749. Result := FHotApps;
  750. except
  751. on E: Exception do
  752. begin
  753. FHotApps := nil;
  754. Result := nil;
  755. Freeandnil(AIdHttp);
  756. end;
  757. end;
  758. Freeandnil(AIdHttp);
  759. end;
  760. function TAppCentre.GetEnable: Boolean;
  761. begin
  762. Result := GetAppCentreConfig.GetEnable;
  763. end;
  764. function TAppCentre.Login(ALoginName: WideString; AHotAppView: IHotAppView):WideString;
  765. begin
  766. Debug(ALoginName, 'TAppCentre.Login');
  767. FHotAppView := AHotAppView;
  768. GetOpenID(ALoginName);
  769. if FOpenID = '' then
  770. Exit;
  771. GetAllApps;
  772. GetHotApps;
  773. end;
  774. procedure TAppCentre.Logout;
  775. var
  776. AHotApp: IHotApp;
  777. AApp: IApp;
  778. begin
  779. FOpenID := '';
  780. while FHandles.Count > 0 do
  781. begin
  782. TerminateProcess(Cardinal(FHandles[0]), 0);
  783. FHandles.Delete(0);
  784. end;
  785. if Assigned(FDownloadThread) then
  786. begin
  787. FDownloadThread.Terminate;
  788. FDownloadThread.WaitFor;
  789. FreeAndNil(FDownloadThread);
  790. end;
  791. FApps := nil;
  792. FHotApps := nil;
  793. FHotAppView := nil;
  794. if AppCentreFrm <> nil then
  795. AppCentreFrm.Close;
  796. end;
  797. procedure TAppCentre.OnDownloadedIcon(AAppKey: string; AStream: TStream);
  798. var
  799. AOldBuffer,
  800. ANewBuffer: TBytes;
  801. begin
  802. if AStream = nil then
  803. Exit;
  804. try
  805. GetAppIconProvider.FindIcon(WideString(AAppKey), AOldBuffer);
  806. except
  807. on E: Exception do
  808. begin
  809. Debug('奇怪异常:'+E.Message, 'TAppCentre.OnDownloadedIcon('+AAppKey+')');
  810. end;
  811. end;
  812. AStream.Position := 0;
  813. SetLength(ANewBuffer, AStream.Size);
  814. AStream.Read(ANewBuffer[0], AStream.Size);
  815. if Length(AOldBuffer) <> 0 then
  816. begin
  817. if MD5Match(BytesToMD5(ANewBuffer), BytesToMD5(AOldBuffer)) then
  818. begin
  819. Debug('图标没有变,无需更新', 'TAppCentre.OnDownloadedIcon('+AAppKey+')');
  820. Exit;
  821. end;
  822. end;
  823. Debug('更新图标', 'TAppCentre.OnDownloadedIcon('+AAppKey+')');
  824. GetAppIconProvider.Insert(AAppKey, ANewBuffer);
  825. if (FHotAppView <> nil) then
  826. FHotAppView.OnASyncAppIconDownloaded(AAppKey, ANewBuffer, AStream.Size);
  827. end;
  828. procedure TAppCentre.OpenAppCentreFrom;
  829. begin
  830. if AppCentreFrm = nil then
  831. begin
  832. AppCentreFrm := TAppCentreForm.Create(nil);
  833. end;
  834. {$IFDEF NXQST}
  835. AppCentreFrm.Caption := '纳税之家';
  836. {$ENDIF}
  837. AppCentreFrm.Top := (Screen.Height - AppCentreFrm.Height) div 2;
  838. AppCentreFrm.Left := (Screen.Width - AppCentreFrm.Width) div 2;
  839. AppCentreFrm.Show();
  840. end;
  841. procedure TAppCentre.OpenMessageCentre(ALoginName, APassword: WideString); stdcall;
  842. var
  843. AURL: string;
  844. config: IAppCentreConfig;
  845. begin
  846. if MessageCentreForm = nil then
  847. begin
  848. MessageCentreForm := TMessageCentreForm.Create(nil);
  849. end;
  850. config := GetAppCentreConfig;
  851. AURL := Format(MESSAGE_CENTRE_URL, [config.GetMessageCentreIP, config.GetMessageCentrePort, ALoginName, APassword]);
  852. MessageCentreForm.Top := (Screen.Height - MessageCentreForm.Height) div 2;
  853. MessageCentreForm.Left := (Screen.Width - MessageCentreForm.Width) div 2;
  854. MessageCentreForm.Show();
  855. MessageCentreForm.chrm1.Load(AURL);
  856. end;
  857. procedure TAppCentre.OpenWebDebuggerTool;
  858. begin
  859. if AppCentreFrm = nil then
  860. Exit;
  861. AppCentreFrm.OpenDebug;
  862. end;
  863. function TAppCentre.URLEncode(const S: string;
  864. const InQueryString: Boolean): string;
  865. begin
  866. Result := TIdURI.URLEncode(S);
  867. end;
  868. end.