// Admin.NET 项目的版æƒã€å•†æ ‡ã€ä¸“利和其他相关æƒåˆ©å‡å—ç›¸åº”æ³•å¾‹æ³•è§„çš„ä¿æŠ¤ã€‚ä½¿ç”¨æœ¬é¡¹ç›®åº”éµå®ˆç›¸å…³æ³•律法规和许å¯è¯çš„è¦æ±‚。 // // 本项目主è¦éµå¾ª MIT 许å¯è¯å’Œ Apache 许å¯è¯ï¼ˆç‰ˆæœ¬ 2.0)进行分å‘和使用。许å¯è¯ä½äºŽæºä»£ç æ ‘æ ¹ç›®å½•ä¸çš„ LICENSE-MIT å’Œ LICENSE-APACHE 文件。 // // ä¸å¾—利用本项目从事å±å®³å›½å®¶å®‰å…¨ã€æ‰°ä¹±ç¤¾ä¼šç§©åºã€ä¾µçŠ¯ä»–äººåˆæ³•æƒç›Šç‰æ³•å¾‹æ³•è§„ç¦æ¢çš„æ´»åЍï¼ä»»ä½•基于本项目二次开å‘è€Œäº§ç”Ÿçš„ä¸€åˆ‡æ³•å¾‹çº çº·å’Œè´£ä»»ï¼Œæˆ‘ä»¬ä¸æ‰¿æ‹…ä»»ä½•è´£ä»»ï¼ using Newtonsoft.Json; namespace Admin.NET.Core.Service; /// <summary> /// 微信API客户端 /// </summary> public partial class WechatApiClientFactory : ISingleton { private readonly IHttpClientFactory _httpClientFactory; public readonly WechatOptions _wechatOptions; public WechatApiClientFactory(IHttpClientFactory httpClientFactory, IOptions<WechatOptions> wechatOptions) { _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory)); _wechatOptions = wechatOptions.Value ?? throw new ArgumentNullException(nameof(wechatOptions)); } /// <summary> /// å¾®ä¿¡å…¬ä¼—å· /// </summary> /// <returns></returns> public WechatApiClient CreateWechatClient() { if (string.IsNullOrEmpty(_wechatOptions.WechatAppId) || string.IsNullOrEmpty(_wechatOptions.WechatAppSecret)) throw Oops.Oh("微信公众å·é…置错误"); var client = WechatApiClientBuilder.Create(new WechatApiClientOptions() { AppId = _wechatOptions.WechatAppId, AppSecret = _wechatOptions.WechatAppSecret, PushToken = _wechatOptions.WechatToken, PushEncodingAESKey = _wechatOptions.WechatEncodingAESKey, }) .UseHttpClient(_httpClientFactory.CreateClient(), disposeClient: false) // 设置 HttpClient ä¸éšå®¢æˆ·ç«¯ä¸€åŒé”€æ¯ .Build(); client.Configure(config => { JsonSerializerSettings jsonSerializerSettings = NewtonsoftJsonSerializer.GetDefaultSerializerSettings(); jsonSerializerSettings.Formatting = Formatting.Indented; config.JsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); // 指定 System.Text.Json JSONåºåˆ—化 // config.JsonSerializer = new SystemTextJsonSerializer(jsonSerializerOptions); // 指定 Newtonsoft.Json JSONåºåˆ—化 }); return client; } /// <summary> /// 微信å°ç¨‹åº /// </summary> /// <returns></returns> public WechatApiClient CreateWxOpenClient() { if (string.IsNullOrEmpty(_wechatOptions.WxOpenAppId) || string.IsNullOrEmpty(_wechatOptions.WxOpenAppSecret)) throw Oops.Oh("微信å°ç¨‹åºé…置错误"); var client = WechatApiClientBuilder.Create(new WechatApiClientOptions() { AppId = _wechatOptions.WxOpenAppId, AppSecret = _wechatOptions.WxOpenAppSecret, PushToken = _wechatOptions.WxToken, PushEncodingAESKey = _wechatOptions.WxEncodingAESKey, }) .UseHttpClient(_httpClientFactory.CreateClient(), disposeClient: false) // 设置 HttpClient ä¸éšå®¢æˆ·ç«¯ä¸€åŒé”€æ¯ .Build(); client.Configure(config => { JsonSerializerSettings jsonSerializerSettings = NewtonsoftJsonSerializer.GetDefaultSerializerSettings(); jsonSerializerSettings.Formatting = Formatting.Indented; config.JsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); // 指定 System.Text.Json JSONåºåˆ—化 // config.JsonSerializer = new SystemTextJsonSerializer(jsonSerializerOptions); // 指定 Newtonsoft.Json JSONåºåˆ—化 }); return client; } }