微信公众号获取用户openId(扩展:小程序获取openId和手机号)

微信公众号获取用户openId

拼接的参数【可以直接配菜单中】:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb2363dd5274190f4&redirect_uri=http://www.xxx.site/login&response_type=code&scope=snsapi_base&state=test#wechat_redirect

遇到的坑:

redirect_uri参数错误

注意要点:redirect_uri回调的地址不能是网站根目录……我特么气死,所以默认进入跳转login页面;

 

 

文章传送门:

官方文档说明

微信授权获取用户openId的方法和步骤

 

扩展:小程序获取openId和手机号

点击查看详情

示例代码:

/**
 * 微信小程序获取用户信息
 * isRegister:是否已注册官网账号
 */
@ApiOperation(value = "微信小程序获取用户信息",httpMethod = "POST", notes = "微信小程序获取用户信息")
@RequestMapping(value = "/WeChat/userInfo", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
@ResponseBody
public String WeChatUserInfo(@RequestParam("code") String code,
                             @RequestParam("encryptedData") String encryptedData,
                             @RequestParam("iv") String iv) throws Exception {
    String result = HttpClientUtils.GET_REQUEST("https://api.weixin.qq.com/sns/jscode2session?appid=公众号appid&secret=公众号秘钥&js_code="+code+"&grant_type=authorization_code");
    System.out.println(result);
    Result resultReturn = new Result();
    try {
        //请求成功sessionMap即可拿到openid和session-key
        Map sessionMap = JSON.parseObject(result, Map.class);
        String sessionKey = (String) sessionMap.get("session_key");
        //解密
        String mobileResult = AESDecodeUtils.decrypt(sessionKey,iv,encryptedData);
        Map mobileMap = JSON.parseObject(mobileResult, Map.class);
        //purePhoneNumber-没有区号的手机号
        String mobileNo = (String) mobileMap.get("purePhoneNumber");
        Map userInfo = userService.findUserByMobileNo(mobileNo);
        resultReturn.setSuccess(true);
        if (userInfo == null){
            userInfo = new HashMap();
            resultReturn.setMsg(mobileNo+"手机号未注册");
            userInfo.put("mobileNo",mobileNo);
            userInfo.put("isRegister",false);
        }else{
            userInfo.put("isRegister",true);
        }
        resultReturn.setObj(userInfo);
    }catch (Exception e){
        e.printStackTrace();
        logger.error("小程序获取个人信息失败!"+e.getMessage());
        resultReturn.setMsg("获取个人信息失败!");
    }
    return JSON.toJSONString(resultReturn);
}

OKK的感觉都很简单………………为什么早些年才入行的时候感觉那么蛋痛呢???

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

多来哈米

还可以打赏???来试一毛

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值