1
0
mirror of https://gitee.com/bookshelfplus/bookshelfplus synced 2025-09-27 20:05:15 +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

@@ -647,7 +647,7 @@ __可选__|string
Third Party Controller
[[_qqusingget]]
[[_callbackusingget]]
==== 快捷登录回调函数
....
GET /api/third-party/callback/{platform}
@@ -655,7 +655,7 @@ GET /api/third-party/callback/{platform}
===== 说明
传入 code 值,进行登录
如果传入 token 那么就是绑定第三方账号到当前登录账号,否则就是通过第三方授权登录
===== 参数
@@ -677,6 +677,8 @@ __可选__||string
__可选__||string
|**Query**|**state** +
__可选__||string
|**Query**|**token** +
__可选__|token|string
|===

View File

@@ -2246,7 +2246,7 @@ table.CodeRay td.code>pre{padding:0}
<p>Third Party Controller</p>
</div>
<div class="sect3">
<h4 id="_qqusingget">2.4.1. 快捷登录回调函数</h4>
<h4 id="_callbackusingget">2.4.1. 快捷登录回调函数</h4>
<div class="literalblock">
<div class="content">
<pre>GET /api/third-party/callback/{platform}</pre>
@@ -2255,7 +2255,7 @@ table.CodeRay td.code>pre{padding:0}
<div class="sect4">
<h5 id="_说明_10">说明</h5>
<div class="paragraph">
<p>传入 code 值,进行登录</p>
<p>如果传入 token 那么就是绑定第三方账号到当前登录账号,否则就是通过第三方授权登录</p>
</div>
</div>
<div class="sect4">
@@ -2369,6 +2369,21 @@ table.CodeRay td.code>pre{padding:0}
<p>string</p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-middle"><div><div class="paragraph">
<p><strong>Query</strong></p>
</div></div></td>
<td class="tableblock halign-left valign-middle"><div><div class="paragraph">
<p><strong>token</strong><br>
<em>可选</em></p>
</div></div></td>
<td class="tableblock halign-left valign-middle"><div><div class="paragraph">
<p>token</p>
</div></div></td>
<td class="tableblock halign-left valign-middle"><div><div class="paragraph">
<p>string</p>
</div></div></td>
</tr>
</tbody>
</table>
</div>
@@ -2969,7 +2984,7 @@ table.CodeRay td.code>pre{padding:0}
</div>
<div id="footer">
<div id="footer-text">
Last updated 2022-04-04 14:35:07 SGT
Last updated 2022-04-04 21:35:33 SGT
</div>
</div>
</body>

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>