mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-08 04:39:00 +08:00
refactor(社交登录): 前后端使用统一枚举,避免重复代码
This commit is contained in:
1 parent
bdf16f42d4
commit
9101357f44
2 files changed
+70
-68
No files matched your search
+58
-55
@@ -27,78 +27,81 @@ import com.pig4cloud.pigx.admin.api.entity.SysUser;
|
||||
import com.pig4cloud.pigx.admin.mapper.SysSocialDetailsMapper;
|
||||
import com.pig4cloud.pigx.admin.service.SysUserService;
|
||||
import com.pig4cloud.pigx.common.core.constant.enums.LoginTypeEnum;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2023-10-14
|
||||
*
|
||||
* <p>
|
||||
* 企业微信登录
|
||||
*/
|
||||
@Slf4j
|
||||
@Component("CP")
|
||||
@AllArgsConstructor
|
||||
@Component("WEIXIN_CP")
|
||||
@RequiredArgsConstructor
|
||||
public class WeChatCPLoginHandler extends AbstractLoginHandler {
|
||||
|
||||
private final SysUserService sysUserService;
|
||||
private final SysUserService sysUserService;
|
||||
|
||||
private final SysSocialDetailsMapper sysSocialDetailsMapper;
|
||||
private final SysSocialDetailsMapper sysSocialDetailsMapper;
|
||||
|
||||
/**
|
||||
* QQ登录传入code
|
||||
* <p>
|
||||
* 通过code 调用qq 获取唯一标识
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String identify(String code) {
|
||||
SysSocialDetails condition = new SysSocialDetails();
|
||||
condition.setType(LoginTypeEnum.WEIXIN_CP.getType());
|
||||
SysSocialDetails socialDetails = sysSocialDetailsMapper.selectOne(new QueryWrapper<>(condition));
|
||||
/**
|
||||
* 企微登录传入code
|
||||
* <p>
|
||||
* 通过code 调用获取唯一标识
|
||||
*
|
||||
* @param code
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public String identify(String code) {
|
||||
SysSocialDetails condition = new SysSocialDetails();
|
||||
condition.setType(LoginTypeEnum.WEIXIN_CP.getType());
|
||||
SysSocialDetails socialDetails = sysSocialDetailsMapper.selectOne(new QueryWrapper<>(condition));
|
||||
|
||||
String getAccessTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s";
|
||||
String getAccessTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s";
|
||||
|
||||
String accessTokenResult = HttpUtil
|
||||
.get(String.format(getAccessTokenUrl, socialDetails.getAppId(), socialDetails.getAppSecret()));
|
||||
log.debug("获取企业微信Token响应报文:{}", accessTokenResult);
|
||||
String accessToken = JSONUtil.parseObj(accessTokenResult).getStr("access_token");
|
||||
String getUserIdUrl = "https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=%s&code=%s";
|
||||
String userResult = HttpUtil.get(String.format(getUserIdUrl, accessToken, code));
|
||||
log.debug("获取企业微信UserId响应报文:{}", userResult);
|
||||
// 企微唯一标识
|
||||
return JSONUtil.parseObj(userResult).getStr("userid");
|
||||
}
|
||||
String accessTokenResult = HttpUtil
|
||||
.get(String.format(getAccessTokenUrl, socialDetails.getAppId(), socialDetails.getAppSecret()));
|
||||
log.debug("获取企业微信Token响应报文:{}", accessTokenResult);
|
||||
String accessToken = JSONUtil.parseObj(accessTokenResult).getStr("access_token");
|
||||
String getUserIdUrl = "https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=%s&code=%s";
|
||||
String userResult = HttpUtil.get(String.format(getUserIdUrl, accessToken, code));
|
||||
log.debug("获取企业微信UserId响应报文:{}", userResult);
|
||||
// 企微唯一标识
|
||||
return JSONUtil.parseObj(userResult).getStr("userid");
|
||||
}
|
||||
|
||||
/**
|
||||
* openId 获取用户信息
|
||||
* @param openId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public UserInfo info(String openId) {
|
||||
SysUser user = sysUserService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getWxCpUserid, openId));
|
||||
/**
|
||||
* openId 获取用户信息
|
||||
*
|
||||
* @param openId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public UserInfo info(String openId) {
|
||||
SysUser user = sysUserService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getWxCpUserid, openId));
|
||||
|
||||
if (user == null) {
|
||||
log.info("企业微信未绑定:{}", openId);
|
||||
return null;
|
||||
}
|
||||
return sysUserService.findUserInfo(user);
|
||||
}
|
||||
if (user == null) {
|
||||
log.info("企业微信未绑定:{}", openId);
|
||||
return null;
|
||||
}
|
||||
return sysUserService.findUserInfo(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定逻辑
|
||||
* @param user 用户实体
|
||||
* @param identify 渠道返回唯一标识
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Boolean bind(SysUser user, String identify) {
|
||||
user.setWxCpUserid(identify);
|
||||
sysUserService.updateById(user);
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* 绑定逻辑
|
||||
*
|
||||
* @param user 用户实体
|
||||
* @param identify 渠道返回唯一标识
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Boolean bind(SysUser user, String identify) {
|
||||
user.setWxCpUserid(identify);
|
||||
sysUserService.updateById(user);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
+12
-13
@@ -42,6 +42,7 @@ import com.pig4cloud.pigx.admin.service.*;
|
||||
import com.pig4cloud.pigx.common.audit.annotation.Audit;
|
||||
import com.pig4cloud.pigx.common.core.constant.CacheConstants;
|
||||
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
|
||||
import com.pig4cloud.pigx.common.core.constant.enums.LoginTypeEnum;
|
||||
import com.pig4cloud.pigx.common.core.exception.ErrorCodes;
|
||||
import com.pig4cloud.pigx.common.core.util.MsgUtils;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
@@ -498,32 +499,30 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||
public R unbinding(String type) {
|
||||
PigxUser user = SecurityUtils.getUser();
|
||||
LambdaUpdateWrapper<SysUser> wrapper = null;
|
||||
if (type.equals("wechat")) {
|
||||
|
||||
// 微信开放平台 (普通用户扫码登录)
|
||||
if (type.equals(LoginTypeEnum.WECHAT.getType())) {
|
||||
wrapper = Wrappers.<SysUser>lambdaUpdate()
|
||||
.set(SysUser::getWxOpenid, null)
|
||||
.eq(SysUser::getUserId, user.getId());
|
||||
} else if (type.equals("gitee")) {
|
||||
// 码云登录 (方便申请)
|
||||
} else if (type.equals(LoginTypeEnum.GITEE.getType())) {
|
||||
wrapper = Wrappers.<SysUser>lambdaUpdate()
|
||||
.set(SysUser::getGiteeLogin, null)
|
||||
.eq(SysUser::getUserId, user.getId());
|
||||
} else if (type.equals("osc")) {
|
||||
wrapper = Wrappers.<SysUser>lambdaUpdate()
|
||||
.set(SysUser::getOscId, null)
|
||||
.eq(SysUser::getUserId, user.getId());
|
||||
} else if (type.equals("tencent")) {
|
||||
wrapper = Wrappers.<SysUser>lambdaUpdate()
|
||||
.set(SysUser::getQqOpenid, null)
|
||||
.eq(SysUser::getUserId, user.getId());
|
||||
} else if (type.equals("cp")) {
|
||||
// 企业微信登录
|
||||
} else if (type.equals(LoginTypeEnum.WEIXIN_CP.getType())) {
|
||||
wrapper = Wrappers.<SysUser>lambdaUpdate()
|
||||
.set(SysUser::getWxCpUserid, null)
|
||||
.eq(SysUser::getUserId, user.getId());
|
||||
} else if (type.equals("dingding")) {
|
||||
// 钉钉登录
|
||||
} else if (type.equals(LoginTypeEnum.DINGTALK.getType())) {
|
||||
wrapper = Wrappers.<SysUser>lambdaUpdate()
|
||||
.set(SysUser::getWxDingUserid, null)
|
||||
.eq(SysUser::getUserId, user.getId());
|
||||
}
|
||||
if (wrapper == null) {
|
||||
|
||||
if (Objects.isNull(wrapper)) {
|
||||
return R.failed("解绑账号类型不存在");
|
||||
}
|
||||
this.update(wrapper);
|
||||
|
||||
Reference in new issue
Block a user