• 微信公众号之获取用户信息 ^


    //调起微信授权
    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);
    }

  • 相关阅读:
    MSN、易趣、大旗被挂马 用户浏览后感染机器狗病毒 狼人:
    世界最大漏洞数据库发布最新研究报告 狼人:
    五角大楼最昂贵武器发展项目遭到黑客攻击 狼人:
    RSA呼吁厂商“创造性协作” 共同反击网络威胁 狼人:
    RSA2009:云计算服务如何保证安全? 狼人:
    黑客工具可将恶意软件隐藏于.Net框架 狼人:
    RSA安全大会将亮相25款热门安全产品 狼人:
    目录访问共享C#怎么访问共享目录
    代码下行Jquery结合Ajax和Web服务使用三层架构实现无刷新分页
    输出次数HDU2192:MagicBuilding
  • 原文地址:https://www.cnblogs.com/wang150601/p/16309629.html
Copyright © 2020-2023  润新知