优化拍单

This commit is contained in:
wule committed 2026-10-15 11:14:14 +08:00
1 parent 79ad130fde
commit 18a4373918
2 files changed
+111 -19

No files matched your search

@@ -395,14 +395,14 @@
Grid.Column="1"
Height="20"
MinWidth="48"
Margin="90,0"
Margin="85,0,0,0"
Padding="4,0"
HorizontalAlignment="Left"
Command="{Binding CopyCurrentFullAddressCommand}"
Content="复制当前完整中转地址"
Content="复制完整中转地址"
FontSize="12"
Visibility="{Binding CurrentFullTransitAddress,Converter={StaticResource HasTextVisibility}}"
Style="{StaticResource BasePageButtonLightStyle}" />
Style="{StaticResource BasePageButtonLightStyle}"
Visibility="{Binding CurrentFullTransitAddress, Converter={StaticResource HasTextVisibility}}" />
<Button
x:Name="btnManualUpdateBuyOrder"
Grid.Column="1"
@@ -497,10 +497,13 @@
TitleHorizontalAlignment="Right"
TitleWidth="{Binding ElementName=tcShopGoodsName, Path=TitleWidth}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<TextBox
x:Name="txtSendProvince"
Height="20"
Margin="0,0,96,0"
BorderBrush="{StaticResource pageColorBrush}"
BorderThickness="1"
Style="{StaticResource BaseRoundCornerTextBoxStyle}"
@@ -510,12 +513,15 @@
Grid.Column="1"
Height="20"
MinWidth="48"
Margin="0"
Margin="3,0,0,0"
Padding="4,0"
HorizontalAlignment="Right"
d:Opacity="0.5"
d:Visibility="Visible"
Content="手动更新发货地"
FontSize="12"
Style="{StaticResource BasePageButtonStyle}" />
Style="{StaticResource BasePageButtonStyle}"
Visibility="Collapsed" />
</Grid>
</wpf:TitleContent>
+98 -12
View File
@@ -25,7 +25,9 @@ using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.InteropServices.ComTypes;
@@ -34,6 +36,7 @@ using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
//using System.Web.Hosting;
using System.Windows.Forms;
using Work;
@@ -638,6 +641,9 @@ namespace TemuKJD.ToolForms
}
}
/// <summary>
/// 自动拍单循环
/// </summary>
public void AotoBuyLoop()
{
Task.Run(async () =>
@@ -690,7 +696,7 @@ namespace TemuKJD.ToolForms
Thread.Sleep(Randomx.GetId(500, 1000));
if (!IsAutoBuy) return;
var ts2 = await EvaluateJsBuyTagNameAndInnerHtml("span", "立即支付", true);
for (int i = 0; i < 5; i++)
for (int i = 0; i < 20; i++)
{
if (ts2.Result?.Value is string rstStr && rstStr == "success")
break;
@@ -706,11 +712,11 @@ namespace TemuKJD.ToolForms
Thread.Sleep(Randomx.GetId(500, 1000));
if (!IsAutoBuy) return;
ts2 = await EvaluateJsBuyTagNameAndInnerHtml("span", "确认付款", true);
for (int i = 0; i < 50; i++)
for (int i = 0; i < 25; i++)
{
if (ts2.Result?.Value is string rstStr && rstStr == "success")
break;
Thread.Sleep(100);
Thread.Sleep(500);
ts2 = await EvaluateJsBuyTagNameAndInnerHtml("span", "确认付款", true);
}
@@ -718,12 +724,12 @@ namespace TemuKJD.ToolForms
Thread.Sleep(Randomx.GetId(500, 1000));
if (!IsAutoBuy) return;
ts2 = await EvaluateJsBuyTagNameAndInnerHtml("div", "输入支付密码");
for (int i = 0; i < 50; i++)
for (int i = 0; i < 20; i++)
{
if (ts2.Result?.Value is string rstStr && rstStr == "success")
break;
Thread.Sleep(100);
Thread.Sleep(500);
ts2 = await EvaluateJsBuyTagNameAndInnerHtml("div", "输入支付密码");
}
Thread.Sleep(Randomx.GetId(500, 1000));
@@ -736,12 +742,12 @@ namespace TemuKJD.ToolForms
Thread.Sleep(Randomx.GetId(500, 1000));
if (!IsAutoBuy) return;
ts2 = await EvaluateJsBuyTagNameAndInnerHtml("button", "下一步", true);
for (int i = 0; i < 50; i++)
for (int i = 0; i < 20; i++)
{
if (ts2.Result?.Value is string rstStr && rstStr == "success")
break;
Thread.Sleep(200);
Thread.Sleep(500);
if (!IsAutoBuy) return;
ts2 = await EvaluateJsBuyTagNameAndInnerHtml("button", "下一步", true);
}
@@ -838,10 +844,9 @@ namespace TemuKJD.ToolForms
/// <returns></returns>
public async Task<string> ChangeAddress(TransitWarehouseParam transit)
{
Thread.Sleep(200);
for (int i = 0; i < 50; i++)
{
Thread.Sleep(100);
{
Thread.Sleep(200);
if (host == null || client == null)
{
host = cefWebBrowser.GetBrowser().GetHost();
@@ -1011,8 +1016,22 @@ namespace TemuKJD.ToolForms
Thread.Sleep(500);
if (!this.Disposing && !this.IsDisposed)
{
//MainForm.instance.Invoke(new Action(() => { this.Dispose(); }));
}
//MainForm.instance.Invoke(new Action(() => { this.Dispose(); }));
// 1. 解除 JS 绑定
cefWebBrowser?.JavascriptObjectRepository?.UnRegisterAll();
cefWebBrowser?.CloseDevTools();
// 2. 优雅关闭浏览器
cefWebBrowser?.GetBrowser()?.CloseBrowser(false);
// 3. 清理残留子进程(按路径判断,避免误杀)
//CleanupBrowserSubprocesses();
// 4. 释放控件
cefWebBrowser?.Dispose();
cefWebBrowser = null;
}
});
}
@@ -1805,5 +1824,72 @@ namespace TemuKJD.ToolForms
{
this.pnlMsg.Visible = false;
}
/// <summary>
/// 清理本应用创建的CefSharp.BrowserSubprocess子进程
/// </summary>
private void CleanupBrowserSubprocesses()
{
try
{
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 = Path.GetDirectoryName(moduleFileName);
if (!string.IsNullOrEmpty(processDir) &&
(processDir.Equals(appBasePath, StringComparison.OrdinalIgnoreCase)
|| appBasePath.Contains(processDir) // 处理子目录情况
|| moduleFileName.Contains(appBasePath)))
{
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}");
}
}
}
}