步步为盈
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.

90 lines
3.3 KiB

3 years ago
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Web.WebView2.Core;
3 years ago
using Microsoft.Web.WebView2.Wpf;
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
3 years ago
using io = System.IO;
namespace BBWY.Client.Views.Order
{
/// <summary>
/// GrabJDMibole.xaml 的交互逻辑
/// </summary>
public partial class GrabJDMibole : Window
{
3 years ago
private WebView2Manager w2m;
3 years ago
//private Stopwatch sw;
private string orderId;
private bool isNavigated;
3 years ago
public GrabJDMibole(string orderId)
{
InitializeComponent();
this.orderId = orderId;
this.Loaded += GrabJDMibole_Loaded;
this.Unloaded += GrabJDMibole_Unloaded;
}
private void GrabJDMibole_Unloaded(object sender, RoutedEventArgs e)
{
3 years ago
grid.Children.Remove(w2m.wb2);
w2m = null;
3 years ago
}
private void GrabJDMibole_Loaded(object sender, RoutedEventArgs e)
{
3 years ago
var sp = (App.Current as App).ServiceProvider;
using (var s = sp.CreateScope())
3 years ago
{
3 years ago
w2m = s.ServiceProvider.GetRequiredService<WebView2Manager>();
3 years ago
}
3 years ago
grid.Children.Add(w2m.wb2);
if(w2m.IsInitializationCompleted)
txtMsg.Text = "Grabbing Mobile";
3 years ago
w2m.CoreWebView2InitializationCompleted = (e) =>
{
isNavigated = true;
3 years ago
w2m.wb2.CoreWebView2.Navigate($"https://neworder.shop.jd.com/order/orderDetail?orderId={orderId}");
};
w2m.OnNavigationCompleted = async (e) =>
3 years ago
{
3 years ago
if (w2m.wb2.CoreWebView2.Source.StartsWith("https://passport.shop.jd.com/login"))
{
//首次打开需要登录
await w2m.wb2.CoreWebView2.ExecuteScriptAsync("window.scrollTo(790,150)");
await w2m.wb2.CoreWebView2.ExecuteScriptAsync("document.querySelector(\"div[data-tab-id='form']\").click()");
}
else if (w2m.wb2.CoreWebView2.Source.StartsWith("https://neworder.shop.jd.com/order/orderDetail"))
{
//进入订单详情页面,触发点击查看手机号
txtMsg.Text = "Grabbing Mobile";
w2m.wb2.Width = 0.1;
w2m.wb2.Height = 0.1;
3 years ago
var js = @"var mobileNode = document.getElementById('mobile');
3 years ago
mobileNode.addEventListener('DOMNodeInserted',function(e){ var m=mobileNode.innerText; window.chrome.webview.postMessage(m);});
document.getElementById('viewOrderMobile').click();";
3 years ago
_ = await w2m.wb2.CoreWebView2.ExecuteScriptAsync(js);
}
};
w2m.OnWebMessageReceived = (e) =>
{
var mobole = e.TryGetWebMessageAsString();
this.Tag = mobole;
this.DialogResult = true;
this.Close();
};
if (w2m.IsInitializationCompleted && !isNavigated)
{
w2m.wb2.CoreWebView2.Navigate($"https://neworder.shop.jd.com/order/orderDetail?orderId={orderId}");
isNavigated = true;
}
3 years ago
}
}
}