最近作工程時用到了三方登錄java
三方登陸實現層代碼以下,思路在代碼的註釋中數據庫
/** * 第三方用戶登陸 * * @param accountType * 第三方帳戶類型 * @param uid * uid * @param accessToken * 連接token * @param gender * 性別 * @param userImgUrl * 圖片url * @param nickname * 暱稱 * @return 登陸用戶信息 * * @throws DataAccessException */ @Override public ResultObject addThirdLogin(String accountType, String uid, String accessToken, String refreshToken,String gender, String userImgUrl, String nickname, String appToken) throws DataAccessException { ResultObject ro = new ResultObject(); if (StringUtils.isBlank(accountType) || StringUtils.isBlank(uid) || StringUtils.isBlank(accessToken)) { ro.setResultCode(ResultCode.FAILED); ro.setResultMsg(ResultMsg.MSG_PARAM_ERROR); return ro; } // 取第三方用戶表裏取該用戶的信息 ThirdUserInfo tui = thirdUserInfoMapper.selectByTypeAndUid(accountType, uid); UserInfoVO userInfoVO = null; if (tui != null) { // 若是有,則直接從用戶表裏取該用戶信息 userInfoVO = userMapper.selectByUserId(tui.getUserId()); //登陸成功後,修改最後登陸時間 userMapper.updLastLogoTime(tui.getUserId().toString(), Utils.getSysTime()); //向數據庫存入刷新token ThirdUserInfo tuii = new ThirdUserInfo(); tuii.setThirdUserId(tui.getThirdUserId()); tuii.setAccessToken(accessToken); tuii.setRefreshToken(refreshToken); tuii.setUpdTime(Utils.getSysTime()); thirdUserInfoMapper.updateByPrimaryKeySelective(tuii); //登陸成功後,更新APP_TOKEN if (appToken == null || "".equals(appToken)) { }else { //不爲空就更新 userMapper.updAppToken(tui.getUserId().toString(), appToken); } } else { // 若是沒有 // 1.向用戶信息表裏添加該用戶 // 2.向三方用戶表裏添加該用戶 // 3.向用戶基本信息表裏添加該用戶 // 4.取用戶信息 String time = Utils.getSysTime(); User user = new User(); user.setAddTime(time); user.setUpdTime(time); //普通用戶 user.setUserType(Constant.USER_TYPE_NORMAL); user.setUserStatus(Constant.USER_STATUS_NORMAL); user.setUserInfoFlag(Constant.USER_INFO_FLAG_FLASE); user.setVipFlag(Constant.USER_VIP_FLAG_NORMAL); user.setUserResumeFlag(Constant.USER_RESUM_FLAG_FALSE); //add by lxl,2015/11/23 user.setPushFlag(Constant.pushFlag); tui = new ThirdUserInfo(); tui.setUid(uid); tui.setAccessToken(accessToken); tui.setGender(gender); tui.setUserImgUrl(userImgUrl); tui.setNickname(nickname); tui.setAddTime(time); tui.setUpdTime(time); tui.setAccountType(accountType); UserInfo userInfo = new UserInfo(); userInfo.setAddTime(time); userInfo.setUpdTime(time); userInfo.setGender(gender); userInfo.setNickName(nickname); userInfo.setUserImg(userImgUrl); int result = userMapper.insertSelective(user); if (result > 0) { userInfo.setUserId(user.getUserId()); tui.setUserId(user.getUserId()); //測試回滾 //tui.setThirdUserId(1000000011); if (result > 0) { result = thirdUserInfoMapper.insertSelective(tui); if (result > 0) { userInfoMapper.insertSelective(userInfo); TaobaoClient client = new DefaultTaobaoClient( Constant.appUrl, Constant.appKey, Constant.secret); OpenimUsersAddRequest req = new OpenimUsersAddRequest(); List<Userinfos> list116570 = new ArrayList<Userinfos>(); Userinfos obj116570 = new Userinfos(); obj116570.setNick(nickname); obj116570.setIconUrl(userImgUrl); obj116570.setEmail(null); obj116570.setMobile(null); obj116570.setTaobaoid(null); obj116570.setUserid(user.getUserId().toString()); obj116570.setPassword(uid); list116570.add(obj116570); req.setUserinfos(list116570); OpenimUsersAddResponse rsp; try { rsp = client.execute(req); System.out.println(rsp.getBody()); if (rsp.isSuccess() == false) { TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); ro.setResultCode(ResultCode.FAILED); ro.setResultMsg(ResultMsg.MSG_NET_ERROR); return ro; } } catch (ApiException e) { e.printStackTrace(); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); ro.setResultCode(ResultCode.FAILED); ro.setResultMsg(ResultMsg.MSG_NET_ERROR); return ro; } } } } if (result > 0) { userInfoVO = userMapper.selectByUserId(user.getUserId()); //登陸成功後,修改最後登陸時間 userMapper.updLastLogoTime(user.getUserId().toString(), Utils.getSysTime()); //向數據庫存入刷新token ThirdUserInfo tuii = new ThirdUserInfo(); tuii.setUserId(tui.getUserId()); tuii.setRefreshToken(refreshToken); tuii.setUpdTime(Utils.getSysTime()); thirdUserInfoMapper.updateByPrimaryKeySelective(tuii); //登陸成功後,更新APP_TOKEN if (appToken == null || "".equals(appToken)) { }else { //不爲空就更新 userMapper.updAppToken(user.getUserId().toString(), appToken); } } else { ro.setResultCode(ResultCode.FAILED); ro.setResultMsg(ResultMsg.MSG_LOGIN_FAILED); } } ro.setResultCode(ResultCode.SUCCESS); ro.setResultMsg(ResultMsg.MSG_LOGIN_SUCCESS); if (userInfoVO != null) { ro.setData(userInfoVO); } else { ro.setData(new UserInfoVO()); } return ro; }