1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 微信公众号获取用户openId(扩展:小程序获取openId和手机号)

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

时间:2020-09-07 03:24:22

相关推荐

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

微信公众号获取用户openId

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

https://open./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")@ResponseBodypublic String WeChatUserInfo(@RequestParam("code") String code,@RequestParam("encryptedData") String encryptedData,@RequestParam("iv") String iv) throws Exception {String result = HttpClientUtils.GET_REQUEST("https://api./sns/jscode2session?appid=公众号appid&secret=公众号秘钥&js_code="+code+"&grant_type=authorization_code");System.out.println(result);Result resultReturn = new Result();try {//请求成功sessionMap即可拿到openid和session-keyMap 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的感觉都很简单………………为什么早些年才入行的时候感觉那么蛋痛呢???

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。