Skip to content

Commit 726a168

Browse files
committed
添加Abp包装
1 parent e355a59 commit 726a168

File tree

8 files changed

+133
-26
lines changed

8 files changed

+133
-26
lines changed

Magicodes.Wx.Sdk.sln

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A2B356C4-A7ED-4C3A-9DA2-7505AFC527D9}"
1111
ProjectSection(SolutionItems) = preProject
1212
azure-pipelines.yml = azure-pipelines.yml
13+
common.props = common.props
1314
LICENSE = LICENSE
1415
README.md = README.md
1516
EndProjectSection
@@ -18,7 +19,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Magicodes.Wx.PublicAccount.
1819
EndProject
1920
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{21B20DAF-56D3-414B-91FE-DD0D176A5277}"
2021
EndProject
21-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Magicodes.Wx.PublicAccount.Sdk.Test", "tests\Magicodes.Wx.PublicAccount.Sdk.Test\Magicodes.Wx.PublicAccount.Sdk.Test.csproj", "{F6098294-2CAC-4320-8AA4-4FA95079BD3F}"
22+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Magicodes.Wx.PublicAccount.Sdk.Test", "tests\Magicodes.Wx.PublicAccount.Sdk.Test\Magicodes.Wx.PublicAccount.Sdk.Test.csproj", "{F6098294-2CAC-4320-8AA4-4FA95079BD3F}"
23+
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Magicodes.Wx.PublicAccount.Sdk.Abp", "src\Magicodes.Wx.PublicAccount.Sdk.Abp\Magicodes.Wx.PublicAccount.Sdk.Abp.csproj", "{28701632-7434-4CC2-89AB-4C6D1F631776}"
2225
EndProject
2326
Global
2427
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -38,6 +41,10 @@ Global
3841
{F6098294-2CAC-4320-8AA4-4FA95079BD3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
3942
{F6098294-2CAC-4320-8AA4-4FA95079BD3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
4043
{F6098294-2CAC-4320-8AA4-4FA95079BD3F}.Release|Any CPU.Build.0 = Release|Any CPU
44+
{28701632-7434-4CC2-89AB-4C6D1F631776}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45+
{28701632-7434-4CC2-89AB-4C6D1F631776}.Debug|Any CPU.Build.0 = Debug|Any CPU
46+
{28701632-7434-4CC2-89AB-4C6D1F631776}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{28701632-7434-4CC2-89AB-4C6D1F631776}.Release|Any CPU.Build.0 = Release|Any CPU
4148
EndGlobalSection
4249
GlobalSection(SolutionProperties) = preSolution
4350
HideSolutionNode = FALSE
@@ -46,6 +53,7 @@ Global
4653
{6C26FC2C-E1A2-4FC4-BE0C-362F4752638C} = {8F81A401-76DB-4F52-A8E1-CEEE6D45DA2A}
4754
{9D11A1F9-3BB4-46FE-A4FF-17FACF505119} = {8F81A401-76DB-4F52-A8E1-CEEE6D45DA2A}
4855
{F6098294-2CAC-4320-8AA4-4FA95079BD3F} = {21B20DAF-56D3-414B-91FE-DD0D176A5277}
56+
{28701632-7434-4CC2-89AB-4C6D1F631776} = {8F81A401-76DB-4F52-A8E1-CEEE6D45DA2A}
4957
EndGlobalSection
5058
GlobalSection(ExtensibilityGlobals) = postSolution
5159
SolutionGuid = {EC173392-7AB2-4DB8-8FAF-D9ED2201A2B3}

common.props

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project>
2+
<PropertyGroup>
3+
<Version>0.0.2</Version>
4+
</PropertyGroup>
5+
<PropertyGroup>
6+
<Authors>雪雁</Authors>
7+
<Company>Magicodes</Company>
8+
<Product>麦扣</Product>
9+
<PackageProjectUrl>https://docs.xin-lai.com/</PackageProjectUrl>
10+
<RepositoryUrl>https://github.com/xin-lai/Magicodes.Wx.Sdk</RepositoryUrl>
11+
<PackageTags>WeChat Sdk;Wx Sdk;Public Account;Magicodes</PackageTags>
12+
<Description>
13+
最简洁最易于使用的微信Sdk,包括公众号Sdk、小程序Sdk、企业微信Sdk等,以及Abp VNext集成。
14+
开源库地址:https://github.com/xin-lai
15+
博客地址:http://www.cnblogs.com/codelove/
16+
公众号:麦扣聊技术
17+
交流QQ群:85318032
18+
19+
</Description>
20+
</PropertyGroup>
21+
<PropertyGroup>
22+
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
23+
</PropertyGroup>
24+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<Import Project="..\..\common.props"></Import>
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="Volo.Abp.Autofac" Version="4.1.1" />
8+
<PackageReference Include="Volo.Abp.AspNetCore" Version="4.1.1" />
9+
<PackageReference Include="Volo.Abp.Caching" Version="4.1.1" />
10+
</ItemGroup>
11+
<ItemGroup>
12+
<ProjectReference Include="..\Magicodes.Wx.PublicAccount.Sdk\Magicodes.Wx.PublicAccount.Sdk.csproj" />
13+
</ItemGroup>
14+
</Project>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Microsoft.Extensions.Configuration;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using System;
4+
using Volo.Abp;
5+
using Volo.Abp.Caching;
6+
using Volo.Abp.Modularity;
7+
using Volo.Abp.Settings;
8+
9+
namespace Magicodes.Wx.PublicAccount.Sdk.Abp
10+
{
11+
public class WxPublicAccountSdkModule : AbpModule
12+
{
13+
public override void ConfigureServices(ServiceConfigurationContext context)
14+
{
15+
context.Services.AddMagicodesWeChatSdk();
16+
}
17+
18+
public override void OnApplicationInitialization(ApplicationInitializationContext context)
19+
{
20+
var app = context.GetApplicationBuilder();
21+
var cache = context.ServiceProvider.GetRequiredService<IDistributedCache<string>>();
22+
23+
app.UseMagicodesWeChatSdk(setup =>
24+
{
25+
if (setup.GetWeChatOptions == null)
26+
{
27+
setup.GetWeChatOptions = () =>
28+
{
29+
var settingProvider = app.ApplicationServices.GetRequiredService<ISettingProvider>();
30+
var appId = settingProvider.GetOrNullAsync("Wx.AppId").GetAwaiter().GetResult();
31+
if (!string.IsNullOrEmpty(appId))
32+
{
33+
var appSecret = settingProvider.GetOrNullAsync("Wx.AppSecret").GetAwaiter().GetResult();
34+
var token = settingProvider.GetOrNullAsync("Wx.Token").GetAwaiter().GetResult();
35+
var weiXinAccount = settingProvider.GetOrNullAsync("Wx.WeiXinAccount").GetAwaiter().GetResult();
36+
return new WeChatOptions()
37+
{
38+
AppId = appId,
39+
AppSecret = appSecret,
40+
Token = token,
41+
WeiXinAccount = weiXinAccount,
42+
};
43+
}
44+
else
45+
{
46+
IConfiguration config = app.ApplicationServices.GetRequiredService<IConfiguration>();
47+
return WeChatHelper.GetWeChatOptionsByConfiguration(config);
48+
}
49+
};
50+
}
51+
setup.GetAccessTokenByAppId = (appid) => cache.Get($"AssessToken::{appid}");
52+
53+
setup.CacheAccessToken = (appid, token) =>
54+
{
55+
cache.Set($"AssessToken::{appid}", token, new Microsoft.Extensions.Caching.Distributed.DistributedCacheEntryOptions()
56+
{
57+
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(115)
58+
});
59+
};
60+
});
61+
62+
}
63+
}
64+
}

src/Magicodes.Wx.PublicAccount.Sdk.AspNet/Magicodes.Wx.PublicAccount.Sdk.AspNet.csproj

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
2+
<Import Project="..\..\common.props"></Import>
33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>
5-
<Version>0.0.1</Version>
6-
<Authors>雪雁</Authors>
7-
<Description>微信公众号Sdk ASP.NET Core扩展</Description>
8-
<PackageProjectUrl>https://github.com/xin-lai/Magicodes.Wx.Sdk</PackageProjectUrl>
9-
<RepositoryUrl>https://github.com/xin-lai/Magicodes.Wx.Sdk</RepositoryUrl>
10-
<RepositoryType>git</RepositoryType>
115
</PropertyGroup>
126

137
<ItemGroup>

src/Magicodes.Wx.PublicAccount.Sdk/Extentions.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,10 @@ public static void UseMagicodesWeChatSdk(this IApplicationBuilder app, Action<We
3131
if (funcs.GetWeChatOptions == null)
3232
{
3333
IConfiguration config = app.ApplicationServices.GetRequiredService<IConfiguration>();
34-
IConfigurationSection wechatConfig = config.GetSection("WeChat");
34+
IConfigurationSection wechatConfig = config.GetSection("Wx");
3535
if (wechatConfig != null)
3636
{
37-
funcs.GetWeChatOptions = () =>
38-
{
39-
return new WeChatOptions()
40-
{
41-
AppId = wechatConfig["AppId"],
42-
AppSecret = wechatConfig["AppSecret"],
43-
Token = wechatConfig["Token"],
44-
WeiXinAccount = wechatConfig["WeiXinAccount"],
45-
};
46-
};
37+
funcs.GetWeChatOptions = () => WeChatHelper.GetWeChatOptionsByConfiguration(config);
4738
}
4839
}
4940
}

src/Magicodes.Wx.PublicAccount.Sdk/Magicodes.Wx.PublicAccount.Sdk.csproj

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
2+
<Import Project="..\..\common.props"></Import>
33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>
5-
<Version>0.0.1</Version>
6-
<Authors>雪雁</Authors>
7-
<Description>微信公众号Sdk</Description>
8-
<PackageProjectUrl>https://github.com/xin-lai/Magicodes.Wx.Sdk</PackageProjectUrl>
9-
<RepositoryUrl>https://github.com/xin-lai/Magicodes.Wx.Sdk</RepositoryUrl>
10-
<RepositoryType>git</RepositoryType>
115
</PropertyGroup>
126
<ItemGroup>
137
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />

src/Magicodes.Wx.PublicAccount.Sdk/WeChatHelper.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Magicodes.Wx.PublicAccount.Sdk.Apis.Sns;
2+
using Microsoft.Extensions.Configuration;
23
using System;
34
using System.Security.Cryptography;
45
using System.Text;
@@ -59,5 +60,22 @@ public static string MD5(string encypStr, string charset)
5960
retStr = retStr.Replace("-", "").ToUpper();
6061
return retStr;
6162
}
63+
64+
/// <summary>
65+
///
66+
/// </summary>
67+
/// <param name="configuration"></param>
68+
public static WeChatOptions GetWeChatOptionsByConfiguration(IConfiguration configuration)
69+
{
70+
IConfigurationSection wechatConfig = configuration.GetSection("Wx");
71+
if (wechatConfig == null) return null;
72+
return new WeChatOptions()
73+
{
74+
AppId = wechatConfig["AppId"],
75+
AppSecret = wechatConfig["AppSecret"],
76+
Token = wechatConfig["Token"],
77+
WeiXinAccount = wechatConfig["WeiXinAccount"],
78+
};
79+
}
6280
}
6381
}

0 commit comments

Comments
 (0)