Commit 4daac078 authored by hezhen's avatar hezhen

123

parent ad63ee67
......@@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
......@@ -45,10 +44,16 @@ public class WeixinController {
if (StringUtils.isBlank(redirec_url)){
redirec_url="";
}
String encrypt_curr_url = Base64.encode(redirec_url);
try {
String encrypt_curr_url = Base64.encode(redirec_url.getBytes("utf-8"));
redirec_url=url+"?" + WECHAT_AUTOLOGIN_CALLBACKURL_KEY+ "=" + encrypt_curr_url;
String oauth_api=weixinService.getAuthorize(redirec_url);
return String.format("redirect:"+oauth_api);
}catch (Exception e){
e.printStackTrace();
log.info("网络异常===" + e.getMessage());
return String.format("网络异常");
}
}
/**
......@@ -64,30 +69,35 @@ public class WeixinController {
log.info("----code为空---");
return String.format("code为空");
}
JSONObject jsonData = weixinService.getAccessToken(code);
String openid = jsonData.getString("openid");
String access_token = jsonData.getString("access_token");
String refresh_token = jsonData.getString("refresh_token");
log.info("-----微信回调userInfo---openid=="+openid+"----access_token==="+access_token);
HttpSession session = request.getSession();
//验证access_token是否失效
JSONObject validateData = weixinService.getValidateData(access_token, openid);
if (!"0".equals(validateData.getString("errcode"))){
//刷新access_token
JSONObject refreshData= weixinService.getRefreshToken(refresh_token);
access_token = refreshData.getString("access_token");
try {
JSONObject jsonData = weixinService.getAccessToken(code);
String openid = jsonData.getString("openid");
String access_token = jsonData.getString("access_token");
String refresh_token = jsonData.getString("refresh_token");
log.info("-----微信回调userInfo---openid=="+openid+"----access_token==="+access_token);
HttpSession session = request.getSession();
//验证access_token是否失效
JSONObject validateData = weixinService.getValidateData(access_token, openid);
if (!"0".equals(validateData.getString("errcode"))){
//刷新access_token
JSONObject refreshData= weixinService.getRefreshToken(refresh_token);
access_token = refreshData.getString("access_token");
}
UserInfo userInfo=new UserInfo();
userInfo.setOpenId(openid);
log.info("UserInfo===" + JSONObject.toJSONString(userInfo));
String encode = Base64.encode(JSONObject.toJSONString(userInfo));
session.removeAttribute(frontSessionKey);
session.setAttribute(frontSessionKey, encode);
callback =new String(Base64.decode(callback), "utf-8");
log.info("callback===" + callback);
return String.format("redirect:"+callback);
}catch (Exception e){
e.printStackTrace();
log.info("网络异常===" + e.getMessage());
return String.format("网络异常");
}
UserInfo userInfo=new UserInfo();
userInfo.setOpenId(openid);
log.error("UserInfo===" + JSONObject.toJSONString(userInfo));
String encode = Base64.encode(JSONObject.toJSONString(userInfo));
session.removeAttribute(frontSessionKey);
session.setAttribute(frontSessionKey, encode);
callback =Base64.encode(callback);
log.error("callback===" + callback);
return String.format("redirect:"+callback);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment