mirror of
https://github.com/zongzibinbin/MallChat.git
synced 2026-03-14 06:03:42 +08:00
Merge remote-tracking branch 'origin/main' into chat
# Conflicts: # mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/msg/TextMsgHandler.java
This commit is contained in:
@@ -6,6 +6,8 @@ import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
@@ -20,12 +22,15 @@ import java.io.Serializable;
|
||||
public class FileMsgDTO implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
@ApiModelProperty("大小(字节)")
|
||||
@NotNull
|
||||
private Long size;
|
||||
|
||||
@ApiModelProperty("下载地址")
|
||||
@NotBlank
|
||||
private String url;
|
||||
|
||||
@ApiModelProperty("文件名(带后缀)")
|
||||
@NotBlank
|
||||
private String fileName;
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
@@ -20,11 +22,14 @@ import java.io.Serializable;
|
||||
public class SoundMsgDTO implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
@ApiModelProperty("大小(字节)")
|
||||
@NotNull
|
||||
private Long size;
|
||||
|
||||
@ApiModelProperty("时长(秒)")
|
||||
@NotNull
|
||||
private Integer second;
|
||||
|
||||
@ApiModelProperty("下载地址")
|
||||
@NotBlank
|
||||
private String url;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import lombok.NoArgsConstructor;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* Description: 视频消息入参
|
||||
@@ -23,20 +22,23 @@ import java.math.BigDecimal;
|
||||
public class VideoMsgDTO implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
@ApiModelProperty("大小(字节)")
|
||||
@NotNull
|
||||
private Long size;
|
||||
|
||||
@ApiModelProperty("下载地址")
|
||||
@NotBlank
|
||||
private String url;
|
||||
|
||||
@ApiModelProperty("缩略图宽度(像素)")
|
||||
@NotNull
|
||||
private Integer thumbWidth = BigDecimal.ROUND_HALF_DOWN;
|
||||
private Integer thumbWidth;
|
||||
|
||||
@ApiModelProperty("缩略图高度(像素)")
|
||||
@NotNull
|
||||
private Integer thumbHeight;
|
||||
|
||||
@ApiModelProperty("缩略图大小(字节)")
|
||||
@NotNull
|
||||
private Long thumbSize;
|
||||
|
||||
@ApiModelProperty("缩略图下载地址")
|
||||
|
||||
@@ -22,7 +22,7 @@ public class GlobalExceptionHandler {
|
||||
e.getBindingResult().getFieldErrors().forEach(x -> errorMsg.append(x.getField()).append(x.getDefaultMessage()).append(","));
|
||||
String message = errorMsg.toString();
|
||||
log.info("validation parameters error!The reason is:{}", message);
|
||||
return ApiResult.fail(-1, message.substring(0, message.length() - 1));
|
||||
return ApiResult.fail(CommonErrorEnum.PARAM_VALID.getErrorCode(), message.substring(0, message.length() - 1));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,7 +34,7 @@ public class GlobalExceptionHandler {
|
||||
e.getBindingResult().getFieldErrors().forEach(x -> errorMsg.append(x.getField()).append(x.getDefaultMessage()).append(","));
|
||||
String message = errorMsg.toString();
|
||||
log.info("validation parameters error!The reason is:{}", message);
|
||||
return ApiResult.fail(-1, message.substring(0, message.length() - 1));
|
||||
return ApiResult.fail(CommonErrorEnum.PARAM_VALID.getErrorCode(), message.substring(0, message.length() - 1));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,16 +3,83 @@ package com.abin.mallchat.common.common.utils;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.abin.mallchat.common.common.exception.BusinessErrorEnum;
|
||||
import com.abin.mallchat.common.common.exception.BusinessException;
|
||||
import com.abin.mallchat.common.common.exception.CommonErrorEnum;
|
||||
import com.abin.mallchat.common.common.exception.ErrorEnum;
|
||||
import org.hibernate.validator.HibernateValidator;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 校验工具类
|
||||
*/
|
||||
public class AssertUtil {
|
||||
|
||||
/**
|
||||
* 校验到失败就结束
|
||||
*/
|
||||
private static Validator failFastValidator = Validation.byProvider(HibernateValidator.class)
|
||||
.configure()
|
||||
.failFast(true)
|
||||
.buildValidatorFactory().getValidator();
|
||||
|
||||
/**
|
||||
* 全部校验
|
||||
*/
|
||||
private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
|
||||
/**
|
||||
* 注解验证参数(校验到失败就结束)
|
||||
* @param obj
|
||||
*/
|
||||
public static <T> void fastFailValidate(T obj) {
|
||||
Set<ConstraintViolation<T>> constraintViolations = failFastValidator.validate(obj);
|
||||
if (constraintViolations.size() > 0) {
|
||||
throwException(CommonErrorEnum.PARAM_VALID,constraintViolations.iterator().next().getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注解验证参数(全部校验,抛出异常)
|
||||
* @param obj
|
||||
*/
|
||||
public static <T> void allCheckValidateThrow(T obj) {
|
||||
Set<ConstraintViolation<T>> constraintViolations = validator.validate(obj);
|
||||
if (constraintViolations.size() > 0) {
|
||||
StringBuilder errorMsg = new StringBuilder();
|
||||
Iterator<ConstraintViolation<T>> iterator = constraintViolations.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
ConstraintViolation<T> violation = iterator.next();
|
||||
//拼接异常信息
|
||||
errorMsg.append(violation.getPropertyPath().toString()).append(":").append(violation.getMessage()).append(",");
|
||||
}
|
||||
//去掉最后一个逗号
|
||||
throwException(CommonErrorEnum.PARAM_VALID, errorMsg.toString().substring(0, errorMsg.length() - 1));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 注解验证参数(全部校验,返回异常信息集合)
|
||||
* @param obj
|
||||
*/
|
||||
public static <T> Map<String,String> allCheckValidate(T obj) {
|
||||
Set<ConstraintViolation<T>> constraintViolations = validator.validate(obj);
|
||||
if (constraintViolations.size() > 0) {
|
||||
Map<String,String> errorMessages= new HashMap<>();
|
||||
Iterator<ConstraintViolation<T>> iterator = constraintViolations.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
ConstraintViolation<T> violation = iterator.next();
|
||||
errorMessages.put(violation.getPropertyPath().toString(),violation.getMessage());
|
||||
}
|
||||
return errorMessages;
|
||||
}
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
//如果不是true,则抛异常
|
||||
public static void isTrue(boolean expression, String msg) {
|
||||
if (!expression) {
|
||||
|
||||
@@ -6,6 +6,8 @@ import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Size;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -19,6 +21,8 @@ import java.util.List;
|
||||
@NoArgsConstructor
|
||||
public class TextMsgReq {
|
||||
|
||||
@NotBlank(message = "内容不能为空")
|
||||
@Size(max = 1024, message = "消息内容过长,服务器扛不住啊,兄dei")
|
||||
@ApiModelProperty("消息内容")
|
||||
private String content;
|
||||
|
||||
@@ -26,5 +30,6 @@ public class TextMsgReq {
|
||||
private Long replyMsgId;
|
||||
|
||||
@ApiModelProperty("艾特的uid")
|
||||
@Size(max = 10, message = "一次别艾特这么多人")
|
||||
private List<Long> atUidList;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.abin.mallchat.common.chat.domain.entity.Message;
|
||||
import com.abin.mallchat.common.chat.domain.entity.msg.FileMsgDTO;
|
||||
import com.abin.mallchat.common.chat.domain.entity.msg.MessageExtra;
|
||||
import com.abin.mallchat.common.chat.domain.enums.MessageTypeEnum;
|
||||
import com.abin.mallchat.common.common.utils.AssertUtil;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.ChatMessageReq;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -29,6 +30,8 @@ public class FileMsgHandler extends AbstractMsgHandler {
|
||||
|
||||
@Override
|
||||
public void checkMsg(ChatMessageReq request, Long uid) {
|
||||
FileMsgDTO body = BeanUtil.toBean(request.getBody(), FileMsgDTO.class);
|
||||
AssertUtil.allCheckValidateThrow(body);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.abin.mallchat.common.chat.domain.entity.Message;
|
||||
import com.abin.mallchat.common.chat.domain.entity.msg.ImgMsgDTO;
|
||||
import com.abin.mallchat.common.chat.domain.entity.msg.MessageExtra;
|
||||
import com.abin.mallchat.common.chat.domain.enums.MessageTypeEnum;
|
||||
import com.abin.mallchat.common.common.utils.AssertUtil;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.ChatMessageReq;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -29,6 +30,8 @@ public class ImgMsgHandler extends AbstractMsgHandler {
|
||||
|
||||
@Override
|
||||
public void checkMsg(ChatMessageReq request, Long uid) {
|
||||
ImgMsgDTO body = BeanUtil.toBean(request.getBody(), ImgMsgDTO.class);
|
||||
AssertUtil.allCheckValidateThrow(body);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.abin.mallchat.common.chat.domain.entity.Message;
|
||||
import com.abin.mallchat.common.chat.domain.entity.msg.MessageExtra;
|
||||
import com.abin.mallchat.common.chat.domain.entity.msg.SoundMsgDTO;
|
||||
import com.abin.mallchat.common.chat.domain.enums.MessageTypeEnum;
|
||||
import com.abin.mallchat.common.common.utils.AssertUtil;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.ChatMessageReq;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -29,6 +30,8 @@ public class SoundMsgHandler extends AbstractMsgHandler {
|
||||
|
||||
@Override
|
||||
public void checkMsg(ChatMessageReq request, Long uid) {
|
||||
SoundMsgDTO body = BeanUtil.toBean(request.getBody(), SoundMsgDTO.class);
|
||||
AssertUtil.allCheckValidateThrow(body);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -44,7 +47,7 @@ public class SoundMsgHandler extends AbstractMsgHandler {
|
||||
|
||||
@Override
|
||||
public Object showMsg(Message msg) {
|
||||
return msg.getExtra().getFileMsg();
|
||||
return msg.getExtra().getSoundMsgDTO();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -57,8 +57,7 @@ public class TextMsgHandler extends AbstractMsgHandler {
|
||||
@Override
|
||||
public void checkMsg(ChatMessageReq request, Long uid) {
|
||||
TextMsgReq body = BeanUtil.toBean(request.getBody(), TextMsgReq.class);
|
||||
AssertUtil.isNotEmpty(body.getContent(), "内容不能为空");
|
||||
AssertUtil.isTrue(body.getContent().length() < 500, "消息内容过长,服务器扛不住啊,兄dei");
|
||||
AssertUtil.allCheckValidateThrow(body);
|
||||
//校验下回复消息
|
||||
if (Objects.nonNull(body.getReplyMsgId())) {
|
||||
Message replyMsg = messageDao.getById(body.getReplyMsgId());
|
||||
@@ -66,7 +65,6 @@ public class TextMsgHandler extends AbstractMsgHandler {
|
||||
AssertUtil.equal(replyMsg.getRoomId(), request.getRoomId(), "只能回复相同会话内的消息");
|
||||
}
|
||||
if (CollectionUtil.isNotEmpty(body.getAtUidList())) {
|
||||
AssertUtil.isFalse(body.getAtUidList().size() > 10, "一次别艾特这么多人");
|
||||
List<Long> atUidList = body.getAtUidList();
|
||||
Map<Long, User> batch = userInfoCache.getBatch(atUidList);
|
||||
AssertUtil.equal(atUidList.size(), batch.values().size(), "@用户不存在");
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.abin.mallchat.common.chat.domain.entity.Message;
|
||||
import com.abin.mallchat.common.chat.domain.entity.msg.MessageExtra;
|
||||
import com.abin.mallchat.common.chat.domain.entity.msg.VideoMsgDTO;
|
||||
import com.abin.mallchat.common.chat.domain.enums.MessageTypeEnum;
|
||||
import com.abin.mallchat.common.common.utils.AssertUtil;
|
||||
import com.abin.mallchat.custom.chat.domain.vo.request.ChatMessageReq;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -29,6 +30,8 @@ public class VideoMsgHandler extends AbstractMsgHandler {
|
||||
|
||||
@Override
|
||||
public void checkMsg(ChatMessageReq request, Long uid) {
|
||||
VideoMsgDTO body = BeanUtil.toBean(request.getBody(), VideoMsgDTO.class);
|
||||
AssertUtil.allCheckValidateThrow(body);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user