Skip to content

Commit 022f9f8

Browse files
authored
doc(BaiduOcr): update sample code (#8075)
1 parent cc9ac4f commit 022f9f8

4 files changed

Lines changed: 186 additions & 68 deletions

File tree

src/BootstrapBlazor.Server/Components/Samples/BaiduOcr.razor

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
@page "/ocr"
1+
@page "/ocr"
22

33
<h3>@Localizer["Title"]</h3>
44

55
<h4>@Localizer["SubTitle"]</h4>
66

77
<Tips>@Localizer["BaiduOcrDesc"]</Tips>
88
<PackageTips Name="BootstrapBlazor.BaiduOcr" />
9+
910
<p><b>@Localizer["BaiduOcrIntro"]</b></p>
10-
<p class="code-label">@((MarkupString)Localizer["BaiduOcrStep1"].Value)</p>
11+
<p class="code-label">@Localizer["BaiduOcrStep1"]</p>
12+
<Pre>services.AddBootstrapBlazorBaiduOcr();</Pre>
13+
14+
<p class="code-label">@((MarkupString)Localizer["BaiduOcrStep2"].Value)</p>
1115
<p>@((MarkupString)Localizer["BaiduOcrStep1Desc"].Value)</p>
1216
<Pre>{
1317
"BaiduOcrOption": {
@@ -16,126 +20,127 @@
1620
"Secret": ""
1721
}
1822
}</Pre>
19-
<p class="code-label">@((MarkupString)Localizer["BaiduOcrStep2"].Value)</p>
23+
24+
<p class="code-label">@((MarkupString)Localizer["BaiduOcrStep3"].Value)</p>
2025
<Pre>[Inject, NotNull]
2126
private IBaiduOcr? OcrService { get; set; }</Pre>
2227

23-
<p class="code-label">@((MarkupString)Localizer["BaiduOcrStep3"].Value)</p>
28+
<p class="code-label">@((MarkupString)Localizer["BaiduOcrStep4"].Value)</p>
2429

2530
<DemoBlock Title="@Localizer["VatInvoiceTitle"]" Introduction="@Localizer["VatInvoiceIntro"]" Name="VatInvoice">
26-
<ButtonUpload TValue="string" OnChange="@OnClickToUploadBlock" BrowserButtonText="VATInvoice" ShowUploadFileList="false" IsDisabled="_isLoading" BrowserButtonIcon="@Icon"></ButtonUpload>
31+
<ButtonUpload TValue="string" OnChange="@OnClickToUploadBlock" BrowserButtonText="@Localizer["VatInvoiceButtonText"]" ShowUploadFileList="false" IsDisabled="_isLoading" BrowserButtonIcon="@Icon"></ButtonUpload>
2732
@if (Invoice != null)
2833
{
2934
<div class="row form-inline g-3 mt-0">
3035
<div class="col-12 col-sm-6">
31-
<Display ShowLabel="true" DisplayText="发票代码" @bind-Value="Invoice.InvoiceCode"></Display>
36+
<Display ShowLabel="true" DisplayText="@Localizer["InvoiceCodeLabel"]" @bind-Value="Invoice.InvoiceCode"></Display>
3237
</div>
3338
<div class="col-12 col-sm-6">
34-
<Display ShowLabel="true" DisplayText="发票号码" Value="Invoice.InvoiceNum"></Display>
39+
<Display ShowLabel="true" DisplayText="@Localizer["InvoiceNumLabel"]" Value="Invoice.InvoiceNum"></Display>
3540
</div>
3641
<div class="col-12 col-sm-6">
37-
<Display ShowLabel="true" DisplayText="发票类型" @bind-Value="Invoice.ServiceType"></Display>
42+
<Display ShowLabel="true" DisplayText="@Localizer["InvoiceServiceTypeLabel"]" @bind-Value="Invoice.ServiceType"></Display>
3843
</div>
3944
<div class="col-12 col-sm-6">
40-
<Display ShowLabel="true" DisplayText="联次信息" Value="Invoice.SheetNum"></Display>
45+
<Display ShowLabel="true" DisplayText="@Localizer["SheetNumLabel"]" Value="Invoice.SheetNum"></Display>
4146
</div>
4247
<div class="col-12 col-sm-6">
43-
<Display ShowLabel="true" DisplayText="发票种类" @bind-Value="Invoice.InvoiceType"></Display>
48+
<Display ShowLabel="true" DisplayText="@Localizer["InvoiceTypeLabel"]" @bind-Value="Invoice.InvoiceType"></Display>
4449
</div>
4550
<div class="col-12 col-sm-6">
46-
<Display ShowLabel="true" DisplayText="发票名称" Value="Invoice.InvoiceTypeOrg"></Display>
51+
<Display ShowLabel="true" DisplayText="@Localizer["InvoiceTypeOrgLabel"]" Value="Invoice.InvoiceTypeOrg"></Display>
4752
</div>
4853
<div class="col-12 col-sm-6">
49-
<Display ShowLabel="true" DisplayText="发票标志" @bind-Value="Invoice.InvoiceTag"></Display>
54+
<Display ShowLabel="true" DisplayText="@Localizer["InvoiceTagLabel"]" @bind-Value="Invoice.InvoiceTag"></Display>
5055
</div>
5156
<div class="col-12 col-sm-6">
52-
<Display ShowLabel="true" DisplayText="开票日期" Value="Invoice.InvoiceDate"></Display>
57+
<Display ShowLabel="true" DisplayText="@Localizer["InvoiceDateLabel"]" Value="Invoice.InvoiceDate"></Display>
5358
</div>
5459
<div class="col-12">
55-
<Display ShowLabel="true" DisplayText="校验码" Value="Invoice.CheckCode"></Display>
60+
<Display ShowLabel="true" DisplayText="@Localizer["CheckCodeLabel"]" Value="Invoice.CheckCode"></Display>
5661
</div>
5762
<div class="col-12 col-sm-6">
58-
<Display ShowLabel="true" DisplayText="购方名称" @bind-Value="Invoice.PurchaserName"></Display>
63+
<Display ShowLabel="true" DisplayText="@Localizer["PurchaserNameLabel"]" @bind-Value="Invoice.PurchaserName"></Display>
5964
</div>
6065
<div class="col-12 col-sm-6">
61-
<Display ShowLabel="true" DisplayText="纳税人识别号" Value="Invoice.PurchaserRegisterNum"></Display>
66+
<Display ShowLabel="true" DisplayText="@Localizer["PurchaserRegisterNumLabel"]" Value="Invoice.PurchaserRegisterNum"></Display>
6267
</div>
6368
<div class="col-12 col-sm-6">
64-
<Display ShowLabel="true" DisplayText="开户行及账号" @bind-Value="Invoice.PurchaserBank"></Display>
69+
<Display ShowLabel="true" DisplayText="@Localizer["PurchaserBankLabel"]" @bind-Value="Invoice.PurchaserBank"></Display>
6570
</div>
6671
<div class="col-12 col-sm-6">
67-
<Display ShowLabel="true" DisplayText="购方地址" Value="Invoice.PurchaserAddress"></Display>
72+
<Display ShowLabel="true" DisplayText="@Localizer["PurchaserAddressLabel"]" Value="Invoice.PurchaserAddress"></Display>
6873
</div>
6974
<div class="col-12 col-sm-6">
70-
<Display ShowLabel="true" DisplayText="销售方名称" @bind-Value="Invoice.SellerName"></Display>
75+
<Display ShowLabel="true" DisplayText="@Localizer["SellerNameLabel"]" @bind-Value="Invoice.SellerName"></Display>
7176
</div>
7277
<div class="col-12 col-sm-6">
73-
<Display ShowLabel="true" DisplayText="纳税人识别号" Value="Invoice.SellerRegisterNum"></Display>
78+
<Display ShowLabel="true" DisplayText="@Localizer["SellerRegisterNumLabel"]" Value="Invoice.SellerRegisterNum"></Display>
7479
</div>
7580
<div class="col-12 col-sm-6">
76-
<Display ShowLabel="true" DisplayText="销售方及账号" @bind-Value="Invoice.SellerBank"></Display>
81+
<Display ShowLabel="true" DisplayText="@Localizer["SellerBankLabel"]" @bind-Value="Invoice.SellerBank"></Display>
7782
</div>
7883
<div class="col-12 col-sm-6">
79-
<Display ShowLabel="true" DisplayText="销售方地址" Value="Invoice.SellerAddress"></Display>
84+
<Display ShowLabel="true" DisplayText="@Localizer["SellerAddressLabel"]" Value="Invoice.SellerAddress"></Display>
8085
</div>
8186
<div class="col-12">
82-
<Display ShowLabel="true" DisplayText="备注" @bind-Value="Invoice.Remarks"></Display>
87+
<Display ShowLabel="true" DisplayText="@Localizer["RemarksLabel"]" @bind-Value="Invoice.Remarks"></Display>
8388
</div>
8489
<div class="col-12 col-sm-6">
85-
<Display ShowLabel="true" DisplayText="价税合计" Value="Invoice.AmountInFiguers"></Display>
90+
<Display ShowLabel="true" DisplayText="@Localizer["AmountInFiguresLabel"]" Value="Invoice.AmountInFiguers"></Display>
8691
</div>
8792
<div class="col-12 col-sm-6">
88-
<Display ShowLabel="true" DisplayText="合计金额" Value="Invoice.TotalAmount"></Display>
93+
<Display ShowLabel="true" DisplayText="@Localizer["TotalAmountLabel"]" Value="Invoice.TotalAmount"></Display>
8994
</div>
9095
<div class="col-12 col-sm-6">
91-
<Display ShowLabel="true" DisplayText="合计税额" @bind-Value="Invoice.TotalTax"></Display>
96+
<Display ShowLabel="true" DisplayText="@Localizer["TotalTaxLabel"]" @bind-Value="Invoice.TotalTax"></Display>
9297
</div>
9398
<div class="col-12 col-sm-6">
9499
</div>
95100
@if (Invoice.CommodityName?.Any() ?? false)
96101
{
97102
<div class="col-12 col-sm-6">
98-
<Display ShowLabel="true" DisplayText="项目名称" Value="Invoice.CommodityName.First().CommodityName"></Display>
103+
<Display ShowLabel="true" DisplayText="@Localizer["CommodityNameLabel"]" Value="Invoice.CommodityName.First().CommodityName"></Display>
99104
</div>
100105
}
101106
@if (Invoice.CommodityTaxRate?.Any() ?? false)
102107
{
103108
<div class="col-12 col-sm-6">
104-
<Display ShowLabel="true" DisplayText="税率" Value="Invoice.CommodityTaxRate.First().CommodityTaxRate"></Display>
109+
<Display ShowLabel="true" DisplayText="@Localizer["CommodityTaxRateLabel"]" Value="Invoice.CommodityTaxRate.First().CommodityTaxRate"></Display>
105110
</div>
106111
}
107112
</div>
108113
}
109114
</DemoBlock>
110115

111116
<DemoBlock Title="@Localizer["VerifyVatInvoiceTitle"]" Introduction="@Localizer["VerifyVatInvoiceIntro"]" Name="Verify">
112-
<p class="code-label">发票种类</p>
117+
<p class="code-label">@Localizer["InvoiceTypeLabel"]</p>
113118
<ul class="ul-demo">
114-
<li>增值税专用发票:special_vat_invoice</li>
115-
<li>增值税电子专用发票:elec_special_vat_invoice</li>
116-
<li>增值税普通发票:normal_invoice</li>
117-
<li>增值税普通发票(电子):elec_normal_invoice</li>
118-
<li>增值税普通发票(卷式):roll_normal_invoice</li>
119-
<li>通行费增值税电子普通发票:toll_elec_normal_invoice</li>
120-
<li>区块链电子发票(目前仅支持深圳地区):blockchain_invoice</li>
121-
<li>全电发票(专用发票):elec_invoice_special</li>
122-
<li>全电发票(普通发票):elec_invoice_normal</li>
123-
<li>货运运输业增值税专用发票:special_freight_transport_invoice</li>
124-
<li>机动车销售发票:motor_vehicle_invoice</li>
125-
<li>二手车销售发票:used_vehicle_invoice</li>
119+
<li>@Localizer["InvoiceTypeOption1"]</li>
120+
<li>@Localizer["InvoiceTypeOption2"]</li>
121+
<li>@Localizer["InvoiceTypeOption3"]</li>
122+
<li>@Localizer["InvoiceTypeOption4"]</li>
123+
<li>@Localizer["InvoiceTypeOption5"]</li>
124+
<li>@Localizer["InvoiceTypeOption6"]</li>
125+
<li>@Localizer["InvoiceTypeOption7"]</li>
126+
<li>@Localizer["InvoiceTypeOption8"]</li>
127+
<li>@Localizer["InvoiceTypeOption9"]</li>
128+
<li>@Localizer["InvoiceTypeOption10"]</li>
129+
<li>@Localizer["InvoiceTypeOption11"]</li>
130+
<li>@Localizer["InvoiceTypeOption12"]</li>
126131
</ul>
127132
<ValidateForm Model="Model" OnValidSubmit="@Verify">
128133
<EditorForm TModel="InvoiceForm" RowType="RowType.Inline" AutoGenerateAllItem="false">
129134
<FieldItems>
130-
<EditorItem @bind-Field="@context.InvoiceType" Text="发票种类" />
131-
<EditorItem @bind-Field="@context.InvoiceCode" Text="发票代码" />
132-
<EditorItem @bind-Field="@context.InvoiceNum" Text="发票号码" />
133-
<EditorItem @bind-Field="@context.InvoiceDate" Text="开票日期" PlaceHolder="YYYYMMDD" />
134-
<EditorItem @bind-Field="@context.CheckCode" Text="校验码" PlaceHolder="后六位" />
135-
<EditorItem @bind-Field="@context.TotalAmount" Text="金额" PlaceHolder="可为空" />
135+
<EditorItem @bind-Field="@context.InvoiceType" Text="@Localizer["InvoiceTypeLabel"]" />
136+
<EditorItem @bind-Field="@context.InvoiceCode" Text="@Localizer["InvoiceCodeLabel"]" />
137+
<EditorItem @bind-Field="@context.InvoiceNum" Text="@Localizer["InvoiceNumLabel"]" />
138+
<EditorItem @bind-Field="@context.InvoiceDate" Text="@Localizer["InvoiceDateLabel"]" PlaceHolder="@Localizer["InvoiceDatePlaceHolder"]" />
139+
<EditorItem @bind-Field="@context.CheckCode" Text="@Localizer["CheckCodeLabel"]" PlaceHolder="@Localizer["CheckCodePlaceHolder"]" />
140+
<EditorItem @bind-Field="@context.TotalAmount" Text="@Localizer["AmountLabel"]" PlaceHolder="@Localizer["AmountPlaceHolder"]" />
136141
</FieldItems>
137142
<Buttons>
138-
<Button ButtonType="ButtonType.Submit" Icon="fa-solid fa-check" Text="Verify"></Button>
143+
<Button ButtonType="ButtonType.Submit" Icon="fa-solid fa-check" Text="@Localizer["VerifyButtonText"]"></Button>
139144
</Buttons>
140145
</EditorForm>
141146
</ValidateForm>

src/BootstrapBlazor.Server/Components/Samples/BaiduOcr.razor.cs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
@@ -52,11 +52,11 @@ private async Task OnClickToUploadBlock(UploadFile file)
5252
Invoice = result.Entity;
5353
if (result.Entity != null)
5454
{
55-
await ToastService.Success("Vat Invoice", "VAT Invoice success!");
55+
await ToastService.Success(Localizer["VatInvoiceToastTitle"], Localizer["VatInvoiceToastSuccess"]);
5656
}
5757
else
5858
{
59-
await ToastService.Information("Vat Invoice", $"{result.ErrorCode}: {result.ErrorMessage}");
59+
await ToastService.Information(Localizer["VatInvoiceToastTitle"], $"{result.ErrorCode}: {result.ErrorMessage}");
6060
}
6161
}
6262
catch (TaskCanceledException)
@@ -77,37 +77,58 @@ private async Task Verify(EditContext context)
7777
var result = await OcrService.VerifyInvoiceAsync(Model.InvoiceCode, Model.InvoiceNum, Model.InvoiceDate, Model.InvoiceType, Model.CheckCode, Model.TotalAmount);
7878
if (result.ErrorCode != 0)
7979
{
80-
await ToastService.Information("Verify Vat", $"{result.ErrorCode}: {result.ErrorMessage}");
80+
await ToastService.Information(Localizer["VerifyVatToastTitle"], $"{result.ErrorCode}: {result.ErrorMessage}");
8181
}
8282
else if (result.Entity != null)
8383
{
84-
await ToastService.Success("Verify Vat", result.Entity?.VerifyMessage ?? "Unknow Error");
84+
await ToastService.Success(Localizer["VerifyVatToastTitle"], result.Entity?.VerifyMessage ?? Localizer["UnknownErrorText"]);
8585
}
8686
}
8787

