Skip to content

Commit 19ada68

Browse files
authored
doc: add asynchronous operation function to the VAT invoice upload button (#332)
1 parent e70118f commit 19ada68

1 file changed

Lines changed: 77 additions & 1 deletion

File tree

src/BootstrapBlazor.Shared/Demos/BaiduOcr/BaiduOcrNormal.razor

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
@inject IBaiduOcr OcrService
22
@inject ToastService ToastService
3+
@implements IDisposable
34

4-
<ButtonUpload TValue="string" OnChange="@OnClickToUpload" BrowserButtonText="VATInvoice" ShowUploadFileList="false"></ButtonUpload>
5+
<ButtonUpload TValue="string" OnChange="@OnClickToUploadBlock" BrowserButtonText="VATInvoice" ShowUploadFileList="false" IsDisabled="IsLoading" BrowserButtonIcon="@Icon"></ButtonUpload>
56

67
@if (Invoice != null)
78
{
@@ -107,4 +108,79 @@
107108
}
108109
}
109110
}
111+
112+
/*以下示例为本站特殊处理逻辑可不参考*/
113+
private bool IsLoading { get; set; }
114+
115+
private string ButtonIcon { get; } = "fa-solid fa-cloud-arrow-up";
116+
117+
private string LoadingIcon { get; } = "fa-solid fa-spinner fa-spin-pulse";
118+
119+
private string Icon => IsLoading ? LoadingIcon : ButtonIcon;
120+
121+
/// <summary>
122+
/// 取消请求令牌
123+
/// </summary>
124+
private CancellationTokenSource? TokenSource { get; set; }
125+
126+
private async Task OnClickToUploadBlock(UploadFile file)
127+
{
128+
if (file.File != null)
129+
{
130+
// 设置 按钮禁用
131+
IsLoading = true;
132+
StateHasChanged();
133+
134+
// 获得上传文件
135+
var payload = await file.GetBytesAsync(file.File.Size);
136+
if (payload?.Any() ?? false)
137+
{
138+
try
139+
{
140+
TokenSource ??= new();
141+
var result = await OcrService.CheckVatInvoiceAsync(payload, TokenSource.Token);
142+
Invoice = result.Entity;
143+
if (result.Entity != null)
144+
{
145+
await ToastService.Success("Vat Invoice", "VAT Invoice success!");
146+
}
147+
else
148+
{
149+
await ToastService.Information("Vat Invoice", $"{result.ErrorCode}: {result.ErrorMessage}");
150+
}
151+
}
152+
catch (TaskCanceledException)
153+
{
154+
155+
}
156+
}
157+
158+
IsLoading = false;
159+
StateHasChanged();
160+
}
161+
}
162+
163+
/// <summary>
164+
/// 关闭网页时调用
165+
/// </summary>
166+
/// <param name="disposing"></param>
167+
/// <returns></returns>
168+
protected virtual void Dispose(bool disposing)
169+
{
170+
if (disposing && TokenSource != null)
171+
{
172+
TokenSource.Cancel();
173+
TokenSource.Dispose();
174+
}
175+
}
176+
177+
/// <summary>
178+
/// 关闭网页时调用
179+
/// </summary>
180+
/// <returns></returns>
181+
public void Dispose()
182+
{
183+
Dispose(true);
184+
GC.SuppressFinalize(this);
185+
}
110186
}

0 commit comments

Comments
 (0)