最近做工程时用到了三方登陆
三方登录实现层代码如下,思路在代码的注释中
/**
* 第三方用户登录
*
* @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 list116570 = new ArrayList();
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;
}