Skip to content

Commit

Permalink
添加Abp包装
Browse files Browse the repository at this point in the history
  • Loading branch information
codelove1314 committed Feb 3, 2021
1 parent e355a59 commit 726a168
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 26 deletions.
10 changes: 9 additions & 1 deletion Magicodes.Wx.Sdk.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A2B356C4-A7ED-4C3A-9DA2-7505AFC527D9}"
ProjectSection(SolutionItems) = preProject
azure-pipelines.yml = azure-pipelines.yml
common.props = common.props
LICENSE = LICENSE
README.md = README.md
EndProjectSection
Expand All @@ -18,7 +19,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Magicodes.Wx.PublicAccount.
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{21B20DAF-56D3-414B-91FE-DD0D176A5277}"
EndProject
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}"
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}"
EndProject
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -38,6 +41,10 @@ Global
{F6098294-2CAC-4320-8AA4-4FA95079BD3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F6098294-2CAC-4320-8AA4-4FA95079BD3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F6098294-2CAC-4320-8AA4-4FA95079BD3F}.Release|Any CPU.Build.0 = Release|Any CPU
{28701632-7434-4CC2-89AB-4C6D1F631776}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28701632-7434-4CC2-89AB-4C6D1F631776}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28701632-7434-4CC2-89AB-4C6D1F631776}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28701632-7434-4CC2-89AB-4C6D1F631776}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -46,6 +53,7 @@ Global
{6C26FC2C-E1A2-4FC4-BE0C-362F4752638C} = {8F81A401-76DB-4F52-A8E1-CEEE6D45DA2A}
{9D11A1F9-3BB4-46FE-A4FF-17FACF505119} = {8F81A401-76DB-4F52-A8E1-CEEE6D45DA2A}
{F6098294-2CAC-4320-8AA4-4FA95079BD3F} = {21B20DAF-56D3-414B-91FE-DD0D176A5277}
{28701632-7434-4CC2-89AB-4C6D1F631776} = {8F81A401-76DB-4F52-A8E1-CEEE6D45DA2A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EC173392-7AB2-4DB8-8FAF-D9ED2201A2B3}
Expand Down
24 changes: 24 additions & 0 deletions common.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project>
<PropertyGroup>
<Version>0.0.2</Version>
</PropertyGroup>
<PropertyGroup>
<Authors>雪雁</Authors>
<Company>Magicodes</Company>
<Product>麦扣</Product>
<PackageProjectUrl>https://docs.xin-lai.com/</PackageProjectUrl>
<RepositoryUrl>https://github.com/xin-lai/Magicodes.Wx.Sdk</RepositoryUrl>
<PackageTags>WeChat Sdk;Wx Sdk;Public Account;Magicodes</PackageTags>
<Description>
最简洁最易于使用的微信Sdk,包括公众号Sdk、小程序Sdk、企业微信Sdk等,以及Abp VNext集成。
开源库地址:https://github.com/xin-lai
博客地址:http://www.cnblogs.com/codelove/
公众号:麦扣聊技术
交流QQ群:85318032

</Description>
</PropertyGroup>
<PropertyGroup>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\common.props"></Import>
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Volo.Abp.Autofac" Version="4.1.1" />
<PackageReference Include="Volo.Abp.AspNetCore" Version="4.1.1" />
<PackageReference Include="Volo.Abp.Caching" Version="4.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Magicodes.Wx.PublicAccount.Sdk\Magicodes.Wx.PublicAccount.Sdk.csproj" />
</ItemGroup>
</Project>
64 changes: 64 additions & 0 deletions src/Magicodes.Wx.PublicAccount.Sdk.Abp/WxPublicAccountSdkModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using Volo.Abp;
using Volo.Abp.Caching;
using Volo.Abp.Modularity;
using Volo.Abp.Settings;

namespace Magicodes.Wx.PublicAccount.Sdk.Abp
{
public class WxPublicAccountSdkModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddMagicodesWeChatSdk();
}

public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
var app = context.GetApplicationBuilder();
var cache = context.ServiceProvider.GetRequiredService<IDistributedCache<string>>();

