From c6b591ede9bec9dd13d3026446387683b9143669 Mon Sep 17 00:00:00 2001 From: CortexCore <2630229280@qq.com> Date: Sat, 27 Jul 2024 14:55:25 +0800 Subject: [PATCH] 1 --- Models/WeChatAccessToken.cs | 25 ++++++++++ Models/WeChatUserInfo.cs | 53 ++++++++++++--------- Services/WeChatMobileService.cs | 81 +++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 21 deletions(-) create mode 100644 Models/WeChatAccessToken.cs create mode 100644 Services/WeChatMobileService.cs diff --git a/Models/WeChatAccessToken.cs b/Models/WeChatAccessToken.cs new file mode 100644 index 0000000..0c902ce --- /dev/null +++ b/Models/WeChatAccessToken.cs @@ -0,0 +1,25 @@ +using Newtonsoft.Json; + +namespace WeChatSharp +{ + public class WeChatAccessToken + { + [JsonProperty("access_token")] + public string AccessToken { get; set; } + + [JsonProperty("expires_in")] + public int ExpiresIn { get; set; } + + [JsonProperty("refresh_token")] + public string RefreshToken { get; set; } + + [JsonProperty("openid")] + public string OpenId { get; set; } + + [JsonProperty("scope")] + public string Scope { get; set; } + + [JsonProperty("unionid")] + public string UnionId { get; set; } + } +} \ No newline at end of file diff --git a/Models/WeChatUserInfo.cs b/Models/WeChatUserInfo.cs index 479a0b7..dac3398 100644 --- a/Models/WeChatUserInfo.cs +++ b/Models/WeChatUserInfo.cs @@ -1,42 +1,53 @@ using Newtonsoft.Json; namespace WeChatSharp; + [Serializable] public record WeChatUserInfo { [JsonProperty(propertyName: "subscribe")] - public int Subscribe { get; internal set; } - [JsonProperty(propertyName: "openid")] - public string OpenId { get; internal set; } - [JsonProperty(propertyName: "nickname")] - public string NickName{ get; internal set; } + public int Subscribe; + + [JsonProperty(propertyName: "openid")] public string OpenId; + + [JsonProperty(propertyName: "nickname")] + public string NickName; + + [JsonProperty(propertyName: "sex")] public int Sex; - [JsonProperty(propertyName: "sex")] - public int Sex { get; internal set; } [JsonProperty(propertyName: "language")] - public string Language{ get; internal set; } - [JsonProperty(propertyName: "city")] - public string City{ get; internal set; } + public string Language; + + [JsonProperty(propertyName: "city")] public string City; + [JsonProperty(propertyName: "province")] - public string Province{ get; internal set; } + public string Province; + [JsonProperty(propertyName: "country")] - public string Country{ get; internal set; } + public string Country; + [JsonProperty(propertyName: "headimgurl")] - public string HeadImgUrl{ get; internal set; } + public string HeadImgUrl; + [JsonProperty(propertyName: "subscribe_time")] - public int SubscribeTime{ get; internal set; } - [JsonProperty(propertyName: "remark")] - public string Remark{ get; internal set; } + public int SubscribeTime; + + [JsonProperty(propertyName: "remark")] public string Remark; + [JsonProperty(propertyName: "groupid")] - public int GroupId{ get; internal set; } + public int GroupId; + [JsonProperty(propertyName: "tagid_list")] - public string[] TagIdList{ get; internal set; } + public string[] TagIdList; + [JsonProperty(propertyName: "subscribe_scene")] - public string SubscribeScene{ get; internal set; } + public string SubscribeScene; + [JsonProperty(propertyName: "qr_scene")] - public int QrScene{ get; internal set; } + public int QrScene; + [JsonProperty(propertyName: "qr_scene_str")] - public string QrSceneStr{ get; internal set; } + public string QrSceneStr; } /* { diff --git a/Services/WeChatMobileService.cs b/Services/WeChatMobileService.cs new file mode 100644 index 0000000..8eb9dad --- /dev/null +++ b/Services/WeChatMobileService.cs @@ -0,0 +1,81 @@ +using Cysharp.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace WeChatSharp.Services; + +public class WeChatMobileService +{ + private readonly ILogger _logger; + + + public string AppId { get; set; } = string.Empty; + public string AppSecret { get; set; } = string.Empty; + + private readonly HttpClient _httpClient=new(); + + public WeChatMobileService(ILogger logger) + { + _logger = logger; + } + + public async UniTask GetUserInfoAsync(string code,string openId) + { + var uri = new UriBuilder("https://api.weixin.qq.com/sns/userinfo") + { + Query = $"access_token={code}&openid={code}" + }; + + _logger.LogInformation($"请求地址:{uri.Uri}"); + + var response = await _httpClient.GetAsync(uri.Uri); + + var json = await response.Content.ReadAsStringAsync(); + var jObject = JsonConvert.DeserializeObject(json); + if (jObject is null) + { + throw new NullReferenceException("json为空值"); + } + + if (jObject.TryGetValue("errcode", out var errcode) && jObject.TryGetValue("errmsg", out var errmsg)) + { + throw new Exception(errmsg.ToObject()); + } + + if (!jObject.TryGetValue("openid", out var openid)) + throw new InvalidOperationException("获取OpenId失败") + { + Source = json + }; + _logger.LogInformation($"获取OpenId成功:{openid.ToObject()}"); + return jObject.ToObject() ?? new WeChatUserInfo(); + } + public async UniTask GetAccessCodeAsync(string code) + { + var uri = new UriBuilder("https://api.weixin.qq.com/sns/oauth2/access_token") + { + Query = $"appid={AppId}&secret={AppSecret}&code={code}&grant_type=authorization_code" + }; + var response = await _httpClient.GetAsync(uri.Uri); + var json = await response.Content.ReadAsStringAsync(); + var jObject = JsonConvert.DeserializeObject(json); + if (jObject is null) + { + throw new NullReferenceException("json为空值"); + } + if (jObject.TryGetValue("errcode", out var errcode) && jObject.TryGetValue("errmsg", out var errmsg)) + { + throw new Exception(errmsg.ToObject()); + } + + if (!jObject.TryGetValue("access_token", out var token)) + throw new InvalidOperationException("获取AccessToken失败") + { + Source = json + }; + _logger.LogInformation($"获取AccessToken成功:{token.ToObject()}"); + + return jObject.ToObject() ?? new WeChatAccessToken(); + } +} \ No newline at end of file