WechatApiHttpClient.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // 大名科技(天津)有限公司 版权所有
  2. //
  3. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动
  6. //
  7. // 任何基于本项目二次开发而产生的一切法律纠纷和责任,均与作者无关
  8. using Newtonsoft.Json;
  9. namespace Admin.NET.Core.Service;
  10. /// <summary>
  11. /// 微信API客户端
  12. /// </summary>
  13. public partial class WechatApiClientFactory : ISingleton
  14. {
  15. private readonly IHttpClientFactory _httpClientFactory;
  16. public readonly WechatOptions _wechatOptions;
  17. public WechatApiClientFactory(IHttpClientFactory httpClientFactory, IOptions<WechatOptions> wechatOptions)
  18. {
  19. _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
  20. _wechatOptions = wechatOptions.Value ?? throw new ArgumentNullException(nameof(wechatOptions));
  21. }
  22. /// <summary>
  23. /// 微信公众号
  24. /// </summary>
  25. /// <returns></returns>
  26. public WechatApiClient CreateWechatClient()
  27. {
  28. if (string.IsNullOrEmpty(_wechatOptions.WechatAppId) || string.IsNullOrEmpty(_wechatOptions.WechatAppSecret))
  29. throw Oops.Oh("微信公众号配置错误");
  30. var client = WechatApiClientBuilder.Create(new WechatApiClientOptions()
  31. {
  32. AppId = _wechatOptions.WechatAppId,
  33. AppSecret = _wechatOptions.WechatAppSecret,
  34. PushToken = _wechatOptions.WechatToken,
  35. PushEncodingAESKey = _wechatOptions.WechatEncodingAESKey,
  36. })
  37. .UseHttpClient(_httpClientFactory.CreateClient(), disposeClient: false) // 设置 HttpClient 不随客户端一同销毁
  38. .Build();
  39. client.Configure(config =>
  40. {
  41. JsonSerializerSettings jsonSerializerSettings = NewtonsoftJsonSerializer.GetDefaultSerializerSettings();
  42. jsonSerializerSettings.Formatting = Formatting.Indented;
  43. config.JsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); // 指定 System.Text.Json JSON序列化
  44. // config.JsonSerializer = new SystemTextJsonSerializer(jsonSerializerOptions); // 指定 Newtonsoft.Json JSON序列化
  45. });
  46. return client;
  47. }
  48. /// <summary>
  49. /// 微信小程序
  50. /// </summary>
  51. /// <returns></returns>
  52. public WechatApiClient CreateWxOpenClient()
  53. {
  54. if (string.IsNullOrEmpty(_wechatOptions.WxOpenAppId) || string.IsNullOrEmpty(_wechatOptions.WxOpenAppSecret))
  55. throw Oops.Oh("微信小程序配置错误");
  56. var client = WechatApiClientBuilder.Create(new WechatApiClientOptions()
  57. {
  58. AppId = _wechatOptions.WxOpenAppId,
  59. AppSecret = _wechatOptions.WxOpenAppSecret,
  60. PushToken = _wechatOptions.WxToken,
  61. PushEncodingAESKey = _wechatOptions.WxEncodingAESKey,
  62. })
  63. .UseHttpClient(_httpClientFactory.CreateClient(), disposeClient: false) // 设置 HttpClient 不随客户端一同销毁
  64. .Build();
  65. client.Configure(config =>
  66. {
  67. JsonSerializerSettings jsonSerializerSettings = NewtonsoftJsonSerializer.GetDefaultSerializerSettings();
  68. jsonSerializerSettings.Formatting = Formatting.Indented;
  69. config.JsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); // 指定 System.Text.Json JSON序列化
  70. // config.JsonSerializer = new SystemTextJsonSerializer(jsonSerializerOptions); // 指定 Newtonsoft.Json JSON序列化
  71. });
  72. return client;
  73. }
  74. }