|  | @@ -26,8 +26,7 @@ import java.util.List;
 | 
	
		
			
				|  |  |  import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
 | 
	
		
			
				|  |  |  import static cn.newfeifan.mall.framework.common.util.collection.CollectionUtils.convertSet;
 | 
	
		
			
				|  |  |  import static cn.newfeifan.mall.framework.common.util.json.JsonUtils.toJsonString;
 | 
	
		
			
				|  |  | -import static cn.newfeifan.mall.module.system.enums.ErrorCodeConstants.AUTH_LOGIN_USER_IS_FIRST;
 | 
	
		
			
				|  |  | -import static cn.newfeifan.mall.module.system.enums.ErrorCodeConstants.SOCIAL_USER_NOT_FOUND;
 | 
	
		
			
				|  |  | +import static cn.newfeifan.mall.module.system.enums.ErrorCodeConstants.*;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /**
 | 
	
		
			
				|  |  |   * 社交用户 Service 实现类
 | 
	
	
		
			
				|  | @@ -120,9 +119,9 @@ public class SocialUserServiceImpl implements SocialUserService {
 | 
	
		
			
				|  |  |                  socialUserBind != null ? socialUserBind.getUserId() : null);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  | -    public SocialUserRespDTO getFirstSocialUserByCode(Integer userType, Integer socialType, String code, String state) {
 | 
	
		
			
				|  |  | +    public SocialUserRespDTO getSocialUserByCode(Integer userType, Integer socialType, String code, String state, Boolean isFirst) {
 | 
	
		
			
				|  |  |          // 获得社交用户
 | 
	
		
			
				|  |  | -        SocialUserDO socialUser = firstAuthSocialUser(socialType, userType, code, state);
 | 
	
		
			
				|  |  | +        SocialUserDO socialUser = authSocialUser(socialType, userType, code, state,isFirst);
 | 
	
		
			
				|  |  |          Assert.notNull(socialUser, "社交用户不能为空");
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          // 获得绑定用户
 | 
	
	
		
			
				|  | @@ -132,6 +131,10 @@ public class SocialUserServiceImpl implements SocialUserService {
 | 
	
		
			
				|  |  |                  socialUserBind != null ? socialUserBind.getUserId() : null);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    public SocialUserDO authSocialUser(Integer socialType, Integer userType, String code, String state) {
 | 
	
		
			
				|  |  | +        return this.authSocialUser(socialType, userType, code, state, false);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * 授权获得对应的社交用户
 | 
	
		
			
				|  |  |       * 如果授权失败,则会抛出 {@link ServiceException} 异常
 | 
	
	
		
			
				|  | @@ -143,7 +146,7 @@ public class SocialUserServiceImpl implements SocialUserService {
 | 
	
		
			
				|  |  |       * @return 授权用户
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      @NotNull
 | 
	
		
			
				|  |  | -    public SocialUserDO authSocialUser(Integer socialType, Integer userType, String code, String state) {
 | 
	
		
			
				|  |  | +    public SocialUserDO authSocialUser(Integer socialType, Integer userType, String code, String state, boolean isFirst) {
 | 
	
		
			
				|  |  |          // 优先从 DB 中获取,因为 code 有且可以使用一次。
 | 
	
		
			
				|  |  |          // 在社交登录时,当未绑定 User 时,需要绑定登录,此时需要 code 使用两次
 | 
	
		
			
				|  |  |          SocialUserDO socialUser = socialUserMapper.selectByTypeAndCodeAnState(socialType, code, state);
 | 
	
	
		
			
				|  | @@ -157,6 +160,9 @@ public class SocialUserServiceImpl implements SocialUserService {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          // 保存到 DB 中
 | 
	
		
			
				|  |  |          socialUser = socialUserMapper.selectByTypeAndOpenid(socialType, authUser.getUuid());
 | 
	
		
			
				|  |  | +        if (isFirst && socialUser == null) {
 | 
	
		
			
				|  |  | +            throw exception(AUTH_LOGIN_USER_IS_FIRST);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |          if (socialUser == null) {
 | 
	
		
			
				|  |  |              socialUser = new SocialUserDO();
 | 
	
		
			
				|  |  |          }
 | 
	
	
		
			
				|  | @@ -171,23 +177,6 @@ public class SocialUserServiceImpl implements SocialUserService {
 | 
	
		
			
				|  |  |          return socialUser;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 调用authSocialUser方法做一个中转判断
 | 
	
		
			
				|  |  | -     * @param socialType 社交平台的类型 {@link SocialTypeEnum}
 | 
	
		
			
				|  |  | -     * @param userType 用户类型
 | 
	
		
			
				|  |  | -     * @param code     授权码
 | 
	
		
			
				|  |  | -     * @param state    state
 | 
	
		
			
				|  |  | -     * @return 授权用户
 | 
	
		
			
				|  |  | -     */
 | 
	
		
			
				|  |  | -    public SocialUserDO firstAuthSocialUser(Integer socialType, Integer userType, String code, String state) {
 | 
	
		
			
				|  |  | -        // 优先从 DB 中获取,因为 code 有且可以使用一次。
 | 
	
		
			
				|  |  | -        SocialUserDO socialUser = socialUserMapper.selectByTypeAndCodeAnState(socialType, code, state);
 | 
	
		
			
				|  |  | -        if(socialUser == null){
 | 
	
		
			
				|  |  | -            throw exception(AUTH_LOGIN_USER_IS_FIRST);
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        return authSocialUser(socialType, userType, code, state);
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      // ==================== 社交用户 CRUD ====================
 | 
	
		
			
				|  |  |  
 |