增加发票助手 开票过程中错误提示

This commit is contained in:
chfei committed 2026-10-21 11:30:39 +08:00
1 parent 3affb4a732
commit 483d87f57d
3 files changed
+108 -53

No files matched your search

@@ -63,13 +63,18 @@ public class PddBrowserOrderInvoiceViewModel : BindableBaseEx
set set
{ {
SetValue(value); SetValue(value);
base.RaisePropertyChanged(nameof(InvoiceStatusText)); InvoiceStatusText = value ? "已提交申请" : "待开票";
} }
} }
public int Quantity => order_Goods?.Sum(a => a.goods_number.Value) ?? 0; public int Quantity => order_Goods?.Sum(a => a.goods_number.Value) ?? 0;
private string _invoiceStatusText = "待开票";
/// <summary> 发票状态文案:已开票 / 待开票 </summary> /// <summary> 发票状态文案:已开票 / 待开票 </summary>
public string InvoiceStatusText => IsInvoiced ? "已提交申请" : "待开票"; public string InvoiceStatusText
{
get => _invoiceStatusText;
set => SetValue(ref _invoiceStatusText, value);
}
public DateTime OrderTime public DateTime OrderTime
@@ -2,6 +2,8 @@
using CefSharp; using CefSharp;
using CefSharp.Handler; using CefSharp.Handler;
using CefSharp.Wpf; using CefSharp.Wpf;
using Newtonsoft.Json.Linq;
using NPOI.XWPF.UserModel;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -11,6 +13,7 @@ using System.Threading.Tasks;
using System.Windows.Controls; using System.Windows.Controls;
using Work._29Tools.CefTools; using Work._29Tools.CefTools;
using Work.BaseForms; using Work.BaseForms;
using WxOrder.Framework.Commons;
using WxOrder.Framework.Helpers; using WxOrder.Framework.Helpers;
namespace _29WxOrderWpf.Ctrls.PDDInvoice.ViewModels; namespace _29WxOrderWpf.Ctrls.PDDInvoice.ViewModels;
@@ -29,11 +32,10 @@ public partial class PDDInvoiceViewModel
BrowserDetailZIndex = -1; BrowserDetailZIndex = -1;
BrowserDetail = WPFCEFBrowser.CreateChromiumWebBrowser("about:blank", false); BrowserDetail = WPFCEFBrowser.CreateChromiumWebBrowser("about:blank", false);
LocalResourceRequestHandler resourceRequestHandler = new LocalResourceRequestHandler();
LocalResourceRequestHandler detailResourceRequestHandler = new LocalResourceRequestHandler(); LocalResourceRequestHandler detailResourceRequestHandler = new LocalResourceRequestHandler();
detailResourceRequestHandler.ResourceLoadCompleteEvent += DetailRequestHandler_ResourceLoadComplete; detailResourceRequestHandler.ResourceLoadCompleteEvent += DetailRequestHandler_ResourceLoadComplete;
BrowserDetail.RequestHandler = new LocalRequestHandler(resourceRequestHandler); BrowserDetail.RequestHandler = new LocalRequestHandler(detailResourceRequestHandler);
BrowserDetail.LifeSpanHandler = new LocalLifeSpanHandler(PopWindowType.SelfWindow); BrowserDetail.LifeSpanHandler = new LocalLifeSpanHandler(PopWindowType.SelfWindow);
BrowserDetail.KeyboardHandler = new KeyboardHandler(); BrowserDetail.KeyboardHandler = new KeyboardHandler();
BrowserDetail.LoadingStateChanged += BrowserDetail_LoadingStateChanged; BrowserDetail.LoadingStateChanged += BrowserDetail_LoadingStateChanged;
@@ -68,12 +70,17 @@ public partial class PDDInvoiceViewModel
string html = await b.GetBrowser().GetSourceAsync(); string html = await b.GetBrowser().GetSourceAsync();
if (html.Contains("window.rawData")) if (html.Contains("window.rawData"))
{ {
await ExecuteInvoiceApplyScriptAsync(b, orderSN); if (html.Contains("\"hasApplied\":true,"))
{
_currentInvoiceTcs?.TrySetResult(new Result<bool> { Data = true });
return;
}
await ExecuteInvoiceApplyScriptAsync(b);
} }
else else
{ {
// 页面异常(未渲染出发票表单)时直接判定失败,避免批量开票长时间等待 // 页面异常(未渲染出发票表单)时直接判定失败,避免批量开票长时间等待
_currentInvoiceTcs?.TrySetResult(false); _currentInvoiceTcs?.TrySetResult(new Result<bool> { Data = false });
} }
} }
}); });
@@ -85,54 +92,40 @@ public partial class PDDInvoiceViewModel
/// 单张申请模式保持原有行为;批量开票模式下轮询脚本写入的 /// 单张申请模式保持原有行为;批量开票模式下轮询脚本写入的
/// <c>window.__invoiceApplyResult</c>,完成后通过 <see cref="_currentInvoiceTcs"/> 回传结果。 /// <c>window.__invoiceApplyResult</c>,完成后通过 <see cref="_currentInvoiceTcs"/> 回传结果。
/// </summary> /// </summary>
private async Task ExecuteInvoiceApplyScriptAsync(ChromiumWebBrowser b, string orderSn) private async Task ExecuteInvoiceApplyScriptAsync(ChromiumWebBrowser b)
{ {
string content = ResourceHelper.GetResourceText("/Ctrls/PDDInvoice/Resources/invoice.js", "29WxFindOrder"); string content = ResourceHelper.GetResourceText("/Ctrls/PDDInvoice/Resources/invoice.js", "29WxFindOrder");
content = content.Replace("{公司}", Config.Title); content = content.Replace("{公司}", Config.Title);
content = content.Replace("{税号}", Config.TaxNo); content = content.Replace("{税号}", Config.TaxNo);
if (string.IsNullOrEmpty(content)) return; if (string.IsNullOrEmpty(content)) return;
await b.EvaluateScriptAsync(content); await b.EvaluateScriptAsync(content);//执行js
if (_currentInvoiceTcs == null)
{
var temp = Orders?.FirstOrDefault(a => a.order_sn == orderSn);
if (temp != null) temp.IsInvoiced = true;
return; // 单张申请模式:保持原有行为
}
// 捕获当前 TCS,避免批量循环超时进入下一个订单后,迟到的轮询误触发新订单的结果
var tcs = _currentInvoiceTcs;
// 检查是否注入成功 // 检查是否注入成功
if (await IsInstalledDetailAsync(b) == false) if (await IsInstalledDetailAsync(b) == false)
{ {
IsBatchInvoicing = false; IsBatchInvoicing = false;
BaseMsgForm.ShowError("JS错误,开票失败"); BaseMsgForm.ShowError("JS错误,开票失败");
tcs.TrySetResult(false); _currentInvoiceTcs.TrySetResult(new Result<bool> { Data = false });
return; return;
} }
//for (int i = 0; i < 120; i++)//循环获取结果
//{
// await Task.Delay(500);
// try
for (int i = 0; i < 120; i++) // {
{ // var res = await b.EvaluateScriptAsync("window.__invoiceApplyResult");
await Task.Delay(500); // if (res.Success && res.Result != null)
try // {
{ // _currentInvoiceTcs.TrySetResult(Convert.ToBoolean(res.Result));
var res = await b.EvaluateScriptAsync("window.__invoiceApplyResult"); // return;
if (res.Success && res.Result != null) // }
{ // }
tcs.TrySetResult(Convert.ToBoolean(res.Result)); // catch
return; // {
} // }
} //}
catch //_currentInvoiceTcs.TrySetResult(false);
{
}
}
tcs.TrySetResult(false);
} }
@@ -151,9 +144,40 @@ public partial class PDDInvoiceViewModel
private void DetailRequestHandler_ResourceLoadComplete(object sender, string url, string postData, string responseData, IRequest request) 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")) if (url.StartsWith("https://mobile.yangkeduo.com/proxy/api/api/order/invoice/apply"))
{ {
var obj = JObject.Parse(responseData);
bool isSuccess = obj.Value<bool?>("result") == true;
if (isSuccess)
{
_currentInvoiceTcs.TrySetResult(new Result<bool> { Data = true });
}
else
{
IsBatchInvoicing = false;
string errMsg = obj.Value<string>("error_msg");
_currentInvoiceTcs.TrySetResult(new Result<bool> { Data = false, Msg = errMsg });
}
return;
//{"order_sn":"260921-458426969742556","mall_id":697300659,"trigger_status":1,"business_type":1,"payer_name":"合肥弘贸电子商务有限公司","invoice_kind":0,"invoice_type":0,"letter_head_id":"690562059266556","letterhead_snapshot_id":"SI298036413700604","paper_certificate_url":"","entrust_certify_url":"","source":1,"anti_content":"0asAfoyqYiPs6gd2J0WQ9QOUtOrZ44_LwPI3lrN83FY_wympvl577D6QppXrSLrHJn8eNWv8uXBTdg6_iPUC1aDYt-JuCI1dMpKg2wsf65KUBPzEhRU8pGAbTnWhcJ29ooVcncR90coLgWQiLJ0Xjo0Y4aHEJnnk2lr2PRkY2Kj9ibYc9QW-i2Mr23mjyQMQIahwAhEX-xjSVJEd35qSDQo9VrbY0Fb4kiiL0tHoKEQqmgMcn1BIH9LQB30NJ9s857b25NDDXQyuIT9hR2fCiRoY-96khLJ8QI6wqNL8Q406RBojM6oYsKqEGEFNn7Qyr4N3q-FELBTkkmyToyzs4cM1iUKl3Zaqe0MtFefxmZcLUj3FFxQ6GdAAfKO2WlAkwx7QuMppG7UtkEpRuNjrkojzegy1jaIBWzPrzevvx3egES52PIHEFZASpZ2gNPXaDGSbVGu8shqGDmXuu94aWgXxEE3CzPfVcUZooGs40ThX0ouQF7IFTKM6-NSsk8ZrVjEHdWO1-EE_SCngMCC1vFeAr1g5NvaUCC_87CibfVcWPe5lhgRaq6dKfSAYDoT_bA0nPm6wSDZSxAQbSk-1JZC-O6OpFqXcrkKVUZk0atnozQtKlf1cxcyCl_EkEMnpwZll4g3jEKUxLK_XCIUz0TjDUbF1dpZOsWd39crq7VktSKKZ32b_e0sk6ZkvAf5F7GKwfvfy6RX5YbcrCcpfsjJqaXNGy2G2Y_Oj0iPcpgaXv0zmJjkB217RMtwxBybZnjdOMIlGJWUFls4unwncEvdy4Udgi7EdgB_MXwgn2zGa1_wHKltF7n37wKjKBKlysnfv8mFfqzH3Jv7L4jj9cCQ7P5Fb3E8KQ9J","payer_register_no":"91340103MAKBDKB764","payer_phone":"","payer_account":"","payer_address":"","payer_bank":""}
//{"server_time":1792493897,"result":true}
/*
{
"server_time": 1792493128,
"error_code": 17003,
"error_msg": "商家已与您协商开票,如有需要,请联系商家"
}*/
} }
} }
} }
@@ -1,7 +1,5 @@
using _29WxOrderWpf.BaseWindows; using _29WxOrderWpf.BaseWindows;
using _29WxOrderWpf.CefHelper; using _29WxOrderWpf.CefHelper;
using WxOrder.Framework.Commons;
using WxOrder.Framework.Commons;
using _29WxOrderWpf.Ctrls.Statistics; using _29WxOrderWpf.Ctrls.Statistics;
using AllBase; using AllBase;
using AllBase.Helper; using AllBase.Helper;
@@ -14,6 +12,7 @@ using CefSharp.Wpf;
using NaiBa.Util.Extension; using NaiBa.Util.Extension;
using Newtonsoft.Json; using Newtonsoft.Json;
using NPOI.SS.Formula.Functions; using NPOI.SS.Formula.Functions;
using Org.BouncyCastle.Asn1.X509;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
@@ -37,6 +36,8 @@ using WPFCommon.Converters;
using WPFCommon.Helpers; using WPFCommon.Helpers;
using WPFCommon.Models; using WPFCommon.Models;
using WxOrder.Framework.Commons; using WxOrder.Framework.Commons;
using WxOrder.Framework.Commons;
using WxOrder.Framework.Commons;
using WxOrder.Framework.Commons.Dialogs; using WxOrder.Framework.Commons.Dialogs;
using WxOrder.Framework.Helpers; using WxOrder.Framework.Helpers;
@@ -284,7 +285,7 @@ public partial class PDDInvoiceViewModel : BindableBaseEx, IDialogAware, IDispos
public ICommand ResetCommand => new DelegateCommand(ResetSearch); public ICommand ResetCommand => new DelegateCommand(ResetSearch);
public ICommand OpenInvoiceCommand => new DelegateCommand<PddBrowserOrderInvoiceViewModel>(OpenInvoice); public ICommand OpenInvoiceCommand => new AsyncDelegateCommand<PddBrowserOrderInvoiceViewModel>(OpenInvoice);
public ICommand ShowInvoiceCommand => new DelegateCommand<PddBrowserOrderInvoiceViewModel>(ShowInvoice); public ICommand ShowInvoiceCommand => new DelegateCommand<PddBrowserOrderInvoiceViewModel>(ShowInvoice);
@@ -434,7 +435,7 @@ public partial class PDDInvoiceViewModel : BindableBaseEx, IDialogAware, IDispos
} }
/// <summary> 当前正在批量处理的订单,页面脚本执行完成后通过它回传结果 </summary> /// <summary> 当前正在批量处理的订单,页面脚本执行完成后通过它回传结果 </summary>
private TaskCompletionSource<bool> _currentInvoiceTcs; private TaskCompletionSource<Result<bool>> _currentInvoiceTcs;
public ICommand BatchInvoiceCommand => new AsyncDelegateCommand(BatchInvoice); public ICommand BatchInvoiceCommand => new AsyncDelegateCommand(BatchInvoice);
@@ -471,8 +472,19 @@ public partial class PDDInvoiceViewModel : BindableBaseEx, IDialogAware, IDispos
//Config.TaxNo = TaxNo; //Config.TaxNo = TaxNo;
//Config.MaxAmount = MaxAmount; //Config.MaxAmount = MaxAmount;
//NaiBa.Util.Helper.ConfigHelper.SaveConfig(ConfigPath, Config); //NaiBa.Util.Helper.ConfigHelper.SaveConfig(ConfigPath, Config);
try
{
await BatchInvoiceAsync(targets);
}
catch (Exception ex)
{
BaseMsgWindow.ShowError("批量开票异常:" + ex.Message);
await BatchInvoiceAsync(targets); }
finally
{
BaseMsgWindow.ShowInfo(BottomMsg);
}
} }
public ICommand StopBatchInvoiceCommand => new AsyncDelegateCommand(StopBatchInvoice); public ICommand StopBatchInvoiceCommand => new AsyncDelegateCommand(StopBatchInvoice);
@@ -541,12 +553,12 @@ public partial class PDDInvoiceViewModel : BindableBaseEx, IDialogAware, IDispos
} }
else else
{ {
_currentInvoiceTcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously); _currentInvoiceTcs = new TaskCompletionSource<Result<bool>>(TaskCreationOptions.RunContinuationsAsynchronously);
BrowserDetail.Load($"https://mobile.yangkeduo.com/transac_return_compensation.html?_t_timestamp=transac_modify_invoice&order_sn={order.order_sn}"); BrowserDetail.Load($"https://mobile.yangkeduo.com/transac_return_compensation.html?_t_timestamp=transac_modify_invoice&order_sn={order.order_sn}");
// 等待页面脚本执行完成(最长 60 秒) // 等待页面脚本执行完成(最长 60 秒)
var done = await Task.WhenAny(_currentInvoiceTcs.Task, Task.Delay(TimeSpan.FromSeconds(60))); Task done = await Task.WhenAny(_currentInvoiceTcs.Task, Task.Delay(TimeSpan.FromSeconds(10)));
ok = done == _currentInvoiceTcs.Task && _currentInvoiceTcs.Task.Result; ok = done == _currentInvoiceTcs.Task && _currentInvoiceTcs.Task.Result.Data;
} }
if (ok) if (ok)
{ {
@@ -562,6 +574,10 @@ public partial class PDDInvoiceViewModel : BindableBaseEx, IDialogAware, IDispos
} }
else else
{ {
if (_currentInvoiceTcs.Task.IsCompleted)
{
order.InvoiceStatusText = _currentInvoiceTcs.Task.Result.Msg;
}
fail++; fail++;
} }
ProgressValue = success + fail; ProgressValue = success + fail;
@@ -580,7 +596,7 @@ public partial class PDDInvoiceViewModel : BindableBaseEx, IDialogAware, IDispos
catch (Exception ex) catch (Exception ex)
{ {
fail = targets.Count - success; fail = targets.Count - success;
BaseMsgWindow.ShowError("批量开票异常:" + ex.Message); throw ex;
} }
finally finally
{ {
@@ -588,7 +604,6 @@ public partial class PDDInvoiceViewModel : BindableBaseEx, IDialogAware, IDispos
ProgressMaximum = targets.Count; ProgressMaximum = targets.Count;
ProgressValue = targets.Count; ProgressValue = targets.Count;
IsBatchInvoicing = false; IsBatchInvoicing = false;
BaseMsgWindow.ShowInfo(BottomMsg);
} }
} }
/// <summary> /// <summary>
@@ -642,7 +657,7 @@ public partial class PDDInvoiceViewModel : BindableBaseEx, IDialogAware, IDispos
/// 申请开票 /// 申请开票
/// </summary> /// </summary>
/// <param name="order"></param> /// <param name="order"></param>
private void OpenInvoice(PddBrowserOrderInvoiceViewModel order) private async Task OpenInvoice(PddBrowserOrderInvoiceViewModel order)
{ {
SelectedOrder = order; SelectedOrder = order;
if (string.IsNullOrEmpty(Config.Title) || string.IsNullOrEmpty(Config.TaxNo)) if (string.IsNullOrEmpty(Config.Title) || string.IsNullOrEmpty(Config.TaxNo))
@@ -650,8 +665,19 @@ public partial class PDDInvoiceViewModel : BindableBaseEx, IDialogAware, IDispos
BaseMsgWindow.ShowError("未填写公司或税号"); BaseMsgWindow.ShowError("未填写公司或税号");
return; return;
} }
BrowserDetail.Load($"https://mobile.yangkeduo.com/transac_return_compensation.html?_t_timestamp=transac_modify_invoice&order_sn={order.order_sn}");
ShowOrderBrowserDetail(); ShowOrderBrowserDetail();
//BrowserDetail.Load($"https://mobile.yangkeduo.com/transac_return_compensation.html?_t_timestamp=transac_modify_invoice&order_sn={order.order_sn}");
try
{
await BatchInvoiceAsync(new List<PddBrowserOrderInvoiceViewModel>
{
order
});
}
catch (Exception ex)
{
BaseMsgWindow.ShowError("开票异常:" + ex.Message);
}
} }
private void ShowInvoice(PddBrowserOrderInvoiceViewModel order) private void ShowInvoice(PddBrowserOrderInvoiceViewModel order)