You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
214 lines
6.9 KiB
214 lines
6.9 KiB
using CommunityToolkit.Mvvm.Messaging;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Web.WebView2.Core;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Data;
|
|
using System.Windows.Documents;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Shapes;
|
|
using WebView2.DevTools.Dom;
|
|
|
|
namespace BBWYB.Client.Views._1688
|
|
{
|
|
/// <summary>
|
|
/// Login1688Window.xaml 的交互逻辑
|
|
/// </summary>
|
|
public partial class Login1688Window : System.Windows.Window
|
|
{
|
|
private WebView2Manager w2m;
|
|
private bool isNavigated;
|
|
private GlobalContext globalContext;
|
|
|
|
public static Login1688Window ShowUrlSearchWindow(string userName, string password)
|
|
{
|
|
Login1688Window window = null;
|
|
|
|
bool? result = null;
|
|
if (!App.Current.Dispatcher.CheckAccess())
|
|
{
|
|
try
|
|
{
|
|
App.Current.Dispatcher.Invoke(() => {
|
|
window = new Login1688Window(userName, password);
|
|
window.ShowDialog();
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
window = new Login1688Window(userName, password);
|
|
window.ShowDialog();
|
|
// 在 UI 线程上执行操作
|
|
}
|
|
return window;
|
|
|
|
}
|
|
|
|
public Login1688Window(string userName, string password)
|
|
{
|
|
InitializeComponent();
|
|
|
|
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())
|
|
{
|
|
w2m =new WebView2Manager();
|
|
globalContext = s.ServiceProvider.GetRequiredService<GlobalContext>();
|
|
}
|
|
UserName = userName;
|
|
Password = password;
|
|
initWebView();
|
|
|
|
|
|
}
|
|
|
|
public string UserName { get; set; }
|
|
public string Password { get; set; }
|
|
|
|
|
|
private void initWebView()
|
|
{
|
|
|
|
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";
|
|
w2m.Init("bbwyb_web_1688");
|
|
w2m.wb2.SetValue(Grid.RowProperty, 1);
|
|
w2m.wb2.Margin = new Thickness(1, 0, 1, 0);
|
|
//grid.Children.Clear();
|
|
gd.Children.Add(w2m.wb2);
|
|
|
|
w2m.wb2.NavigationCompleted += Wb2_NavigationCompleted;
|
|
|
|
|
|
w2m.CoreWebView2InitializationCompleted += (s) =>
|
|
{
|
|
w2m.wb2.CoreWebView2.Navigate(url);
|
|
};
|
|
|
|
}
|
|
|
|
private void Wb2_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
|
|
{
|
|
if (sender != null && sender is Microsoft.Web.WebView2.Wpf.WebView2)
|
|
{
|
|
var wv2 = sender as Microsoft.Web.WebView2.Wpf.WebView2;
|
|
|
|
|
|
if (wv2.CoreWebView2.Source.StartsWith("https://login.taobao.com/"))
|
|
{
|
|
|
|
App.Current.Dispatcher.Invoke(async () =>
|
|
{
|
|
await ExecuteJavaScript($"document.getElementById('fm-login-id').value='{UserName}'",1);
|
|
Thread.Sleep(100);
|
|
await ExecuteJavaScript($"document.getElementById('fm-login-password').value='{Password}'",1);
|
|
Thread.Sleep(100);
|
|
await ExecuteJavaScript("document.querySelector('.fm-button.fm-submit.password-login').click();", 1);
|
|
});
|
|
}
|
|
|
|
|
|
|
|
if (wv2.CoreWebView2.Source == ("https://www.1688.com/"))//手机端登录 保存账号密码
|
|
{
|
|
|
|
App.Current.Dispatcher.Invoke(async () =>
|
|
{
|
|
await GetCookies("https://www.1688.com/");
|
|
w2m.Close();
|
|
GC.Collect();
|
|
this.Close();
|
|
});
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 执行js函数
|
|
/// </summary>
|
|
/// <param name="js"></param>
|
|
/// <param name="type">0 有返回值 1无返回值 </param>
|
|
/// <returns></returns>
|
|
public async Task<(bool istrue, string res)> ExecuteJavaScript(string js, int type = 0)
|
|
{
|
|
try
|
|
{
|
|
string script = $"()=>{{ return {js}}}";
|
|
|
|
if (type != 0)
|
|
script = $"()=>{{ {js}}}";
|
|
|
|
|
|
string result = "";
|
|
await Application.Current.Dispatcher.Invoke(async () =>
|
|
{
|
|
await w2m.wb2?.EnsureCoreWebView2Async();
|
|
|
|
await using var devToolsContext = await w2m.wb2?.CoreWebView2?.CreateDevToolsContextAsync().WaitAsync((TimeSpan.FromSeconds(5)));//
|
|
|
|
result = await devToolsContext.EvaluateFunctionAsync<string>(script).WaitAsync(TimeSpan.FromSeconds(5));
|
|
});
|
|
return new(true, result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// 处理可能的异常
|
|
Console.WriteLine(ex.Message);
|
|
|
|
return new(false, ex.Message);
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
public string downCookies { get; set; } = "";
|
|
|
|
static object obj { get; set; } = new object();
|
|
public Dictionary<string, string> dicCookies { get; set; } = new Dictionary<string, string>();
|
|
/// <summary>
|
|
/// 获取cookie
|
|
/// </summary>
|
|
public async Task GetCookies(string url)
|
|
{
|
|
|
|
if (w2m.wb2.CoreWebView2 != null)
|
|
{
|
|
var cookieManager = w2m.wb2.CoreWebView2.CookieManager;
|
|
List<CoreWebView2Cookie> 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]}; ";
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
|