WechatApiHttpClient.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
  2. //
  3. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
  4. using Newtonsoft.Json;
  5. namespace Admin.NET.Core.Service;
  6. /// <summary>
  7. /// 微信API客户端
  8. /// </summary>
  9. public partial class WechatApiClientFactory : ISingleton
  10. {
  11. private readonly IHttpClientFactory _httpClientFactory;
  12. public readonly WechatOptions _wechatOptions;
  13. public WechatApiClientFactory(IHttpClientFactory httpClientFactory, IOptions<WechatOptions> wechatOptions)
  14. {
  15. _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
  16. _wechatOptions = wechatOptions.Value ?? throw new ArgumentNullException(nameof(wechatOptions));
  17. }
  18. /// <summary>
  19. /// 微信公众号
  20. /// </summary>
  21. /// <returns></returns>
  22. public WechatApiClient CreateWechatClient()
  23. {
  24. if (string.IsNullOrEmpty(_wechatOptions.WechatAppId) || string.IsNullOrEmpty(_wechatOptions.WechatAppSecret))
  25. throw Oops.Oh("微信公众号配置错误");
  26. var client = WechatApiClientBuilder.Create(new WechatApiClientOptions()
  27. {
  28. AppId = _wechatOptions.WechatAppId,
  29. AppSecret = _wechatOptions.WechatAppSecret,
  30. })
  31. .UseHttpClient(_httpClientFactory.CreateClient(), disposeClient: false) // 设置 HttpClient 不随客户端一同销毁
  32. .Build();
  33. client.Configure(config =>
  34. {
  35. JsonSerializerSettings jsonSerializerSettings = NewtonsoftJsonSerializer.GetDefaultSerializerSettings();
  36. jsonSerializerSettings.Formatting = Formatting.Indented;
  37. config.JsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); // 指定 System.Text.Json JSON序列化
  38. // config.JsonSerializer = new SystemTextJsonSerializer(jsonSerializerOptions); // 指定 Newtonsoft.Json JSON序列化
  39. });
  40. return client;
  41. }
  42. /// <summary>
  43. /// 微信小程序
  44. /// </summary>
  45. /// <returns></returns>
  46. public WechatApiClient CreateWxOpenClient()
  47. {
  48. if (string.IsNullOrEmpty(_wechatOptions.WxOpenAppId) || string.IsNullOrEmpty(_wechatOptions.WxOpenAppSecret))
  49. throw Oops.Oh("微信小程序配置错误");
  50. var client = WechatApiClientBuilder.Create(new WechatApiClientOptions()
  51. {
  52. AppId = _wechatOptions.WxOpenAppId,
  53. AppSecret = _wechatOptions.WxOpenAppSecret
  54. })
  55. .UseHttpClient(_httpClientFactory.CreateClient(), disposeClient: false) // 设置 HttpClient 不随客户端一同销毁
  56. .Build();
  57. client.Configure(config =>
  58. {
  59. JsonSerializerSettings jsonSerializerSettings = NewtonsoftJsonSerializer.GetDefaultSerializerSettings();
  60. jsonSerializerSettings.Formatting = Formatting.Indented;
  61. config.JsonSerializer = new NewtonsoftJsonSerializer(jsonSerializerSettings); // 指定 System.Text.Json JSON序列化
  62. // config.JsonSerializer = new SystemTextJsonSerializer(jsonSerializerOptions); // 指定 Newtonsoft.Json JSON序列化
  63. });
  64. return client;
  65. }
  66. }