/** * 获取第三方用户信息 * * @param siteMark * @param openId * @param accessToken * @return */ public static BindSite getBindSite(SiteMark siteMark, String openId, String accessToken) { BindSite bs = new BindSite(); bs.setSiteMark(siteMark); bs.setOpenId(openId); bs.setAccessToken(accessToken); String nickName = null; if (SiteMark.QQ.equals(siteMark)) { UserInfo ui = new UserInfo(accessToken, openId); UserInfoBean uib = null; try { uib = ui.getUserInfo(); } catch (QQConnectException e) { logger.error(e); throw new ServiceException(ErrService.UserS.ACC_108, "QQ_API调用异常", "第三方平台异常,请从新尝试!"); } nickName = uib.getNickname(); } else if (SiteMark.SINA.equals(siteMark)) { Users um = new Users(); um.client.setToken(accessToken); try { weibo4j.model.User user = um.showUserById(openId); nickName = user.getScreenName(); } catch (WeiboException e) { logger.error(e); throw new ServiceException(ErrService.UserS.ACC_108, "SINA_API调用异常", "第三方平台异常,请从新尝试!"); } } bs.setNickName(nickName); return bs; }
@Override public void cancelBindSite(Long bindSiteId) { BindSite bs = bindSiteDao.queryById(bindSiteId); if (bs == null) { throw new ServiceException(ErrService.UserS.ACC_108, "已经解绑成功,不允许再次操作!"); } if (bs.getRegFlag()) { throw new ServiceException(ErrService.UserS.ACC_108, "第三方账号注册,不允许解除绑定关系!"); } bindSiteDao.deletById(bindSiteId); }
private void changeToken(BindSite bsOther, long tokenExpireIn) { bsOther.setStartDate(new Date()); bindSiteDao.updateToken( bsOther.getBindSiteId(), bsOther.getAccessToken(), bsOther.getNickName(), bsOther.getPhotoPath(), bsOther.getStartDate(), new Date(bsOther.getStartDate().getTime() + tokenExpireIn)); }
@Override public BindSite checkLogin( BindSite.SiteMark siteMark, String openId, String accessToken, long tokenExpireIn) { // 第三方用户信息在当前平台保存的信息 BindSite bs = bindSiteDao.queryByOpenId(siteMark, openId); // 获取第三方用户信息 BindSite bsOther = UserServiceImpl.getBindSite(siteMark, openId, accessToken); if (bs != null) { // 更新token等信息 bsOther.setBindSiteId(bs.getBindSiteId()); this.changeToken(bsOther, tokenExpireIn); } else { // 注册用户 User user = register(bsOther.getNickName(), bsOther.getPhotoPath()); // 绑定平台 bs = bindSiteUser(user.getUserId(), siteMark, openId, accessToken, tokenExpireIn, true); } return bs; }
@Override public BindSite bindSiteUser( Long userId, SiteMark siteMark, String openId, String accessToken, long tokenExpireIn, boolean regFlag) { if (userId == null || userId <= 0) { throw new IllegalArgumentException(); } BindSite bs3 = bindSiteDao.queryByOpenId(siteMark, openId); if (bs3 != null) { throw new ServiceException(ErrService.UserS.ACC_108, "当前第三方账号已经被其他账号绑定!"); } // 当前用户是否已经绑定 BindSite bs = bindSiteDao.queryByUserId(siteMark, userId); // 获取第三方用户信息 BindSite bsOther = UserServiceImpl.getBindSite(siteMark, openId, accessToken); if (bs != null) { // 更新token等信息 bsOther.setBindSiteId(bs.getBindSiteId()); this.changeToken(bsOther, tokenExpireIn); } else { // 绑定平台 bs = new BindSite(); bs.setAccessToken(accessToken); bs.setOpenId(openId); bs.setNickName(bsOther.getNickName()); bs.setRegFlag(regFlag); bs.setSiteMark(siteMark); bs.setUserId(userId); bs.setStartDate(new Date()); bs.setEndDate(new Date(bs.getStartDate().getTime() + tokenExpireIn)); bs.setPhotoPath(bsOther.getPhotoPath()); bindSiteDao.insert(bs); } return bs; }