This commit is contained in:
parent
50b3600c10
commit
c6b591ede9
|
@ -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; }
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
/*
|
||||
{
|
||||
|
|
|
@ -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<WeChatMobileService> _logger;
|
||||
|
||||
|
||||
public string AppId { get; set; } = string.Empty;
|
||||
public string AppSecret { get; set; } = string.Empty;
|
||||
|
||||
private readonly HttpClient _httpClient=new();
|
||||
|
||||
public WeChatMobileService(ILogger<WeChatMobileService> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async UniTask<WeChatUserInfo> 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<JObject>(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<string>());
|
||||
}
|
||||
|
||||
if (!jObject.TryGetValue("openid", out var openid))
|
||||
throw new InvalidOperationException("获取OpenId失败")
|
||||
{
|
||||
Source = json
|
||||
};
|
||||
_logger.LogInformation($"获取OpenId成功:{openid.ToObject<string>()}");
|
||||
return jObject.ToObject<WeChatUserInfo>() ?? new WeChatUserInfo();
|
||||
}
|
||||
public async UniTask<WeChatAccessToken> 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<JObject>(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<string>());
|
||||
}
|
||||
|
||||
if (!jObject.TryGetValue("access_token", out var token))
|
||||
throw new InvalidOperationException("获取AccessToken失败")
|
||||
{
|
||||
Source = json
|
||||
};
|
||||
_logger.LogInformation($"获取AccessToken成功:{token.ToObject<string>()}");
|
||||
|
||||
return jObject.ToObject<WeChatAccessToken>() ?? new WeChatAccessToken();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue