@Before(UserInterceptor.class) public void setting() throws InterruptedException { String method = getRequest().getMethod(); if (method.equalsIgnoreCase(Constants.GET)) { setAttr("save", getPara("save")); render("front/user/setting.ftl"); } else if (method.equalsIgnoreCase(Constants.POST)) { User user = getSessionAttr(Constants.USER_SESSION); String url = getPara("url"); String nickname = getPara("nickname"); if (!user.getStr("nickname").equalsIgnoreCase(nickname) && User.me.findByNickname(nickname) != null) { error("此昵称已被注册,请更换昵称"); } else { if (!StrUtil.isBlank(url)) { if (!url.substring(0, 7).equalsIgnoreCase("http://")) { url = "http://" + url; } } user.set("url", StrUtil.transHtml(url)) .set("nickname", StrUtil.noHtml(nickname).trim()) .set("signature", StrUtil.transHtml(getPara("signature"))) .update(); // 保存成功 setSessionAttr(Constants.USER_SESSION, user); success(); } } }
/** * 跳转qq登录 * * @throws QQConnectException */ public void qqlogin() throws QQConnectException { String source = getPara("source"); if (!StrUtil.isBlank(source)) { getSession().setAttribute("source", source); } redirect(new Oauth().getAuthorizeURL(getRequest())); }
/** * 新浪微博登录 * * @throws WeiboException * @throws IOException */ public void weibologin() throws WeiboException, IOException { String source = getPara("source"); if (!StrUtil.isBlank(source)) { getSession().setAttribute("source", source); } cn.weibo.Oauth oauth = new cn.weibo.Oauth(); redirect(oauth.authorize("code")); }
@Before(UserInterceptor.class) public void message() { String uid = getPara(0); if (StrUtil.isBlank(uid)) renderText(Constants.OP_ERROR_MESSAGE); List<Notification> notifications = Notification.me.findNotReadByAuthorId(uid); setAttr("notifications", notifications); Page<Notification> oldMessages = Notification.me.paginate(getParaToInt("p", 1), defaultPageSize(), uid); setAttr("oldMessages", oldMessages); // 将消息置为已读 Notification.me.updateNotification(uid); render("front/user/message.ftl"); }
@Before(UserInterceptor.class) public void cancelBind() { String pt = getPara("pt"); if (StrUtil.isBlank(pt)) { error("非法操作"); } else { User user = (User) getSession().getAttribute(Constants.USER_SESSION); if (pt.equalsIgnoreCase(Constants.QQ)) { user.set("qq_open_id", null).set("qq_avatar", null).set("qq_nickname", null).update(); } else if (pt.equalsIgnoreCase(Constants.SINA)) { user.set("sina_open_id", null).set("sina_avatar", null).set("sina_nickname", null).update(); } /* else if(pt.equalsIgnoreCase(Constants.ThirdLogin.WECHAT)) { user.set("wechat_open_id", null) .set("wechat_avatar", null) .update(); }*/ setSessionAttr(Constants.USER_SESSION, user); setCookie( Constants.USER_COOKIE, StrUtil.getEncryptionToken(user.getStr("token")), 30 * 24 * 60 * 60); success(); } }
/** * qq登录回调方法 * * @throws QQConnectException */ public void qqlogincallback() throws QQConnectException, IOException { HttpServletRequest request = getRequest(); AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request); String accessToken = null, openID = null; if (accessTokenObj.getAccessToken().equals("")) { renderText("用户取消了授权或没有获取到响应参数"); } else { accessToken = accessTokenObj.getAccessToken(); // 利用获取到的accessToken 去获取当前用的openid -------- start OpenID openIDObj = new OpenID(accessToken); openID = openIDObj.getUserOpenID(); UserInfo qzoneUserInfo = new UserInfo(accessToken, openID); UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo(); String avatar = userInfoBean.getAvatar().getAvatarURL50(); String nickname = userInfoBean.getNickname(); User user = (User) getSession().getAttribute(Constants.USER_SESSION); if (user == null) { user = User.me.findByOpenID(openID, "qq"); String id = StrUtil.getUUID(); if (user == null) { user = new User(); user.set("id", id) .set("qq_nickname", nickname) .set("qq_avatar", avatar) .set("qq_open_id", openID); } else { user.set("qq_nickname", nickname).set("qq_avatar", avatar); } setSessionAttr("open_id", openID); setSessionAttr("thirdlogin_type", "qq"); setSessionAttr("unsave_user", user); } else { User user1 = User.me.findByOpenID(openID, "qq"); if (user1 != null && !user1.get("id").equals(user.get("id"))) { getResponse().setCharacterEncoding("utf-8"); getResponse() .getWriter() .write( "<script>alert('此QQ号已经绑定其他账户,请更换绑定');location.href=\'/user/setting\'</script>"); return; } else { user.set("qq_nickname", nickname) .set("qq_open_id", openID) .set("qq_avatar", avatar) .update(); } } if (StrUtil.isBlank(user.getStr("email"))) { redirect("/reg.html?third=qq"); } else { setSessionAttr(Constants.USER_SESSION, user); setCookie( Constants.USER_COOKIE, StrUtil.getEncryptionToken(user.getStr("token")), 30 * 24 * 60 * 60); String source = (String) getSession().getAttribute("source"); if (!StrUtil.isBlank(source)) { if (source.equalsIgnoreCase("usersetting")) { getSession().removeAttribute("source"); redirect("/user/setting"); } } else { redirect("/"); } } } }
/** * 新浪微博登录后回调 * * @throws WeiboException */ public void weibologincallback() throws WeiboException, IOException { String code = getPara("code"); cn.weibo.Oauth oauth = new cn.weibo.Oauth(); String error = getPara("error"); if (!StrUtil.isBlank(error) && error.equals("access_denied")) { renderText("用户拒绝了新浪微博登录"); } else { cn.weibo.http.AccessToken accessToken = oauth.getAccessTokenByCode(code); Users users = new Users(accessToken.getAccessToken()); cn.weibo.model.User weiboUser = users.showUserById(accessToken.getUid()); if (weiboUser != null) { User user = (User) getSession().getAttribute(Constants.USER_SESSION); if (user == null) { user = User.me.findByOpenID(weiboUser.getId(), "sina"); String id = StrUtil.getUUID(); if (user == null) { user = new User(); user.set("id", id) .set("sina_nickname", weiboUser.getScreenName()) .set("sina_avatar", weiboUser.getAvatarLarge()) .set("sina_open_id", weiboUser.getId()); } else { user.set("sina_nickname", weiboUser.getScreenName()) .set("sina_avatar", weiboUser.getAvatarLarge()); } setSessionAttr("open_id", weiboUser.getId()); setSessionAttr("thirdlogin_type", "sina"); setSessionAttr("unsave_user", user); } else { User user1 = User.me.findByOpenID(weiboUser.getId(), "sina"); if (user1 != null && !user1.get("id").equals(user.get("id"))) { getResponse().setCharacterEncoding("utf-8"); getResponse() .getWriter() .write( "<script>alert('此微博账号已经绑定其他账户,请更换绑定');location.href=\'/user/setting\'</script>"); return; } else { user.set("sina_nickname", weiboUser.getScreenName()) .set("sina_avatar", weiboUser.getAvatarLarge()) .set("sina_open_id", weiboUser.getId()) .update(); } } if (StrUtil.isBlank(user.getStr("email"))) { redirect("/reg.html?third=qq"); } else { setSessionAttr(Constants.USER_SESSION, user); setCookie( Constants.USER_COOKIE, StrUtil.getEncryptionToken(user.getStr("token")), 30 * 24 * 60 * 60); String source = (String) getSession().getAttribute("source"); if (!StrUtil.isBlank(source)) { if (source.equalsIgnoreCase("usersetting")) { getSession().removeAttribute("source"); redirect("/user/setting"); } } else { redirect("/"); } } } else { renderText("新浪微博登陆失败"); } } }