{
try {
//先判断cookie中是否存在,不存在就执行下面的
if (code == null) {
String redirectUrl =URLEncoder.encode(redirect_url, "gb2312");
String getCodeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect"; // //
String url = getCodeUrl.replace("APPID", AppID).replace("SCOPE", SCOPE).replace("REDIRECT_URI", redirectUrl);
log.debug("getCodeUrl="+url);
return "redirect:" + url;
}else{
//获取token & openid
String getTokenUrl="https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
String url=getTokenUrl.replace("APPID",AppID).replace("SECRET",AppSecret).replace("CODE",code);//code为第一步请求所获取的
String jsonStr=HttpClientUtil.doGet(url);
JSONObject result = JSONObject.fromObject(jsonStr);
String openid=(String) result.get("openid");//获取到openid
String access_token=(String)result.get("access_token");
log.debug("openid="+openid);
if (StringUtils.isNotBlank(openid)){
//获取用户信息
String userinfo = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
userinfo = userinfo .replace("ACCESS_TOKEN", access_token).replace("OPENID", openid);
String a = HttpClientUtil.doGet(userinfo );
System.out.println(a);
//存入cookie
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "xxxxxxxx";
}