mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-17 20:45:06 +08:00
初步完成微信公众号登录
This commit is contained in:
@ -8,10 +8,11 @@ import cn.iocoder.yudao.adminserver.modules.system.enums.permission.MenuTypeEnum
|
||||
import cn.iocoder.yudao.adminserver.modules.system.service.auth.SysAuthService;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysRoleService;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialService;
|
||||
import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO;
|
||||
import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService;
|
||||
import cn.iocoder.yudao.coreservice.modules.system.service.user.SysUserCoreService;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
|
||||
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
|
||||
@ -132,7 +133,7 @@ public class SysAuthController {
|
||||
@DeleteMapping("/social-unbind")
|
||||
@ApiOperation("取消社交绑定")
|
||||
public CommonResult<Boolean> socialUnbind(@RequestBody SysAuthSocialUnbindReqVO reqVO) {
|
||||
socialService.unbindSocialUser(getLoginUserId(), reqVO.getType(), reqVO.getUnionId());
|
||||
socialService.unbindSocialUser(getLoginUserId(), reqVO.getType(), reqVO.getUnionId(), UserTypeEnum.ADMIN);
|
||||
return CommonResult.success(true);
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth;
|
||||
|
||||
import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum;
|
||||
import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum;
|
||||
import cn.iocoder.yudao.framework.common.validation.InEnum;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
@ -1,6 +1,6 @@
|
||||
package cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth;
|
||||
|
||||
import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum;
|
||||
import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum;
|
||||
import cn.iocoder.yudao.framework.common.validation.InEnum;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
@ -1,6 +1,6 @@
|
||||
package cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth;
|
||||
|
||||
import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum;
|
||||
import cn.iocoder.yudao.coreservice.modules.system.enums.social.SysSocialTypeEnum;
|
||||
import cn.iocoder.yudao.framework.common.validation.InEnum;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
@ -1,82 +0,0 @@
|
||||
package cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social;
|
||||
|
||||
import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO;
|
||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.*;
|
||||
|
||||
/**
|
||||
* 社交用户
|
||||
* 通过 {@link SysSocialUserDO#getUserId()} 关联到对应的 {@link SysUserDO}
|
||||
*
|
||||
* @author weir
|
||||
*/
|
||||
@TableName(value = "sys_social_user", autoResultMap = true)
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class SysSocialUserDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 自增主键
|
||||
*/
|
||||
@TableId
|
||||
private Long id;
|
||||
/**
|
||||
* 关联的用户编号
|
||||
*/
|
||||
private Long userId;
|
||||
/**
|
||||
* 用户类型
|
||||
*
|
||||
* 枚举 {@link UserTypeEnum}
|
||||
*/
|
||||
private Integer userType;
|
||||
|
||||
/**
|
||||
* 社交平台的类型
|
||||
*
|
||||
* 枚举 {@link UserTypeEnum}
|
||||
*/
|
||||
private Integer type;
|
||||
|
||||
/**
|
||||
* 社交 openid
|
||||
*/
|
||||
private String openid;
|
||||
/**
|
||||
* 社交 token
|
||||
*/
|
||||
private String token;
|
||||
/**
|
||||
* 社交的全局编号
|
||||
*
|
||||
* 例如说,微信平台的 https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/union-id.html
|
||||
* 如果没有 unionId 的平台,直接使用 openid 作为该字段的值
|
||||
*/
|
||||
private String unionId;
|
||||
/**
|
||||
* 原始 Token 数据,一般是 JSON 格式
|
||||
*/
|
||||
private String rawTokenInfo;
|
||||
|
||||
/**
|
||||
* 用户昵称
|
||||
*/
|
||||
private String nickname;
|
||||
/**
|
||||
* 用户头像
|
||||
*/
|
||||
private String avatar;
|
||||
/**
|
||||
* 原始用户数据,一般是 JSON 格式
|
||||
*/
|
||||
private String rawUserInfo;
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,28 +0,0 @@
|
||||
package cn.iocoder.yudao.adminserver.modules.system.dal.mysql.social;
|
||||
|
||||
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface SysSocialUserMapper extends BaseMapperX<SysSocialUserDO> {
|
||||
|
||||
default List<SysSocialUserDO> selectListByTypeAndUnionId(Integer userType, Collection<Integer> types, String unionId) {
|
||||
return selectList(new QueryWrapper<SysSocialUserDO>().eq("user_type", userType)
|
||||
.in("type", types).eq("union_id", unionId));
|
||||
}
|
||||
|
||||
default List<SysSocialUserDO> selectListByTypeAndUserId(Integer userType, Collection<Integer> types, Long userId) {
|
||||
return selectList(new QueryWrapper<SysSocialUserDO>().eq("user_type", userType)
|
||||
.in("type", types).eq("user_id", userId));
|
||||
}
|
||||
|
||||
default List<SysSocialUserDO> selectListByUserId(Integer userType, Long userId) {
|
||||
return selectList(new QueryWrapper<SysSocialUserDO>().eq("user_type", userType).eq("user_id", userId));
|
||||
}
|
||||
|
||||
}
|
@ -19,12 +19,5 @@ public interface SysRedisKeyConstants {
|
||||
"captcha_code:%s", // 参数为 uuid
|
||||
STRING, String.class, RedisKeyDefine.TimeoutTypeEnum.DYNAMIC);
|
||||
|
||||
RedisKeyDefine SOCIAL_AUTH_USER = new RedisKeyDefine("社交的授权用户",
|
||||
"social_auth_user:%d:%s", // 参数为 type,code
|
||||
STRING, AuthUser.class, Duration.ofDays(1));
|
||||
|
||||
RedisKeyDefine SOCIAL_AUTH_STATE = new RedisKeyDefine("社交的 state",
|
||||
"social_auth_state:%s", // 参数为 state
|
||||
STRING, String.class, Duration.ofHours(24)); // 值为 state
|
||||
|
||||
}
|
||||
|
@ -1,38 +0,0 @@
|
||||
package cn.iocoder.yudao.adminserver.modules.system.dal.redis.social;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
import me.zhyd.oauth.model.AuthCallback;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import static cn.iocoder.yudao.adminserver.modules.system.dal.redis.SysRedisKeyConstants.SOCIAL_AUTH_USER;
|
||||
|
||||
/**
|
||||
* 社交 {@link me.zhyd.oauth.model.AuthUser} 的 RedisDAO
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Repository
|
||||
public class SysSocialAuthUserRedisDAO {
|
||||
|
||||
@Resource
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
public AuthUser get(Integer type, AuthCallback authCallback) {
|
||||
String redisKey = formatKey(type, authCallback.getCode());
|
||||
return JsonUtils.parseObject(stringRedisTemplate.opsForValue().get(redisKey), AuthUser.class);
|
||||
}
|
||||
|
||||
public void set(Integer type, AuthCallback authCallback, AuthUser authUser) {
|
||||
String redisKey = formatKey(type, authCallback.getCode());
|
||||
stringRedisTemplate.opsForValue().set(redisKey, JsonUtils.toJsonString(authUser), SOCIAL_AUTH_USER.getTimeout());
|
||||
}
|
||||
|
||||
private static String formatKey(Integer type, String code) {
|
||||
return String.format(SOCIAL_AUTH_USER.getKeyTemplate(), type, code);
|
||||
}
|
||||
|
||||
}
|
@ -90,8 +90,6 @@ public interface SysErrorCodeConstants {
|
||||
ErrorCode ERROR_CODE_NOT_EXISTS = new ErrorCode(1002013000, "错误码不存在");
|
||||
ErrorCode ERROR_CODE_DUPLICATE = new ErrorCode(1002013001, "已经存在编码为【{}】的错误码");
|
||||
|
||||
// ========== 社交模块 1002014000 ==========
|
||||
ErrorCode SOCIAL_AUTH_FAILURE = new ErrorCode(1002014000, "社交授权失败,原因是:{}");
|
||||
ErrorCode SOCIAL_UNBIND_NOT_SELF = new ErrorCode(1002014001, "社交解绑失败,非当前用户绑定");
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,55 +0,0 @@
|
||||
package cn.iocoder.yudao.adminserver.modules.system.enums.social;
|
||||
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.iocoder.yudao.framework.common.core.IntArrayValuable;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 社交平台的类型枚举
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum SysSocialTypeEnum implements IntArrayValuable {
|
||||
|
||||
GITEE(10, "GITEE"), // https://gitee.com/api/v5/oauth_doc#/
|
||||
DINGTALK(20, "DINGTALK"), // https://developers.dingtalk.com/document/app/obtain-identity-credentials
|
||||
WECHAT_ENTERPRISE(30, "WECHAT_ENTERPRISE"), // https://xkcoding.com/2019/08/06/use-justauth-integration-wechat-enterprise.html
|
||||
;
|
||||
|
||||
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(SysSocialTypeEnum::getType).toArray();
|
||||
|
||||
public static final List<Integer> WECHAT_ALL = ListUtil.toList(WECHAT_ENTERPRISE.type);
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
private final Integer type;
|
||||
/**
|
||||
* 类型的标识
|
||||
*/
|
||||
private final String source;
|
||||
|
||||
@Override
|
||||
public int[] array() {
|
||||
return ARRAYS;
|
||||
}
|
||||
|
||||
public static SysSocialTypeEnum valueOfType(Integer type) {
|
||||
return ArrayUtil.firstMatch(o -> o.getType().equals(type), values());
|
||||
}
|
||||
|
||||
public static List<Integer> getRelationTypes(Integer type) {
|
||||
if (WECHAT_ALL.contains(type)) {
|
||||
return WECHAT_ALL;
|
||||
}
|
||||
return ListUtil.toList(type);
|
||||
}
|
||||
|
||||
}
|
@ -6,18 +6,18 @@ import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth.SysAu
|
||||
import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth.SysAuthSocialLogin2ReqVO;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth.SysAuthSocialLoginReqVO;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.convert.auth.SysAuthConvert;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.enums.logger.SysLoginLogTypeEnum;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.enums.logger.SysLoginResultEnum;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.service.auth.SysAuthService;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.service.common.SysCaptchaService;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialService;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService;
|
||||
import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.social.SysSocialUserDO;
|
||||
import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO;
|
||||
import cn.iocoder.yudao.coreservice.modules.system.service.auth.SysUserSessionCoreService;
|
||||
import cn.iocoder.yudao.coreservice.modules.system.service.logger.SysLoginLogCoreService;
|
||||
import cn.iocoder.yudao.coreservice.modules.system.service.logger.dto.SysLoginLogCreateReqDTO;
|
||||
import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService;
|
||||
import cn.iocoder.yudao.coreservice.modules.system.service.user.SysUserCoreService;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
@ -75,6 +75,8 @@ public class SysAuthServiceImpl implements SysAuthService {
|
||||
@Resource
|
||||
private SysSocialService socialService;
|
||||
|
||||
private static final UserTypeEnum userTypeEnum = UserTypeEnum.ADMIN;
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
// 获取 username 对应的 SysUserDO
|
||||
@ -198,7 +200,7 @@ public class SysAuthServiceImpl implements SysAuthService {
|
||||
|
||||
// 如果未绑定 SysSocialUserDO 用户,则无法自动登录,进行报错
|
||||
String unionId = socialService.getAuthUserUnionId(authUser);
|
||||
List<SysSocialUserDO> socialUsers = socialService.getAllSocialUserList(reqVO.getType(), unionId);
|
||||
List<SysSocialUserDO> socialUsers = socialService.getAllSocialUserList(reqVO.getType(), unionId, userTypeEnum);
|
||||
if (CollUtil.isEmpty(socialUsers)) {
|
||||
throw exception(AUTH_THIRD_LOGIN_NOT_BIND);
|
||||
}
|
||||
@ -216,7 +218,7 @@ public class SysAuthServiceImpl implements SysAuthService {
|
||||
loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表
|
||||
|
||||
// 绑定社交用户(更新)
|
||||
socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser);
|
||||
socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum);
|
||||
|
||||
// 缓存登录用户到 Redis 中,返回 sessionId 编号
|
||||
return userSessionCoreService.createUserSession(loginUser, userIp, userAgent);
|
||||
@ -233,7 +235,7 @@ public class SysAuthServiceImpl implements SysAuthService {
|
||||
loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表
|
||||
|
||||
// 绑定社交用户(新增)
|
||||
socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser);
|
||||
socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum);
|
||||
|
||||
// 缓存登录用户到 Redis 中,返回 sessionId 编号
|
||||
return userSessionCoreService.createUserSession(loginUser, userIp, userAgent);
|
||||
@ -246,7 +248,7 @@ public class SysAuthServiceImpl implements SysAuthService {
|
||||
Assert.notNull(authUser, "授权用户不为空");
|
||||
|
||||
// 绑定社交用户(新增)
|
||||
socialService.bindSocialUser(userId, reqVO.getType(), authUser);
|
||||
socialService.bindSocialUser(userId, reqVO.getType(), authUser, userTypeEnum);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -267,7 +269,7 @@ public class SysAuthServiceImpl implements SysAuthService {
|
||||
reqDTO.setLogType(SysLoginLogTypeEnum.LOGOUT_SELF.getType());
|
||||
reqDTO.setTraceId(TracerUtils.getTraceId());
|
||||
reqDTO.setUserId(userId);
|
||||
reqDTO.setUserType(UserTypeEnum.ADMIN.getValue());
|
||||
reqDTO.setUserType(userTypeEnum.getValue());
|
||||
reqDTO.setUsername(username);
|
||||
reqDTO.setUserAgent(ServletUtils.getUserAgent());
|
||||
reqDTO.setUserIp(ServletUtils.getClientIP());
|
||||
|
@ -1,80 +0,0 @@
|
||||
package cn.iocoder.yudao.adminserver.modules.system.service.social;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum;
|
||||
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 社交 Service 接口,例如说社交平台的授权登录
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
public interface SysSocialService {
|
||||
|
||||
/**
|
||||
* 获得社交平台的授权 URL
|
||||
*
|
||||
* @param type 社交平台的类型 {@link SysSocialTypeEnum}
|
||||
* @param redirectUri 重定向 URL
|
||||
* @return 社交平台的授权 URL
|
||||
*/
|
||||
String getAuthorizeUrl(Integer type, String redirectUri);
|
||||
|
||||
/**
|
||||
* 获得授权的用户
|
||||
* 如果授权失败,则会抛出 {@link ServiceException} 异常
|
||||
*
|
||||
* @param type 社交平台的类型 {@link SysSocialTypeEnum}
|
||||
* @param code 授权码
|
||||
* @param state state
|
||||
* @return 授权用户
|
||||
*/
|
||||
@NotNull
|
||||
AuthUser getAuthUser(Integer type, String code, String state);
|
||||
|
||||
default String getAuthUserUnionId(AuthUser authUser) {
|
||||
return StrUtil.blankToDefault(authUser.getToken().getUnionId(), authUser.getUuid());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得 unionId 对应的某个社交平台的“所有”社交用户
|
||||
* 注意,这里的“所有”,指的是类似【微信】平台,包括了小程序、公众号、PC 网站,他们的 unionId 是一致的
|
||||
*
|
||||
* @param type 社交平台的类型 {@link SysSocialTypeEnum}
|
||||
* @param unionId 社交平台的 unionId
|
||||
* @return 社交用户列表
|
||||
*/
|
||||
List<SysSocialUserDO> getAllSocialUserList(Integer type, String unionId);
|
||||
|
||||
/**
|
||||
* 获得指定用户的社交用户列表
|
||||
*
|
||||
* @param userId 用户编号
|
||||
* @return 社交用户列表
|
||||
*/
|
||||
List<SysSocialUserDO> getSocialUserList(Long userId);
|
||||
|
||||
/**
|
||||
* 绑定社交用户
|
||||
*
|
||||
* @param userId 用户编号
|
||||
* @param type 社交平台的类型 {@link SysSocialTypeEnum}
|
||||
* @param authUser 授权用户
|
||||
*/
|
||||
void bindSocialUser(Long userId, Integer type, AuthUser authUser);
|
||||
|
||||
/**
|
||||
* 取消绑定社交用户
|
||||
*
|
||||
* @param userId 用户编号
|
||||
* @param type 社交平台的类型 {@link SysSocialTypeEnum}
|
||||
* @param unionId 社交平台的 unionId
|
||||
*/
|
||||
void unbindSocialUser(Long userId, Integer type, String unionId);
|
||||
|
||||
}
|
@ -1,178 +0,0 @@
|
||||
package cn.iocoder.yudao.adminserver.modules.system.service.social.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.social.SysSocialUserMapper;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.dal.redis.social.SysSocialAuthUserRedisDAO;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.enums.social.SysSocialTypeEnum;
|
||||
import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialService;
|
||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.xkcoding.justauth.AuthRequestFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhyd.oauth.model.AuthCallback;
|
||||
import me.zhyd.oauth.model.AuthResponse;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import me.zhyd.oauth.request.AuthRequest;
|
||||
import me.zhyd.oauth.utils.AuthStateUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import static cn.iocoder.yudao.adminserver.modules.system.enums.SysErrorCodeConstants.SOCIAL_AUTH_FAILURE;
|
||||
import static cn.iocoder.yudao.adminserver.modules.system.enums.SysErrorCodeConstants.SOCIAL_UNBIND_NOT_SELF;
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
|
||||
|
||||
/**
|
||||
* 社交 Service 实现类
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Service
|
||||
@Valid
|
||||
@Slf4j
|
||||
public class SysSocialServiceImpl implements SysSocialService {
|
||||
|
||||
@Resource
|
||||
private AuthRequestFactory authRequestFactory;
|
||||
|
||||
@Resource
|
||||
private SysSocialAuthUserRedisDAO authSocialUserRedisDAO;
|
||||
|
||||
@Resource
|
||||
private SysSocialUserMapper socialUserMapper;
|
||||
|
||||
@Override
|
||||
public String getAuthorizeUrl(Integer type, String redirectUri) {
|
||||
// 获得对应的 AuthRequest 实现
|
||||
AuthRequest authRequest = authRequestFactory.get(SysSocialTypeEnum.valueOfType(type).getSource());
|
||||
// 生成跳转地址
|
||||
String authorizeUri = authRequest.authorize(AuthStateUtils.createState());
|
||||
return HttpUtils.replaceUrlQuery(authorizeUri, "redirect_uri", redirectUri);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthUser getAuthUser(Integer type, String code, String state) {
|
||||
AuthCallback authCallback = buildAuthCallback(code, state);
|
||||
// 从缓存中获取
|
||||
AuthUser authUser = authSocialUserRedisDAO.get(type, authCallback);
|
||||
if (authUser != null) {
|
||||
return authUser;
|
||||
}
|
||||
|
||||
// 请求获取
|
||||
authUser = this.getAuthUser0(type, authCallback);
|
||||
// 缓存。原因是 code 有且可以使用一次。在社交登录时,当未绑定 User 时,需要绑定登录,此时需要 code 使用两次
|
||||
authSocialUserRedisDAO.set(type, authCallback, authUser);
|
||||
return authUser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysSocialUserDO> getAllSocialUserList(Integer type, String unionId) {
|
||||
List<Integer> types = SysSocialTypeEnum.getRelationTypes(type);
|
||||
return socialUserMapper.selectListByTypeAndUnionId(UserTypeEnum.ADMIN.getValue(), types, unionId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysSocialUserDO> getSocialUserList(Long userId) {
|
||||
return socialUserMapper.selectListByUserId(UserTypeEnum.ADMIN.getValue(), userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void bindSocialUser(Long userId, Integer type, AuthUser authUser) {
|
||||
// 获得 unionId 对应的 SysSocialUserDO 列表
|
||||
String unionId = getAuthUserUnionId(authUser);
|
||||
List<SysSocialUserDO> socialUsers = this.getAllSocialUserList(type, unionId);
|
||||
|
||||
// 逻辑一:如果 userId 之前绑定过该 type 的其它账号,需要进行解绑
|
||||
this.unbindOldSocialUser(userId, type, unionId);
|
||||
|
||||
// 逻辑二:如果 socialUsers 指定的 userId 改变,需要进行更新
|
||||
// 例如说,一个微信 unionId 对应了多个社交账号,结果其中有个关联了新的 userId,则其它也要跟着修改
|
||||
// 考虑到 socialUsers 一般比较少,直接 for 循环更新即可
|
||||
socialUsers.forEach(socialUser -> {
|
||||
if (Objects.equals(socialUser.getUserId(), userId)) {
|
||||
return;
|
||||
}
|
||||
socialUserMapper.updateById(new SysSocialUserDO().setId(socialUser.getId()).setUserId(userId));
|
||||
});
|
||||
|
||||
// 逻辑三:如果 authUser 不存在于 socialUsers 中,则进行新增;否则,进行更新
|
||||
SysSocialUserDO socialUser = CollUtil.findOneByField(socialUsers, "openid", authUser.getUuid());
|
||||
SysSocialUserDO saveSocialUser = SysSocialUserDO.builder() // 新增和更新的通用属性
|
||||
.token(authUser.getToken().getAccessToken()).rawTokenInfo(toJsonString(authUser.getToken()))
|
||||
.nickname(authUser.getNickname()).avatar(authUser.getAvatar()).rawUserInfo(toJsonString(authUser.getRawUserInfo()))
|
||||
.build();
|
||||
if (socialUser == null) {
|
||||
saveSocialUser.setUserId(userId).setUserType(UserTypeEnum.ADMIN.getValue())
|
||||
.setType(type).setOpenid(authUser.getUuid()).setUnionId(unionId);
|
||||
socialUserMapper.insert(saveSocialUser);
|
||||
} else {
|
||||
saveSocialUser.setId(socialUser.getId());
|
||||
socialUserMapper.updateById(saveSocialUser);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unbindSocialUser(Long userId, Integer type, String unionId) {
|
||||
// 获得 unionId 对应的所有 SysSocialUserDO 社交用户
|
||||
List<SysSocialUserDO> socialUsers = this.getAllSocialUserList(type, unionId);
|
||||
if (CollUtil.isEmpty(socialUsers)) {
|
||||
return;
|
||||
}
|
||||
// 校验,是否解绑的是非自己的
|
||||
socialUsers.forEach(socialUser -> {
|
||||
if (!Objects.equals(socialUser.getUserId(), userId)) {
|
||||
throw exception(SOCIAL_UNBIND_NOT_SELF);
|
||||
}
|
||||
});
|
||||
|
||||
// 解绑
|
||||
socialUserMapper.deleteBatchIds(CollectionUtils.convertSet(socialUsers, SysSocialUserDO::getId));
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void unbindOldSocialUser(Long userId, Integer type, String newUnionId) {
|
||||
List<Integer> types = SysSocialTypeEnum.getRelationTypes(type);
|
||||
List<SysSocialUserDO> oldSocialUsers = socialUserMapper.selectListByTypeAndUserId(
|
||||
UserTypeEnum.ADMIN.getValue(), types, userId);
|
||||
// 如果新老的 unionId 是一致的,说明无需解绑
|
||||
if (CollUtil.isEmpty(oldSocialUsers) || Objects.equals(newUnionId, oldSocialUsers.get(0).getUnionId())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 解绑
|
||||
socialUserMapper.deleteBatchIds(CollectionUtils.convertSet(oldSocialUsers, SysSocialUserDO::getId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求社交平台,获得授权的用户
|
||||
*
|
||||
* @param type 社交平台的类型
|
||||
* @param authCallback 授权回调
|
||||
* @return 授权的用户
|
||||
*/
|
||||
private AuthUser getAuthUser0(Integer type, AuthCallback authCallback) {
|
||||
AuthRequest authRequest = authRequestFactory.get(SysSocialTypeEnum.valueOfType(type).getSource());
|
||||
AuthResponse<?> authResponse = authRequest.login(authCallback);
|
||||
log.info("[getAuthUser0][请求社交平台 type({}) request({}) response({})]", type, toJsonString(authCallback),
|
||||
toJsonString(authResponse));
|
||||
if (!authResponse.ok()) {
|
||||
throw exception(SOCIAL_AUTH_FAILURE, authResponse.getMsg());
|
||||
}
|
||||
return (AuthUser) authResponse.getData();
|
||||
}
|
||||
|
||||
private static AuthCallback buildAuthCallback(String code, String state) {
|
||||
return AuthCallback.builder().code(code).state(state).build();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user