微信开发之用户信息20170922

来源:互联网 发布:最优化理论 无线通信 编辑:程序博客网 时间:2024/04/29 05:04


public static JSONObject service(JSONObject params, ActionContext context) throws JsonGenerationException, JsonMappingException, IOException, WxErrorException{
/*====================写一个类来封装这段代码,begin======================*/
JSONObject map=null;
String userCode = params.getString("code");
System.out.println("code是:"+userCode);
//获取token
String id = "wxbb7c699a7f1ac509"; //要有封装类,你们框架中应该有
String  Scret = "06kHjVoG9bKzMaTW0yfKQct5ttw_kmnslTfBydHTyD8";//要有封装类,你们框架中应该有
String token1=null;
String  url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+id+"&corpsecret="+Scret;
try{
URL getUrl =new URL(url);
HttpURLConnection http=(HttpURLConnection)getUrl.openConnection();
http.setRequestMethod("GET"); 
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
http.connect();
java.io.InputStream is = http.getInputStream(); 
int size = is.available(); 
byte[] b = new byte[size];
is.read(b);
String message = new String(b, "UTF-8");
System.out.println("message"+message);
JSONObject json = JSONObject.parseObject(message);
System.out.println("json:"+json.toString());
//access_Token token = null;
// return json;
// token.获取到的token
Integer error = json.getInteger("errcode");
//1、能获取到access_token,接口无返回errcode
//2、能获取到access_token,接口返回errcode为0, errmsg为空或者"ok"
if(error==null||error==0){
//这里是获取token  没有userid   下边才有
token1= json.getString("access_token");


System.out.println("第一个token1的值是::::::"+token1);
}else{
System.out.println("获取token失败1"+json.getString("errmsg"));
return null;
}


}catch(MalformedURLException e){
System.out.println("获取token失败2");
e.printStackTrace();


}
if(StringUtils.isBlank(token1)){
System.out.println("3token1为空");
return null;
}
/*====================写一个类来封装这段代码,end======================*/


/*====================写一个类来封装这段代码,begin======================*/
try {
//获取用户信息、现在这边后台处理 code he TOKEN。
url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token="+token1+"&code="+userCode;
URL getUrl =new URL(url);
HttpURLConnection http=(HttpURLConnection)getUrl.openConnection();
http.setRequestMethod("GET"); 
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
//
http.connect();
java.io.InputStream is = http.getInputStream(); 
int size = is.available(); 
byte[] b = new byte[size];
is.read(b);
String message = new String(b, "UTF-8");
JSONObject json = JSONObject.parseObject(message);
System.out.println(json.toString());
//{"DeviceId":"10000505959491CO","UserId":"sunlidong","errcode":0,"errmsg":"ok"}


String UserId = json.getString("UserId");
// JSONObject map=null;
map = new JSONObject();
map.put("openid",UserId);
return  map;
} catch (Exception e) {
System.out.println("获取用户信息失败");
e.printStackTrace();
return null;
}
 




/*====================写一个类来封装这段代码,end======================*/


//调用刚刚封装的获取token的方法,接收token参数


//判断token是否为空,为空 提示错误信息,不为空调用刚刚封装的获取userid的方法,接收JSONObject map参数


}
原创粉丝点击