|
|
@ -14,7 +14,7 @@ namespace GDZZ.Core;
|
|
|
|
public class WechatOAuth : IWechatOAuth, ISingleton
|
|
|
|
public class WechatOAuth : IWechatOAuth, ISingleton
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private readonly string _authorizeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize";
|
|
|
|
private readonly string _authorizeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize";
|
|
|
|
private readonly string _accessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";
|
|
|
|
private readonly string _accessTokenUrl = "https://api.weixin.qq.com/sns/jscode2session";
|
|
|
|
private readonly string _refreshTokenUrl = "https://api.weixin.qq.com/sns/oauth2/refresh_token";
|
|
|
|
private readonly string _refreshTokenUrl = "https://api.weixin.qq.com/sns/oauth2/refresh_token";
|
|
|
|
private readonly string _userInfoUrl = "https://api.weixin.qq.com/sns/userinfo";
|
|
|
|
private readonly string _userInfoUrl = "https://api.weixin.qq.com/sns/userinfo";
|
|
|
|
|
|
|
|
|
|
|
@ -46,23 +46,26 @@ public class WechatOAuth : IWechatOAuth, ISingleton
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// 获取微信Token
|
|
|
|
/// 换取JSCodeSessionKey
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="code"></param>
|
|
|
|
/// <param name="code"></param>
|
|
|
|
/// <param name="state"></param>
|
|
|
|
/// <param name="state"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <returns></returns>
|
|
|
|
public async Task<TokenModel> GetAccessTokenAsync(string code, string state = "")
|
|
|
|
public async Task<TokenModel> GetAccessTokenAsync(string code, string state = "")
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var param = new Dictionary<string, string>()
|
|
|
|
var param = new Dictionary<string, string>()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
["appid"] = _oauthConfig.app_id,
|
|
|
|
["appid"] = _oauthConfig.app_id,
|
|
|
|
["secret"] = _oauthConfig.app_key,
|
|
|
|
["secret"] = _oauthConfig.app_key,
|
|
|
|
["code"] = code,
|
|
|
|
["js_code"] = code,
|
|
|
|
["grant_type"] = "authorization_code"
|
|
|
|
["grant_type"] = "authorization_code"
|
|
|
|
};
|
|
|
|
};
|
|
|
|
var accessTokenModel = await $"{_accessTokenUrl}?{param.ToQueryString()}".GetAsAsync<TokenModel>();
|
|
|
|
var accessTokenModel = await $"{_accessTokenUrl}?{param.ToQueryString()}".GetAsAsync<TokenModel>();
|
|
|
|
if (accessTokenModel.HasError())
|
|
|
|
if (accessTokenModel.HasError())
|
|
|
|
throw Oops.Oh($"{ accessTokenModel.ErrorDescription}");
|
|
|
|
throw Oops.Oh($"{ accessTokenModel.Errmsg}");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//获取用户信息
|
|
|
|
|
|
|
|
|
|
|
|
return accessTokenModel;
|
|
|
|
return accessTokenModel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -101,7 +104,7 @@ public class WechatOAuth : IWechatOAuth, ISingleton
|
|
|
|
};
|
|
|
|
};
|
|
|
|
var refreshTokenModel = await $"{_refreshTokenUrl}?{param.ToQueryString()}".GetAsAsync<TokenModel>();
|
|
|
|
var refreshTokenModel = await $"{_refreshTokenUrl}?{param.ToQueryString()}".GetAsAsync<TokenModel>();
|
|
|
|
if (refreshTokenModel.HasError())
|
|
|
|
if (refreshTokenModel.HasError())
|
|
|
|
throw Oops.Oh($"{ refreshTokenModel.ErrorDescription}");
|
|
|
|
throw Oops.Oh($"{ refreshTokenModel.Errmsg}");
|
|
|
|
return refreshTokenModel;
|
|
|
|
return refreshTokenModel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|