-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
348 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...mmon.SharedCache.StackExchangeRedis/AbpWeChatCommonSharedCacheStackExchangeRedisModule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis.Localization; | ||
using Volo.Abp.Caching.StackExchangeRedis; | ||
using Volo.Abp.Localization; | ||
using Volo.Abp.Localization.ExceptionHandling; | ||
using Volo.Abp.Modularity; | ||
using Volo.Abp.VirtualFileSystem; | ||
|
||
namespace EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis | ||
{ | ||
[DependsOn( | ||
typeof(AbpCachingStackExchangeRedisModule), | ||
typeof(AbpWeChatCommonModule))] | ||
public class AbpWeChatCommonSharedCacheStackExchangeRedisModule : AbpModule | ||
{ | ||
public override void ConfigureServices(ServiceConfigurationContext context) | ||
{ | ||
Configure<AbpVirtualFileSystemOptions>(options => | ||
{ | ||
options.FileSets.AddEmbedded<AbpWeChatCommonSharedCacheStackExchangeRedisModule>(); | ||
}); | ||
|
||
Configure<AbpLocalizationOptions>(options => | ||
{ | ||
options.Resources | ||
.Add<SharedCacheStackExchangeRedisResource>("en") | ||
.AddVirtualJson("/Localization/StackExchangeRedis"); | ||
}); | ||
|
||
Configure<AbpExceptionLocalizationOptions>(options => | ||
{ | ||
options.MapCodeNamespace("EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis", typeof(SharedCacheStackExchangeRedisResource)); | ||
}); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...dCache.StackExchangeRedis/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Import Project="..\..\..\common.props" /> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<RootNamespace>EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis</RootNamespace> | ||
<Description>ABP vNext微信模块,基于StackExchangeRedis提供多服务间共享access_token等缓存数据的功能扩展。</Description> | ||
<LangVersion>8</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Volo.Abp.Caching.StackExchangeRedis" Version="3.0.4" /> | ||
<ProjectReference Include="..\EasyAbp.Abp.WeChat.Common\EasyAbp.Abp.WeChat.Common.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<EmbeddedResource Include="Localization\StackExchangeRedis\*.json" /> | ||
<Content Remove="Localization\StackExchangeRedis\*.json" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Infrastructure" /> | ||
</ItemGroup> | ||
|
||
</Project> |
75 changes: 75 additions & 0 deletions
75
...ckExchangeRedis/Infrastructure/AccessToken/SharedStackExchangeRedisAccessTokenProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using EasyAbp.Abp.WeChat.Common.Infrastructure.AccessToken; | ||
using EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis.Settings; | ||
using Microsoft.Extensions.Caching.Distributed; | ||
using Microsoft.Extensions.Caching.StackExchangeRedis; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Newtonsoft.Json.Linq; | ||
using Volo.Abp.Caching.StackExchangeRedis; | ||
using Volo.Abp.DependencyInjection; | ||
using Volo.Abp.Settings; | ||
|
||
namespace EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis.Infrastructure.AccessToken | ||
{ | ||
[Dependency(ServiceLifetime.Singleton, ReplaceServices = true)] | ||
public class SharedStackExchangeRedisAccessTokenProvider : IAccessTokenProvider | ||
{ | ||
public const string CachePrefix = "CurrentAccessToken:"; | ||
public const string SettingName = SharedCacheStackExchangeRedisSettings.RedisConfiguration; | ||
|
||
private readonly ISettingProvider _settingProvider; | ||
private readonly IHttpClientFactory _httpClientFactory; | ||
|
||
public SharedStackExchangeRedisAccessTokenProvider( | ||
ISettingProvider settingProvider, | ||
IHttpClientFactory httpClientFactory) | ||
{ | ||
_settingProvider = settingProvider; | ||
_httpClientFactory = httpClientFactory; | ||
} | ||
|
||
public async Task<string> GetAccessTokenAsync(string appId, string appSecret) | ||
{ | ||
var redisCache = new AbpRedisCache(new RedisCacheOptions | ||
{ | ||
Configuration = await _settingProvider.GetOrNullAsync(SettingName) | ||
}); | ||
|
||
var key = $"{CachePrefix}{appId}"; | ||
|
||
var accessToken = await redisCache.GetStringAsync(key); | ||
|
||
if (accessToken != null) | ||
{ | ||
return accessToken; | ||
} | ||
|
||
var client = _httpClientFactory.CreateClient(); | ||
|
||
var requestUrl = $"https://api.weixin.qq.com/cgi-bin/token?grant_type={GrantTypes.ClientCredential}&appid={appId}&secret={appSecret}"; | ||
|
||
var resultStr = await (await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, requestUrl))) | ||
.Content.ReadAsStringAsync(); | ||
|
||
var resultJson = JObject.Parse(resultStr); | ||
|
||
var accessTokenObj = resultJson.SelectToken("$.access_token"); | ||
|
||
if (accessTokenObj == null) | ||
{ | ||
throw new NullReferenceException($"无法获取到 AccessToken,微信 API 返回的内容为:{resultStr}"); | ||
} | ||
|
||
accessToken = accessTokenObj.Value<string>(); | ||
|
||
await redisCache.SetStringAsync(key, accessToken, new DistributedCacheEntryOptions | ||
{ | ||
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(115) | ||
}); | ||
|
||
return accessToken; | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...mmon.SharedCache.StackExchangeRedis/Localization/SharedCacheStackExchangeRedisResource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Volo.Abp.Localization; | ||
|
||
namespace EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis.Localization | ||
{ | ||
[LocalizationResourceName("EasyAbpAbpWeChatCommonSharedCacheStackExchangeRedis")] | ||
public class SharedCacheStackExchangeRedisResource | ||
{ | ||
|
||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
....Abp.WeChat.Common.SharedCache.StackExchangeRedis/Localization/StackExchangeRedis/ar.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"culture": "ar", | ||
"texts": { | ||
"SharedCacheRedisConfiguration": "SharedCacheRedisConfiguration", | ||
"SharedCacheRedisConfigurationDescription": "SharedCacheRedisConfigurationDescription" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
....Abp.WeChat.Common.SharedCache.StackExchangeRedis/Localization/StackExchangeRedis/cs.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"culture": "cs", | ||
"texts": { | ||
"SharedCacheRedisConfiguration": "SharedCacheRedisConfiguration", | ||
"SharedCacheRedisConfigurationDescription": "SharedCacheRedisConfigurationDescription" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
....Abp.WeChat.Common.SharedCache.StackExchangeRedis/Localization/StackExchangeRedis/en.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"culture": "en", | ||
"texts": { | ||
"SharedCacheRedisConfiguration": "SharedCacheRedisConfiguration", | ||
"SharedCacheRedisConfigurationDescription": "SharedCacheRedisConfigurationDescription" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...p.WeChat.Common.SharedCache.StackExchangeRedis/Localization/StackExchangeRedis/pl-PL.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"culture": "pl-PL", | ||
"texts": { | ||
"SharedCacheRedisConfiguration": "SharedCacheRedisConfiguration", | ||
"SharedCacheRedisConfigurationDescription": "SharedCacheRedisConfigurationDescription" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...p.WeChat.Common.SharedCache.StackExchangeRedis/Localization/StackExchangeRedis/pt-BR.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"culture": "pt-BR", | ||
"texts": { | ||
"SharedCacheRedisConfiguration": "SharedCacheRedisConfiguration", | ||
"SharedCacheRedisConfigurationDescription": "SharedCacheRedisConfigurationDescription" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
....Abp.WeChat.Common.SharedCache.StackExchangeRedis/Localization/StackExchangeRedis/ru.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"culture": "ru", | ||
"texts": { | ||
"SharedCacheRedisConfiguration": "SharedCacheRedisConfiguration", | ||
"SharedCacheRedisConfigurationDescription": "SharedCacheRedisConfigurationDescription" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
....Abp.WeChat.Common.SharedCache.StackExchangeRedis/Localization/StackExchangeRedis/sl.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"culture": "sl", | ||
"texts": { | ||
"SharedCacheRedisConfiguration": "SharedCacheRedisConfiguration", | ||
"SharedCacheRedisConfigurationDescription": "SharedCacheRedisConfigurationDescription" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
....Abp.WeChat.Common.SharedCache.StackExchangeRedis/Localization/StackExchangeRedis/tr.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"culture": "tr", | ||
"texts": { | ||
"SharedCacheRedisConfiguration": "SharedCacheRedisConfiguration", | ||
"SharedCacheRedisConfigurationDescription": "SharedCacheRedisConfigurationDescription" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
....Abp.WeChat.Common.SharedCache.StackExchangeRedis/Localization/StackExchangeRedis/vi.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"culture": "vi", | ||
"texts": { | ||
"SharedCacheRedisConfiguration": "SharedCacheRedisConfiguration", | ||
"SharedCacheRedisConfigurationDescription": "SharedCacheRedisConfigurationDescription" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...WeChat.Common.SharedCache.StackExchangeRedis/Localization/StackExchangeRedis/zh-Hans.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"culture": "zh-Hans", | ||
"texts": { | ||
"SharedCacheRedisConfiguration": "共享缓存RedisConfiguration", | ||
"SharedCacheRedisConfigurationDescription": "共享Redis缓存的连接配置,用于在多个服务之间分享access_token等动态数据" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...WeChat.Common.SharedCache.StackExchangeRedis/Localization/StackExchangeRedis/zh-Hant.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"culture": "zh-Hant", | ||
"texts": { | ||
"SharedCacheRedisConfiguration": "共享缓存RedisConfiguration", | ||
"SharedCacheRedisConfigurationDescription": "共享Redis缓存的连接配置,用于在多个服务之间分享access_token等动态数据" | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...che.StackExchangeRedis/Settings/SharedCacheStackExchangeRedisSettingDefinitionProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis.Localization; | ||
using Volo.Abp.Localization; | ||
using Volo.Abp.Settings; | ||
|
||
namespace EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis.Settings | ||
{ | ||
public class SharedCacheStackExchangeRedisSettingDefinitionProvider : SettingDefinitionProvider | ||
{ | ||
public override void Define(ISettingDefinitionContext context) | ||
{ | ||
//Define your own settings here. Example: | ||
//context.Add(new SettingDefinition(DenturePlusSettings.MySetting1)); | ||
|
||
context.Add(new SettingDefinition( | ||
SharedCacheStackExchangeRedisSettings.RedisConfiguration, | ||
null, | ||
L("SharedCacheRedisConfiguration"), | ||
L("SharedCacheRedisConfigurationDescription"))); | ||
} | ||
|
||
private static LocalizableString L(string name) | ||
{ | ||
return LocalizableString.Create<SharedCacheStackExchangeRedisResource>(name); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...t.Common.SharedCache.StackExchangeRedis/Settings/SharedCacheStackExchangeRedisSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis.Settings | ||
{ | ||
public static class SharedCacheStackExchangeRedisSettings | ||
{ | ||
private const string Prefix = "EasyAbp.Abp.WeChat.Common.SharedCache"; | ||
|
||
//Add your own setting names here. Example: | ||
//public const string MySetting1 = Prefix + ".MySetting1"; | ||
|
||
public const string RedisConfiguration = Prefix + ".RedisConfiguration"; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/DefaultAccessTokenProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Caching.Distributed; | ||
using Newtonsoft.Json.Linq; | ||
using Volo.Abp.Caching; | ||
using Volo.Abp.DependencyInjection; | ||
|
||
namespace EasyAbp.Abp.WeChat.Common.Infrastructure.AccessToken | ||
{ | ||
public class DefaultAccessTokenProvider : IAccessTokenProvider, ISingletonDependency | ||
{ | ||
private readonly IDistributedCache<string> _distributedCache; | ||
private readonly IHttpClientFactory _httpClientFactory; | ||
|
||
public DefaultAccessTokenProvider(IDistributedCache<string> distributedCache, | ||
IHttpClientFactory httpClientFactory) | ||
{ | ||
_distributedCache = distributedCache; | ||
_httpClientFactory = httpClientFactory; | ||
} | ||
|
||
public virtual async Task<string> GetAccessTokenAsync(string appId, string appSecret) | ||
{ | ||
return await _distributedCache.GetOrAddAsync($"CurrentAccessToken:{appId}", | ||
async () => | ||
{ | ||
var client = _httpClientFactory.CreateClient(); | ||
|
||
var requestUrl = $"https://api.weixin.qq.com/cgi-bin/token?grant_type={GrantTypes.ClientCredential}&appid={appId}&secret={appSecret}"; | ||
|
||
var resultStr = await (await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, requestUrl))) | ||
.Content.ReadAsStringAsync(); | ||
|
||
var resultJson = JObject.Parse(resultStr); | ||
|
||
var accessTokenObj = resultJson.SelectToken("$.access_token"); | ||
|
||
if (accessTokenObj == null) | ||
{ | ||
throw new NullReferenceException($"无法获取到 AccessToken,微信 API 返回的内容为:{resultStr}"); | ||
} | ||
|
||
return accessTokenObj.Value<string>(); | ||
}, | ||
() => new DistributedCacheEntryOptions | ||
{ | ||
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(115) | ||
}); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/IAccessTokenProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace EasyAbp.Abp.WeChat.Common.Infrastructure.AccessToken | ||
{ | ||
public interface IAccessTokenProvider | ||
{ | ||
Task<string> GetAccessTokenAsync(string appId, string appSecret); | ||
} | ||
} |
Oops, something went wrong.