UAppCentre.pas 25 KB

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