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

第三方账号取消授权功能完成

This commit is contained in:
2022-04-04 23:23:41 +08:00
parent 7cca39d2de
commit 3986458622
7 changed files with 94 additions and 1 deletions

View File

@@ -21,6 +21,28 @@
</main>
<%- include("../component/footer-user.html"); %>
<script>
function thirdPartyWithdraw(platform) {
postRequest("/third-party/withdrawThirdPartyBings", { token: localStorage.token, platform: platform })
.then(function (response) {
var axiosData = response.data;
var status = axiosData.status;
var data = axiosData.data;
if (status === "success") {
console.log(data);
if(data == "success") {
alert("取消绑定成功!");
location.reload();
} else {
alert("出错啦,刷新页面重新试试吧");
}
} else {
alert(`出错啦!${data.errMsg} (错误码: ${data.errCode}) `);
location.reload();
}
}).catch(function (error) {
console.log(error);
});
}
if(localStorage.token != null) {
getRequest("/third-party/getBindingStatus", { token: localStorage.token })
.then(function (response) {

View File

@@ -17,7 +17,8 @@ public enum BusinessErrorCode implements CommonError {
// 40000开头为第三方登录相关错误定义
THIRD_PARTY_LOGIN_FAIL(40001, "第三方登录失败"),
THIRD_PARTY_ACCOUNT_ALREADY_BOUND(40002, "该账号已被其他账号绑定");
THIRD_PARTY_ACCOUNT_ALREADY_BOUND(40002, "该账号已被其他账号绑定"),
THIRD_PARTY_UNBIND_FAIL(40003, "第三方账号解绑失败");
private BusinessErrorCode(int errCode, String errMsg) {

View File

@@ -93,6 +93,21 @@ public class ThirdPartyController extends BaseController {
return CommonReturnType.create(bindingPlatformList);
}
@ApiOperation(value = "取消第三方平台绑定", notes = "传入当前登录用户 token 和平台 platform (不区分大小写),返回 bool 值true 为取消绑定成功")
@RequestMapping(value = "withdrawThirdPartyBings", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType unbindThirdPartAccount(@RequestParam(value = "token", required = true) String token,
@RequestParam(value = "platform", required = true) String platform) throws BusinessException {
if(platform == null || platform.isEmpty()){
throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "参数错误,第三方平台不能为空");
}
Boolean isSuccess = thirdPartyUserService.unbindThirdPartAccount(token, platform);
if(!isSuccess){
throw new BusinessException(BusinessErrorCode.THIRD_PARTY_UNBIND_FAIL, "取消绑定失败,系统错误");
}
return CommonReturnType.create("success");
}
// 创建授权request
private AuthRequest getAuthRequest(String platform) throws BusinessException {
switch (platform.toLowerCase()) {

View File

@@ -68,4 +68,13 @@ public interface ThirdPartyUserAuthDOMapper {
* @mbg.generated
*/
ThirdPartyUserAuthDO selectByUserId(Integer id);
/**
* 通过 用户 id 和 第三方用户 id 删除绑定关系
*
* @param userId
* @param thirdPartyUserId
* @return
*/
Integer deleteByUserIdAndThirdPartyUserId(Integer userId, Integer thirdPartyUserId);
}

View File

@@ -22,6 +22,7 @@ import plus.bookshelf.Service.Service.ThirdPartyUserService;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
@Service
public class ThirdPartyUserServiceImpl implements ThirdPartyUserService {
@@ -98,6 +99,7 @@ public class ThirdPartyUserServiceImpl implements ThirdPartyUserService {
/**
* 绑定第三方账号 回调函数
*
* @param authResponse
* @param token
* @return
@@ -169,6 +171,7 @@ public class ThirdPartyUserServiceImpl implements ThirdPartyUserService {
/**
* 获取用户已绑定的第三方平台
*
* @param token
* @return
*/
@@ -186,6 +189,34 @@ public class ThirdPartyUserServiceImpl implements ThirdPartyUserService {
return thirdPartyUserModelList;
}
@Override
@Transactional
public Boolean unbindThirdPartAccount(String token, String platform) throws BusinessException {
UserModel userModel = userService.getUserByToken(redisTemplate, token);
platform = platform.toUpperCase();
ThirdPartyUserDO[] userBindThirdParties = thirdPartyUserDOMapper.getUserBindThirdParties(userModel.getId());
for (ThirdPartyUserDO thirdPartyUserDO : userBindThirdParties) {
if (thirdPartyUserDO.getSource().toUpperCase().equals(platform)) {
// 用户 Id
Integer userId = userModel.getId();
// 第三方账号 Id
Integer thirdPartyUserId = thirdPartyUserDO.getId();
// 首先在 Auth 表中删除
int affectRows = thirdPartyUserAuthDOMapper.deleteByUserIdAndThirdPartyUserId(userId, thirdPartyUserId);
if (affectRows > 0) {
// 删除成功,删除第三方账号信息
int affectRows2 = thirdPartyUserDOMapper.deleteByPrimaryKey(thirdPartyUserId);
if (affectRows2 > 0) {
// 删除成功
return true;
}
}
return false;
}
}
return false;
}
private ThirdPartyUserModel convertThirdPartyUserDOToModel(ThirdPartyUserDO thirdPartyUserDO) throws InvocationTargetException, IllegalAccessException {
ThirdPartyUserModel thirdPartyUserModel = new ThirdPartyUserModel();
BeanUtils.copyProperties(thirdPartyUserDO, thirdPartyUserModel);

View File

@@ -36,4 +36,13 @@ public interface ThirdPartyUserService {
* @return
*/
List<ThirdPartyUserModel> getBindingStatus(String token) throws BusinessException, InvocationTargetException, IllegalAccessException;
/**
* 解绑第三方账号
* @param token 用户token
* @param platform 第三方平台
* @return
*/
@Transactional
Boolean unbindThirdPartAccount(String token, String platform) throws BusinessException;
}

View File

@@ -112,4 +112,10 @@
from third_party_user_auth_relation
where user_id = #{userId,jdbcType=INTEGER}
</select>
<delete id="deleteByUserIdAndThirdPartyUserId" parameterType="java.lang.Integer">
delete from third_party_user_auth_relation
where user_id = #{userId,jdbcType=INTEGER}
and third_party_user_id = #{thirdPartyUserId,jdbcType=INTEGER}
limit 1
</delete>
</mapper>