using BBWYB.Client.Helpers; using Microsoft.Extensions.DependencyInjection; using Microsoft.Web.WebView2.Core; using SJ.Controls; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows; namespace BBWYB.Client.Views._1688 { /// /// NewLogin1688Window.xaml 的交互逻辑 /// public partial class NewLogin1688Window : Window { CustomWebView2 webView2; CoreWebView2 web; public NewLogin1688Window(string userName, string password) { InitializeComponent(); webView2 = new CustomWebView2("bbwyb_web_1688"); webView2.CoreWebView2InitializationCompleted += WebView2_CoreWebView2InitializationCompleted; this.Width = SystemParameters.WorkArea.Size.Width * 0.8; this.Height = SystemParameters.WorkArea.Size.Height * 0.7; var sp = (App.Current as App).ServiceProvider; using (var s = sp.CreateScope()) { globalContext = s.ServiceProvider.GetRequiredService(); } UserName = userName; Password = password; gd.Children.Add(webView2); } GlobalContext globalContext; public string UserName { get; set; } public string Password { get; set; } private void WebView2_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e) { // webView2.CoreWebView2.DocumentTitleChanged += CoreWebView2_DocumentTitleChanged; web = webView2.CoreWebView2; var url = "https://login.taobao.com/?redirect_url=https%3A%2F%2Flogin.1688.com%2Fmember%2Fjump.htm%3Ftarget%3Dhttps%253A%252F%252Flogin.1688.com%252Fmember%252FmarketSigninJump.htm%253FDone%253Dhttps%25253A%25252F%25252Fwww.1688.com%25252F&style=tao_custom&from=1688web"; webView2.CoreWebView2.Navigate(url); //注册winning脚本c#互操作 // webView2.CoreWebView2.AddHostObjectToScript("hyCoreModel", globalContext); //注册全局变量winning // webView2.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync("let hyCoreModel= window.chrome.webview.hostObjects.hyCoreModel;"); // web.AddWebResourceRequestedFilter("nacollector://home/img/*", CoreWebView2WebResourceContext.All); //web.NavigationCompleted += Web_NavigationCompleted; webView2.NavigationCompleted += Web_NavigationCompleted; } string lastUrl = ""; private void Web_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e) { if (sender != null && sender is Microsoft.Web.WebView2.Wpf.WebView2) { var wv2 = sender as Microsoft.Web.WebView2.Wpf.WebView2; var url = wv2.CoreWebView2.Source; if (url.StartsWith("https://login.taobao.com/") && lastUrl != url) { lastUrl = url; App.Current.Dispatcher.Invoke(() => { DoJavaScript($"document.getElementById('fm-login-id').value='{UserName}'",0); DoJavaScript($"document.getElementById('fm-login-password').value='{Password}'",0); DoJavaScript("document.querySelector('.fm-button.fm-submit.password-login').click();",0); }); } if (url.StartsWith("https://www.1688.com/") && lastUrl != url)//手机端登录 保存账号密码 { lastUrl = wv2.CoreWebView2.Source; App.Current.Dispatcher.Invoke(async () => { await GetCookies("https://www.1688.com/"); webView2.DisposeWeb(); this.Close(); }); } } } public string downCookies { get; set; } = ""; static object obj { get; set; } = new object(); public Dictionary dicCookies { get; set; } = new Dictionary(); /// /// 获取cookie /// public async Task GetCookies(string url) { if (webView2.CoreWebView2 != null) { var cookieManager = webView2.CoreWebView2.CookieManager; List cookies = await cookieManager.GetCookiesAsync(url); foreach (CoreWebView2Cookie cookie in cookies) { if (dicCookies.Keys.Contains(cookie.Name)) { dicCookies[cookie.Name] = cookie.Value; continue; } dicCookies.Add(cookie.Name, cookie.Value); } //存本地下次打开植入 foreach (var item in dicCookies.Keys) { downCookies += $"{item}={dicCookies[item]}; "; } } } /// /// 执行js /// public (bool isOk, object result) DoJavaScript(string js, double timeSleep =5) { try { string result = null; string script = $"(()=>{{{js}}})()"; Application.Current.Dispatcher.Invoke(async () => { result = await web.ExecuteScriptAsync(script); }).Wait(TimeSpan.FromSeconds(timeSleep)); if (int.TryParse(result, out var val)) { return (true, val); } return (true, result); } catch (Exception ex) { return (false, null); } } } }