|
@@ -6,15 +6,21 @@ import cn.kdan.pdf.backend.core.model.Members;
|
|
|
import cn.kdan.pdf.backend.core.model.SocialAccounts;
|
|
|
import cn.kdan.pdf.backend.core.model.SocialAccountsExample;
|
|
|
import cn.kdan.pdf.backend.core.params.BindAccountParam;
|
|
|
+import cn.kdan.pdf.backend.core.pojo.app.SocialAccountVo;
|
|
|
import cn.kdan.pdf.backend.core.pojo.third.Tencent;
|
|
|
import cn.kdan.pdf.backend.core.pojo.third.Wechat;
|
|
|
+import cn.kdan.pdf.backend.core.service.MemberService;
|
|
|
import cn.kdan.pdf.backend.core.service.SocialAccountService;
|
|
|
import cn.kdan.pdf.backend.core.utils.TencentUtils;
|
|
|
import cn.kdan.pdf.backend.core.utils.WechatUtils;
|
|
|
+import exception.BackendRuntimeException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.ObjectUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import utils.CommonUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.Date;
|
|
@@ -30,6 +36,8 @@ public class SocialAccountServiceImpl implements SocialAccountService {
|
|
|
|
|
|
@Resource
|
|
|
private SocialAccountsMapper socialAccountsMapper;
|
|
|
+ @Resource
|
|
|
+ private MemberService memberService;
|
|
|
|
|
|
@Override
|
|
|
public SocialAccounts selectByAccountId(String accountId) {
|
|
@@ -52,14 +60,20 @@ public class SocialAccountServiceImpl implements SocialAccountService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public SocialAccounts selectByMemberId(String userId, Integer provider) {
|
|
|
+ public List<SocialAccounts> selectByMemberId(String userId) {
|
|
|
+ SocialAccountsExample example = new SocialAccountsExample();
|
|
|
+ example.createCriteria().andMemberIdEqualTo(userId);
|
|
|
+ return socialAccountsMapper.selectByExample(example);
|
|
|
+ }
|
|
|
+
|
|
|
+ public SocialAccounts selectByMemberIdAndProvider(String userId, Integer provider){
|
|
|
SocialAccountsExample example = new SocialAccountsExample();
|
|
|
example.createCriteria().andMemberIdEqualTo(userId).andProviderEqualTo(provider);
|
|
|
List<SocialAccounts> list = socialAccountsMapper.selectByExample(example);
|
|
|
- if(CollectionUtils.isEmpty(list)){
|
|
|
- return null;
|
|
|
+ if(!CollectionUtils.isEmpty(list)){
|
|
|
+ return list.get(0);
|
|
|
}
|
|
|
- return list.get(0);
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -70,18 +84,25 @@ public class SocialAccountServiceImpl implements SocialAccountService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String bindSocialAccount(Members member, BindAccountParam param) {
|
|
|
+ public String bindSocialAccount(Members member, BindAccountParam param, SocialAccountVo socialAccountVo) {
|
|
|
String accountId = "";
|
|
|
String accessToken = param.getAccessToken();
|
|
|
String openid = param.getOpenid();
|
|
|
String provider = param.getProvider();
|
|
|
+ socialAccountVo.setProvider(provider);
|
|
|
if(ProviderEnum.TENCENT.getName().equals(provider)){
|
|
|
Tencent userInfo = TencentUtils.getUserInfo(accessToken);
|
|
|
String unionId = userInfo.getUnionId();
|
|
|
+ if(StringUtils.isEmpty(unionId)){
|
|
|
+ log.info("未找到第三方账号,accessToken:{},openid:{}",accessToken,openid);
|
|
|
+ throw new BackendRuntimeException("未找到第三方账号");
|
|
|
+ }
|
|
|
accountId = unionId;
|
|
|
SocialAccounts socialAccount = selectByAccountId(unionId);
|
|
|
//看看有没有该账号,没有就新增一条,有就更新用户id
|
|
|
if(ObjectUtils.isEmpty(socialAccount)){
|
|
|
+ socialAccount = new SocialAccounts();
|
|
|
+ socialAccount.setId(CommonUtils.generateId());
|
|
|
socialAccount.setAccountId(unionId);
|
|
|
socialAccount.setMemberId(member.getId());
|
|
|
socialAccount.setName(userInfo.getNickname());
|
|
@@ -89,15 +110,25 @@ public class SocialAccountServiceImpl implements SocialAccountService {
|
|
|
socialAccount.setAvatar(userInfo.getFigureUrl());
|
|
|
insertSocialAccount(socialAccount);
|
|
|
} else{
|
|
|
+ //如果有第三方账号,需要先将原用户账号删除,再进行绑定当前用户账号
|
|
|
+ String memberId = socialAccount.getMemberId();
|
|
|
+ memberService.deleteByMemberId(memberId);
|
|
|
socialAccount.setMemberId(member.getId());
|
|
|
updateSocialAccount(socialAccount);
|
|
|
}
|
|
|
+ BeanUtils.copyProperties(socialAccount,socialAccountVo);
|
|
|
}else{
|
|
|
Wechat userInfo = WechatUtils.getUserInfo(openid, accessToken);
|
|
|
String unionId = userInfo.getUnionId();
|
|
|
+ if(StringUtils.isEmpty(unionId)){
|
|
|
+ log.info("未找到第三方账号,accessToken:{},openid:{}",accessToken,openid);
|
|
|
+ throw new BackendRuntimeException("未找到第三方账号");
|
|
|
+ }
|
|
|
accountId = unionId;
|
|
|
SocialAccounts socialAccount = selectByAccountId(unionId);
|
|
|
if(ObjectUtils.isEmpty(socialAccount)){
|
|
|
+ socialAccount = new SocialAccounts();
|
|
|
+ socialAccount.setId(CommonUtils.generateId());
|
|
|
socialAccount.setAvatar(userInfo.getHeadImgUrl());
|
|
|
socialAccount.setAccountId(unionId);
|
|
|
socialAccount.setProvider(ProviderEnum.getCodeByName(provider));
|
|
@@ -105,9 +136,13 @@ public class SocialAccountServiceImpl implements SocialAccountService {
|
|
|
socialAccount.setName(userInfo.getNickname());
|
|
|
insertSocialAccount(socialAccount);
|
|
|
} else{
|
|
|
+ //如果有第三方账号,需要先将原用户账号删除,再进行绑定当前用户账号
|
|
|
+ String memberId = socialAccount.getMemberId();
|
|
|
+ memberService.deleteByMemberId(memberId);
|
|
|
socialAccount.setMemberId(member.getId());
|
|
|
updateSocialAccount(socialAccount);
|
|
|
}
|
|
|
+ BeanUtils.copyProperties(socialAccount,socialAccountVo);
|
|
|
}
|
|
|
return accountId;
|
|
|
}
|
|
@@ -124,26 +159,12 @@ public class SocialAccountServiceImpl implements SocialAccountService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String unbindSocialAccount(Members currentUser, BindAccountParam param) {
|
|
|
- String accountId = "";
|
|
|
- String accessToken = param.getAccessToken();
|
|
|
- String openid = param.getOpenid();
|
|
|
- String provider = param.getProvider();
|
|
|
+ public void unbindSocialAccount(Members currentUser, String provider, SocialAccountVo accountVo) {
|
|
|
+ accountVo.setProvider(provider);
|
|
|
SocialAccountsExample example = new SocialAccountsExample();
|
|
|
- if(ProviderEnum.TENCENT.getName().equals(provider)){
|
|
|
- Tencent userInfo = TencentUtils.getUserInfo(accessToken);
|
|
|
- String unionId = userInfo.getUnionId();
|
|
|
- accountId = unionId;
|
|
|
- example.createCriteria().andAccountIdEqualTo(unionId);
|
|
|
- socialAccountsMapper.deleteByExample(example);
|
|
|
- } else {
|
|
|
- Wechat userInfo = WechatUtils.getUserInfo(openid, accessToken);
|
|
|
- String unionId = userInfo.getUnionId();
|
|
|
- accountId = unionId;
|
|
|
- SocialAccounts socialAccount = selectByAccountId(unionId);
|
|
|
- example.createCriteria().andAccountIdEqualTo(unionId);
|
|
|
- socialAccountsMapper.deleteByExample(example);
|
|
|
- }
|
|
|
- return accountId;
|
|
|
+ SocialAccounts socialAccount = selectByMemberIdAndProvider(currentUser.getId(), ProviderEnum.getCodeByName(provider));
|
|
|
+ BeanUtils.copyProperties(socialAccount,accountVo);
|
|
|
+ example.createCriteria().andAccountIdEqualTo(socialAccount.getAccountId());
|
|
|
+ socialAccountsMapper.deleteByExample(example);
|
|
|
}
|
|
|
}
|