Skip to content

Commit bca1a53

Browse files
ArgoZhangdensen2014
andcommitted
doc: add verify vat invoice sample code (#344)
Co-authored-by: alex chow <zhouchuanglin@gmail.com>
1 parent ca8147d commit bca1a53

3 files changed

Lines changed: 101 additions & 11 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
@inject IBaiduOcr OcrService
2+
@inject ToastService ToastService
3+
@implements IDisposable
4+
5+
<ValidateForm Model="Model" OnValidSubmit="@Verify">
6+
<EditorForm TModel="InvoiceForm" RowType="RowType.Inline" AutoGenerateAllItem="false">
7+
<FieldItems>
8+
<EditorItem @bind-Field="@context.InvoiceCode" Text="发票代码" />
9+
<EditorItem @bind-Field="@context.InvoiceNum" Text="发票号码" />
10+
<EditorItem @bind-Field="@context.InvoiceDate" Text="开票日期" />
11+
<EditorItem @bind-Field="@context.CheckCode" Text="校验码" />
12+
<EditorItem @bind-Field="@context.TotalAmount" Text="金额" />
13+
</FieldItems>
14+
<Buttons>
15+
<Button ButtonType="ButtonType.Submit" Icon="fa-solid fa-check" Text="Verify" />
16+
</Buttons>
17+
</EditorForm>
18+
</ValidateForm>
19+
20+
<div class="mt-3">@InvoiceVerifyResult?.VerifyMessage</div>
21+
22+
@code {
23+
/// <summary>
24+
/// 取消请求令牌
25+
/// </summary>
26+
private CancellationTokenSource? TokenSource { get; set; }
27+
28+
private InvoiceVerifyResult? InvoiceVerifyResult { get; set; }
29+
30+
private InvoiceForm Model { get; set; } = new() { InvoiceType = "elec_special_vat_invoice", TotalAmount = "0" };
31+
32+
private async Task Verify(EditContext context)
33+
{
34+
var result = await OcrService.VerifyInvoiceAsync(Model.InvoiceCode, Model.InvoiceNum, Model.InvoiceDate, Model.InvoiceType, Model.CheckCode, Model.TotalAmount);
35+
InvoiceVerifyResult = result.Entity;
36+
StateHasChanged();
37+
}
38+
39+
/// <summary>
40+
/// 关闭网页时调用
41+
/// </summary>
42+
/// <param name="disposing"></param>
43+
/// <returns></returns>
44+
protected virtual void Dispose(bool disposing)
45+
{
46+
if (disposing && TokenSource != null)
47+
{
48+
TokenSource.Cancel();
49+
TokenSource.Dispose();
50+
}
51+
}
52+
53+
/// <summary>
54+
/// 关闭网页时调用
55+
/// </summary>
56+
/// <returns></returns>
57+
public void Dispose()
58+
{
59+
Dispose(true);
60+
GC.SuppressFinalize(this);
61+
}
62+
63+
private class InvoiceForm
64+
{
65+
[Required(ErrorMessage = "发票代码不能为空")]
66+
[NotNull]
67+
public string? InvoiceCode { get; set; }
68+
69+
[Required(ErrorMessage = "发票号码不能为空")]
70+
[NotNull]
71+
public string? InvoiceNum { get; set; }
72+
73+
[Required(ErrorMessage = "开票日期不能为空")]
74+
[NotNull]
75+
public string? InvoiceDate { get; set; }
76+
77+
[NotNull]
78+
public string? InvoiceType { get; set; }
79+
80+
[Required(ErrorMessage = "校验码不能为空")]
81+
[NotNull]
82+
public string? CheckCode { get; set; }
83+
84+
[NotNull]
85+
public string? TotalAmount { get; set; }
86+
}
87+
}

src/BootstrapBlazor.Shared/Samples/BaiduOcr.razor

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@
1515
<div>@((MarkupString)Localizer["BaiduOcrStep2"].Value)</div>
1616

1717
<DemoBlock Title="@Localizer["BasicUsageTitle"]" Introduction="@Localizer["BasicUsageIntro"]" Name="Normal" Demo="typeof(Demos.BaiduOcr.BaiduOcrNormal)" />
18+
19+
<DemoBlock Title="@Localizer["BasicUsageTitle"]" Introduction="@Localizer["BasicUsageIntro"]" Name="Normal" Demo="typeof(Demos.BaiduOcr.BaiduOcrVerifyVatInvoice)" />

src/Extensions/Components/BootstrapBlazor.BaiduOcr/Services/BaiduOcr.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Baidu.Aip.Ocr;
66
using Microsoft.Extensions.Options;
77
using Newtonsoft.Json.Linq;
8+
using System.Collections.Generic;
89

910
namespace BootstrapBlazor.Components;
1011

@@ -71,11 +72,11 @@ public Task<BaiduOcrResult<InvoiceEntity>> CheckVatInvoiceAsync(byte[] image, Ca
7172
protected async Task<string> GetAccessToken()
7273
{
7374
var client = HttpClientFactory.CreateClient();
74-
var para = new Dictionary<string, string>()
75+
var para = new List<KeyValuePair<string?, string?>>()
7576
{
76-
{ "grant_type", "client_credentials" },
77-
{ "client_id", Options.CurrentValue.ApiKey },
78-
{ "client_secret", Options.CurrentValue.Secret }
77+
new("grant_type", "client_credentials"),
78+
new("client_id", Options.CurrentValue.ApiKey),
79+
new("client_secret", Options.CurrentValue.Secret)
7980
};
8081

8182
var resp = await client.PostAsync("https://aip.baidubce.com/oauth/2.0/token", new FormUrlEncodedContent(para));
@@ -102,21 +103,21 @@ public Task<BaiduOcrResult<InvoiceVerifyResult>> VerifyInvoiceAsync(string invoi
102103
var client = HttpClientFactory.CreateClient();
103104

104105
// 拼装参数
105-
var para = new Dictionary<string, string>()
106+
var para = new List<KeyValuePair<string?, string?>>()
106107
{
107-
{ "invoice_code", invoiceCode },
108-
{ "invoice_num", invoiceNum },
109-
{ "invoice_date", invoiceDate },
110-
{ "invoice_type", invoiceType },
108+
new("invoice_code", invoiceCode),
109+
new("invoice_num", invoiceNum),
110+
new("invoice_date", invoiceDate),
111+
new("invoice_type", invoiceType),
111112
};
112113

113114
if (!string.IsNullOrEmpty(checkCode))
114115
{
115-
para["check_code"] = checkCode;
116+
para.Add(new("check_code", checkCode));
116117
}
117118
if (!string.IsNullOrEmpty(totalAmount))
118119
{
119-
para["total_amount"] = totalAmount;
120+
para.Add(new("total_amount", totalAmount));
120121
}
121122

122123
// 提交数据

0 commit comments

Comments
 (0)