1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- using System.Threading.Tasks;
- using GxPress.Common.Http;
- using Newtonsoft.Json;
- using GxPress.Repository.Interface;
- using GxPress.Service.Interface.WeChat;
- namespace GxPress.Service.Implement.WeChat
- {
- /// <summary>
- /// 微信开发平台
- /// </summary>
- public class WeChatOpen:IWeChatOpen
- {
- private readonly IUserRepository userRepository;
- public WeChatOpen(IUserRepository userRepository)
- {
- this.userRepository = userRepository;
- }
- public async Task<string> GetWeChatOpenUserInfoAsync(string code)
- {
- var url = $@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx7f8c16978c2ab126&secret=d157ee6a4b2c9d437dab4ebc37fd2fa8&code={code}&grant_type=authorization_code";
- var response = await HttpClientHelper.GetHeadersAsync(url, null);
- if (response.IsSuccessStatusCode)
- {
- var content = await response.Content.ReadAsStringAsync();
- var data = JsonConvert.DeserializeObject<WeChatOpenResult>(content);
- if (string.IsNullOrEmpty(data.errmsg))
- {
- return data.openid;
- }
- }
- return string.Empty;
- }
- }
- //如果好用,请收藏地址,帮忙分享。
- public class WeChatOpenResult
- {
- /// <summary>
- ///
- /// </summary>
- public string access_token { get; set; }
- /// <summary>
- ///
- /// </summary>
- public int expires_in { get; set; }
- /// <summary>
- ///
- /// </summary>
- public string refresh_token { get; set; }
- /// <summary>
- ///
- /// </summary>
- public string openid { get; set; }
- /// <summary>
- ///
- /// </summary>
- public string scope { get; set; }
- /// <summary>
- ///
- /// </summary>
- public string unionid { get; set; }
- /// <summary>
- ///
- /// </summary>
- public int errcode { get; set; }
- /// <summary>
- ///
- /// </summary>
- public string errmsg { get; set; }
- }
- }
|