/**
* 第三方回调
*
* @return
* @throws Exception
*/
@RequestMapping("/authRequst")
public void authRequst(HttpServletRequest request, HttpServletResponse response) throws Exception {
StringBuilder param = new StringBuilder();
//项目的根路径
String basePath =request.getContextPath();
String url = "http://" + request.getServerName()+basePath + Constant.URL_OAUTH2_CALLBACK;
String service = Constant.USER_WECHAT_OPEN_CALLBACK_URL;
/*
* 获取微信配置
*/
WeChatConfig weChatConfig = weChatConfigService.getWeChatConfig();
param.append(Constant.GET_CODE_URL);
param.append("?appid=");
param.append(weChatConfig.getOpenAppId());
param.append("&redirect_uri=");
param.append(HttpUtils.urlEncode(url + "/callback?service=" + service));
param.append("&response_type=code");
param.append("&scope=snsapi_login");
param.append("#wechat_redirect");
response.sendRedirect(param.toString());
}
// 微信第三方登录回调地址
public static final String USER_WECHAT_OPEN_CALLBACK_URL = "/api/open/user/wxQRLoginCallback";
// 微信第三方使用网站应用授权登录
public static final String GET_CODE_URL = "https://open.weixin.qq.com/connect/qrconnect";
// 微信获取用户基本信息
public static final String GET_USER_INFO = "https://api.weixin.qq.com/sns/userinfo";
/**
* 服务器认证
*
* @return
* @throws Exception
*/
@RequestMapping("/callback")
public void callback(HttpServletRequest request, HttpServletResponse response) throws Exception {
if (SiteContext.getCurrentSite() != null) {
/*
* 获取微信配置
*/
WeChatConfig weChatConfig = weChatConfigService.getWeChatConfig();
String code = request.getParameter("code");
StringBuilder param = new StringBuilder();
String uid = null, result = null, nickname = null, access_token = null, photo = null, url = null, sex = null;
String unionid = null;
String redirectUrl = null;
redirectUrl = request.getParameter("service");
url = Constant.GET_OPENID_URL;
NameValuePair[] nvp = new NameValuePair[]{new NameValuePair("grant_type", "authorization_code"), new NameValuePair("appid", weChatConfig.getOpenAppId()), new NameValuePair("secret", weChatConfig.getOpenSecret()), new NameValuePair("code", code)};
result = HttpUtils.executeGet(url, nvp);
System.out.println(result);
JSONObject json = JSONObject.fromObject(result);
if (json.get("errcode") != null) {
System.out.println("获取accessToken失败");
response.sendRedirect("/error");
}
uid = (String) json.get("openid");
unionid = (String) json.get("unionid");
/*
* 获取openId,放在session之后,用户登录验证的时候就会获取
*/
SessionContext.set(Constant.SESSION_KEY_OPENID, uid);
SessionContext.set(Constant.SESSION_KEY_UNIONID, unionid);
SessionContext.set(Constant.KEY_SESSION_GETUSER_URL, Constant.GET_USER_INFO);
access_token = (String) json.get("access_token");
Cache cache = cacheService.getCache(Constant.KEY_CACHE_WECHAT_TOKEN);
cache.put(weChatConfig.getAppId(), access_token);
WeChatUserInfo userInfo = weChatService.getUserInfo();
try {
nickname = userInfo.getNickname();
photo = userInfo.getHeadimgurl();
sex = userInfo.getSex();
}catch (Exception e){
System.out.println("json===="+json);
e.printStackTrace();
throw e;
}
if (StringUtils.isNotBlank(redirectUrl)) {
redirectUrl += "?nickName=" + HttpUtils.urlEncode(HttpUtils.urlEncode(nickname));
redirectUrl += "&photo=" + photo;
redirectUrl += "&gender=" + sex; // 1为男性,2为女性
System.out.println("redirectUrl====" + redirectUrl);
response.sendRedirect(HttpUtils.urlDecode(redirectUrl));
}else{
response.sendRedirect("/error");
}
} else {
response.sendRedirect("/error");
}
}