//调起微信授权
public RedirectResult GetOpenId(int adminId)
{
//_logger.Info($"获取GetOpenId:adminId=" + adminId);
var _ConfigCon = ApolloToConfigure.Get();
var _weixinCon = ApolloToConfigure.GetWChat();
string _url = "https://open.weixin.qq.com/connect/oauth2/authorize";
_url += "?appid=" + _weixinCon.AppId;
//指定回调接口url及参数
var redirect_url = $"{_ConfigCon.BackgroundGatewayAddress}/TZsbWebAdminApi/AdminWchat/WeiXinOAuth?adminId=" + adminId;
_url += "&redirect_uri=" + SystemString.StringEncode(redirect_url);
_url += "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
//scope=snsapi_base 静默授权&&&scope=snsapi_userinfo手动授权
RedirectResult redirectResult = new RedirectResult(_url, true);
return redirectResult;
}
//获取用户信息--通过用户openId获取用户详情
public async Task WeiXinOAuth(string code, int adminId)
{
var scriptRetry = string.Empty;
var _ConfigCon = ApolloToConfigure.Get();
var _weixinCon = ApolloToConfigure.GetWChat();
//微信配置
string _url = "https://api.weixin.qq.com/sns/oauth2/access_token";
_url += "?appid=" + _weixinCon.AppId;
_url += "&secret=" + _weixinCon.AppSecret;
_url += "&code=" + code;
_url += "&grant_type=authorization_code";
var results = WebServiceApi.GetService(_url);
var uModel = Newtonsoft.Json.JsonConvert.DeserializeObject<WeiXinOpenIdDto>(results);
if (uModel != null && !string.IsNullOrEmpty(uModel.openid) && adminId > 0)
{
#region 通过openid获取公众号微信用户信息,判断用户是否关注公众号
//获取微信token
var _access_token = Application.WeiXin.AdminMessage.BaseAdminSendMessage.Get_Access_token();
//通过微信openid和token获取用户详细信息
string url = "https://api.weixin.qq.com/cgi-bin/user/info";
url += "?access_token=" + _access_token;
url += "&openid=" + uModel.openid;
var userinfo = WebServiceApi.GetService(url);
var userinfoModel = Newtonsoft.Json.JsonConvert.DeserializeObject<WeiXinUserInfoDto>(userinfo);
if (userinfoModel != null && !string.IsNullOrEmpty(userinfoModel.openid))
{
#region 判断用户是否关注了公众号,已关注则直接更新管理员基本信息,反之则跳转关注二维码图片链接
//获取到微信openid,则根据adminid更新管理员基本信息
if (userinfoModel.subscribe)
{
scriptRetry = $"<script>alert('公众号关注成功请刷新查看!');window.location.href='{_ConfigCon.AdminMobileSiteUrl}'</script>";
await CreateResponseMessage(scriptRetry, HttpStatusCode.OK);
return;
}
else
{
scriptRetry = $"<div><img style='vertical-align:middle;' src='{_ConfigCon.AdminWeiXinImageUrl}' height='500px' width='500px'/><span style='display:inline-block; vertical-align:middle;'></span><p style='font-size:50px;'>请识别二维码并关注公众号</p></div>";
await CreateResponseMessage(scriptRetry, HttpStatusCode.OK);
return;
}
#endregion
}
else
{
scriptRetry = $"<script>alert('获取用户信息异常,请重新扫描!');</script>";
await CreateResponseMessage(scriptRetry, HttpStatusCode.OK);
return;
}
#endregion
}
else
{
scriptRetry = $"<script>alert('公众号关注失败请重新关注!');</script>";
await CreateResponseMessage(scriptRetry, HttpStatusCode.OK);
return;
}
}
/// <summary>
/// 返回消息
/// </summary>
/// <param name="script"></param>
/// <param name="code"></param>
/// <returns></returns>
private async Task CreateResponseMessage(string script, HttpStatusCode code)
{
var accept = Request.GetTypedHeaders().Accept;
var data = Encoding.UTF8.GetBytes(script);
if (accept.Any(x => x.MediaType == "text/html"))
{
Response.ContentType = "text/html;charset=utf-8";
}
else
{
Response.ContentType = "text/plain";
}
Response.StatusCode = (int)code;
await Response.Body.WriteAsync(data, 0, data.Length);
}