1 回复
[@RequestMapping](/user/RequestMapping)(value = "/subscribeEven")
[@ResponseBody](/user/ResponseBody)
public Object subscribeEven(HttpServletRequest req, HttpServletResponse resp){
LOGGER.warn("1111111111111111111111111111111111");
boolean isGet = req.getMethod().toLowerCase().equals("get");
if (isGet){ //验证URL
//将token、timestamp、nonce这三个参数拼接成一个字符串并按照sha1的算法进行加密,得到一个加密字符串
//并将该字符串与获得的signature参数进行比较,如若相同,将echostr参数内容原样返回,代表接入生效,否则失败。
String signature = req.getParameter("signature");
String timestamp = req.getParameter("timestamp");
String nonce = req.getParameter("nonce");
String echostr = req.getParameter("echostr");
String[] arr=new String[]{tokken,timestamp,nonce};
sort(arr); //字典排序
StringBuilder stringBuilder=new StringBuilder();
for (int i = 0; i < arr.length; i++) {
stringBuilder.append(arr[i]);
}
String tmpStr =null;
MessageDigest md=null;
try {
md=MessageDigest.getInstance("SHA-1"); //SHA加密算法
byte[] digest=md.digest(stringBuilder.toString().getBytes());
tmpStr = StringUtils.byteToStr(digest);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return tmpStr.equals(signature.toUpperCase())==true?echostr:null;
}
try {
//把
Map<String, String> xmlToMap = XmlUtil.xmlToMap(req);
LOGGER.warn(xmlToMap);
if ("event".equals(xmlToMap.get("MsgType"))){ // 事件类型
String event = xmlToMap.get("Event");
if ("subscribe".equals(event)){ // 关注事件
String fromUserName = xmlToMap.get("FromUserName"); // 用户的openid
WechatMember subscribeUser = WechatUtil.getMemberInfoByOpenID(fromUserName); //获取用户的信息
LOGGER.warn("subscribeUser = " +subscribeUser);
if (null != subscribeUser){
Integer subscribe = subscribeUser.getSubscribe();
String sub = StringUtil.getOpVal(subscribe);
if ("1".equals(sub)){ //用户已关注
String unionid = subscribeUser.getUnionid();
String openid = subscribeUser.getOpenid();
String nickname = subscribeUser.getNickname();
Users users = usersService.getUserByAccount(unionid, IUsersService.IDENTITY_MEMBER);
if (null!=users){ //存在用户则更新授权
usersService.updateOpenIdByAccount(openid, unionid);
LOGGER.debug("账号为 " + unionid + " 的openid更改成功!");
}else { //不存在用户则注册
users=new Users();
users.setAccount(unionid);//账号
users.setPhone(null);//手机号
users.setIdentity(IUsersService.IDENTITY_MEMBER);//身份 会员3
users.setPassword(StringUtil.generatePassword(StringUtil.getRandomString(6)));//密码随机
users.setName(nickname);//姓名
users.setDelFlag(1);
users.setUserfrom(2); //来自微信
//产生随机6位的邀请码
String inviteCode = StringUtil.getRandomString(6);
users.setInviteCode(inviteCode);
users.setNickname(nickname);//昵称
String token = StringUtil.generateToken(unionid, new Date().getTime());
users.setToken(token);
users.setTokenTime(new Date());
users.setIdentity(IUsersService.IDENTITY_MEMBER);
//app类型
users.setLoginApp(11);
usersService.addUsers(users,null,null,null);//添加会员
}
}
//回复用户信息
String contrnt="欢迎!!";
TextMessage text = new TextMessage();
text.setFromUserName(xmlToMap.get("toUserName"));
text.setToUserName(fromUserName);
text.setMsgType("text"); //文本类型
text.setCreateTime(new Date().getTime());
text.setContent(contrnt);
String textMsgToxml = XmlUtil.textMsgToxml(text);
resp.getWriter().write(textMsgToxml);
return textMsgToxml;
}
}else if ("unsubscribe".equals(event)){ // 注销事件
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (BizException e) {
e.printStackTrace();
}
return null;
}