优化拍单 修复异常拍单和状态不准确订单无法拦截退回问题
This commit is contained in:
1 parent
e71ef72475
commit
75aae91b66
6 files changed
+130
-25
No files matched your search
@@ -11,6 +11,8 @@ using NaiBa.Util.Extension;
|
||||
using Org.BouncyCastle.Asn1.Ocsp;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Security.Policy;
|
||||
using System.Text;
|
||||
@@ -35,6 +37,8 @@ namespace _29WxOrderWpf.ViewModels
|
||||
// 拼多多买手退货退款售后地址
|
||||
// https://mobile.yangkeduo.com/complaint.html?order_sn=260718-101481213440980&after_sales_reason=0&after_sales_type=2&after_sales_code=1002
|
||||
|
||||
List<Process> cefSubProcesses = new List<Process>();
|
||||
|
||||
public static TaobaoLoginViewModel TaobaoLoginInstance { get; set; } = null;
|
||||
|
||||
/// <summary>浏览器创建完成后触发(视图在此把浏览器加入可视树)</summary>
|
||||
@@ -160,7 +164,6 @@ namespace _29WxOrderWpf.ViewModels
|
||||
}
|
||||
Task.Run(() =>
|
||||
{
|
||||
|
||||
AutoLogin();
|
||||
});
|
||||
}
|
||||
@@ -171,6 +174,90 @@ namespace _29WxOrderWpf.ViewModels
|
||||
public void Stop()
|
||||
{
|
||||
_isWindowLoaded = false;
|
||||
|
||||
// 1. 解除 JS 绑定
|
||||
CefWebBrowser?.JavascriptObjectRepository?.UnRegisterAll();
|
||||
CefWebBrowser?.CloseDevTools();
|
||||
|
||||
// 2. 优雅关闭浏览器
|
||||
CefWebBrowser?.GetBrowser()?.CloseBrowser(false);
|
||||
|
||||
// 3. 清理残留子进程(按路径判断,避免误杀)
|
||||
CleanupBrowserSubprocesses(this.cefSubProcesses);
|
||||
|
||||
// 4. 释放控件
|
||||
CefWebBrowser?.Dispose();
|
||||
CefWebBrowser = null;
|
||||
}
|
||||
|
||||
public static void CleanupBrowserSubprocesses(List<Process> processes = null)
|
||||
{
|
||||
// 有可能释放错误导致程序崩溃,废弃
|
||||
return;
|
||||
try
|
||||
{
|
||||
if (processes == null)
|
||||
processes = new List<Process>();
|
||||
|
||||
string appBasePath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\', '/');
|
||||
Process[] subprocesses = Process.GetProcessesByName("CefSharp.BrowserSubprocess");
|
||||
|
||||
foreach (Process process in subprocesses)
|
||||
{
|
||||
bool shouldKill = false;
|
||||
try
|
||||
{
|
||||
// 安全地获取进程模块信息,进程可能在我们检查时已退出
|
||||
if (process != null && !process.HasExited)
|
||||
{
|
||||
string moduleFileName = process.MainModule?.FileName;
|
||||
if (!string.IsNullOrEmpty(moduleFileName))
|
||||
{
|
||||
// 判断进程是否从当前应用目录启动
|
||||
string processDir = System.IO.Path.GetDirectoryName(moduleFileName);
|
||||
if (!string.IsNullOrEmpty(processDir) &&
|
||||
(processDir.Equals(appBasePath, StringComparison.OrdinalIgnoreCase)
|
||||
|| appBasePath.Contains(processDir) // 处理子目录情况
|
||||
|| moduleFileName.Contains(appBasePath)))
|
||||
{
|
||||
if (!processes.Any(p => p.Handle.ParseToInt() == process.Handle.ParseToInt()))
|
||||
shouldKill = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
// 进程可能在检查过程中已退出,忽略
|
||||
continue;
|
||||
}
|
||||
catch (Win32Exception ex) when (ex.NativeErrorCode == 5) // 访问被拒绝
|
||||
{
|
||||
// 可能是权限问题或其他应用的进程,跳过
|
||||
continue;
|
||||
}
|
||||
|
||||
if (shouldKill)
|
||||
{
|
||||
try
|
||||
{
|
||||
process.Kill();
|
||||
process.WaitForExit(500); // 等待最多500毫秒,确保进程终止
|
||||
}
|
||||
catch (Exception killEx)
|
||||
{
|
||||
// 记录日志,但通常进程已死
|
||||
//System.Diagnostics.Debug.WriteLine($"终止进程时出错: {killEx.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 这里最好不要用MessageBox,尤其是在关闭流程中,可能引发线程问题
|
||||
// 建议记录到日志文件
|
||||
//System.Diagnostics.Debug.WriteLine($"清理子进程时发生异常: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -184,6 +271,8 @@ namespace _29WxOrderWpf.ViewModels
|
||||
{
|
||||
if (CefWebBrowser == null)
|
||||
{
|
||||
this.cefSubProcesses = Process.GetProcessesByName("CefSharp.BrowserSubprocess").ToList();
|
||||
|
||||
if (!string.IsNullOrEmpty(InitUrl))
|
||||
CefWebBrowser = WPFCEFBrowser.CreateChromiumWebBrowser(InitUrl, false);
|
||||
else
|
||||
@@ -223,7 +312,7 @@ namespace _29WxOrderWpf.ViewModels
|
||||
|
||||
while (!CefWebBrowser.IsBrowserInitialized || !CefWebBrowser.GetBrowser().MainFrame.IsValid)
|
||||
await Task.Delay(20);
|
||||
await WPFCEFBrowser.DeleteCookieAsync(CefWebBrowser.GetCookieManager(null));
|
||||
//await WPFCEFBrowser.DeleteCookieAsync(CefWebBrowser.GetCookieManager(null));
|
||||
|
||||
//cef 138 如果没有设置RequestContext 可能为空,直接getCookieManager
|
||||
//await WPFCEFBrowser.SetCookieAsync(JsonHelper.SerializeObject(cookieLst), CefWebBrowser.GetCookieManager(null));
|
||||
|
||||
Reference in new issue
Block a user