WechatApiHttpClient.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. })
  35. .UseHttpClient(_httpClientFactory.CreateClient(), disposeClient: false) // 设置 HttpClient 不随客户端一同销毁
  36. .Build();
  37. client.Configure(config =>
  38. {
  39. JsonSerializerSettings jsonSerializerSettings = NewtonsoftJsonSerializer.GetDefaultSerializerSettings();
  40. jsonSerializerSettings.Formatting = Formatting.Indented;
  41. config.JsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); // 指定 System.Text.Json JSON序列化
  42. // config.JsonSerializer = new SystemTextJsonSerializer(jsonSerializerOptions); // 指定 Newtonsoft.Json JSON序列化
  43. });
  44. return client;
  45. }
  46. /// <summary>
  47. /// 微信小程序
  48. /// </summary>
  49. /// <returns></returns>
  50. public WechatApiClient CreateWxOpenClient()
  51. {
  52. if (string.IsNullOrEmpty(_wechatOptions.WxOpenAppId) || string.IsNullOrEmpty(_wechatOptions.WxOpenAppSecret))
  53. throw Oops.Oh("微信小程序配置错误");
  54. var client = WechatApiClientBuilder.Create(new WechatApiClientOptions()
  55. {
  56. AppId = _wechatOptions.WxOpenAppId,
  57. AppSecret = _wechatOptions.WxOpenAppSecret
  58. })
  59. .UseHttpClient(_httpClientFactory.CreateClient(), disposeClient: false) // 设置 HttpClient 不随客户端一同销毁
  60. .Build();
  61. client.Configure(config =>
  62. {
  63. JsonSerializerSettings jsonSerializerSettings = NewtonsoftJsonSerializer.GetDefaultSerializerSettings();
  64. jsonSerializerSettings.Formatting = Formatting.Indented;
  65. config.JsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); // 指定 System.Text.Json JSON序列化
  66. // config.JsonSerializer = new SystemTextJsonSerializer(jsonSerializerOptions); // 指定 Newtonsoft.Json JSON序列化
  67. });
  68. return client;
  69. }
  70. }