WeChatOpen.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Threading.Tasks;
  2. using GxPress.Common.Http;
  3. using Newtonsoft.Json;
  4. using GxPress.Repository.Interface;
  5. using GxPress.Service.Interface.WeChat;
  6. namespace GxPress.Service.Implement.WeChat
  7. {
  8. /// <summary>
  9. /// 微信开发平台
  10. /// </summary>
  11. public class WeChatOpen:IWeChatOpen
  12. {
  13. private readonly IUserRepository userRepository;
  14. public WeChatOpen(IUserRepository userRepository)
  15. {
  16. this.userRepository = userRepository;
  17. }
  18. public async Task<string> GetWeChatOpenUserInfoAsync(string code)
  19. {
  20. var url = $@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx7f8c16978c2ab126&secret=d157ee6a4b2c9d437dab4ebc37fd2fa8&code={code}&grant_type=authorization_code";
  21. var response = await HttpClientHelper.GetHeadersAsync(url, null);
  22. if (response.IsSuccessStatusCode)
  23. {
  24. var content = await response.Content.ReadAsStringAsync();
  25. var data = JsonConvert.DeserializeObject<WeChatOpenResult>(content);
  26. if (string.IsNullOrEmpty(data.errmsg))
  27. {
  28. return data.openid;
  29. }
  30. }
  31. return string.Empty;
  32. }
  33. }
  34. //如果好用,请收藏地址,帮忙分享。
  35. public class WeChatOpenResult
  36. {
  37. /// <summary>
  38. ///
  39. /// </summary>
  40. public string access_token { get; set; }
  41. /// <summary>
  42. ///
  43. /// </summary>
  44. public int expires_in { get; set; }
  45. /// <summary>
  46. ///
  47. /// </summary>
  48. public string refresh_token { get; set; }
  49. /// <summary>
  50. ///
  51. /// </summary>
  52. public string openid { get; set; }
  53. /// <summary>
  54. ///
  55. /// </summary>
  56. public string scope { get; set; }
  57. /// <summary>
  58. ///
  59. /// </summary>
  60. public string unionid { get; set; }
  61. /// <summary>
  62. ///
  63. /// </summary>
  64. public int errcode { get; set; }
  65. /// <summary>
  66. ///
  67. /// </summary>
  68. public string errmsg { get; set; }
  69. }
  70. }