unit uYouYouMgt;
interface
uses
uSimpleBase, uEdgeCls, uIdhttpEx, System.SysUtils, System.StrUtils, uWebView2Horse, WebView2_TLb, uWebView2common,
Winapi.ActiveX, Comobj, System.Variants;
type
TYouYouMgt = class(TSimpleBase)
private
FCmd: integer;
FIdhttp: TIdhttpEx;
FSessionId: string;
FWebView2Horse: TWebview2Horse;
procedure OnExecScriptProcTing(AResult: HRESULT; AResultObjectAsJson: string);
procedure OnCreateDoc();
procedure OnWebResourceResponseReceivedTing(Sender: ICoreWebView2; Args: ICoreWebView2WebResourceResponseReceivedEventArgs);
procedure OnGetCookiesTing(Sender: TWebview2Horse);
public
procedure Init;
procedure GetVeriyCode;
procedure Login;
public
constructor Create;
destructor Destroy; override;
public
BoEdgeBrowser: uEdgeCls.TEdgeBrowser;
MoblieNum: string;
MainUrl: string;
CodeStr: string;
end;
implementation
{ TYouYouMgt }
uses
uOperateIndy, SuperObject, uStrUtils;
procedure TYouYouMgt.OnCreateDoc;
begin
// FWebView2Horse.UpdateCookies(BoEdgeBrowser.DefaultInterface); 此句为设置到网页中去
// 增加 拦截网址打开后的功能,让 OnWebResourceResponseReceivedTing 生效
FWebView2Horse.AddFilter(BoEdgeBrowser.DefaultInterface, '*');
end;
procedure TYouYouMgt.OnExecScriptProcTing(AResult: HRESULT; AResultObjectAsJson: string);
begin
end;
procedure TYouYouMgt.OnGetCookiesTing(Sender: TWebview2Horse);
begin
// 此处已获取到 cookie , 在 FWebView2Horse.CookieList 中
DoOnMsg(FWebView2Horse.CookieList.ToDetail); // 显示cookie
end;
procedure TYouYouMgt.OnWebResourceResponseReceivedTing(Sender: ICoreWebView2; Args: ICoreWebView2WebResourceResponseReceivedEventArgs);
var
LResponse: ICoreWebView2WebResourceResponseView;
// LResponseHeaders: ICoreWebView2HttpResponseHeaders;
LRequest: ICoreWebView2WebResourceRequest;
// LRequestHeaders: ICoreWebView2HttpRequestHeaders;
// LIt: ICoreWebView2HttpHeadersCollectionIterator;
pUri: PWideChar;
// pMethod: PWideChar;
// pName, pValue: PWideChar;
// LHasNext: integer;
// pContentEncoding: PWideChar;
// sContentEncoding: string;
sTmpUrl: string;
sUri: string;
sPayLoad: string;
J, E: ISuperObject;
begin
Args.Get_Request(LRequest);
LRequest.Get_uri(pUri);
sUri := string(pUri);
// DispMsg(pUri);
Args.Get_Response(LResponse);
DispMsg(string(pUri)); // 显示拦截到的 url;
sTmpUrl := 'http://xxx/' ;// 弄一个合适的网址,只要打开此网址后有 cookie 就行了.
if PosEx(sTmpUrl, sUri) > 0 then
begin
DispMsg('检测到已登录URL请求:' + sTmpUrl);
FWebView2Horse.GetCookies(BoEdgeBrowser.DefaultInterface); // 获取 cookie;
end;
end;
constructor TYouYouMgt.Create;
begin
inherited;
FIdhttp := TIdhttpEx.Create(nil);
FWebView2Horse := TWebview2Horse.Create;
// 当打开 url 后
FWebView2Horse.OnWebResourceResponseReceived := self.OnWebResourceResponseReceivedTing;
FWebView2Horse.OnGetCookies := self.OnGetCookiesTing;
end;
destructor TYouYouMgt.Destroy;
begin
FIdhttp.Free;
FWebView2Horse.Free;
inherited;
end;
procedure TYouYouMgt.GetVeriyCode;
var
sUrl: string;
sHtml: string;
sPayLoad: string;
J: ISuperObject;
sUid: string;
G: TGUID;
begin
sUrl := 'https://api.youpin898.com/api/user/Auth/SendSignInSmsCode';
FIdhttp.Request.UserAgent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36';
FIdhttp.Request.Accept := 'application/json, text/plain, */*';
FIdhttp.Request.AcceptEncoding := 'gzip, deflate, br';
FIdhttp.Request.AcceptLanguage := 'zh-CN,zh;q=0.9,de;q=0.8';
FIdhttp.Request.ContentType := 'application/json';
J := so();
CreateGUID(G);
sUid := LowerCase(G.ToString);
sUid := ReplaceAll(sUid, '{', '');
sUid := ReplaceAll(sUid, '}', '');
DispMsg(sUid);
FSessionId := sUid;
J.S['Mobile'] := MoblieNum;
J.S['SessionId'] := FSessionId;
sPayLoad := J.AsString;
DispMsg(sPayLoad);
if IdhttpPost(FIdhttp, sPayLoad, sUrl, sHtml) then
begin
// DispMsg(FIdhttp.CookieMgr.CurCookies);
end;
DispMsg(sHtml);
end;
procedure TYouYouMgt.Init;
begin
if Assigned(BoEdgeBrowser) then
begin
BoEdgeBrowser.OnEdgeMsg := self.OnDispMsgTing;
BoEdgeBrowser.AppendCreateViewCompletedWorkProc(OnCreateDoc);
end;
end;
procedure TYouYouMgt.Login;
var
sUrl: string;
sHtml: string;
sPayLoad: string;
J, jData: ISuperObject;
w: TWkCookie;
begin
sUrl := 'https://api.youpin898.com/api/user/Auth/SmsSignIn';
FIdhttp.Request.UserAgent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36';
FIdhttp.Request.Accept := 'application/json, text/plain, */*';
FIdhttp.Request.AcceptEncoding := 'gzip, deflate, br';
FIdhttp.Request.AcceptLanguage := 'zh-CN,zh;q=0.9,de;q=0.8';
FIdhttp.Request.ContentType := 'application/json';
J := so();
J.S['Mobile'] := MoblieNum;
J.S['SessionId'] := FSessionId;
J.S['Code'] := CodeStr;
sPayLoad := J.AsString;
DispMsg(sPayLoad);
if IdhttpPost(FIdhttp, sPayLoad, sUrl, sHtml) then
begin
// DispMsg(FIdhttp.CookieMgr.CurCookies);
end;
DispMsg(sHtml);
{ sHtml := LoadTextFromFile('登录成功.txt');
J := so(sHtml);
DispMsg(J.AsString); }
J := so(sHtml);
jData := J.O['Data'];
FWebView2Horse.CookieList.ClearAndFreeAllItems;
w := FWebView2Horse.CookieList.AddNewOne;
w.CookieName := 'token';
w.Value := jData.S['Token'];
w.Domain := '.youpin898.com';
DispMsg(w.ToStr);
BoEdgeBrowser.Navigate(MainUrl);
end;
end.