发表于: 2017-12-30 22:56:06
1 673
今天完成的事情:
1.微信登录授权接口完毕
2.获取jsapitiket接口完毕
/**
* 获取jsApiTicket
*
* @param url
* @param model
* X@return
*/
@RequestMapping(value = "/a/u/wechat/signature", method = RequestMethod.GET)
public String getSignature(String url, Model model) {
boolean importantParamIsNotNull = CheckTheParameters.allOfParamIsNotNull(url);
if (!importantParamIsNotNull || url.contains("#")) {
model.addAttribute("code", -3);
return "reclamation-funlearn-user-service/user/json/signatureResult";
}
String jsApiTicket = WeChatTokenThread.getJsApiTicket();
String nonceStr = weChatUtil.createNonceStr();
//随机串中不能包含'-'
nonceStr = nonceStr.replace("-", "");
String timestamp = weChatUtil.createTimestamp();
String string1;
String signature = "";
System.out.println(nonceStr);
System.out.println(timestamp);
System.out.println(jsApiTicket);
//参数名必须小写且有序
string1 = "jsapi_ticket=" + jsApiTicket +
"&noncestr=" + nonceStr +
"×tamp=" + timestamp +
"&url=" + url;
System.out.println(string1);
try {
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(string1.getBytes("UTF-8"));
signature = weChatUtil.byteToHex(crypt.digest());
model.addAttribute("code", 0);
model.addAttribute("nonceStr", nonceStr);
model.addAttribute("timestamp", timestamp);
model.addAttribute("signature", signature);
} catch (NoSuchAlgorithmException e) {
log.error(e);
model.addAttribute("code", -1);
} catch (UnsupportedEncodingException e) {
log.error("encryption failed to param, name called 'string1' The string1 is " + string1);
log.error(e);
model.addAttribute("code", -1);
}
return "reclamation-funlearn-user-service/user/json/signatureResult";
}
/**
* 微信网页授权登录回调接口
*
* @param request
* @param model
* @return
*/
@RequestMapping(value = "/a/wechat/login/callback")
public String weChatLoginCallback(HttpServletRequest request, HttpServletResponse response, Model model) {
UserInfoOfWeChat userInfoOfWeChat = new UserInfoOfWeChat();
String code = request.getParameter("code");
Long uid = null;
String openId = null;
String wechatToken = null;
try {
//获取openId
String resultOfJson = weChatUtil.getUserOpenIdAndAccessTokenOfWeChat(code);
Map<String, String> resultOfMap = weChatUtil.jsonToMap(resultOfJson);
openId = resultOfMap.get("openid");
wechatToken = resultOfMap.get("access_token");
//通过openId判断用户是否绑定
uid = null != openId ? OpenLoginUtil.openIdIsExistent(openId, 1) : null;
//当前微信用户账号于本系统账号有绑定
if (null != uid) {
String token = DesUtil.getStrMi(uid + "," + new Date().getTime());
Cookie cookie = new Cookie("token", token);
cookie.setPath("/");
cookie.setMaxAge(60 * 60 * 3);
response.addCookie(cookie);
userInfoOfWeChat.setOpenid(openId);
model.addAttribute("token", token);
model.addAttribute("code", 0);
} else {
userInfoOfWeChat = weChatUtil.getUserInfoOfWeChatByOpenIdAndAccessToken(openId, wechatToken);
log.info("user information is " + userInfoOfWeChat.toString());
model.addAttribute("code", -2100);
}
model.addAttribute("userInfo", userInfoOfWeChat);
} catch (Exception e) {
log.error(e);
model.addAttribute("code", -1);
}
return "reclamation-funlearn-user-service/user/json/openLoginResult";
}
明天计划完成的事情:
前端文件上传接口
后端文件上传接口
后端登录接口
遇到的困难:
无
收获:
应用了多线程,然后明白了对于多线程中注入时不能直接注入,必须将注入的属性设置为static,然后通过set方法注入.
评论