From 079d3b7650056564ed1a47d8f9323f054aa617aa Mon Sep 17 00:00:00 2001 From: Argo-Asicotech Date: Wed, 18 Jan 2023 20:53:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=A2=9E=E5=8A=A0=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=8F=96=E6=B6=88=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Demos/BaiduOcr/BaiduOcrNormal.razor | 78 ++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/src/BootstrapBlazor.Shared/Demos/BaiduOcr/BaiduOcrNormal.razor b/src/BootstrapBlazor.Shared/Demos/BaiduOcr/BaiduOcrNormal.razor index 4922932d4d7..d62ce716fc7 100644 --- a/src/BootstrapBlazor.Shared/Demos/BaiduOcr/BaiduOcrNormal.razor +++ b/src/BootstrapBlazor.Shared/Demos/BaiduOcr/BaiduOcrNormal.razor @@ -1,7 +1,8 @@ @inject IBaiduOcr OcrService @inject ToastService ToastService +@implements IDisposable - + @if (Invoice != null) { @@ -107,4 +108,79 @@ } } } + + /*以下示例为本站特殊处理逻辑可不参考*/ + private bool IsLoading { get; set; } + + private string ButtonIcon { get; } = "fa-solid fa-cloud-arrow-up"; + + private string LoadingIcon { get; } = "fa-solid fa-spinner fa-spin-pulse"; + + private string Icon => IsLoading ? LoadingIcon : ButtonIcon; + + /// + /// 取消请求令牌 + /// + private CancellationTokenSource? TokenSource { get; set; } + + private async Task OnClickToUploadBlock(UploadFile file) + { + if (file.File != null) + { + // 设置 按钮禁用 + IsLoading = true; + StateHasChanged(); + + // 获得上传文件 + var payload = await file.GetBytesAsync(file.File.Size); + if (payload?.Any() ?? false) + { + try + { + TokenSource ??= new(); + var result = await OcrService.CheckVatInvoiceAsync(payload, TokenSource.Token); + Invoice = result.Entity; + if (result.Entity != null) + { + await ToastService.Success("Vat Invoice", "VAT Invoice success!"); + } + else + { + await ToastService.Information("Vat Invoice", $"{result.ErrorCode}: {result.ErrorMessage}"); + } + } + catch (TaskCanceledException) + { + + } + } + + IsLoading = false; + StateHasChanged(); + } + } + + /// + /// 关闭网页时调用 + /// + /// + /// + protected virtual void Dispose(bool disposing) + { + if (disposing && TokenSource != null) + { + TokenSource.Cancel(); + TokenSource.Dispose(); + } + } + + /// + /// 关闭网页时调用 + /// + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } }