refactor: 优化代码

This commit is contained in:
Fu Diwei 2024-07-18 15:56:20 +08:00
parent 08c675c6ba
commit fdb2d2a982
11 changed files with 38 additions and 22 deletions

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net462; netstandard2.0; net6.0</TargetFrameworks>
<TargetFrameworks>net462; netstandard2.0; net6.0; net8.0</TargetFrameworks>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<NullableReferenceTypes>true</NullableReferenceTypes>

View File

@ -93,7 +93,11 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
if (!string.Equals(paddingMode, PADDING_MODE_NOPADDING, StringComparison.OrdinalIgnoreCase))
throw new NotSupportedException();
#if NET8_0_OR_GREATER
using (AesGcm aes = new AesGcm(keyBytes, TAG_LENGTH_BYTE))
#else
using (AesGcm aes = new AesGcm(keyBytes))
#endif
{
byte[] cipherWithoutTagBytes = new byte[cipherBytes.Length - TAG_LENGTH_BYTE];
byte[] tagBytes = new byte[TAG_LENGTH_BYTE];
@ -216,7 +220,11 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
if (!string.Equals(paddingMode, PADDING_MODE_NOPADDING, StringComparison.OrdinalIgnoreCase))
throw new NotSupportedException();
#if NET8_0_OR_GREATER
using (AesGcm aes = new AesGcm(keyBytes, TAG_LENGTH_BYTE))
#else
using (AesGcm aes = new AesGcm(keyBytes))
#endif
{
byte[] cipherBytes = new byte[plainBytes.Length];
byte[] tagBytes = new byte[TAG_LENGTH_BYTE];

View File

@ -17,7 +17,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
CertificateEntry? entry = client.PlatformCertificateManager.GetEntry(strSerialNumber);
if (!entry.HasValue)
{
return ErroredResult.Fail(new Exception($"The platform certificate manager does not contain a certificate with serial number \"{strSerialNumber}\"."));
return ErroredResult.Fail(new Exception($"The platform certificate manager does not contain a certificate matched the serial number \"{strSerialNumber}\"."));
}
return GenerateSignatureResultByCertificate(
@ -41,7 +41,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3
CertificateEntry? entry = await ((ICertificateManagerAsync)client.PlatformCertificateManager).GetEntryAsync(strSerialNumber, cancellationToken).ConfigureAwait(false);
if (!entry.HasValue)
{
return ErroredResult.Fail(new Exception($"The platform certificate manager does not contain a certificate with serial number \"{strSerialNumber}\"."));
return ErroredResult.Fail(new Exception($"The platform certificate manager does not contain a certificate matched the serial number \"{strSerialNumber}\"."));
}
return GenerateSignatureResultByCertificate(

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net462; netstandard2.0; net6.0</TargetFrameworks>
<TargetFrameworks>net462; netstandard2.0; net6.0; net8.0</TargetFrameworks>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<NullableReferenceTypes>true</NullableReferenceTypes>

View File

@ -55,6 +55,8 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings
[System.Text.Json.Serialization.JsonConstructor]
public CertificateEntry(string algorithmType, string serialNumber, string certificate, DateTimeOffset effectiveTime, DateTimeOffset expireTime)
{
certificate = certificate?.Trim()!;
if (string.IsNullOrEmpty(algorithmType))
throw new ArgumentException("The value of `algorithmType` can not be empty.", nameof(algorithmType));
if (string.IsNullOrEmpty(certificate))
@ -63,8 +65,8 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings
throw new ArgumentException("The value of `serialNumber` can not be empty.", nameof(serialNumber));
if (!ALGORITHM_TYPE_RSA.Equals(algorithmType) && !ALGORITHM_TYPE_SM2.Equals(algorithmType))
throw new ArgumentException("The value of `algorithmType` an invalid value.", nameof(algorithmType));
if (!certificate.Trim().StartsWith("-----BEGIN CERTIFICATE-----") || !certificate.Trim().EndsWith("-----END CERTIFICATE-----"))
throw new ArgumentException("The value of `certificate` is an invalid certificate file content.", nameof(certificate));
if (!(certificate.StartsWith("-----BEGIN CERTIFICATE-----") && certificate.EndsWith("-----END CERTIFICATE-----")))
throw new ArgumentException("The value of `certificate` is an invalid certificate file content. Maybe you forgot to decrypt?", nameof(certificate));
AlgorithmType = algorithmType;
SerialNumber = serialNumber.ToUpper();
@ -80,12 +82,14 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings
/// <param name="certificate"></param>
public CertificateEntry(string algorithmType, string certificate)
{
certificate = certificate?.Trim()!;
if (string.IsNullOrEmpty(algorithmType))
throw new ArgumentException("The value of `algorithmType` can not be empty.", nameof(algorithmType));
if (string.IsNullOrEmpty(certificate))
throw new ArgumentException("The value of `certificate` can not be empty.", nameof(certificate));
if (!certificate.Trim().StartsWith("-----BEGIN CERTIFICATE-----") || !certificate.Trim().EndsWith("-----END CERTIFICATE-----"))
throw new ArgumentException("The value of `certificate` is an invalid certificate file content.", nameof(certificate));
if (!(certificate.StartsWith("-----BEGIN CERTIFICATE-----") && certificate.EndsWith("-----END CERTIFICATE-----")))
throw new ArgumentException("The value of `certificate` is an invalid certificate file content. Maybe you forgot to decrypt?", nameof(certificate));
AlgorithmType = algorithmType;
Certificate = certificate;

View File

@ -45,8 +45,12 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Utilities
{
if (!string.Equals(paddingMode, PADDING_MODE_NOPADDING, StringComparison.OrdinalIgnoreCase))
throw new NotSupportedException();
#if NET8_0_OR_GREATER
using (AesGcm aes = new AesGcm(keyBytes, TAG_LENGTH_BYTE))
#else
using (AesGcm aes = new AesGcm(keyBytes))
#endif
{
byte[] cipherWithoutTagBytes = new byte[cipherBytes.Length - TAG_LENGTH_BYTE];
byte[] tagBytes = new byte[TAG_LENGTH_BYTE];

View File

@ -6,18 +6,18 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.ExtendedSDK.Finance.InteropServices
{
internal static class MarshalerHelper
{
public static string PtrToStringAnsi(IntPtr ptr)
public static string? PtrToStringAnsi(IntPtr ptr)
{
if (ptr == IntPtr.Zero)
return default!;
return null;
return Marshal.PtrToStringAnsi(ptr)!;
}
public static string PtrToStringUTF8(IntPtr ptr)
public static string? PtrToStringUTF8(IntPtr ptr)
{
if (ptr == IntPtr.Zero)
return default!;
return null;
#if NETCOREAPP || NET5_0_OR_GREATER
return Marshal.PtrToStringUTF8(ptr)!;

View File

@ -159,7 +159,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.ExtendedSDK.Finance
IsRunOnWindows() ? FinanceDllWindowsPInvoker.GetContentFromSlice(dataPtr) :
IsRunOnLinux() ? FinanceDllLinuxPInvoker.GetContentFromSlice(dataPtr) :
throw new PlatformNotSupportedException();
string dataContent = MarshalerHelper.PtrToStringUTF8(dataContentPtr);
string dataContent = MarshalerHelper.PtrToStringUTF8(dataContentPtr)!;
response = JsonSerializer.Deserialize<Models.GetChatRecordsResponse>(dataContent);
response._InternalRawBytes = Encoding.UTF8.GetBytes(dataContent);
@ -253,7 +253,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.ExtendedSDK.Finance
IsRunOnWindows() ? FinanceDllWindowsPInvoker.GetContentFromSlice(dataPtr) :
IsRunOnLinux() ? FinanceDllLinuxPInvoker.GetContentFromSlice(dataPtr) :
throw new PlatformNotSupportedException();
string dataContent = MarshalerHelper.PtrToStringUTF8(dataContentPtr);
string dataContent = MarshalerHelper.PtrToStringUTF8(dataContentPtr)!;
try
{

View File

@ -7,11 +7,6 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.ExtendedSDK.Finance
/// </summary>
public abstract class WechatWorkFinanceResponse : ICommonResponse
{
internal protected WechatWorkFinanceResponse()
{
_InternalRawBytes = Array.Empty<byte>();
}
[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
internal byte[] _InternalRawBytes;
@ -34,6 +29,11 @@ namespace SKIT.FlurlHttpClient.Wechat.Work.ExtendedSDK.Finance
return _InternalRawBytes;
}
internal protected WechatWorkFinanceResponse()
{
_InternalRawBytes = Array.Empty<byte>();
}
/// <summary>
/// 获取企业微信会话内容存档 API 返回的返回值。
/// </summary>

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net472; net6.0</TargetFrameworks>
<TargetFrameworks>net472; net6.0; net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<NullableReferenceTypes>true</NullableReferenceTypes>

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net472; net6.0</TargetFrameworks>
<TargetFrameworks>net472; net6.0; net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<NullableReferenceTypes>true</NullableReferenceTypes>