String code = request.getParameter(“code”);
WachatAjaxDomain openIdDomain = CommonController.getInstance().getOpenId(code);
if (openIdDomain==null || openIdDomain.getMap()==null || openIdDomain.getMap().get(“openid”)==null) {//获取openid失败,重新请求
String url = “https://open.weixin.qq.com/connect/oauth2/authorize?appid=…&redirect_uri=…&flag=1&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect“;
//记录日志
SystemLog systemLog = new SystemLog(1, “获取openid超时,重新跳转请求。”, null, null);
systemLogService.addSystemLog(systemLog);
return new ModelAndView(url);
}
String openid = (String) openIdDomain.getMap().get(“openid”);
String flag = request.getParameter(“flag”);
if (flag != null) {
//记录日志
SystemLog systemLog = new SystemLog(20, “获取openid超时,重新跳转请求。”, null, flag);
systemLogService.addSystemLog(systemLog);
}
/**获取openid的方法*/
WachatAjaxDomain ajaxDomain = new WachatAjaxDomain();
String url = “https://api.weixin.qq.com/sns/oauth2/access_token?appid=”+WachatCommon.WEIXIN_APPID+”&secret=”+WachatCommon.WEIXIN_SECRET+”&grant_type=authorization_code&code=”+code;
URL U = null;
try {
U = new URL(url);
URLConnection connection = U.openConnection();
connection.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
String resultStr = “”;
while ((line = in.readLine()) != null) {
resultStr += line;
}
in.close();
JSONObject jsonObject = JSONObject.fromObject(resultStr);
Object errcode = jsonObject.get(“errcode”);
if(errcode==null){
ajaxDomain.setStatusCode(“200”);
Map<String, Object> mapRe = new HashMap<String, Object>();
mapRe.put(“openid”, jsonObject.get(“openid”));
mapRe.put(“expires_in”, jsonObject.get(“expires_in”));
mapRe.put(“scope”, jsonObject.get(“scope”));
mapRe.put(“access_token”, jsonObject.get(“access_token”));
ajaxDomain.setMap(mapRe);
}else{
ajaxDomain.setStatusCode(“300”);
ajaxDomain.setMessage(jsonObject.getString(“errmsg”));
}
} catch (Exception e) {
e.printStackTrace();
ajaxDomain.setMessage(“获取access_token异常!”);
}
return ajaxDomain;
现在大致有两三千个用户,一天会产生几百条用户获取不到openid的数据库插入记录。求高手分析一下原因,谢谢了。
30
应用创建成功后资料还可以修改吗?能添加测试账号吗?