app.UseMagicodesWeChatSdk(setup =>
{
if (setup.GetWeChatOptions == null)
{
setup.GetWeChatOptions = () =>
{
var settingProvider = app.ApplicationServices.GetRequiredService<ISettingProvider>();
var appId = settingProvider.GetOrNullAsync("Wx.AppId").GetAwaiter().GetResult();
if (!string.IsNullOrEmpty(appId))
{
var appSecret = settingProvider.GetOrNullAsync("Wx.AppSecret").GetAwaiter().GetResult();
var token = settingProvider.GetOrNullAsync("Wx.Token").GetAwaiter().GetResult();
var weiXinAccount = settingProvider.GetOrNullAsync("Wx.WeiXinAccount").GetAwaiter().GetResult();
return new WeChatOptions()
{
AppId = appId,
AppSecret = appSecret,
Token = token,
WeiXinAccount = weiXinAccount,
};
}
else
{
IConfiguration config = app.ApplicationServices.GetRequiredService<IConfiguration>();
return WeChatHelper.GetWeChatOptionsByConfiguration(config);
}
};
}
setup.GetAccessTokenByAppId = (appid) => cache.Get($"AssessToken::{appid}");

setup.CacheAccessToken = (appid, token) =>
{
cache.Set($"AssessToken::{appid}", token, new Microsoft.Extensions.Caching.Distributed.DistributedCacheEntryOptions()
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(115)
});
};
});

}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\..\common.props"></Import>
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>0.0.1</Version>
<Authors>雪雁</Authors>
<Description>微信公众号Sdk ASP.NET Core扩展</Description>
<PackageProjectUrl>https://github.com/xin-lai/Magicodes.Wx.Sdk</PackageProjectUrl>
<RepositoryUrl>https://github.com/xin-lai/Magicodes.Wx.Sdk</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>

<ItemGroup>
Expand Down
13 changes: 2 additions & 11 deletions src/Magicodes.Wx.PublicAccount.Sdk/Extentions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,10 @@ public static void UseMagicodesWeChatSdk(this IApplicationBuilder app, Action<We
if (funcs.GetWeChatOptions == null)
{
IConfiguration config = app.ApplicationServices.GetRequiredService<IConfiguration>();
IConfigurationSection wechatConfig = config.GetSection("WeChat");
IConfigurationSection wechatConfig = config.GetSection("Wx");
if (wechatConfig != null)
{
funcs.GetWeChatOptions = () =>
{
return new WeChatOptions()
{
AppId = wechatConfig["AppId"],
AppSecret = wechatConfig["AppSecret"],
Token = wechatConfig["Token"],
WeiXinAccount = wechatConfig["WeiXinAccount"],
};
};
funcs.GetWeChatOptions = () => WeChatHelper.GetWeChatOptionsByConfiguration(config);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\..\common.props"></Import>
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>0.0.1</Version>
<Authors>雪雁</Authors>
<Description>微信公众号Sdk</Description>
<PackageProjectUrl>https://github.com/xin-lai/Magicodes.Wx.Sdk</PackageProjectUrl>
<RepositoryUrl>https://github.com/xin-lai/Magicodes.Wx.Sdk</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
Expand Down
18 changes: 18 additions & 0 deletions src/Magicodes.Wx.PublicAccount.Sdk/WeChatHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Magicodes.Wx.PublicAccount.Sdk.Apis.Sns;
using Microsoft.Extensions.Configuration;
using System;
using System.Security.Cryptography;
using System.Text;
Expand Down Expand Up @@ -59,5 +60,22 @@ public static string MD5(string encypStr, string charset)
retStr = retStr.Replace("-", "").ToUpper();
return retStr;
}

/// <summary>
///
/// </summary>
/// <param name="configuration"></param>
public static WeChatOptions GetWeChatOptionsByConfiguration(IConfiguration configuration)
{
IConfigurationSection wechatConfig = configuration.GetSection("Wx");
if (wechatConfig == null) return null;
return new WeChatOptions()
{
AppId = wechatConfig["AppId"],
AppSecret = wechatConfig["AppSecret"],
Token = wechatConfig["Token"],
WeiXinAccount = wechatConfig["WeiXinAccount"],
};
}
}
}

0 comments on commit 726a168

Please sign in to comment.