Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
@inject IBaiduOcr OcrService
@inject ToastService ToastService
@implements IDisposable

<ValidateForm Model="Model" OnValidSubmit="@Verify">
<EditorForm TModel="InvoiceForm" RowType="RowType.Inline" AutoGenerateAllItem="false">
<FieldItems>
<EditorItem @bind-Field="@context.InvoiceCode" Text="发票代码" />
<EditorItem @bind-Field="@context.InvoiceNum" Text="发票号码" />
<EditorItem @bind-Field="@context.InvoiceDate" Text="开票日期" />
<EditorItem @bind-Field="@context.CheckCode" Text="校验码" />
<EditorItem @bind-Field="@context.TotalAmount" Text="金额" />
</FieldItems>
<Buttons>
<Button ButtonType="ButtonType.Submit" Icon="fa-solid fa-check" Text="Verify" />
</Buttons>
</EditorForm>
</ValidateForm>

<div class="mt-3">@InvoiceVerifyResult?.VerifyMessage</div>

@code {
/// <summary>
/// 取消请求令牌
/// </summary>
private CancellationTokenSource? TokenSource { get; set; }

private InvoiceVerifyResult? InvoiceVerifyResult { get; set; }

private InvoiceForm Model { get; set; } = new() { InvoiceType = "elec_special_vat_invoice", TotalAmount = "0" };

private async Task Verify(EditContext context)
{
var result = await OcrService.VerifyInvoiceAsync(Model.InvoiceCode, Model.InvoiceNum, Model.InvoiceDate, Model.InvoiceType, Model.CheckCode, Model.TotalAmount);
InvoiceVerifyResult = result.Entity;
StateHasChanged();
}

/// <summary>
/// 关闭网页时调用
/// </summary>
/// <param name="disposing"></param>
/// <returns></returns>
protected virtual void Dispose(bool disposing)
{
if (disposing && TokenSource != null)
{
TokenSource.Cancel();
TokenSource.Dispose();
}
}

/// <summary>
/// 关闭网页时调用
/// </summary>
/// <returns></returns>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

private class InvoiceForm
{
[Required(ErrorMessage = "发票代码不能为空")]
[NotNull]
public string? InvoiceCode { get; set; }

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

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

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

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

[NotNull]
public string? TotalAmount { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/BootstrapBlazor.Shared/Samples/BaiduOcr.razor
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
<div>@((MarkupString)Localizer["BaiduOcrStep2"].Value)</div>

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

<DemoBlock Title="@Localizer["BasicUsageTitle"]" Introduction="@Localizer["BasicUsageIntro"]" Name="Normal" Demo="typeof(Demos.BaiduOcr.BaiduOcrVerifyVatInvoice)" />
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Baidu.Aip.Ocr;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;

namespace BootstrapBlazor.Components;

Expand Down Expand Up @@ -71,11 +72,11 @@ public Task<BaiduOcrResult<InvoiceEntity>> CheckVatInvoiceAsync(byte[] image, Ca
protected async Task<string> GetAccessToken()
{
var client = HttpClientFactory.CreateClient();
var para = new Dictionary<string, string>()
var para = new List<KeyValuePair<string?, string?>>()
{
{ "grant_type", "client_credentials" },
{ "client_id", Options.CurrentValue.ApiKey },
{ "client_secret", Options.CurrentValue.Secret }
new("grant_type", "client_credentials"),
new("client_id", Options.CurrentValue.ApiKey),
new("client_secret", Options.CurrentValue.Secret)
};

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

// 拼装参数
var para = new Dictionary<string, string>()
var para = new List<KeyValuePair<string?, string?>>()
{
{ "invoice_code", invoiceCode },
{ "invoice_num", invoiceNum },
{ "invoice_date", invoiceDate },
{ "invoice_type", invoiceType },
new("invoice_code", invoiceCode),
new("invoice_num", invoiceNum),
new("invoice_date", invoiceDate),
new("invoice_type", invoiceType),
};

if (!string.IsNullOrEmpty(checkCode))
{
para["check_code"] = checkCode;
para.Add(new("check_code", checkCode));
}
if (!string.IsNullOrEmpty(totalAmount))
{
para["total_amount"] = totalAmount;
para.Add(new("total_amount", totalAmount));
}

// 提交数据
Expand Down