liujc 2 years ago
parent
commit
dfbd918900

+ 124 - 120
mec-auth/mec-auth-server/src/main/java/com/ym/mec/auth/core/provider/PhoneAuthenticationProvider.java

@@ -29,66 +29,65 @@ import com.ym.mec.common.service.IdGeneratorService;
 
 public class PhoneAuthenticationProvider extends AbstractAuthenticationProvider {
 
-	private UserDetailsService userDetailsService;
+    private UserDetailsService userDetailsService;
 
-	private IdGeneratorService smsCodeService;
+    private IdGeneratorService smsCodeService;
 
-	private SysUserService sysUserService;
-	
-	private SysUserDeviceService sysUserDeviceService;
+    private SysUserService sysUserService;
 
-	private RedisCache<String,Object> redisCache;
+    private SysUserDeviceService sysUserDeviceService;
 
-	@Override
-	protected void additionalAuthenticationChecks(UserDetails userDetails, Authentication authentication) throws AuthenticationException {
+    private RedisCache<String,Object> redisCache;
 
-		if (authentication.getCredentials() == null) {
-			throw new BadCredentialsException(this.messages.getMessage("PhoneAuthenticationProvider.badCredentials", "Bad credentials"));
-		}
-	}
+    @Override
+    protected void additionalAuthenticationChecks(UserDetails userDetails, Authentication authentication) throws AuthenticationException {
 
-	@Override
-	@Transactional(rollbackFor = Exception.class)
-	public UserDetails retrieveUser(String username, Authentication authentication) throws AuthenticationException {
-		LoginEntity loginEntity = (LoginEntity) authentication.getCredentials();
-		if (loginEntity == null) {
-			throw new BadCredentialsException("Bad credentials");
-		}
-		String smsCode = loginEntity.getSmsCode();
-		String phone = loginEntity.getPhone();
-		String clientId = loginEntity.getClientId();
+        if (authentication.getCredentials() == null) {
+            throw new BadCredentialsException(this.messages.getMessage("PhoneAuthenticationProvider.badCredentials", "Bad credentials"));
+        }
+    }
 
-		// 验证码验证
-		if (!clientId.startsWith("QR_") && !smsCodeService.verifyValidCode(phone, smsCode)) {
-			throw new BadCredentialsException("验证码校验失败");
-		}
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    protected UserDetails retrieveUser(String username, Authentication authentication) throws AuthenticationException {
+        LoginEntity loginEntity = (LoginEntity) authentication.getCredentials();
+        if (loginEntity == null) {
+            throw new BadCredentialsException("Bad credentials");
+        }
+        String smsCode = loginEntity.getSmsCode();
+        String phone = loginEntity.getPhone();
+        String clientId = loginEntity.getClientId();
 
+        // 验证码验证
+        if (!clientId.startsWith("QR_") && !smsCodeService.verifyValidCode(phone, smsCode)) {
+            throw new BadCredentialsException("验证码校验失败");
+        }
 
-		Boolean isRegister = loginEntity.getIsRegister();
-		
-		String deviceNum = loginEntity.getDeviceNum();
 
+        Boolean isRegister = loginEntity.getIsRegister();
 
+        String deviceNum = loginEntity.getDeviceNum();
 
-		SysUserInfo userInfo;
 
-		if (clientId.startsWith("QR_")) {
-			Object data = redisCache.get(loginEntity.getPhone());
-			if (data == null) {
-				throw new LockedException("用户不存在");
-			} else {
-				redisCache.delete(loginEntity.getPhone());
-				QRLoginDto loginDto = (QRLoginDto) data;
-				if (loginDto.getPrivateKey().equals(loginEntity.getSmsCode())) {
-					userInfo = loginDto.getUserInfo();
-					username = username.replaceAll(loginDto.getCode(),userInfo.getSysUser().getPhone());
-				} else {
-					throw new LockedException("用户不存在");
-				}
-			}
-		} else {
-			userInfo = sysUserService.queryUserInfoByPhone(phone);
-		}
+        SysUserInfo userInfo;
+
+        if (clientId.startsWith("QR_")) {
+            Object data = redisCache.get(loginEntity.getPhone());
+            if (data == null) {
+                throw new LockedException("用户不存在");
+            } else {
+                redisCache.delete(loginEntity.getPhone());
+                QRLoginDto loginDto = (QRLoginDto) data;
+                if (loginDto.getPrivateKey().equals(loginEntity.getSmsCode())) {
+                    userInfo = loginDto.getUserInfo();
+                    username = username.replaceAll(loginDto.getCode(),userInfo.getSysUser().getPhone());
+                } else {
+                    throw new LockedException("用户不存在");
+                }
+            }
+        } else {
+            userInfo = sysUserService.queryUserInfoByPhone(phone);
+        }
 
 		if (userInfo == null) {
 
@@ -100,9 +99,9 @@ public class PhoneAuthenticationProvider extends AbstractAuthenticationProvider
 			if (isRegister == false || StringUtils.equals("SYSTEM", clientId)) {
 				throw new LockedException("用户不存在");
 			}
-			
+
 			userInfo = sysUserService.initUser(loginEntity.getTenantId(), loginEntity.getOrganId(), loginEntity.getPhone(), clientId);
-			
+
 			if (StringUtils.isNotBlank(deviceNum)) {
 				sysUserDeviceService.bindDevice(clientId, userInfo.getSysUser().getId(), deviceNum, userInfo.getSysUser().getTenantId());
 			}
@@ -135,75 +134,80 @@ public class PhoneAuthenticationProvider extends AbstractAuthenticationProvider
 //					!loginEntity.getTenantId().equals(user.getTenantId())){
 //				throw new LockedException("用户已注册");
 //			}
-			if (StringUtils.isNotBlank(deviceNum)) {
-				sysUserDeviceService.bindDevice(clientId, user.getId(), deviceNum, userInfo.getSysUser().getTenantId());
-			}
-
-			if (clientId.startsWith("QR_")) {
-			} else  if (!userInfo.getSysUser().getUserType().contains(clientId)) {
-				if (isRegister == false || StringUtils.equals("SYSTEM", clientId)) {
-					throw new LockedException("用户不存在");
-				} else {
-					user.setUserType(user.getUserType() + "," + clientId);
-					user.setUpdateTime(new Date());
-
-					// 添加userType以及附加信息
-					if (StringUtils.equals("STUDENT", clientId)) {
-						user.setOrganId(Integer.parseInt(loginEntity.getOrganId()));
-						sysUserService.saveStudent(user.getId(),loginEntity.getTenantId());
-					} else if (StringUtils.equals("TEACHER", clientId)) {
-						sysUserService.saveTeacher(user.getId(),loginEntity.getTenantId());
-					}
-					sysUserService.update(user);
-				}
-			}
-		}
-
-		UserDetails loadedUser;
-		try {
-			loadedUser = userDetailsService.loadUserByUsername(username);
-		} catch (UsernameNotFoundException e) {
-			throw e;
-		} catch (Exception e) {
-			throw new InternalAuthenticationServiceException(e.getMessage(), e);
-		}
-
-		if (loadedUser == null) {
-			throw new InternalAuthenticationServiceException("账户不存在");
-		} else {
-			return loadedUser;
-		}
-	}
-
-	@Override
-	protected Authentication createSuccessAuthentication(Object principal, Authentication authentication, UserDetails user) {
-		PhoneAuthenticationToken result = new PhoneAuthenticationToken(principal, authentication.getCredentials(), user.getAuthorities());
-		result.setDetails(authentication.getDetails());
-		return result;
-	}
-
-	@Override
-	public boolean supports(Class<?> aClass) {
-		return PhoneAuthenticationToken.class.isAssignableFrom(aClass);
-	}
-
-	public void setUserDetailsService(UserDetailsService userDetailsService) {
-		this.userDetailsService = userDetailsService;
-	}
-
-	public void setRedisCache(RedisCache<String, Object> redisCache) {
-		this.redisCache = redisCache;
-	}
-
-	public void setSysUserService(SysUserService sysUserService) {
-		this.sysUserService = sysUserService;
-	}
-
-	public void setSmsCodeService(IdGeneratorService smsCodeService) {
-		this.smsCodeService = smsCodeService;
-	}
-
-	public void setSysUserDeviceService(SysUserDeviceService sysUserDeviceService) {
-		this.sysUserDeviceService = sysUserDeviceService;
-	}
+            if (StringUtils.isNotBlank(deviceNum)) {
+                sysUserDeviceService.bindDevice(clientId, user.getId(), deviceNum, userInfo.getSysUser().getTenantId());
+            }
+
+            if (clientId.startsWith("QR_")) {
+            } else  if (!userInfo.getSysUser().getUserType().contains(clientId)) {
+                if (isRegister == false || StringUtils.equals("SYSTEM", clientId)) {
+                    throw new LockedException("用户不存在");
+                } else {
+                    user.setUserType(user.getUserType() + "," + clientId);
+                    user.setUpdateTime(new Date());
+
+                    // 添加userType以及附加信息
+                    if (StringUtils.equals("STUDENT", clientId)) {
+                        if (StringUtils.isBlank(loginEntity.getOrganId())) {
+                            user.setOrganId(sysUserService.getLesseeOrganId());
+                            loginEntity.setTenantId(1);
+                        } else {
+                            user.setOrganId(Integer.parseInt(loginEntity.getOrganId()));
+                        }
+                        sysUserService.saveStudent(user.getId(),loginEntity.getTenantId());
+                    } else if (StringUtils.equals("TEACHER", clientId)) {
+                        sysUserService.saveTeacher(user.getId(),loginEntity.getTenantId());
+                    }
+                    sysUserService.update(user);
+                }
+            }
+        }
+
+        UserDetails loadedUser;
+        try {
+            loadedUser = userDetailsService.loadUserByUsername(username);
+        } catch (UsernameNotFoundException e) {
+            throw e;
+        } catch (Exception e) {
+            throw new InternalAuthenticationServiceException(e.getMessage(), e);
+        }
+
+        if (loadedUser == null) {
+            throw new InternalAuthenticationServiceException("账户不存在");
+        } else {
+            return loadedUser;
+        }
+    }
+
+    @Override
+    protected Authentication createSuccessAuthentication(Object principal, Authentication authentication, UserDetails user) {
+        PhoneAuthenticationToken result = new PhoneAuthenticationToken(principal, authentication.getCredentials(), user.getAuthorities());
+        result.setDetails(authentication.getDetails());
+        return result;
+    }
+
+    @Override
+    public boolean supports(Class<?> aClass) {
+        return PhoneAuthenticationToken.class.isAssignableFrom(aClass);
+    }
+
+    public void setUserDetailsService(UserDetailsService userDetailsService) {
+        this.userDetailsService = userDetailsService;
+    }
+
+    public void setRedisCache(RedisCache<String, Object> redisCache) {
+        this.redisCache = redisCache;
+    }
+
+    public void setSysUserService(SysUserService sysUserService) {
+        this.sysUserService = sysUserService;
+    }
+
+    public void setSmsCodeService(IdGeneratorService smsCodeService) {
+        this.smsCodeService = smsCodeService;
+    }
+
+    public void setSysUserDeviceService(SysUserDeviceService sysUserDeviceService) {
+        this.sysUserDeviceService = sysUserDeviceService;
+    }
 }

