chore(tenpayv3): 新增基于 .NET Framework 4.7 的示例项目

This commit is contained in:
Fu Diwei
2021-12-28 14:05:39 +08:00
parent b2ce287136
commit 2dcfcab262
58 changed files with 1114 additions and 204 deletions

View File

@@ -1,32 +1,25 @@
using System;
using System.IO;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Controllers
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Controllers
{
using SKIT.FlurlHttpClient.Wechat.Api.Events;
[ApiController]
[Route("notify")]
[Route("api/notify")]
public class WechatNotifyController : ControllerBase
{
private readonly ILogger _logger;
private readonly Options.WechatOptions _wechatOptions;
private readonly Services.HttpClients.IWechatApiHttpClientFactory _wechatApiHttpClientFactory;
public WechatNotifyController(
ILoggerFactory loggerFactory,
IOptions<Options.WechatOptions> wechatOptions,
Services.HttpClients.IWechatApiHttpClientFactory wechatApiHttpClientFactory)
{
_logger = loggerFactory.CreateLogger(GetType());
_wechatOptions = wechatOptions.Value;
_wechatApiHttpClientFactory = wechatApiHttpClientFactory;
}

View File

@@ -1,25 +1,19 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SKIT.FlurlHttpClient.Wechat.Api;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Controllers
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Controllers
{
using SKIT.FlurlHttpClient.Wechat.Api;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
[ApiController]
[Route("user")]
[Route("api/user")]
public class WechatUserController : ControllerBase
{
private readonly ILogger _logger;
private readonly Services.Repositories.IWechatAccessTokenEntityRepository _wechatAccessTokenEntityRepository;
private readonly Services.HttpClients.IWechatApiHttpClientFactory _wechatApiHttpClientFactory;
public WechatUserController(
@@ -38,9 +32,6 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Controllers
[FromQuery(Name = "app_id")] string appId,
[FromQuery(Name = "open_id")] string openId)
{
// 获取用户基本信息
// 文档https://developers.weixin.qq.com/doc/offiaccount/User_Management/Get_users_basic_information_UnionID.html#UinonId
var entity = _wechatAccessTokenEntityRepository.FirstOrDefault(e => e.AppId == appId);
var client = _wechatApiHttpClientFactory.Create(appId);
var request = new CgibinUserInfoRequest() { AccessToken = entity?.AccessToken, OpenId = openId };

View File

@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Models
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Models
{
public class WechatAccessTokenEntity
{

View File

@@ -1,16 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Options
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Options
{
public partial class WechatOptions : IOptions<WechatOptions>
{
WechatOptions IOptions<WechatOptions>.Value => this;
public WechatAccount[] Accounts { get; set; } = Array.Empty<WechatAccount>();
public Types.WechatAccount[] Accounts { get; set; } = Array.Empty<Types.WechatAccount>();
public string CallbackState { get; set; } = string.Empty;
@@ -19,13 +16,16 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Options
partial class WechatOptions
{
public class WechatAccount
{
public string? GhId { get; set; }
public static class Types
{
public class WechatAccount
{
public string? GhId { get; set; }
public string AppId { get; set; } = string.Empty;
public string AppId { get; set; } = string.Empty;
public string AppSecret { get; set; } = string.Empty;
public string AppSecret { get; set; } = string.Empty;
}
}
}
}

View File

@@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample
{
public class Program
{
@@ -15,6 +12,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(builder =>
{
builder.UseStartup<Startup>();

View File

@@ -4,11 +4,14 @@
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<NullableReferenceTypes>true</NullableReferenceTypes>
<RootNamespace>SKIT.FlurlHttpClient.Wechat.Api.Sample</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DistributedLock.Core" Version="1.0.2" />
<PackageReference Include="DistributedLock.FileSystem" Version="1.0.0" />
<PackageReference Include="Autofac" Version="6.3.0" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.2.0" />
<PackageReference Include="DistributedLock.Core" Version="1.0.4" />
<PackageReference Include="DistributedLock.FileSystem" Version="1.0.1" />
<PackageReference Include="NMemory" Version="3.1.3" />
</ItemGroup>

View File

@@ -6,11 +6,12 @@ using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SKIT.FlurlHttpClient.Wechat.Api;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.BackgroundServices
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.BackgroundServices
{
using SKIT.FlurlHttpClient.Wechat.Api;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
class WechatAccessTokenRefreshingBackgroundService : BackgroundService
{
private readonly ILogger _logger;

View File

@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Medallion.Threading;
using Medallion.Threading;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.DistributedLock
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.DistributedLock
{
public interface IDistributedLockFactory
{

View File

@@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Medallion.Threading;
using Medallion.Threading.FileSystem;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.DistributedLock.Implements
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.DistributedLock.Implements
{
class DistributedLockFactory : IDistributedLockFactory
{
@@ -14,7 +11,9 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.DistributedLock.I
public IDistributedLock Create(string lockName)
{
// NOTICE: 单机演示基于文件实现分布式锁,生产项目请替换成其他实现
// NOTICE:
// 单机演示基于文件实现分布式锁,生产项目请替换成其他实现。
return new FileDistributedLock(_lockFileDirectory, lockName);
}
}

View File

@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.HttpClients
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.HttpClients
{
public interface IWechatApiHttpClientFactory
{

View File

@@ -1,13 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;
using Flurl.Http.Configuration;
using Microsoft.Extensions.Options;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.HttpClients.Implements
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.HttpClients.Implements
{
partial class WechatApiHttpClientFactory : IWechatApiHttpClientFactory
{
@@ -26,6 +24,10 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.HttpClients.Imple
public WechatApiClient Create(string appId)
{
// NOTICE:
// 这里的工厂方法是为了演示多租户而存在的,可根据 AppId 生成不同的 API 客户端。
// 如果你的项目只存在唯一一个租户,那么直接注入 `WechatApiClient` 即可。
var wechatAccountOptions = _wechatOptions.Accounts?.FirstOrDefault(e => string.Equals(appId, e.AppId));
if (wechatAccountOptions == null)
throw new Exception("未在配置项中找到该 AppId 对应的微信账号。");

View File

@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.Repositories
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.Repositories
{
public interface IWechatAccessTokenEntityRepository : IEnumerable<Models.WechatAccessTokenEntity>
{

View File

@@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NMemory;
using NMemory;
using NMemory.Tables;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.Repositories.Implements
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.Repositories.Implements
{
internal class GlobalDatabase
{

View File

@@ -1,10 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5.Services.Repositories.Implements
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.Repositories.Implements
{
public class WechatAccessTokenEntityRepository : IWechatAccessTokenEntityRepository
{

View File

@@ -8,7 +8,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample
{
public class Startup
{
@@ -23,7 +23,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample_Net5
{
services.AddControllers();
// ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݼ<EFBFBD> `appsettings.json` <20>ļ<EFBFBD><C4BC><EFBFBD>
services.AddOptions();
services.Configure<Options.WechatOptions>(Configuration.GetSection(nameof(Options.WechatOptions)));