• 微信企业号 获取用户信息


    业务操作最基础的一个功能是获取访客的身份,传统的获取方式是提供一个登录页面用以访客登录。

    在微信企业号中,用户在微信中访问页面时,可以根据相关API获取此用户的微信账号信息,以此来匹配业务服务器存储的相关用户信息。

    目录

    1.  介绍

    2.  代码示例

    1. 介绍

    1.1 说明

    企业号的网页开发,说白了就是移动端web开发,特殊点在于如何获取微信用户的身份信息。

    在企业号中可以进行如下步骤获取微信用户信息:

    访问一个业务页面时,可通过OAuth验证接口获取此用户信息 → 根据code获取userId → 根据userId获取微信信息。

    1.2 详细流程

    ① 获取code

    APIhttp://qydev.weixin.qq.com/wiki/index.php?title=OAuth%E9%AA%8C%E8%AF%81%E6%8E%A5%E5%8F%A3#.E4.BC.81.E4.B8.9A.E8.8E.B7.E5.8F.96code

    说明:网页经过OAuth2.0验证后,重定向到原来网页并在url后面添加code信息。

    :http://akmsg.com/a.html => OAhth2.0 => http://akmsg.com/a.html?code=CODE&state=STATE

    ② 根据code获取userId

    APIhttp://qydev.weixin.qq.com/wiki/index.php?title=OAuth%E9%AA%8C%E8%AF%81%E6%8E%A5%E5%8F%A3#.E6.A0.B9.E6.8D.AEcode.E8.8E.B7.E5.8F.96.E6.88.90.E5.91.98.E4.BF.A1.E6.81.AF

    说明:调用此接口后将会获得 userId;注:userId为加密后的微信账号。

    ③ 根据userId获取微信信息

    APIhttp://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98#.E8.8E.B7.E5.8F.96.E6.88.90.E5.91.98

    说明:调用此接口后将会获得此访问者在企业号登记的具体信息;如:姓名、微信号、手机号、邮箱、职位等等。

    ④ 根据微信信息获取逻辑用户信息

    说明:从上一步骤获取的微信信息,可以用来跟业务逻辑进行匹配获取此用户在业务层中的用户信息。

    1.3 流程图

    2. 代码示例

    2.1 代码(C#)

    逻辑:Asp.net对客户端发送的请求进行判断,符合微信企业号页面规则的将进行微信企业号用户身份认证操作。

    此功能对访问请求的三种情况进行分别判断:

    1.第一次访问,没code :进行OAuth验证

    2.有code,没cookie :获取code对应的信息

    3.有code,有cookie :验证cookie

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    /// <summary>
    /// 验证微信访问
    /// </summary>
    public static void Auth(HttpContext webContext)
    {
     
        string requestURL = webContext.Request.Url.AbsoluteUri;
     
        try
        {
            // 用户访问微信页面有3种情况:
            // 1.第一次访问,没code
            // 2.有code,没cookie;
            // 3.有code,有cookie
     
            // 1.第一次访问,没code,没cookie:跳转到Oauth2.0认证
            if (string.IsNullOrEmpty(webContext.Request["code"]))
            {
                string url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect", CORPID, webContext.Server.UrlEncode(requestURL));
                webContext.Response.Redirect(url, false);
            }
            else if (!string.IsNullOrEmpty(webContext.Request["code"]) && string.IsNullOrEmpty(CookieHelper.GetCookie("WXToken")))
            {
                // 2.有code,没cookie:根据code获取userID
                string code = webContext.Request["code"];
                string userId = "";
                string userInfo = "";
     
                #region 1)根据code获取userId
     
                string url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={0}&code={1}", GetAccessToken(), code);
                string responseText = HttpHelper.Instance.get(url);
                /*
                    API:http://qydev.weixin.qq.com/wiki/index.php?title=OAuth%E9%AA%8C%E8%AF%81%E6%8E%A5%E5%8F%A3#.E6.A0.B9.E6.8D.AEcode.E8.8E.B7.E5.8F.96.E6.88.90.E5.91.98.E4.BF.A1.E6.81.AF
                    正确的Json返回示例:
                    {
                       "UserId":"USERID",
                       "DeviceId":"DEVICEID"
                    }
                    未关注企业号时返回:
                    {
                       "OpenId":"OPENID",
                       "DeviceId":"DEVICEID"
                    }
                    错误的Json返回示例:
                    {
                       "errcode": "40029",
                       "errmsg": "invalid code"
                    }
                */
                WeChatUserCodeEntity codeEn = JsonHelper.GetEntity<WeChatUserCodeEntity>(responseText);
                if (codeEn.errcode > 0)
                {
                    throw new Exception(codeEn.errmsg);
                }
                else if (string.IsNullOrEmpty(codeEn.UserId))
                {
                    throw new Exception("请先关注企业号!");
                }
                userId = codeEn.UserId;
     
     
                #endregion
     
                #region 2)根据userId获取用户信息
     
                url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={0}&userid={1}", GetAccessToken(), userId);
                responseText = HttpHelper.Instance.get(url);
                /*
                    API:http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98#.E8.8E.B7.E5.8F.96.E6.88.90.E5.91.98
                    正确的Json返回示例:
                    {
                       "errcode": 0,
                       "errmsg": "ok",
                       "userid": "zhangsan",
                       "name": "李四",
                       "department": [1, 2],
                       "position": "后台工程师",
                       "mobile": "15913215421",
                       "gender": "1",
                       "email": "zhangsan@gzdev.com",
                       "weixinid": "lisifordev", 
                       "avatar": "http://wx.qlogo.cn/mmopen/ajNVdqHZLLA3WJ6DSZUfiakYe37PKnQhBIeOQBO4czqrnZDS79FH5Wm5m4X69TBicnHFlhiafvDwklOpZeXYQQ2icg/0",
                       "status": 1,
                       "extattr": {"attrs":[{"name":"爱好","value":"旅游"},{"name":"卡号","value":"1234567234"}]}
                    }
                    错误的Json返回示例:
                    {
                       "errcode": "40029",
                       "errmsg": "invalid code"
                    }
                */
                WeChatUserInfoEntity userInfoEn = JsonHelper.GetEntity<WeChatUserInfoEntity>(responseText);
                if (userInfoEn.errcode > 0)
                {
                    throw new Exception(userInfoEn.errmsg);
                }
                userInfo = responseText;
     
                #endregion
     
                // 3.把userInfo传入到cookie里
                CookieHelper.SetCookie("WXToken", userInfo, -1);
            }
            else if (!string.IsNullOrEmpty(webContext.Request["code"]) && !string.IsNullOrEmpty(CookieHelper.GetCookie("WXToken")))
            {
                #region 3.有code,有cookie:校验cookie
                // TODO:在上面进行存入cookie时可采用AES加密,在这部进行解密校验
                // CookieHelper.SetCookie("WXToken", "", -1);
                #endregion
            }
            else
            {
                throw new Exception("非授权访问!");
            }
     
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

      

    2.2 运行图

    1) 用户已关注访问时

    2) 用户不属于企业通讯录访问时

      

    2.3 Dmeo下载(C#)

    下载地址http://files.cnblogs.com/files/polk6/Wechat.QYH.zip

    ==================================系列文章==========================================

    本篇文章:1.3 微信企业号 获取用户信息

    微信开发文章导航

     
  • 相关阅读:
    LeetCode#1047-Remove All Adjacent Duplicates In String-删除字符串中的所有相邻重复项
    LeetCode#345-Reverse Vowels of a String-反转字符串中的元音字母
    LeetCode#344-Reverse String-反转字符串
    LeetCode#232-Implement Queue using Stacks-用栈实现队列
    LeetCode#225-Implement Stack using Queues-用队列实现栈
    LeetCode#20-Valid Parentheses-有效的括号
    树的遍历
    [leetcode] 树(Ⅰ)
    二叉树图形化显示
    你错在成长于文明的边陲
  • 原文地址:https://www.cnblogs.com/amylis_chen/p/7484600.html
Copyright © 2020-2023  润新知