| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957 |
- 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;
- /// <summary>
- /// 残联定制
- /// </summary>
- 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.
|