发表于: 2018-01-26 23:04:38
1 506
今天完成的事情:
完成了服务配置。运行起来 开始编写接口:
学习公司的东西:
编写微信接口:
public static JSONObject getOpenInfo(String url)throws Exception{
StringBuffer buffer = new StringBuffer();
// String url ="https://api.weixin.qq.com/sns/oauth2/access_token?" +
// "appid="+appid+"&secret="+appsecret+"&code="+code+"&grant_type=authorization_code";
URL urlConnection = new URL(url);
HttpURLConnection httpUrlConn =(HttpURLConnection)urlConnection.openConnection();
httpUrlConn.setRequestMethod("GET");
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
//关闭资源
bufferedReader.close();
inputStreamReader.close();
//释放资源
inputStream.close();
httpUrlConn.disconnect();
return JSONObject.fromObject(buffer.toString());
}
public @ResponseBody Long getId(HttpServletRequest request){
String code = request.getParameter("code");
String url ="https://api.weixin.qq.com/sns/oauth2/access_token?" +
"appid="+appid+"&secret="+appsecret+"&code="+code+"&grant_type=authorization_code";
log.info("code---->"+code);
try{
System.out.println("**************");
JSONObject jsonObject = WechatUtil.getOpenInfo(url);
String access_token =(String)jsonObject.get("access_token");
String openid =(String)jsonObject.get("openid");
log.info("access_token---->"+access_token);
log.info("openid---->"+openid);
Long id = usersService.getUseridByOpenid(openid);
if(id !=null){
Users users = usersService.getObjectById(id);
if(users.getStatus()==1){
log.info("user is freezed");
return 0L;
}
session.setAttribute("id",id);
return id;
}else {
url = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openid+"&lang=zh_CN";
log.info("*******第二行"+url+"************");
jsonObject = WechatUtil.getOpenInfo(url);
Users users = new Users();
users.setOpenid(openid);
users.setName((String)jsonObject.get("nickname"));
users.setImage((String)jsonObject.get("headimgurl"));
users.setAddress((String)jsonObject.get("province")+
(String)jsonObject.get("city")+(String)jsonObject.get("country"));
Long id1 = usersService.insert(users);
session.setAttribute("id",id1);
return id1;
}
}catch (Exception e){
log.info("getUserid error");
e.printStackTrace();
}
return 0L;
}
明天计划的事情:
明日请假
遇到的问题:
暂无
收获:
评论