88-
private class InvoiceForm
88+
private class InvoiceForm : IValidatableObject
8989
{
90-
[Required(ErrorMessage = "发票代码不能为空")]
9190
[NotNull]
9291
public string? InvoiceCode { get; set; }
9392

94-
[Required(ErrorMessage = "发票号码不能为空")]
9593
[NotNull]
9694
public string? InvoiceNum { get; set; }
9795

98-
[Required(ErrorMessage = "开票日期不能为空")]
9996
[NotNull]
10097
public string? InvoiceDate { get; set; }
10198

10299
[NotNull]
103100
public string? InvoiceType { get; set; }
104101

105-
[Required(ErrorMessage = "校验码不能为空")]
106102
[NotNull]
107103
public string? CheckCode { get; set; }
108104

109105
[NotNull]
110106
public string? TotalAmount { get; set; }
107+
108+
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
109+
{
110+
var localizer = validationContext.GetRequiredService<IStringLocalizer<BaiduOcr>>();
111+
112+
if (string.IsNullOrWhiteSpace(InvoiceCode))
113+
{
114+
yield return new ValidationResult(localizer["InvoiceCodeRequiredError"], [nameof(InvoiceCode)]);
115+
}
116+
117+
if (string.IsNullOrWhiteSpace(InvoiceNum))
118+
{
119+
yield return new ValidationResult(localizer["InvoiceNumRequiredError"], [nameof(InvoiceNum)]);
120+
}
121+
122+
if (string.IsNullOrWhiteSpace(InvoiceDate))
123+
{
124+
yield return new ValidationResult(localizer["InvoiceDateRequiredError"], [nameof(InvoiceDate)]);
125+
}
126+
127+
if (string.IsNullOrWhiteSpace(CheckCode))
128+
{
129+
yield return new ValidationResult(localizer["CheckCodeRequiredError"], [nameof(CheckCode)]);
130+
}
131+
}
111132
}
112133

113134
/// <summary>

0 commit comments

Comments
 (0)