1
0
mirror of https://gitee.com/bookshelfplus/bookshelfplus synced 2025-09-22 01:30:40 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

后端获取用户绑定第三方平台接口完成

This commit is contained in:
2022-04-04 22:18:06 +08:00
parent 48f01a0024
commit 9fa00a2788
7 changed files with 99 additions and 12 deletions

View File

@@ -8,7 +8,6 @@ import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.request.*;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.apache.tomcat.jni.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@@ -17,15 +16,14 @@ import plus.bookshelf.Common.Error.BusinessException;
import plus.bookshelf.Common.Response.CommonReturnType;
import plus.bookshelf.Common.ThirdParty.ThirdPartyConfig;
import plus.bookshelf.Controller.VO.UserVO;
import plus.bookshelf.Dao.DO.ThirdPartyUserAuthDO;
import plus.bookshelf.Dao.DO.ThirdPartyUserDO;
import plus.bookshelf.Dao.Mapper.ThirdPartyUserAuthDOMapper;
import plus.bookshelf.Dao.Mapper.ThirdPartyUserDOMapper;
import plus.bookshelf.Service.Impl.ThirdPartyUserServiceImpl;
import plus.bookshelf.Service.Model.ThirdPartyUserModel;
import plus.bookshelf.Service.Model.UserModel;
import java.util.Map;
import java.util.Objects;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@Api(tags = "第三方登录")
@Controller
@@ -83,6 +81,18 @@ public class ThirdPartyController extends BaseController {
}
}
@ApiOperation(value = "获取用户已绑定的第三方平台", notes = "传入当前登录用户 token ,返回已绑定的第三方平台")
@RequestMapping(value = "getBindingStatus", method = {RequestMethod.GET})
@ResponseBody
public CommonReturnType getBindingStatus(@RequestParam(value = "token", required = false) String token) throws BusinessException, InvocationTargetException, IllegalAccessException {
List<ThirdPartyUserModel> bindingStatus = thirdPartyUserService.getBindingStatus(token);
List<String> bindingPlatformList = new ArrayList<>();
for (ThirdPartyUserModel thirdPartyUserModel : bindingStatus) {
bindingPlatformList.add(thirdPartyUserModel.getSource());
}
return CommonReturnType.create(bindingPlatformList);
}
// 创建授权request
private AuthRequest getAuthRequest(String platform) throws BusinessException {
switch (platform.toLowerCase()) {

View File

@@ -67,4 +67,11 @@ public interface ThirdPartyUserDOMapper {
* @return
*/
Integer getLastInsertId();
/**
* 获取用户登录的所有第三方平台信息
* @param userId
* @return
*/
ThirdPartyUserDO[] getUserBindThirdParties(Integer userId);
}

View File

@@ -4,6 +4,7 @@ import me.zhyd.oauth.enums.AuthUserGender;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@@ -14,9 +15,14 @@ import plus.bookshelf.Dao.DO.ThirdPartyUserAuthDO;
import plus.bookshelf.Dao.DO.ThirdPartyUserDO;
import plus.bookshelf.Dao.Mapper.ThirdPartyUserAuthDOMapper;
import plus.bookshelf.Dao.Mapper.ThirdPartyUserDOMapper;
import plus.bookshelf.Service.Model.ThirdPartyUserModel;
import plus.bookshelf.Service.Model.UserModel;
import plus.bookshelf.Service.Service.ThirdPartyUserService;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
@Service
public class ThirdPartyUserServiceImpl implements ThirdPartyUserService {
@@ -90,6 +96,13 @@ public class ThirdPartyUserServiceImpl implements ThirdPartyUserService {
}
}
/**
* 绑定第三方账号 回调函数
* @param authResponse
* @param token
* @return
* @throws BusinessException
*/
@Override
@Transactional
public Boolean bindThirdPartAccountCallback(AuthResponse authResponse, String token) throws BusinessException {
@@ -154,6 +167,31 @@ public class ThirdPartyUserServiceImpl implements ThirdPartyUserService {
}
}
/**
* 获取用户已绑定的第三方平台
* @param token
* @return
*/
@Override
@Transactional
public List<ThirdPartyUserModel> getBindingStatus(String token) throws BusinessException, InvocationTargetException, IllegalAccessException {
// 函数中已判空,不需要再判断
UserModel userModel = userService.getUserByToken(redisTemplate, token);
ThirdPartyUserDO[] userBindThirdParties = thirdPartyUserDOMapper.getUserBindThirdParties(userModel.getId());
List<ThirdPartyUserModel> thirdPartyUserModelList = new ArrayList<>();
for (ThirdPartyUserDO thirdPartyUserDO : userBindThirdParties) {
ThirdPartyUserModel thirdPartyUserModel = convertThirdPartyUserDOToModel(thirdPartyUserDO);
thirdPartyUserModelList.add(thirdPartyUserModel);
}
return thirdPartyUserModelList;
}
private ThirdPartyUserModel convertThirdPartyUserDOToModel(ThirdPartyUserDO thirdPartyUserDO) throws InvocationTargetException, IllegalAccessException {
ThirdPartyUserModel thirdPartyUserModel = new ThirdPartyUserModel();
BeanUtils.copyProperties(thirdPartyUserDO, thirdPartyUserModel);
return thirdPartyUserModel;
}
private ThirdPartyUserDO getThirdPartyUserDOFromAuthData(Object authData) {
AuthUser data = (AuthUser) authData;
String uuid = data.getUuid();

View File

@@ -3,8 +3,12 @@ package plus.bookshelf.Service.Service;
import me.zhyd.oauth.model.AuthResponse;
import org.springframework.transaction.annotation.Transactional;
import plus.bookshelf.Common.Error.BusinessException;
import plus.bookshelf.Service.Model.ThirdPartyUserModel;
import plus.bookshelf.Service.Model.UserModel;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
public interface ThirdPartyUserService {
/**
@@ -25,4 +29,11 @@ public interface ThirdPartyUserService {
*/
@Transactional
Boolean bindThirdPartAccountCallback(AuthResponse authResponse, String token) throws BusinessException;
/**
* 获取用户登录的所有第三方平台信息
* @param token
* @return
*/
List<ThirdPartyUserModel> getBindingStatus(String token) throws BusinessException, InvocationTargetException, IllegalAccessException;
}

View File

@@ -286,4 +286,8 @@
<select id="getLastInsertId" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID();
</select>
<select id="getUserBindThirdParties" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select <include refid="Base_Column_List" /> from third_party_user_info
where id in (select third_party_user_id from third_party_user_auth_relation where user_id = #{userId,jdbcType=INTEGER})
</select>
</mapper>