示例#1
0
 /**
  * 新浪微博登录
  *
  * @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"));
 }
示例#2
0
 /**
  * 新浪微博登录后回调
  *
  * @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("新浪微博登陆失败");
     }
   }
 }