//生成二维码
@RequestMapping(value = "/qrCode")
public void getQrCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 第一步:用户同意授权,获取code
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wxAppID +
"&redirect_uri=" + wxUrl+"/yhzx/userLogin/callback" +
"&response_type=code" +
"&scope=snsapi_userinfo" +
"&state=STATE#wechat_redirect";
//生成二维码的,扫描后跳转上面的REDIRECT_URL地址
QRCodeUtil.createCodeToOutputStream(url, response.getOutputStream());
}
//回调
@RequestMapping(value = "/callback")
public Result pcCallback(String code, String state) throws Exception {
//第二步:通过code换取网页授权access_token
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + wxAppID +
"&secret=" + secret +
"&code=" + code +
"&grant_type=authorization_code";
ResponseEntity<String> res = restTemplate.getForEntity(url, String.class);
JSONObject jsonObject = JSONObject.parseObject(res.getBody());
System.out.println("jsonObject============="+jsonObject);
String openId = jsonObject.get("openid").toString();
String accessToken = jsonObject.get("access_token").toString();
//第三步:获取微信用户信息
String url1 = "https://api.weixin.qq.com/sns/userinfo?
access_token="+accessToken+"&openid="+openId+"&lang=zh_CN";
ResponseEntity<String> res1 = restTemplate.getForEntity(url1, String.class);
JSONObject jsonObject1 = JSONObject.parseObject(res1.getBody());
System.out.println("jsonObject1============="+jsonObject1);
return ResultUtil.success(jsonObject1);
}