服务端重定向到h5的login.vue页面获取参数
@RequestMapping({"/callback"})
public void callback(@RequestParam String code, HttpServletResponse response)
throws IOException {
this.globeCode = null;
if (StringUtils.isEmpty(code)) {
this.logger.info("code is null,please check authorize method");
return;
}
this.logger.info("code ===" + code);
this.globeCode = code;
getAccessToken();
String userInfo = getBaseUserInfo();
JSONObject object = JSONObject.parseObject(userInfo);
if ("true".equals(object.getString("success"))) {
}
String identityInfo = getIdentityInfo();
JSONObject identityObj = JSONObject.parseObject(identityInfo);
if ("true".equals(identityObj.getString("success"))) {
Sys_User sys_user = userRepository.findByZhAndXmAndAndBjAndIsdel(com.elink.util.StringUtils.getStringByObj(identityObj.getJSONObject("data").getString("userName"))
, com.elink.util.StringUtils.getStringByObj(identityObj.getJSONObject("data").getString("nickName")), 3, 0);
if (sys_user == null) {
sys_user = new Sys_User();
sys_user.setBj(3);
sys_user.setXm(com.elink.util.StringUtils.getStringByObj(identityObj.getJSONObject("data").getString("nickName")));
sys_user.setZh(com.elink.util.StringUtils.getStringByObj(identityObj.getJSONObject("data").getString("userName")));
sys_user.setPhone(com.elink.util.StringUtils.getStringByObj(identityObj.getJSONObject("data").getString("mobile")));
sys_user.setTx(com.elink.util.StringUtils.getStringByObj(identityObj.getJSONObject("data").getString("head_pic")));
sys_user.setIsdel(0);
userRepository.save(sys_user);
}
String token = JwtUtil.geneJsonWebToken(sys_user);
StringBuffer requestUrl = new StringBuffer("http://61.185.224.91:16780");
requestUrl.append("?token=" + token);
requestUrl.append("&xm=" + URLEncoder.encode(sys_user.getXm()));
requestUrl.append("&tx=" + sys_user.getTx());
requestUrl.append("&userId=" + sys_user.getId());
System.out.println("重定向到login.vue=" + requestUrl.toString());
response.sendRedirect(requestUrl.toString());
}
}
onLoad() {
_this = this;
var token = _this.getQueryVariable('token');
var xm = decodeURI(_this.getQueryVariable('xm'));
var tx = _this.getQueryVariable('tx');
var userId = _this.getQueryVariable('userId');
_this.h5Login(token,xm,tx,userId);
}
getQueryVariable(variable){
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
注意:需要使用getQueryVariable方法获取参数,使用onLoad中的参数无法获取重定向过来的参数。