WechatApiHttpClient.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // 麻省理工学院许可证
  2. //
  3. // 版权所有 (c) 2021-2023 zuohuaijun,大名科技(天津)有限公司 联系电话/微信:18020030720 QQ:515096995
  4. //
  5. // 特此免费授予获得本软件的任何人以处理本软件的权利,但须遵守以下条件:在所有副本或重要部分的软件中必须包括上述版权声明和本许可声明。
  6. //
  7. // 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。
  8. // 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。
  9. namespace Admin.NET.Core.Service;
  10. /// <summary>
  11. /// 微信API客户端
  12. /// </summary>
  13. public partial class WechatApiHttpClient : ITransient
  14. {
  15. public readonly WechatOptions _wechatOptions;
  16. public WechatApiHttpClient(IOptions<WechatOptions> wechatOptions)
  17. {
  18. _wechatOptions = wechatOptions.Value;
  19. }
  20. /// <summary>
  21. /// 微信公众号
  22. /// </summary>
  23. /// <returns></returns>
  24. public WechatApiClient CreateWechatClient()
  25. {
  26. if (string.IsNullOrEmpty(_wechatOptions.WechatAppId) || string.IsNullOrEmpty(_wechatOptions.WechatAppSecret))
  27. throw Oops.Oh("微信公众号配置错误");
  28. var wechatApiClient = new WechatApiClient(new WechatApiClientOptions()
  29. {
  30. AppId = _wechatOptions.WechatAppId,
  31. AppSecret = _wechatOptions.WechatAppSecret,
  32. });
  33. wechatApiClient.Configure(settings =>
  34. {
  35. settings.JsonSerializer = new FlurlNewtonsoftJsonSerializer();
  36. });
  37. return wechatApiClient;
  38. }
  39. /// <summary>
  40. /// 微信小程序
  41. /// </summary>
  42. /// <returns></returns>
  43. public WechatApiClient CreateWxOpenClient()
  44. {
  45. if (string.IsNullOrEmpty(_wechatOptions.WxOpenAppId) || string.IsNullOrEmpty(_wechatOptions.WxOpenAppSecret))
  46. throw Oops.Oh("微信小程序配置错误");
  47. var WechatApiClient = new WechatApiClient(new WechatApiClientOptions()
  48. {
  49. AppId = _wechatOptions.WxOpenAppId,
  50. AppSecret = _wechatOptions.WxOpenAppSecret
  51. });
  52. WechatApiClient.Configure(settings =>
  53. {
  54. settings.JsonSerializer = new FlurlNewtonsoftJsonSerializer();
  55. });
  56. return WechatApiClient;
  57. }
  58. }