251 lines
7.0 KiB
C#
251 lines
7.0 KiB
C#
using _29WxOrderWpf.CefHelper;
|
|
using _29WxOrderWpf.Commands;
|
|
using AllBase;
|
|
using CefSharp;
|
|
using CefSharp.Handler;
|
|
using CefSharp.Wpf;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using Work._29Tools.CefTools;
|
|
using WxOrder.Framework.Helpers;
|
|
|
|
namespace _29WxOrderWpf.Ctrls.PDDInvoice.ViewModels;
|
|
|
|
public partial class PDDInvoiceViewModel
|
|
{
|
|
/// <summary>
|
|
/// 霈W��𡑒”瘚讛��?
|
|
/// </summary>
|
|
public ChromiumWebBrowser Browser { get; private set; }
|
|
/// <summary>
|
|
/// ��捆瘚讛��?
|
|
/// 霈W�霂行�
|
|
/// �𤑳巨霂行�
|
|
/// �唾窈�𤑳巨
|
|
/// </summary>
|
|
public ChromiumWebBrowser BrowserDetail { get; private set; }
|
|
|
|
partial void InitBrowser()
|
|
{
|
|
BrowserZIndex = -1;
|
|
BrowserDetailZIndex = -1;
|
|
|
|
Browser = WPFCEFBrowser.CreateChromiumWebBrowser("about:blank", false);
|
|
|
|
LocalResourceRequestHandler resourceRequestHandler = new LocalResourceRequestHandler();
|
|
//resourceRequestHandler.BeforeResourceLoadEvent += RequestHandler_BeforeResourceLoad;
|
|
resourceRequestHandler.ResourceLoadCompleteEvent += RequestHandler_ResourceLoadComplete;
|
|
Browser.RequestHandler = new LocalRequestHandler(resourceRequestHandler);
|
|
Browser.LifeSpanHandler = new LocalLifeSpanHandler(PopWindowType.SelfWindow);
|
|
Browser.KeyboardHandler = new KeyboardHandler();
|
|
Browser.LoadingStateChanged += Browser_LoadingStateChanged;
|
|
|
|
BrowserDetail = WPFCEFBrowser.CreateChromiumWebBrowser("about:blank", false);
|
|
LocalResourceRequestHandler detailResourceRequestHandler = new LocalResourceRequestHandler();
|
|
detailResourceRequestHandler.ResourceLoadCompleteEvent += DetailRequestHandler_ResourceLoadComplete;
|
|
BrowserDetail.RequestHandler = new LocalRequestHandler(resourceRequestHandler);
|
|
BrowserDetail.LifeSpanHandler = new LocalLifeSpanHandler(PopWindowType.SelfWindow);
|
|
BrowserDetail.KeyboardHandler = new KeyboardHandler();
|
|
BrowserDetail.LoadingStateChanged += BrowserDetail_LoadingStateChanged;
|
|
}
|
|
|
|
private void Browser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
|
|
{
|
|
if (e.IsLoading == false)
|
|
{
|
|
var b = (ChromiumWebBrowser)sender;
|
|
b.Dispatcher.InvokeAsync(async () =>
|
|
{
|
|
if (b.Address != null && b.Address.StartsWith("https://mobile.yangkeduo.com/orders.html"))
|
|
{
|
|
StartAutoScroll();
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
private void BrowserDetail_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
|
|
{
|
|
if (e.IsLoading == false)
|
|
{
|
|
var b = (ChromiumWebBrowser)sender;
|
|
b.Dispatcher.InvokeAsync(async () =>
|
|
{
|
|
if (b.Address != null && b.Address.StartsWith("https://mobile.yangkeduo.com/transac_return_compensation.html"))
|
|
{
|
|
string html = await b.GetBrowser().GetSourceAsync();
|
|
if (html.Contains("window.rawData"))
|
|
{
|
|
await ExecuteInvoiceApplyScriptAsync(b);
|
|
}
|
|
else
|
|
{
|
|
// 憿菟𢒰撘�虜嚗�𧊋皜脫��箏�蟡刻”�𤏪��嗥凒�亙ế摰𡁜仃韐伐��踹��寥�撘�蟡券鵭�園𡢿蝑匧�
|
|
_currentInvoiceTcs?.TrySetResult(false);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 瘜典��𤑳巨憛怠��𡁏𧋦撟嗆�銵䎚�?
|
|
/// �訫��唾窈璅∪�靽脲����銵䔶蛹嚗𥟇鸌�誩�蟡冽芋撘譍�頧株砭�𡁏𧋦�坔��?
|
|
/// <c>window.__invoiceApplyResult</c>嚗���𣂼��朞� <see cref="_currentInvoiceTcs"/> �硺�蝏𤘪��?
|
|
/// </summary>
|
|
private async Task ExecuteInvoiceApplyScriptAsync(ChromiumWebBrowser b)
|
|
{
|
|
string content = ResourceHelper.GetResourceText("/Ctrls/PDDInvoice/Resources/invoice.js", "29WxFindOrder");
|
|
content = content.Replace("{�砍虬}", InvoiceTitle);
|
|
content = content.Replace("{蝔𤾸噡}", TaxNo);
|
|
if (string.IsNullOrEmpty(content)) return;
|
|
|
|
await b.EvaluateScriptAsync(content);
|
|
|
|
if (_currentInvoiceTcs == null) return; // �訫��唾窈璅∪�嚗帋�����㕑�銝?
|
|
|
|
// �寥�璅∪�嚗𡁶�敺��甇亥��祆�銵���?
|
|
// �閗繮敶枏� TCS嚗屸��齿鸌�誩儐�航��嗉��乩�銝�銝芾恥�訫�嚗諹��啁�頧株砭霂航圻�烐鰵霈W�����?
|
|
var tcs = _currentInvoiceTcs;
|
|
for (int i = 0; i < 120; i++)
|
|
{
|
|
await Task.Delay(500);
|
|
try
|
|
{
|
|
var res = await b.EvaluateScriptAsync("window.__invoiceApplyResult");
|
|
if (res.Success && res.Result != null)
|
|
{
|
|
tcs.TrySetResult(Convert.ToBoolean(res.Result));
|
|
return;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
tcs.TrySetResult(false);
|
|
}
|
|
|
|
public ICommand CloseInvoiceDetailCommand => new DelegateCommand(HidenOrderBrowserDetail);
|
|
|
|
public int BrowserZIndex
|
|
{
|
|
get => GetValue<int>();
|
|
set => SetValue(value);
|
|
}
|
|
public int BrowserDetailZIndex
|
|
{
|
|
get => GetValue<int>();
|
|
set => SetValue(value);
|
|
}
|
|
/// <summary>
|
|
/// �曄內銝餅�閫�膥
|
|
/// </summary>
|
|
public void ShowOrderBrowser()
|
|
{
|
|
BrowserZIndex = 10;
|
|
BrowserDetailZIndex = -1;
|
|
}
|
|
/// <summary>
|
|
/// �曄內摮鞉�閫�膥
|
|
/// </summary>
|
|
public void ShowOrderBrowserDetail()
|
|
{
|
|
BrowserZIndex = -1;
|
|
BrowserDetailZIndex = 10;
|
|
}
|
|
public void HidenOrderBrowser()
|
|
{
|
|
BrowserZIndex = -1;
|
|
}
|
|
public void HidenOrderBrowserDetail()
|
|
{
|
|
BrowserDetailZIndex = -1;
|
|
}
|
|
public bool IsReadOrder
|
|
{
|
|
get => GetValue<bool>();
|
|
set => SetValue(value);
|
|
}
|
|
private int top = 0;
|
|
private DateTime lastGetOrderDataTime = DateTime.Now;
|
|
private void RequestHandler_ResourceLoadComplete(object sender, string url, string postData, string responseData, IRequest request)
|
|
{
|
|
if (url.StartsWith("https://mobile.yangkeduo.com/orders.html"))
|
|
{
|
|
//if (isAutoScroll == false)
|
|
//{
|
|
// StartAutoScroll();
|
|
//}
|
|
|
|
StartAutoScroll();
|
|
}
|
|
if (url.StartsWith("https://mobile.yangkeduo.com/proxy/api/api/aristotle/order_list_v"))
|
|
{
|
|
lastGetOrderDataTime = DateTime.Now;
|
|
parserData(responseData);
|
|
|
|
//File.WriteAllText(@$"E:\{Guid.NewGuid().ToString()}.txt", responseData);
|
|
}
|
|
}
|
|
public void StartAutoScroll()
|
|
{
|
|
if (IsReadOrder) return;
|
|
|
|
IsReadOrder = true;
|
|
_ = autoScroll(_cancellationTokenSource.Token);
|
|
}
|
|
|
|
// �𨀣迫皛𡁜𢆡
|
|
public void StopAutoScroll()
|
|
{
|
|
IsReadOrder = false;
|
|
_cancellationTokenSource?.Cancel();
|
|
_cancellationTokenSource?.Dispose();
|
|
_cancellationTokenSource = null;
|
|
}
|
|
|
|
private async Task autoScroll(CancellationToken token)
|
|
{
|
|
while (IsReadOrder && !token.IsCancellationRequested)
|
|
{
|
|
if ((DateTime.Now - lastGetOrderDataTime).Seconds > 30) StopAutoScroll();
|
|
|
|
top += Randomx.GetId(450, 600);
|
|
var jsStr = @"window.scroll({
|
|
top: " + top + @",
|
|
left: 0,
|
|
behavior: 'smooth' // �舫�㚁�雿踵��典像皛?
|
|
});";
|
|
|
|
var client = Browser.GetDevToolsClient();
|
|
if (client != null)
|
|
{
|
|
var result = client.Runtime.EvaluateAsync(jsStr, null, true, null, null, true, null, true);
|
|
result.Wait(5000);
|
|
}
|
|
await Task.Delay(Randomx.GetId(100, 300));
|
|
//Thread.Sleep();
|
|
}
|
|
}
|
|
|
|
|
|
private void DetailRequestHandler_ResourceLoadComplete(object sender, string url, string postData, string responseData, IRequest request)
|
|
{
|
|
if (url.StartsWith("https://mobile.yangkeduo.com/transac_return_compensation.html"))
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
}
|