unit UAppCentre; interface uses mybean.core.objects, Classes, SysUtils, StrUtils, superobject, Dialogs, DateUtils, InterfaceAppCentre, Graphics,IdHttp,IdHttpEx,EncdDecd,XXTEA, Forms, IconRequest, TypInfo, UAppCentreConfig, InterfaceUI; type TAppCentre = class(TMyBeanInterfacedObject, IAppCentre) private FLogined: Boolean; FLoginName: string; FOpenID: string; FAppKey :string; FAppSecret :string; FEnable: Boolean; FApps: IInterfaceList; FHotApps: IInterfaceList; FMainApplication: TApplication; FHotAppView: IHotAppView; FDownloadThread: TIconRequest; FHandles: TList; function URLEncode(const S: string; const InQueryString: Boolean): string; function ProcessAppJsonStr(AResponeStr: string): IInterfaceList; function ProcessHotAppJsonStr(AResponeStr: string): IInterfaceList; function ParseApp(AJsonObject: ISuperObject): IApp; procedure InitHttpHeader(AIdHttp: TIdHTTP); procedure GetOpenID(ALoginName: string); function GetDefaultBrowser: string; procedure DownloadIcon(AApps: IInterfaceList); procedure OnDownloadedIcon(AAppKey: string; AStream: TStream); procedure ExecuteApp(AURL: string; AApp: IApp); procedure OnRedirect(Sender: TObject; var dest: string; var NumRedirect: Integer; var Handled: boolean; var VMethod: string); function Find(AAppkey: string): IApp; procedure ExecuteBS(AURL: string); function GetPublicKey(AApp: IApp): string; function RSAEncrypt(APublicKey, AData: string): string; public destructor Destroy; override; constructor Create; override; function Login(ALoginName: WideString; AHotAppView: IHotAppView):WideString; stdcall; procedure Logout; stdcall; function GetHotApps: IInterfaceList; stdcall; function GetAllApps: IInterfaceList; stdcall; function RemoveHotApp(AAppKey: AnsiString): Boolean; stdcall; function AddHotApp(AAppKey: AnsiString; AHotApp: IHotApp): Boolean; stdcall; function GetEnable(): Boolean; stdcall; function GetAppCentreURL: AnsiString; stdcall; procedure OpenAppCentreFrom; stdcall; procedure CloseAppCentreForm; stdcall; procedure ChangeUIColor(AColor: TColor); stdcall; procedure Hello; stdcall; procedure SSO(AAppKey: AnsiString; AUrl: AnsiString = ''); stdcall; procedure SetMainApplication(AMainApplication: TApplication); stdcall; procedure OpenWebDebuggerTool; stdcall; procedure CloseWebDebuggerTool; stdcall; procedure OpenMessageCentre(ALoginName, APassword: WideString); stdcall; /// /// 残联定制 /// function AuthFromCanlian(ALoginName, APassword: WideString): Boolean; stdcall; end; {$INCLUDE ..\Client\LXTalk.inc} implementation uses LoggerImport, AppCentreForm, UApp, ShellApi, Registry, Windows, DataProviderImport, RealICQUtility, MD5_32, AppCentreImport, AppCentreCom_TLB, Types, IdURI, MessageCentre, UIHandler; const APPICON_DIR: string = 'Users\AppIcon'; LOGIN_URL: string = 'http://%s:%d/api/basic/openid?appKey=%s&appSecret=%s&loginName=%s'; GETHOTAPPS_URL : string = 'http://%s:%d/api/service/userfeaturedapps?status=1'; GETAPPS_URL : string = 'http://%s:%d/api/service/applist?key='; SSO_URL: string = 'http://%s:%d/home/jump?appid=%s&clientuin=%s&clientkey=%s&redirect_uri=%s&toappid=%s&message=%s'; APPCENTRE_LOGIN_URL: string = 'http://%s:%d/Account/Login?appKey=%s&ticket=%s'; CANLIAN_AUTH_URL: string = 'http://%s:%d/api/basic/ValiAccount?Username=%s&Password=%s'; MESSAGE_CENTRE_URL: string = 'http://%s:%d/account/sso?LoginName=%s&Password=%s&URL=/Widgets/home/index'; //xxtea(appsecret,openid=openid×tamp=14774832748) PUBLIC_KEY_URL: string = 'http://%s:%d/home/GenerationKey?appid=%s&ticket=%s'; var config: TAppCentreConfig; { TAppCentre } function TAppCentre.AddHotApp(AAppKey: AnsiString; AHotApp: IHotApp): Boolean; var WSAppKey: string; iLoop: Integer; AApp: IApp; begin Result := False; WSAppKey := string(AAppKey); if (FApps = nil) or (FHotApps = nil) or (AHotApp = nil) then Exit; AApp := nil; for iLoop := 0 to FHotApps.Count - 1 do if SameText(string((FHotApps[iLoop] as IHotApp).GetUserApp.GetAppKey), AAppKey) then begin AApp := (FHotApps[iLoop] as IHotApp).GetUserApp; Break; end; if AApp <> nil then Exit; AApp := nil; for iLoop := 0 to FApps.Count - 1 do if SameText(string((FApps[iLoop] as IApp).GetAppKey), AAppKey) then begin AApp := (FApps[iLoop] as IApp); Break; end; if AApp = nil then Exit; AHotApp.SetUserApp(AApp); FHotApps.Add(AHotApp); if FHotAppView <> nil then FHotAppView.AddHotApp(AHotApp); Result := True; end; function TAppCentre.AuthFromCanlian(ALoginName, APassword: WideString): Boolean; var AURL, ARep, WLoginName, WPassword: string; AConfig: IAppCentreConfig; AIdHttp: TIdHTTP; begin AConfig := GetAppCentreConfig; WLoginName := string(ALoginName); WPassword := string(APassword); AURL := Format(CANLIAN_AUTH_URL, [AConfig.GetIP, AConfig.GetPort, WLoginName, WPassword]); AIdHttp := TIdHTTP.Create(nil); Result := False; AIdHttp.Request.Accept := ''; AIdHttp.Request.ContentType := 'application/json; charset=utf-8'; try AURL := AIdHttp.URL.ParamsEncode(AURL); ARep := AIdHttp.Get(AURL); Result := StrToBoolDef(ARep, False); finally Freeandnil(AIdHttp); end; end; procedure TAppCentre.ChangeUIColor(AColor: TColor); begin // if GetAppCentreConfig.GetShowType = stOnMainform then // Exit; if AppCentreFrm = nil then begin AppCentreFrm := TAppCentreForm.Create(nil); end; AppCentreFrm.ChangeUIColor(AColor); end; procedure TAppCentre.CloseAppCentreForm; begin if AppCentreFrm <> nil then begin AppCentreFrm.Close(); end; end; procedure TAppCentre.CloseWebDebuggerTool; begin if AppCentreFrm = nil then Exit; end; constructor TAppCentre.Create; begin inherited; FHandles := TList.Create; end; destructor TAppCentre.Destroy; begin while FHandles.Count > 0 do begin TerminateProcess(Cardinal(FHandles[0]), 0); FHandles.Delete(0); end; FreeAndNil(FHandles); // if AppCentreFrm <> nil then // FreeAndNil(AppCentreFrm); inherited; end; procedure TAppCentre.DownloadIcon(AApps: IInterfaceList); begin if Assigned(FDownloadThread) then FreeAndNil(FDownloadThread); FDownloadThread := TIconRequest.Create(AApps); FDownloadThread.OnCompleted := OnDownloadedIcon; end; procedure TAppCentre.GetOpenID(ALoginName: string); var AIdHttp : TIdHTTP; AOpenID : string; AConfig: IAppCentreConfig; AURL: string; begin FOpenID := ''; AConfig := GetAppCentreConfig; FLoginName := ALoginName; AIdHttp := TIdHTTP.Create(nil); AIdHttp.Request.Accept := ''; AIdHttp.Request.ContentType := 'application/json; charset=utf-8'; try AURL := Format(LOGIN_URL, [AConfig.GetIP, AConfig.GetPort, AConfig.GetLxtAppKey, AConfig.GetLxtAppSecret, FloginName]); AOpenID := AIdHttp.Get(AURL); AOpenID := UTF8Decode(AOpenID); AOpenID := Copy(AOpenID,2, Length(AOpenID) - 2); FOpenID := AOpenID; except on E: Exception do begin FOpenID := ''; Freeandnil(AIdHttp); LoggerImport.Error(E.Message, 'GetOpenID'); end; end; Freeandnil(AIdHttp); end; function TAppCentre.GetAppCentreURL: AnsiString; const GET_DATA: string = '{"openID":"%s","timestamp":%d}'; var AAnsiStrTicket: AnsiString; AURL: string; AContent: string; AConfig: IAppCentreConfig; //Cardinal begin AConfig := GetAppCentreConfig; AContent := Format(GET_DATA, [FOpenID, (DateTimeToUnix(Now) - 8 * 60 * 60)]); AAnsiStrTicket := Encrypt( AContent, AConfig.GetLxtAppSecret+AConfig.GetLxtAppKey); AURL := Format(APPCENTRE_LOGIN_URL, [ AConfig.GetClientIP, AConfig.GetClientPort, AConfig.GetLxtAppKey, AAnsiStrTicket ]); Result := AnsiString(AURL); end; procedure TAppCentre.Hello; begin ShowMessage('TAppCentre.Hello'); end; procedure TAppCentre.InitHttpHeader(AIdHttp: TIdHTTP); var openIDEx: string; begin openIDEx := 'Basic '+EncodeString(FOpenID); AIdHttp.Request.Accept := ''; AIdHttp.Request.CustomHeaders.Values['Authorization'] := openIDEx; AIdHttp.Request.CustomHeaders.Values['Content-Type'] := 'application/x-www-form-urlencoded; charset=utf-8'; end; function TAppCentre.ParseApp(AJsonObject: ISuperObject): IApp; var App: TApp; joApp: ISuperObject; begin App := TApp.Create; App.ID := AJsonObject.I['ID']; App.AppID := AJsonObject.I['AppID']; joApp := AJsonObject.O['Application']; if joApp = nil then begin Result := App; LoggerImport.Error(Format('AppID:%d', [App.AppID]), 'TAppCentre.ParseApp'); Exit; end; App.Icon := (joApp.S['Icon']); App.Title := (joApp.S['Title']); App.Code := (joApp.S['Code']); App.Url := (joApp.S['Uri']); App.CallBackUri := (joApp.S['CallBackUri']); App.AppKey := (joApp.S['AppKey']); App.AppSecret := (joApp.S['AppSecret']); App.Style := joApp.I['Style']; App.AppStatus := joApp.I['Status']; App.UserID := AJsonObject.I['UserID']; App.AccessToken := (AJsonObject.S['AccessToken']); App.AccessTokenExpired:= (AJsonObject.S['AccessTokenExpired']); App.RefreshToken := (AJsonObject.S['RefreshToken']); App.OpenID := (AJsonObject.S['OpenID']); App.OpenKey := (AJsonObject.S['OpenKey']); App.Scope := (AJsonObject.S['Scope']); App.UserStatus := AJsonObject.I['Status']; App.IsAccessTokenValid:= AJsonObject.B['IsAccessTokenValid']; App.Embedded := joApp.B['Embedded']; Result := App; end; function TAppCentre.ProcessAppJsonStr(AResponeStr: string): IInterfaceList; var jo: ISuperObject; ja: TSuperArray; App :IApp; iLoop: Integer; begin Result := TInterfaceList.Create; jo := SO(AResponeStr); if jo = nil then begin LoggerImport.Error('JSON解析有问题', 'ProcessAppJsonStr'); Exit; end; ja := SO(AResponeStr).AsArray; for iLoop := 0 to ja.Length - 1 do begin App := ParseApp(ja.O[iLoop]); Result.Add(App); end; end; function TAppCentre.ProcessHotAppJsonStr(AResponeStr: string): IInterfaceList; var ja: TSuperArray; joHotApp: ISuperObject; AHotApp: THotApp; iLoop, jLoop: Integer; begin ja := SO(AResponeStr).AsArray; Result := TInterfaceList.Create; for iLoop := 0 to ja.Length - 1 do begin AHotApp := THotApp.Create; joHotApp := ja.O[iLoop]; AHotApp.ID := joHotApp.I['ID']; AHotApp.UserAppID := joHotApp.I['UserAppID']; AHotApp.Position := joHotApp.I['Position']; AHotApp.Status := joHotApp.I['Status']; AHotApp.OpenID := joHotApp.S['OpenID']; AHotApp.CreateDate := joHotApp.S['CreateDate']; if (joHotApp.O['UserApp'] <> nil) then AHotApp.UserApp := ParseApp(joHotApp.O['UserApp']); if AHotApp.UserApp = nil then begin for jLoop := 0 to FApps.Count - 1 do begin if ((FApps[jLoop] as IApp).GetAppID = AHotApp.UserAppID) then begin AHotApp.UserApp := FApps[jLoop] as IApp; Break; end; end; end; // else // begin // RemoveHotApp() // Continue; // end; // if True then if AHotApp.UserApp <> nil then Result.Add(AHotApp); end; end; function TAppCentre.RemoveHotApp(AAppKey: AnsiString): Boolean; var WSAppKey: string; iLoop: Integer; AApp: IHotApp; begin Result := False; WSAppKey := string(AAppKey); Debug('移除应用:'+WSAppKey, 'TAppCentre.RemoveHotApp'); if (FApps = nil) or (FHotApps = nil) then Exit; AApp := nil; for iLoop := 0 to FHotApps.Count do if SameText(string((FHotApps[iLoop] as IHotApp).GetUserApp.GetAppKey), AAppKey) then begin AApp := (FHotApps[iLoop] as IHotApp); Break; end; if AApp = nil then Exit; FHotApps.Delete(iLoop); { TODO -olqq -c : 刷新主面板 2015/2/1 21:56:56 } if AApp.GetUserApp = nil then Exit; if FHotAppView <> nil then FHotAppView.RemoveHotApp(AApp); Result := True; end; function TAppCentre.GetDefaultBrowser: string; var reg: TRegistry; begin reg := TRegistry.Create; try reg.RootKey := HKEY_CLASSES_ROOT; reg.OpenKey('http\\shell\\open\\command',false); result:=reg.ReadString(''); result:=Copy(result,Pos('"',result)+1,Length(result)-1); result:=Copy(result,1,Pos('"',result)-1); reg.CloseKey; finally if (result='') then result:='IEXPLORE.EXE'; reg.Free; end; end; procedure TAppCentre.SetMainApplication(AMainApplication: TApplication); begin FMainApplication := AMainApplication; end; procedure TAppCentre.OnRedirect(Sender: TObject; var dest: string; var NumRedirect: Integer; var Handled: boolean; var VMethod: string); const TOKEN: string = 'access_token='; var AToken, AParamsStr, AAppKey: string; AParams: TStrings; iStart, iEnd, iCount, i: Integer; AApp: IApp; begin if NumRedirect = 2 then begin AParamsStr := dest; AAppKey := (Sender as TIdHTTP).Name; AParams := TRealICQUtility.SplitString(AParamsStr, '&'); try for i := 0 to AParams.Count - 1 do begin iStart := Pos(TOKEN, AParams[i]); if iStart > 0 then begin Inc(iStart, Length(TOKEN)); // iCount := Length(AParams[i]) - iStart; AToken := Copy(AParams[i], iStart); Break; end; end; finally AParams.Free; end; if Length(AToken) = 0 then begin LoggerImport.Error('没有获取到Token.', 'TAppCentre.OnRedirect'); Exit; end; AApp := Find(AAppKey); if AApp = nil then Exit; ExecuteApp(PChar(AToken), AApp); // iStart := Pos(TOKEN // AToken := Copy((Sender as TIdhttp).URL.Params, end; // AURL := (Sender as TIdhttp).URL.Params; // AURL := dest; end; function TAppCentre.Find(AAppkey: string): IApp; var AFound: Boolean; iLoop: Integer; begin Result := nil; if FApps = nil then Exit; AFound := False; for iLoop := 0 to FApps.Count - 1 do begin Result := FApps[iLoop] as IApp; Debug(Result.GetAppKey + ' == ' + AAppKey, 'TAppCentre.Find'); AFound := SameText(string(Result.GetAppKey), string(AAppKey)); if AFound then Break end; if not AFound then begin Result := nil; end; end; function TAppCentre.GetPublicKey(AApp: IApp): string; var AURL: string; AMsg, ATicket, ARep: string; AConfig: IAppCentreConfig; AIdHttp: TIdHTTP; AJo: ISuperObject; begin AConfig := GetAppCentreConfig; AMsg := 'openid='+FOpenID+'×tamp='+IntToStr((DateTimeToUnix(Now) - 8 * 60 * 60)*1000); ATicket := Encrypt(AnsiString(AMsg), AApp.GetAppSecret); AURL := Format(PUBLIC_KEY_URL, [ AConfig.GetIP, AConfig.GetPort, AApp.GetAppKey, ATicket]); AIdHttp := TIdHTTP.Create(nil); try ARep := AIdHttp.Get(AURL); except on E: Exception do begin FreeAndNil(AIdHttp); ShowMessage(E.Message); end; end; AJo := SO(ARep); if AJo.I['ret'] = 0 then begin ShowMessage(AJo.S['msg']); Exit; end; Result := AJo.S['publicKey']; end; function TAppCentre.RSAEncrypt(APublicKey, AData: string): string; var ADateTamp: string; AIdHttp: TIdHTTP; Test: string; begin // ADateTamp := 'ticket='+IntToStr((DateTimeToUnix(Now) - 8 * 60 * 60))+'&'; Result := Encrypt(AData, UpperCase(APublicKey)); // AIdHttp := TIdHttp.Create(nil); // Test := AIdHttp.Get('http://120.26.136.253:51005/home/testKey?key='+UpperCase(APublicKey)+'&value='+ ADateTamp); end; procedure TAppCentre.SSO(AAppKey: AnsiString; AUrl: AnsiString = ''); const TOKEN: string = 'access_token'; DATA_FORMAT: string = 'timestamp=%s&secret=%s'; var AToken: string; ADateTamp: string; AClientKey: AnsiString; ASSOURL: string; iLoop: Integer; AApp: IApp; AFound: Boolean; ARredirect_uri: string; AConfig: IAppCentreConfig; AHandle: THandle; AKey, Test: string; AIdHttp: TIdHTTP; AForm: IUIForm; AViewManager: IViewManager; begin Debug(AAppKey, 'TAppCentre.SSO'); AApp := Find(AAppKey); if AApp = nil then begin LoggerImport.Error('没有找到对应应用的AppKey', 'TAppCentre.SSO'); Exit; end; AKey := GetPublicKey(AApp); if AKey = '' then begin LoggerImport.Error('PublicKey是空的', 'TAppCentre.SSO'); Exit; end; AConfig := GetAppCentreConfig; ADateTamp := 'ticket='+IntToStr((DateTimeToUnix(Now) - 8 * 60 * 60)); AClientKey := Encrypt(AnsiString(ADateTamp), AConfig.GetLxtAppSecret+AApp.GetOpenKey); // AIdHttp := TIdHttp.Create(nil); // Test := AIdHttp.Get('http://120.26.136.253:51005/home/testKey?key='+AConfig.GetLxtAppSecret+AApp.GetOpenKey+'&value='+ AnsiString(ADateTamp)); //'http://oa.wswin.cn:8989/home/jump?appid='+AppKey+'&clientuin='+openID+'&clientkey='+clientkey+'&redirect_uri='+redirect_uri+'&toappid='+AppKey; if AUrl = '' then AUrl := AApp.GetUrl; ASSOURL := Format(SSO_URL, [ AConfig.GetIP, AConfig.GetPort, AConfig.GetLxtAppKey, FOpenID, AClientKey, AUrl, AApp.GetAppKey, RSAEncrypt(AKey, Format(DATA_FORMAT,[IntToStr((DateTimeToUnix(Now) - 8 * 60 * 60)*1000), AApp.GetAppSecret])) ]); ///http://120.26.136.253:51005/home/testKey?key=a&value=b Debug(ASSOURL, 'TAppCentre.SSO'); ASSOURL := URLEncode(ASSOURL, True); if AApp.IsBSApp then begin if FMainApplication <> nil then AHandle := FMainApplication.Handle else AHandle := 0; AViewManager := GetAppCentreUIHandler.GetViewManager; if (AApp.GetEmbedded) and (AViewManager <> nil) then begin AForm := AViewManager.GetView('TEmbeddedViewForm'); if AForm = nil then ExecuteBS(ASSOURL) else begin AForm.SetFormInfo(Format('{"url":"%s","center":true}', [ASSOURL])); AForm.Show; end; end else ExecuteBS(ASSOURL); // ShellExecute(AHandle, 'open' , PChar(GetDefaultBrowser), PChar(AURL), '',SW_SHOWDEFAULT); end else begin ExecuteApp(ASSOURL, AApp); // AIdHttp := TIdHTTP.Create(nil); // AIdHttp.Name := AApp.GetAppKey; // try // AIdHttp.RedirectMaximum := 3; // AIdHttp.HandleRedirects := True; // AIdHttp.OnRedirect := OnRedirect; // AIdHttp.Get(AURL); // finally // AIdHttp.Free; // end; end; end; procedure TAppCentre.ExecuteBS(AURL: string); var AHandle: Cardinal; ShExecInfo: SHELLEXECUTEINFO; ADir: PAnsiChar; AppCentreCom: ILXTAppCentreCom; ADlgOpen: TOpenDialog; ADirStr: string; begin ADirStr := GetDefaultBrowser; ShExecInfo.cbSize := SizeOf(SHELLEXECUTEINFO); ShExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS; ShExecInfo.Wnd := 0; ShExecInfo.lpVerb := PChar('open'); ShExecInfo.lpFile := PChar(ADirStr); ShExecInfo.lpParameters := PChar(AURL); ShExecInfo.lpDirectory := PChar(ExtractFilePath(ADirStr)); ShExecInfo.nShow := SW_SHOWMAXIMIZED; ShExecInfo.hInstApp := 0; if ShellExecuteEx(@ShExecInfo) then begin FHandles.Add(Pointer(ShExecInfo.hProcess)); end else ShowMessage(SysErrorMessage(GetLastError)); end; procedure TAppCentre.ExecuteApp(AURL: string; AApp: IApp); var AHandle: Cardinal; ShExecInfo: SHELLEXECUTEINFO; ADir: PAnsiChar; AppCentreCom: ILXTAppCentreCom; ADlgOpen: TOpenDialog; ADirStr: string; begin try AppCentreCom := CoLXTAppCentreCom.Create; if AppCentreCom = nil then begin LoggerImport.Error(SysErrorMessage(GetLastError), 'TAppCentre.ExecuteApp'); ShowMessage('您电脑上没有安装AppCentreCom.dll组件.'); LoggerImport.Error('您电脑上没有安装AppCentreCom.dll组件.', 'TAppCentre.ExecuteApp'); end; ADir := AppCentreCom.GetInstallationDirectory(PAnsiChar(AApp.GetAppKey)); if (ADir = nil) or not FileExists(string(ADir)) then ADir := PAnsiChar(AApp.GetUrl()); except on Ex: Exception do begin LoggerImport.Error(SysErrorMessage(GetLastError), 'TAppCentre.ExecuteApp'); Exit; end; end; if (ADir = nil) or not FileExists(string(ADir)) then begin if not FileExists(string(ADir)) then ShowMessage('程序目录可能已经更换,请重新指定.'); ADlgOpen := TOpenDialog.Create(Application); try if ADlgOpen.Execute(Application.Handle) then ADirStr := ADlgOpen.FileName; if ADirStr = '' then Exit; ADir := PAnsiChar(AnsiString(ADirStr)); AppCentreCom.RegisterInstallationDirectory(PAnsiChar(AApp.GetAppKey), ADir); finally ADlgOpen.Free; end; end; ShExecInfo.cbSize := SizeOf(SHELLEXECUTEINFO); ShExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS; ShExecInfo.Wnd := 0; ShExecInfo.lpVerb := nil; ShExecInfo.lpFile := PChar(string(ADir)); ShExecInfo.lpParameters := PChar(AURL); ShExecInfo.lpDirectory := PChar(ExtractFilePath(string(ADir))); ShExecInfo.nShow := SW_SHOW; ShExecInfo.hInstApp := 0; if ShellExecuteEx(@ShExecInfo) then FHandles.Add(Pointer(ShExecInfo.hProcess)) else ShowMessage(SysErrorMessage(GetLastError)); end; //获取所有应用列表 function TAppCentre.GetAllApps: IInterfaceList; var AIdHttp:TIdHTTP; ResponeStr,openkey, AURL: String; begin if FOpenID = '' then begin Result := nil; Exit; end; if FApps <> nil then begin Result := FApps; Exit; end; Result := nil; AIdHttp := TIdHTTP.Create(nil); InitHttpHeader(AIdHttp); try AURL := Format(GETAPPS_URL,[GetAppCentreConfig.GetIP, GetAppCentreConfig.GetPort]); ResponeStr:=AIdHttp.Get(AURL); ResponeStr := Utf8ToAnsi(ResponeStr); FApps := ProcessAppJsonStr(ResponeStr); Debug(Format('Apps Count:%d', [FApps.Count]), 'TAppCentre.GetAllApps'); Result := FApps; DownloadIcon(FApps); except on E: Exception do begin FApps := nil; Result := nil; LoggerImport.Error(E.Message, 'TAppCentre.GetAllApps'); Freeandnil(AIdHttp); end; end; Freeandnil(AIdHttp); end; //获取用户常用的应用列表 function TAppCentre.GetHotApps: IInterfaceList; var AIdHttp:TIdHTTP; ResponeStr: String; AURL: string; begin if FOpenID = '' then begin Result := nil; Exit; end; if FHotApps <> nil then begin Result := FHotApps; Exit; end; Result := nil; AIdHttp := TIdHTTP.Create(nil); InitHttpHeader(AIdHttp); try AURL := Format(GETHOTAPPS_URL,[GetAppCentreConfig.GetIP, GetAppCentreConfig.GetPort]); ResponeStr:=AIdHttp.Get(AURL); ResponeStr := Utf8ToAnsi(ResponeStr); FHotApps := ProcessHotAppJsonStr(ResponeStr); Result := FHotApps; except on E: Exception do begin FHotApps := nil; Result := nil; Freeandnil(AIdHttp); end; end; Freeandnil(AIdHttp); end; function TAppCentre.GetEnable: Boolean; begin Result := GetAppCentreConfig.GetEnable; end; function TAppCentre.Login(ALoginName: WideString; AHotAppView: IHotAppView):WideString; begin Debug(ALoginName, 'TAppCentre.Login'); FHotAppView := AHotAppView; GetOpenID(ALoginName); if FOpenID = '' then Exit; GetAllApps; GetHotApps; end; procedure TAppCentre.Logout; var AHotApp: IHotApp; AApp: IApp; begin FOpenID := ''; while FHandles.Count > 0 do begin TerminateProcess(Cardinal(FHandles[0]), 0); FHandles.Delete(0); end; if Assigned(FDownloadThread) then begin FDownloadThread.Terminate; FDownloadThread.WaitFor; FreeAndNil(FDownloadThread); end; FApps := nil; FHotApps := nil; FHotAppView := nil; if AppCentreFrm <> nil then AppCentreFrm.Close; end; procedure TAppCentre.OnDownloadedIcon(AAppKey: string; AStream: TStream); var AOldBuffer, ANewBuffer: TBytes; begin if AStream = nil then Exit; try GetAppIconProvider.FindIcon(WideString(AAppKey), AOldBuffer); except on E: Exception do begin Debug('奇怪异常:'+E.Message, 'TAppCentre.OnDownloadedIcon('+AAppKey+')'); end; end; AStream.Position := 0; SetLength(ANewBuffer, AStream.Size); AStream.Read(ANewBuffer[0], AStream.Size); if Length(AOldBuffer) <> 0 then begin if MD5Match(BytesToMD5(ANewBuffer), BytesToMD5(AOldBuffer)) then begin Debug('图标没有变,无需更新', 'TAppCentre.OnDownloadedIcon('+AAppKey+')'); Exit; end; end; Debug('更新图标', 'TAppCentre.OnDownloadedIcon('+AAppKey+')'); GetAppIconProvider.Insert(AAppKey, ANewBuffer); if (FHotAppView <> nil) then FHotAppView.OnASyncAppIconDownloaded(AAppKey, ANewBuffer, AStream.Size); end; procedure TAppCentre.OpenAppCentreFrom; begin if AppCentreFrm = nil then begin AppCentreFrm := TAppCentreForm.Create(nil); end; {$IFDEF NXQST} AppCentreFrm.Caption := '纳税之家'; {$ENDIF} AppCentreFrm.Top := (Screen.Height - AppCentreFrm.Height) div 2; AppCentreFrm.Left := (Screen.Width - AppCentreFrm.Width) div 2; AppCentreFrm.Show(); end; procedure TAppCentre.OpenMessageCentre(ALoginName, APassword: WideString); stdcall; var AURL: string; config: IAppCentreConfig; begin if MessageCentreForm = nil then begin MessageCentreForm := TMessageCentreForm.Create(nil); end; config := GetAppCentreConfig; AURL := Format(MESSAGE_CENTRE_URL, [config.GetMessageCentreIP, config.GetMessageCentrePort, ALoginName, APassword]); MessageCentreForm.Top := (Screen.Height - MessageCentreForm.Height) div 2; MessageCentreForm.Left := (Screen.Width - MessageCentreForm.Width) div 2; MessageCentreForm.Show(); MessageCentreForm.chrm1.Load(AURL); end; procedure TAppCentre.OpenWebDebuggerTool; begin if AppCentreFrm = nil then Exit; AppCentreFrm.OpenDebug; end; function TAppCentre.URLEncode(const S: string; const InQueryString: Boolean): string; begin Result := TIdURI.URLEncode(S); end; end.