+ 11 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/entity/ImLiveBroadcastRoomMember.java

@@ -51,6 +51,9 @@ public class ImLiveBroadcastRoomMember implements Serializable {
     @ApiModelProperty(value = "禁言状态:0取消;1禁言")
     private Integer banStatus;
 
+    @TableField("microphone_flag_")
+    @ApiModelProperty(value = "是否能上麦:0否;1是")
+    private Boolean microphoneFlag;
     @TableField("live_room_status_")
     @ApiModelProperty(value = "直播状态: 0离开;1观看")
     private Integer liveRoomStatus;
@@ -74,6 +77,14 @@ public class ImLiveBroadcastRoomMember implements Serializable {
 
     private static final long serialVersionUID = 1L;
 
+    public Boolean getMicrophoneFlag() {
+        return microphoneFlag;
+    }
+
+    public void setMicrophoneFlag(Boolean microphoneFlag) {
+        this.microphoneFlag = microphoneFlag;
+    }
+
     public Integer getWhetherMicStatus() {
         return whetherMicStatus;
     }

+ 3 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/vo/ImLiveBroadcastRoomMemberVo.java

@@ -61,6 +61,9 @@ public class ImLiveBroadcastRoomMemberVo implements java.io.Serializable {
     @ApiModelProperty("游客凭据")
     private String fingerprint;
 
+    @ApiModelProperty(value = "是否能上麦:false否;true是")
+    private boolean microphoneFlag;
+
     public ImLiveBroadcastRoomSimpleMemberVo getSimpleMemberVo(ImLiveBroadcastRoomMemberVo memberVo) {
         return JSON.parseObject(JSON.toJSONString(memberVo), ImLiveBroadcastRoomSimpleMemberVo.class);
     }

+ 2 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/ImLiveBroadcastRoomService.java

@@ -87,6 +87,8 @@ public interface ImLiveBroadcastRoomService extends IService<ImLiveBroadcastRoom
 
     void joinRoom(String roomUid, Integer userId);
 
+    void joinRoom(String roomUid, Integer userId, Boolean microphoneFlag);
+
     /**
      * 游客加入直播间
      * @param roomUid 直播间编号

+ 18 - 3
mec-biz/src/main/java/com/ym/mec/biz/service/impl/ImLiveBroadcastRoomServiceImpl.java

@@ -1429,6 +1429,12 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
      * @param userId  用户id
      */
     public void joinRoom(String roomUid, Integer userId) {
+        joinRoom(roomUid, userId, true);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void joinRoom(String roomUid, Integer userId,Boolean microphoneFlag) {
         //查询房间信息
         ImLiveBroadcastRoomVo imLiveBroadcastRoomVo = getImLiveBroadcastRoomVo(roomUid);
         if (Objects.isNull(imLiveBroadcastRoomVo)) {
@@ -1448,7 +1454,7 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
         }
 
         // 加入直播间用户信息
-        joinRoomUserInfo(roomUid, userId, imLiveBroadcastRoomVo);
+        joinRoomUserInfo(roomUid, userId, imLiveBroadcastRoomVo,microphoneFlag);
     }
 
     /**
@@ -1458,6 +1464,9 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
      * @param imLiveBroadcastRoomVo ImLiveBroadcastRoomVo
      */
     private void joinRoomUserInfo(String roomUid, Integer userId, ImLiveBroadcastRoomVo imLiveBroadcastRoomVo) {
+        joinRoomUserInfo(roomUid, userId, imLiveBroadcastRoomVo,true);
+    }
+    private void joinRoomUserInfo(String roomUid, Integer userId, ImLiveBroadcastRoomVo imLiveBroadcastRoomVo,Boolean microphoneFlag) {
 
         //记录用户当前房间uid
         redissonClient.getBucket(LIVE_USER_ROOM.replace(USER_ID, userId.toString())).set(roomUid, 12L, TimeUnit.HOURS);
@@ -1480,13 +1489,14 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
             if (Objects.isNull(liveRoomMember)) {
 
                 // 初次进入房间
-                getLiveRoomUserInfo(userId, imLiveBroadcastRoomVo);
+                getLiveRoomUserInfo(userId, imLiveBroadcastRoomVo, microphoneFlag);
             } else {
                 // 更新直播间用户信息
                 ImLiveBroadcastRoomMember roomMember = new ImLiveBroadcastRoomMember();
                 roomMember.setId(liveRoomMember.getId());
                 roomMember.setOnlineStatus(1);
                 roomMember.setLiveRoomStatus(1);
+                roomMember.setMicrophoneFlag(microphoneFlag);
 
                 liveBroadcastRoomMemberService.updateById(roomMember);
             }
@@ -1538,7 +1548,7 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
         ImLiveBroadcastRoomVo roomVo = getVisitorLiveBroadcastRoomVo(roomUid);
 
         // 加入直播间用户信息
-        joinRoomUserInfo(roomUid, userId, roomVo);
+        joinRoomUserInfo(roomUid, userId, roomVo,false);
     }
 
     /**
@@ -1842,6 +1852,10 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
      */
     @NotNull
     private RoomUserInfoVo getLiveRoomUserInfo(Integer userId, ImLiveBroadcastRoomVo imLiveBroadcastRoomVo) {
+        return getLiveRoomUserInfo(userId, imLiveBroadcastRoomVo,true);
+
+    }
+    private RoomUserInfoVo getLiveRoomUserInfo(Integer userId, ImLiveBroadcastRoomVo imLiveBroadcastRoomVo,Boolean microphoneFlag) {
 
         RoomUserInfoVo userInfo = getUserInfo(userId);
         userInfo.setFirstJoinTime(DateTime.now().toDate());
@@ -1857,6 +1871,7 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
         roomMember.setOnlineStatus(1);
         roomMember.setBanStatus(0);
         roomMember.setLiveRoomStatus(1);
+        roomMember.setMicrophoneFlag(microphoneFlag);
 
         // 保存直播间用户信息
         liveBroadcastRoomMemberService.save(roomMember);

+ 2 - 1
mec-biz/src/main/resources/config/mybatis/ImLiveBroadcastRoomMemberMapper.xml

@@ -36,7 +36,8 @@
             su.organ_id_ AS organId,
             a.join_time_ as joinTime,
             a.total_time_ as totalViewTime,
-            if(ib.user_id_ is null, 0, 1) as blackFlag
+            if(ib.user_id_ is null, 0, 1) as blackFlag,
+            a.microphone_flag_ as microphoneFlag
         from
             im_live_broadcast_room_member as a
             left join sys_user as su on su.id_ = a.user_id_

+ 5 - 2
mec-student/src/main/java/com/ym/mec/student/controller/ImLiveBroadcastRoomController.java

@@ -56,8 +56,11 @@ public class ImLiveBroadcastRoomController extends BaseController {
 
     @ApiOperation("学生-进入房间")
     @GetMapping("/joinRoom")
-    public HttpResponseResult<Object> joinRoom(String roomUid, Integer userId) {
-        imLiveBroadcastRoomService.joinRoom(roomUid, userId);
+    public HttpResponseResult<Object> joinRoom(String roomUid, Integer userId,Boolean microphoneFlag) {
+        if (microphoneFlag == null) {
+            microphoneFlag = true;
+        }
+        imLiveBroadcastRoomService.joinRoom(roomUid, userId,microphoneFlag);
         return succeed();
     }