微信公众号获取用户openId
拼接的参数【可以直接配菜单中】:
遇到的坑:
redirect_uri参数错误
注意要点:redirect_uri回调的地址不能是网站根目录……我特么气死,所以默认进入跳转login页面;
文章传送门:
扩展:小程序获取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的感觉都很简单………………为什么早些年才入行的时候感觉那么蛋痛呢???