/**
* 使用微信登陆。JAVA版
*
* @return
* @throws IOException
*/
public String wxLogin() throws IOException, QQConnectException {
getResponse().setContentType("text/html;charset=utf-8");
// 请求被拦截的路径
// String bUri = getRequest().getHeader("referer");
String aUri = CommonUtil.getRequestURL(getRequest());
getRequest().getSession().setAttribute("bUri", aUri);
String requestUrl = ConfigUtil.OAUTH2_URL.replace("APPID", ConfigUtil.APPID).replace("REDIRECT_URI",
CommonUtil.urlEncodeUTF8(ConfigUtil.REDIRECT_URI)).replace("STATE", PayCommonUtil.buildRandom(3) + aUri);
logger.error("微信 wxLogin.bUri =====================================================================" + aUri);
logger.error("微信 wxLogin.requestUrl =====================================================================" + requestUrl);
getResponse().sendRedirect(requestUrl);
return null;
}
/**
* 通过code获取token
*
* @param order
* @throws IOException
*/
public String payapireturnurl() throws IOException {
// HttpServletRequest request = (HttpServletRequest)
// ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
// HttpServletResponse response = (HttpServletResponse)
// ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
String wxcode = code;// 微信code
String wxstate = state;// 微信state
logger.error("微信 payapireturnurl.wxcode ============" + wxcode);
logger.error("微信 payapireturnurl.wxstate ============" + wxstate);
token = CommonUtil.getOAUTH2(ConfigUtil.APPID, ConfigUtil.APP_SECRECT, code, getRequest());
try {
String accessToken = null, openID = null, nickname = null;
long tokenExpireIn = 0L;
if (token==null||token.getAccessToken().equals("")) {
// 我们的网站被CSRF攻击了或者用户取消了授权
// 做一些数据统计工作
logger.error("没有获取到响应参数");
} else {
accessToken = token.getAccessToken();
tokenExpireIn = token.getExpiresIn();
// 利用获取到的accessToken 去获取当前用的openid -------- start
openID = token.getOpenId();
logger.error("欢迎你,代号为 " + openID + " 的用户!");
// 查询本地数据库,如果没有此用户则创建一个
Account acc = new Account();
acc.setOpenId(openID);
acc = accountService.selectOne(acc);
logger.error("查询微信用户。acc===========" + acc);
if (acc == null) {
logger.error("查询不到此微信用户。准备创建一个。");
acc = new Account();
acc.setOpenId(openID);
acc.setAccessToken(accessToken);
acc.setLoginType(LoginTypeEnum.wx);// 设置为微信登陆
acc.setAccountType(acc.getLoginType().toString());
logger.error("AccountType ===============." + acc.getLoginType().toString());
acc.setAccountType(acc.getLoginType().toString());
accountService.insertOutAccount(acc);
logger.error("创建成功。");
logger.error("注册完毕.");
acc.setLoginType("wx");// 设置为微信登陆
acc.setNickname(nickname);
logger.error("登陆完毕.");
}
String bUri = (String) getRequest().getSession().getAttribute("bUri");
if (bUri == null || bUri.equals("")) {
bUri = state.substring(3);
}
logger.error("微信 payapireturnurl.bUri ============" + bUri);
if (bUri != null && bUri != "") {
getResponse().sendRedirect(bUri);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
// return "payapireturnurl";
}