67,550
社区成员




@RequestMapping("/weChatGetCode")
public String weChatGetCode() throws UnsupportedEncodingException {
String redirect_uri=URLEncoder.encode("http://*********/weChatUserFollow", "UTF-8");
StringBuffer url=new StringBuffer("https://open.weixin.qq.com/connect/oauth2/authorize?redirect_uri="+redirect_uri+
"&appid="+resource.getString("wx.appid")+"&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
return "redirect:"+url;
}
@RequestMapping("/weChatUserFollow")
public @ResponseBody String weChatUserFollow(HttpServletRequest request){
HttpSession session = request.getSession();
String code = request.getParameter("code");//获取code
Map params = new HashMap();
params.put("secret", resource.getString("wx.secret"));
params.put("appid", resource.getString("wx.appid"));
params.put("grant_type", "authorization_code");
params.put("code", code);
String result = HttpGetUtil.httpRequestToString(
"https://api.weixin.qq.com/sns/oauth2/access_token", params);
JSONObject jsonObject = JSONObject.parseObject(result);
String openid = jsonObject.get("openid").toString();
session.setAttribute("openid",openid);
String access_token=jsonObject.get("access_token").toString();
Map params2 = new HashMap();
params2.put("access_token",access_token);
params2.put("openid",openid);
params2.put("lang","zh_CN");
String rs2 = HttpGetUtil.httpRequestToString("https://api.weixin.qq.com/cgi-bin/user/info", params2);
JSONObject jsonObject2 = JSONObject.parseObject(rs2);
if(jsonObject2.get("subscribe").toString().equals("1")){
return JSON.toJSONString("true");
}else{
return JSON.toJSONString("false");
}
}