微信第三方登录

需要记录用户操作记录,保存用户openid

$(function() {
var access_code = '';
var uri = encodeURIComponent("当前页面,需要授权");
var access_code = getQueryString('code');
if (access_code == null) {
var fromurl = location.href; //获取授权code的回调地址,获取到code,直接返回到当前页
var url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=OPENID&redirect_uri=' + uri + '&response_type=code&scope=snsapi_base&state=123#wechat_redirect';
location.href = url;
} else {
$.ajax({  //获取用户唯一标识openid
url : 'getWxOpenid',
type : 'post',
data : {
code : access_code
},
dataType : 'text',
success : function(data) {
if (data == "" || data == null) {
alert("登录失败,请稍后再试或者选择其他登录方式");
} else {
var uri = encodeURIComponent("用户登录成功跳转页面,需要授权");
var url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=OPENID&' +
'redirect_uri=' + uri + '&response_type=code&scope=snsapi_userinfo&state=' + data + '#wechat_redirect';
location.href = url;
}
}
});
}
});

 

//用户只是登录浏览

var uri = encodeURIComponent("用户登录成功跳转页面,需要授权");
var url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=OPENID&' +
'redirect_uri=' + uri + '&response_type=code&scope=snsapi_userinfo&state=' + data + '#wechat_redirect';
location.href = url;

 

 

 

 

后台代码

 

@RequestMapping(value = "getWxOpenid", method = RequestMethod.POST)
@ResponseBody
public String getOpenid(@RequestParam(value = "code", required = false) String code, HttpServletRequest request,
ModelMap model) throws IOException {
GetWxInformation openId=new GetWxInformation();
System.out.println("code:" + code);
Properties p = new Properties();
InputStream input = GetWxInformation.class.getResourceAsStream("/payConfig.properties");
p.load(input);
String appid = p.get("APP_ID").toString();
String APPSECRET = p.get("APPSECRET").toString();
String openid = openId.openId(code,appid,APPSECRET);
System.out.println("openid1:"+openid);
String accsstoken = openId.accessToken(appid,APPSECRET);
System.out.println("accsstoken:"+accsstoken);
String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+accsstoken+"&openid="+openid;
GetMethod get = null;
get = new GetMethod(url);

HttpClient client = new HttpClient();
client.getParams().setParameter("http.protocol.content-charset", "utf-8");
client.getParams().setBooleanParameter("http.protocol.expect-continue", false);
client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
client.getHttpConnectionManager().getParams().setSoTimeout(30000);

get.addRequestHeader("Content-type", "text/html;charset=UTF-8");

int statusCode = 0;
try {
statusCode = client.executeMethod(get);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

if (statusCode == 200) {
InputStream resInputStream = null;
try {
resInputStream = get.getResponseBodyAsStream();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(resInputStream, "utf-8"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println("line:" + line);
JSONObject jsonObject = new JSONObject();
try {
jsonObject = JSONObject.fromObject(line);
String subscribe = jsonObject.getString("subscribe");
openid = jsonObject.getString("openid");
System.out.println("openid2"+openid);
String nickname = jsonObject.getString("nickname");
String sex = jsonObject.getString("sex");
String language = jsonObject.getString("language");
String city = jsonObject.getString("city");
String province = jsonObject.getString("province");
String country = jsonObject.getString("country");
String headimgurl = jsonObject.getString("headimgurl");
String subscribe_time = jsonObject.getString("subscribe_time");
System.out.println(subscribe+","+openid+","+nickname+","+sex+","+
language+","+city+","+province+","+country+","+
headimgurl+","+subscribe_time);
} catch (Exception e) {
jsonObject = JSONObject.fromObject(line);
String errcode = jsonObject.getString("errcode");
System.out.println("errcode:" + errcode);
}
}
}

return openid;
}


/**
* 获取用户accssToken
* @author Tianhao
* @created 2017年7月27日 下午4:55:23
* @param code
* @return
* @throws IOException
*/
public String accessToken(String appid,String APPSECRET) throws IOException {
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+APPSECRET;
GetMethod get = null;
get = new GetMethod(url);

HttpClient client = new HttpClient();
client.getParams().setParameter("http.protocol.content-charset", "utf-8");
client.getParams().setBooleanParameter("http.protocol.expect-continue", false);
client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
client.getHttpConnectionManager().getParams().setSoTimeout(30000);

get.addRequestHeader("Content-type", "text/plain;charset=UTF-8");

int statusCode = 0;
String access_token = "";
try {
statusCode = client.executeMethod(get);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

if (statusCode == 200) {
InputStream resInputStream = null;
try {
resInputStream = get.getResponseBodyAsStream();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(resInputStream, "utf-8"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println("line:" + line);
JSONObject jsonObject = new JSONObject();
try {
jsonObject = JSONObject.fromObject(line);
access_token = jsonObject.getString("access_token");
} catch (Exception e) {
jsonObject = JSONObject.fromObject(line);
String errcode = jsonObject.getString("errcode");
System.out.println("errcode:" + errcode);
}
}
}
return access_token;

}


/**
* 获取用户openid和access_token
* @author Tianhao
* @created 2017年7月27日 下午5:04:01
* @param code
* @return
* @throws IOException
*/
public String openId(String code,String appid,String APPSECRET) throws IOException {
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret="
+ APPSECRET + "&code=" + code + "&grant_type=authorization_code";
GetMethod get = null;
get = new GetMethod(url);

HttpClient client = new HttpClient();
client.getParams().setParameter("http.protocol.content-charset", "utf-8");
client.getParams().setBooleanParameter("http.protocol.expect-continue", false);
client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
client.getHttpConnectionManager().getParams().setSoTimeout(30000);

get.addRequestHeader("Content-type", "text/plain;charset=UTF-8");

int statusCode = 0;
try {
statusCode = client.executeMethod(get);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
String openid ="";
if (statusCode == 200) {
InputStream resInputStream = null;
try {
resInputStream = get.getResponseBodyAsStream();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(resInputStream, "utf-8"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println("line:" + line);
JSONObject jsonObject = new JSONObject();
try {
jsonObject = JSONObject.fromObject(line);
openid = jsonObject.getString("openid");
} catch (Exception e) {
jsonObject = JSONObject.fromObject(line);
String errcode = jsonObject.getString("errcode");
System.out.println("errcode:" + errcode);
}
}
}
return openid;

}

posted @ 2017-07-28 11:38  田浩  Views(255)  Comments(0)    收藏  举报