forked from JeffreySu/WeiXinMPSDK
-
Notifications
You must be signed in to change notification settings - Fork 0
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
4 changed files
with
392 additions
and
2 deletions.
There are no files selected for viewing
287 changes: 285 additions & 2 deletions
287
Samples/Senparc.Weixin.MP.Sample.Consoles/Senparc.Weixin.MP.Sample.Consoles/Program.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 |
---|---|---|
@@ -1,12 +1,295 @@ | ||
using System; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using Senparc.CO2NET; | ||
using Senparc.CO2NET.Cache; | ||
using Senparc.CO2NET.RegisterServices; | ||
using Senparc.Weixin.Entities; | ||
using Senparc.Weixin.RegisterServices; | ||
using System; | ||
|
||
namespace Senparc.Weixin.MP.Sample.Consoles | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
Console.WriteLine("Hello World!"); | ||
var dt1 = SystemTime.Now; | ||
var services = new ServiceCollection(); | ||
var configBuilder = new ConfigurationBuilder(); | ||
configBuilder.AddJsonFile("appsettings.json"); | ||
Console.WriteLine("完成 appsettings.json 添加"); | ||
|
||
var config = configBuilder.Build(); | ||
Console.WriteLine("完成 ServiceCollection 和 ConfigurationBuilder 初始化"); | ||
|
||
services.AddMemoryCache();//使用本地缓存必须添加 | ||
|
||
/* | ||
* CO2NET 是从 Senparc.Weixin 分离的底层公共基础模块,经过了长达 6 年的迭代优化,稳定可靠。 | ||
* 关于 CO2NET 在所有项目中的通用设置可参考 CO2NET 的 Sample: | ||
* https://github.com/Senparc/Senparc.CO2NET/blob/master/Sample/Senparc.CO2NET.Sample.netcore/Startup.cs | ||
*/ | ||
|
||
services.AddSenparcGlobalServices(config);//Senparc.CO2NET 全局注册 | ||
Console.WriteLine("完成 AddSenparcGlobalServices 注册"); | ||
|
||
|
||
SenparcSetting senparcSetting = new SenparcSetting() | ||
{ | ||
IsDebug = true, | ||
DefaultCacheNamespace = "DefaultCache", | ||
//Cache_Redis_Configuration = "localhost:6379"//Redis 连接字符串 | ||
//Cache_Redis_Configuration = "localhost:6379,password=senparc,connectTimeout=1000,connectRetry=2,syncTimeout=10000,defaultDatabase=3",//密码及其他配置 | ||
SenparcUnionAgentKey = "SenparcUnionAgentKey"//无需修改 | ||
}; | ||
|
||
var senparcWeixinSetting = SenparcDI.GetService<IOptions<SenparcWeixinSetting>>(); | ||
|
||
|
||
// 启动 CO2NET 全局注册,必须! | ||
IRegisterService register = RegisterService.Start(senparcSetting) | ||
//关于 UseSenparcGlobal() 的更多用法见 CO2NET Demo:https://github.com/Senparc/Senparc.CO2NET/blob/master/Sample/Senparc.CO2NET.Sample.netcore/Startup.cs | ||
.UseSenparcGlobal(); | ||
|
||
Console.WriteLine("完成 RegisterService.Start().UseSenparcGlobal() 启动设置"); | ||
Console.WriteLine($"设定程序目录为:{Senparc.CO2NET.Config.RootDictionaryPath}"); | ||
|
||
#region CO2NET 全局配置 | ||
|
||
#region 全局缓存配置(按需) | ||
|
||
//当同一个分布式缓存同时服务于多个网站(应用程序池)时,可以使用命名空间将其隔离(非必须) | ||
register.ChangeDefaultCacheNamespace("DefaultCO2NETCache"); | ||
Console.WriteLine($"默认缓存命名空间替换为:{CO2NET.Config.DefaultCacheNamespace}"); | ||
|
||
|
||
#region 配置和使用 Redis -- DPBMARK Redis | ||
|
||
//配置全局使用Redis缓存(按需,独立) | ||
var redisConfigurationStr = senparcSetting.Cache_Redis_Configuration; | ||
var useRedis = !string.IsNullOrEmpty(redisConfigurationStr) && redisConfigurationStr != "#{Cache_Redis_Configuration}#"/*默认值,不启用*/; | ||
if (useRedis)//这里为了方便不同环境的开发者进行配置,做成了判断的方式,实际开发环境一般是确定的,这里的if条件可以忽略 | ||
{ | ||
/* 说明: | ||
* 1、Redis 的连接字符串信息会从 Config.SenparcSetting.Cache_Redis_Configuration 自动获取并注册,如不需要修改,下方方法可以忽略 | ||
/* 2、如需手动修改,可以通过下方 SetConfigurationOption 方法手动设置 Redis 链接信息(仅修改配置,不立即启用) | ||
*/ | ||
Senparc.CO2NET.Cache.Redis.Register.SetConfigurationOption(redisConfigurationStr); | ||
Console.WriteLine("完成 Redis 设置"); | ||
|
||
|
||
//以下会立即将全局缓存设置为 Redis | ||
Senparc.CO2NET.Cache.Redis.Register.UseKeyValueRedisNow();//键值对缓存策略(推荐) | ||
Console.WriteLine("启用 Redis UseKeyValue 策略"); | ||
|
||
//Senparc.CO2NET.Cache.Redis.Register.UseHashRedisNow();//HashSet储存格式的缓存策略 | ||
|
||
//也可以通过以下方式自定义当前需要启用的缓存策略 | ||
//CacheStrategyFactory.RegisterObjectCacheStrategy(() => RedisObjectCacheStrategy.Instance);//键值对 | ||
//CacheStrategyFactory.RegisterObjectCacheStrategy(() => RedisHashSetObjectCacheStrategy.Instance);//HashSet | ||
} | ||
//如果这里不进行Redis缓存启用,则目前还是默认使用内存缓存 | ||
|
||
#endregion // DPBMARK_END | ||
|
||
#region 配置和使用 Memcached -- DPBMARK Memcached | ||
|
||
//配置Memcached缓存(按需,独立) | ||
var memcachedConfigurationStr = senparcSetting.Cache_Memcached_Configuration; | ||
var useMemcached = !string.IsNullOrEmpty(memcachedConfigurationStr) && memcachedConfigurationStr != "#{Cache_Memcached_Configuration}#"; | ||
|
||
if (useMemcached) //这里为了方便不同环境的开发者进行配置,做成了判断的方式,实际开发环境一般是确定的,这里的if条件可以忽略 | ||
{ | ||
/* 说明: | ||
* 1、Memcached 的连接字符串信息会从 Config.SenparcSetting.Cache_Memcached_Configuration 自动获取并注册,如不需要修改,下方方法可以忽略 | ||
/* 2、如需手动修改,可以通过下方 SetConfigurationOption 方法手动设置 Memcached 链接信息(仅修改配置,不立即启用) | ||
*/ | ||
Senparc.CO2NET.Cache.Memcached.Register.SetConfigurationOption(memcachedConfigurationStr); | ||
Console.WriteLine("完成 Memcached 设置"); | ||
|
||
//以下会立即将全局缓存设置为 Memcached | ||
Senparc.CO2NET.Cache.Memcached.Register.UseMemcachedNow(); | ||
Console.WriteLine("启用 Memcached UseKeyValue 策略"); | ||
|
||
|
||
//也可以通过以下方式自定义当前需要启用的缓存策略 | ||
CacheStrategyFactory.RegisterObjectCacheStrategy(() => MemcachedObjectCacheStrategy.Instance); | ||
Console.WriteLine("立即启用 Memcached 策略"); | ||
} | ||
|
||
#endregion // DPBMARK_END | ||
|
||
#endregion | ||
|
||
#region 注册日志(按需,建议) | ||
|
||
register.RegisterTraceLog(ConfigTraceLog);//配置TraceLog | ||
|
||
#endregion | ||
|
||
#endregion | ||
|
||
#region 微信相关配置 | ||
|
||
|
||
/* 微信配置开始 | ||
* | ||
* 建议按照以下顺序进行注册,尤其须将缓存放在第一位! | ||
*/ | ||
|
||
//注册开始 | ||
|
||
//开始注册微信信息,必须! | ||
register.UseSenparcWeixin(senparcWeixinSetting.Value, senparcSetting) | ||
//注意:上一行没有 ; 下面可接着写 .RegisterXX() | ||
|
||
#region 注册公众号或小程序(按需) | ||
|
||
//注册公众号(可注册多个) -- DPBMARK MP | ||
.RegisterMpAccount(senparcWeixinSetting.Value, "【盛派网络小助手】公众号")// DPBMARK_END | ||
|
||
|
||
//注册多个公众号或小程序(可注册多个) -- DPBMARK MiniProgram | ||
.RegisterWxOpenAccount(senparcWeixinSetting.Value, "【盛派网络小助手】小程序")// DPBMARK_END | ||
|
||
//除此以外,仍然可以在程序任意地方注册公众号或小程序: | ||
//AccessTokenContainer.Register(appId, appSecret, name);//命名空间:Senparc.Weixin.MP.Containers | ||
#endregion | ||
|
||
#region 注册企业号(按需) -- DPBMARK Work | ||
|
||
//注册企业微信(可注册多个) | ||
.RegisterWorkAccount(senparcWeixinSetting.Value, "【盛派网络】企业微信") | ||
|
||
//除此以外,仍然可以在程序任意地方注册企业微信: | ||
//AccessTokenContainer.Register(corpId, corpSecret, name);//命名空间:Senparc.Weixin.Work.Containers | ||
#endregion // DPBMARK_END | ||
|
||
#region 注册微信支付(按需) -- DPBMARK TenPay | ||
|
||
//注册旧微信支付版本(V2)(可注册多个) | ||
.RegisterTenpayOld(senparcWeixinSetting.Value, "【盛派网络小助手】公众号")//这里的 name 和第一个 RegisterMpAccount() 中的一致,会被记录到同一个 SenparcWeixinSettingItem 对象中 | ||
|
||
//注册最新微信支付版本(V3)(可注册多个) | ||
.RegisterTenpayV3(senparcWeixinSetting.Value, "【盛派网络小助手】公众号")//记录到同一个 SenparcWeixinSettingItem 对象中 | ||
|
||
#endregion // DPBMARK_END | ||
|
||
#region 注册微信第三方平台(按需) -- DPBMARK Open | ||
|
||
//注册第三方平台(可注册多个) | ||
.RegisterOpenComponent(senparcWeixinSetting.Value, | ||
//getComponentVerifyTicketFunc | ||
componentAppId => | ||
{ | ||
var dir = Path.Combine(ServerUtility.ContentRootMapPath("~/App_Data/OpenTicket")); | ||
if (!Directory.Exists(dir)) | ||
{ | ||
Directory.CreateDirectory(dir); | ||
} | ||
var file = Path.Combine(dir, string.Format("{0}.txt", componentAppId)); | ||
using (var fs = new FileStream(file, FileMode.Open)) | ||
{ | ||
using (var sr = new StreamReader(fs)) | ||
{ | ||
var ticket = sr.ReadToEnd(); | ||
return ticket; | ||
} | ||
} | ||
}, | ||
|
||
//getAuthorizerRefreshTokenFunc | ||
(componentAppId, auhtorizerId) => | ||
{ | ||
var dir = Path.Combine(ServerUtility.ContentRootMapPath("~/App_Data/AuthorizerInfo/" + componentAppId)); | ||
if (!Directory.Exists(dir)) | ||
{ | ||
Directory.CreateDirectory(dir); | ||
} | ||
var file = Path.Combine(dir, string.Format("{0}.bin", auhtorizerId)); | ||
if (!File.Exists(file)) | ||
{ | ||
return null; | ||
} | ||
using (Stream fs = new FileStream(file, FileMode.Open)) | ||
{ | ||
var binFormat = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); | ||
var result = (RefreshAuthorizerTokenResult)binFormat.Deserialize(fs); | ||
return result.authorizer_refresh_token; | ||
} | ||
}, | ||
|
||
//authorizerTokenRefreshedFunc | ||
(componentAppId, auhtorizerId, refreshResult) => | ||
{ | ||
var dir = Path.Combine(ServerUtility.ContentRootMapPath("~/App_Data/AuthorizerInfo/" + componentAppId)); | ||
if (!Directory.Exists(dir)) | ||
{ | ||
Directory.CreateDirectory(dir); | ||
} | ||
var file = Path.Combine(dir, string.Format("{0}.bin", auhtorizerId)); | ||
using (Stream fs = new FileStream(file, FileMode.Create)) | ||
{ | ||
//这里存了整个对象,实际上只存RefreshToken也可以,有了RefreshToken就能刷新到最新的AccessToken | ||
var binFormat = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); | ||
binFormat.Serialize(fs, refreshResult); | ||
fs.Flush(); | ||
} | ||
}, "【盛派网络】开放平台") | ||
|
||
//除此以外,仍然可以在程序任意地方注册开放平台: | ||
//ComponentContainer.Register();//命名空间:Senparc.Weixin.Open.Containers | ||
#endregion // DPBMARK_END | ||
|
||
; | ||
|
||
/* 微信配置结束 */ | ||
|
||
#endregion | ||
|
||
|
||
Console.WriteLine("Hello Senparc.Weixin!"); | ||
Console.WriteLine($"Total initialization time: {(SystemTime.Now - dt1).TotalMilliseconds}ms"); | ||
|
||
Console.WriteLine($"当前缓存策略: {CacheStrategyFactory.GetObjectCacheStrategyInstance()}"); | ||
|
||
Console.ReadLine(); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// 配置微信跟踪日志 | ||
/// </summary> | ||
static void ConfigTraceLog() | ||
{ | ||
//这里设为Debug状态时,/App_Data/WeixinTraceLog/目录下会生成日志文件记录所有的API请求日志,正式发布版本建议关闭 | ||
|
||
//如果全局的IsDebug(Senparc.CO2NET.Config.IsDebug)为false,此处可以单独设置true,否则自动为true | ||
CO2NET.Trace.SenparcTrace.SendCustomLog("系统日志", "系统启动");//只在Senparc.Weixin.Config.IsDebug = true的情况下生效 | ||
|
||
//全局自定义日志记录回调 | ||
CO2NET.Trace.SenparcTrace.OnLogFunc = () => | ||
{ | ||
//加入每次触发Log后需要执行的代码 | ||
}; | ||
|
||
|
||
//当发生基于WeixinException的异常时触发 | ||
WeixinTrace.OnWeixinExceptionFunc = ex => | ||
{ | ||
//加入每次触发WeixinExceptionLog后需要执行的代码 | ||
//发送模板消息给管理员 -- DPBMARK Redis | ||
var eventService = new Senparc.Weixin.MP.Sample.CommonService.EventService(); | ||
eventService.ConfigOnWeixinExceptionFunc(ex); // DPBMARK_END | ||
}; | ||
|
||
Console.WriteLine("完成日志设置,已经记录 1 条系统启动日志"); | ||
} | ||
} | ||
} |
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
76 changes: 76 additions & 0 deletions
76
Samples/Senparc.Weixin.MP.Sample.Consoles/Senparc.Weixin.MP.Sample.Consoles/appsettings.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,76 @@ | ||
{ | ||
"Logging": { | ||
"IncludeScopes": false, | ||
"LogLevel": { | ||
"Default": "Warning" | ||
} | ||
}, | ||
|
||
//以下信息会被自动识别,如无特殊说明,不用的参数可以删除,但修改 key 后将会无法自动识别! | ||
|
||
//CO2NET 设置 | ||
"SenparcSetting": { | ||
//以下为 CO2NET 的 SenparcSetting 全局配置,请勿修改 key,勿删除任何项 | ||
|
||
"IsDebug": true, | ||
"DefaultCacheNamespace": "DefaultCache", | ||
|
||
//分布式缓存 | ||
"Cache_Redis_Configuration": "#{Cache_Redis_Configuration}#", //Redis配置 | ||
//"Cache_Redis_Configuration": "localhost:6379",//不包含密码 | ||
//"Cache_Redis_Configuration": "localhost:6379,password=senparc,connectTimeout=1000,connectRetry=2,syncTimeout=10000,defaultDatabase=3",//密码及其他配置 | ||
"Cache_Memcached_Configuration": "#{Cache_Memcached_Configuration}#", //Memcached配置 | ||
"SenparcUnionAgentKey": "#{SenparcUnionAgentKey}#" //SenparcUnionAgentKey | ||
}, | ||
//Senparc.Weixin SDK 设置 | ||
"SenparcWeixinSetting": { | ||
//以下为 Senparc.Weixin 的 SenparcWeixinSetting 微信配置 | ||
|
||
//微信全局 | ||
"IsDebug": true, | ||
|
||
//以下不使用的参数可以删除,key 修改后将会失效 | ||
|
||
//公众号 | ||
"Token": "#{Token}#", | ||
"EncodingAESKey": "#{EncodingAESKey}#", | ||
"WeixinAppId": "#{WeixinAppId}#", | ||
"WeixinAppSecret": "#{WeixinAppSecret}#", | ||
//小程序 | ||
"WxOpenAppId": "#{WxOpenAppId}#", | ||
"WxOpenAppSecret": "#{WxOpenAppSecret}#", | ||
"WxOpenToken": "#{WxOpenToken}#", | ||
"WxOpenEncodingAESKey": "#{WxOpenEncodingAESKey}#", | ||
//企业微信 | ||
"WeixinCorpId": "#{WeixinCorpId}#", | ||
"WeixinCorpSecret": "#{WeixinCorpSecret}#", | ||
|
||
//微信支付 | ||
//微信支付V2(旧版) | ||
"WeixinPay_PartnerId": "#{WeixinPay_PartnerId}#", | ||
"WeixinPay_Key": "#{WeixinPay_Key}#", | ||
"WeixinPay_AppId": "#{WeixinPay_AppId}#", | ||
"WeixinPay_AppKey": "#{WeixinPay_AppKey}#", | ||
"WeixinPay_TenpayNotify": "#{WeixinPay_TenpayNotify}#", | ||
//微信支付V3(新版) | ||
"TenPayV3_MchId": "#{TenPayV3_MchId}#", | ||
"TenPayV3_SubMchId": "#{TenPayV3_SubMchId}#",//子商户,没有可留空 | ||
"TenPayV3_Key": "#{TenPayV3_Key}#", | ||
"TenPayV3_AppId": "#{TenPayV3_AppId}#", | ||
"TenPayV3_AppSecret": "#{TenPayV3_AppSecret}#", | ||
"TenPayV3_TenpayNotify": "#{TenPayV3_TenpayNotify}#", //http://YourDomainName/TenpayV3/PayNotifyUrl | ||
//如果不设置TenPayV3_WxOpenTenpayNotify,默认在 TenPayV3_TenpayNotify 的值最后加上 "WxOpen" | ||
"TenPayV3_WxOpenTenpayNotify": "#{TenPayV3_WxOpenTenpayNotify}#", //http://YourDomainName/TenpayV3/PayNotifyUrlWxOpen | ||
|
||
//开放平台 | ||
"Component_Appid": "#{Component_Appid}#", | ||
"Component_Secret": "#{Component_Secret}#", | ||
"Component_Token": "#{Component_Token}#", | ||
"Component_EncodingAESKey": "#{Component_EncodingAESKey}#", | ||
|
||
//扩展及代理参数 | ||
"AgentUrl": "#{AgentUrl}#", | ||
"AgentToken": "#{AgentToken}#", | ||
"SenparcWechatAgentKey": "#{SenparcWechatAgentKey}#" | ||
} | ||
} |
Oops, something went wrong.