前提:将微信小程序绑定到微信开放平台帐号 !!!
@RequestMapping("/wxGetUnionId")
public Result wx(@RequestBody Map<String,Object> map) {
Map<String, Object> remap = new HashMap<>();
//1、向微信服务器 使用登录凭证 code 获取 session_key 和 openid
String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + xcxAppID +
"&secret=" + xcxSecret +
"&js_code=" + map.get("code").toString() +
"&grant_type=authorization_code";
ResponseEntity<String> res = restTemplate.getForEntity(url, String.class);
JSONObject jsonObject = JSONObject.parseObject(res.getBody());
System.out.println("jsonObject============="+jsonObject);
remap.put("unionId",jsonObject.get("unionid").toString());
return ResultUtil.success(remap);
}