diff --git a/docs/mallchat.sql b/docs/mallchat.sql index 944a4eb..d747885 100644 --- a/docs/mallchat.sql +++ b/docs/mallchat.sql @@ -21,9 +21,10 @@ CREATE TABLE `item_config` ( -- Records of item_config -- ---------------------------- INSERT INTO `item_config` VALUES (1, 1, NULL, '用户可以使用改名卡,更改自己的名字。mallchat名称全局唯一,快抢订你的专属昵称吧', '2023-03-25 22:27:30.511', '2023-03-25 22:27:30.511'); -INSERT INTO `item_config` VALUES (2, 2, 'https://cdn-icons-png.flaticon.com/512/10667/10667168.png ', '爆赞徽章,单条消息被点赞超过10次,即可获得', '2023-05-07 17:50:31.090', '2023-05-07 18:12:05.824'); +INSERT INTO `item_config` VALUES (2, 2, 'https://cdn-icons-png.flaticon.com/128/1533/1533913.png', '爆赞徽章,单条消息被点赞超过10次,即可获得', '2023-05-07 17:50:31.090', '2023-05-07 18:12:05.824'); INSERT INTO `item_config` VALUES (3, 2, 'https://cdn-icons-png.flaticon.com/512/6198/6198527.png ', '抹茶聊天前10名注册的用户才能获得的专属徽章', '2023-05-07 17:50:31.100', '2023-05-07 18:12:01.448'); INSERT INTO `item_config` VALUES (4, 2, 'https://cdn-icons-png.flaticon.com/512/10232/10232583.png', '抹茶聊天前100名注册的用户才能获得的专属徽章', '2023-05-07 17:50:31.109', '2023-05-07 17:56:36.059'); +INSERT INTO `item_config` VALUES (5, 2, 'https://cdn-icons-png.flaticon.com/128/2909/2909937.png', '抹茶知识星球成员的专属徽章', '2023-05-07 17:50:31.109', '2023-05-07 17:56:36.059'); -- ---------------------------- -- Table structure for message diff --git a/mallchat-common/pom.xml b/mallchat-common/pom.xml index 2a9d84a..aefa9d6 100644 --- a/mallchat-common/pom.xml +++ b/mallchat-common/pom.xml @@ -93,9 +93,8 @@ 2.0.9 - org.hibernate.validator - hibernate-validator - 6.0.1.Final + org.springframework.boot + spring-boot-starter-validation org.springframework.boot diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/GlobalExceptionHandler.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/GlobalExceptionHandler.java index 01c841d..66e0b70 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/GlobalExceptionHandler.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/GlobalExceptionHandler.java @@ -2,6 +2,7 @@ package com.abin.mallchat.common.common.exception; import com.abin.mallchat.common.common.domain.vo.response.ApiResult; import lombok.extern.slf4j.Slf4j; +import org.springframework.validation.BindException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; @@ -23,6 +24,18 @@ public class GlobalExceptionHandler { return ApiResult.fail(-1, message.substring(0, message.length() - 1)); } + /** + * validation参数校验异常 + */ + @ExceptionHandler(value = BindException.class) + public ApiResult bindException(BindException e) { + StringBuilder errorMsg = new StringBuilder(); + 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)); + } + /** * 处理空指针的异常 */ diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/user/dao/UserBackpackDao.java b/mallchat-common/src/main/java/com/abin/mallchat/common/user/dao/UserBackpackDao.java index 22d7827..0bcd4ab 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/user/dao/UserBackpackDao.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/user/dao/UserBackpackDao.java @@ -22,11 +22,10 @@ import java.util.List; public class UserBackpackDao extends ServiceImpl { public Integer getCountByValidItemId(Long uid, Long itemId) { - LambdaQueryWrapper wrapper = new QueryWrapper().lambda() - .eq(UserBackpack::getUid, uid) + return lambdaQuery().eq(UserBackpack::getUid, uid) .eq(UserBackpack::getItemId, itemId) - .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus()); - return count(wrapper); + .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus()) + .count(); } public UserBackpack getFirstValidItem(Long uid, Long itemId) { diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/user/domain/enums/ItemEnum.java b/mallchat-common/src/main/java/com/abin/mallchat/common/user/domain/enums/ItemEnum.java index 5966fce..7428cfe 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/user/domain/enums/ItemEnum.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/user/domain/enums/ItemEnum.java @@ -20,6 +20,7 @@ public enum ItemEnum { LIKE_BADGE(2L, ItemTypeEnum.BADGE, "爆赞徽章"), REG_TOP10_BADGE(3L, ItemTypeEnum.BADGE, "前十注册徽章"), REG_TOP100_BADGE(4L, ItemTypeEnum.BADGE, "前100注册徽章"), + PLANET(5L, ItemTypeEnum.BADGE, "知识星球"), ; private final Long id; diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/user/service/impl/UserBackpackServiceImpl.java b/mallchat-common/src/main/java/com/abin/mallchat/common/user/service/impl/UserBackpackServiceImpl.java index b9b1a69..ae05046 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/user/service/impl/UserBackpackServiceImpl.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/user/service/impl/UserBackpackServiceImpl.java @@ -35,11 +35,18 @@ public class UserBackpackServiceImpl implements IUserBackpackService { private ItemCache itemCache; @Autowired private ApplicationEventPublisher applicationEventPublisher; + @Autowired + private UserBackpackServiceImpl userBackpackService; @Override - @RedissonLock(key = "#uid", waitTime = 5000)//相同用户会同时发奖,需要排队不能直接拒绝 public void acquireItem(Long uid, Long itemId, IdempotentEnum idempotentEnum, String businessId) { + //组装幂等号 String idempotent = getIdempotent(itemId, idempotentEnum, businessId); + userBackpackService.doAcquireItem(uid, itemId, idempotent); + } + + @RedissonLock(key = "#idempotent", waitTime = 5000)//相同幂等如果同时发奖,需要排队等上一个执行完,取出之前数据返回 + public void doAcquireItem(Long uid, Long itemId, String idempotent) { UserBackpack userBackpack = userBackpackDao.getByIdp(idempotent); //幂等检查 if (Objects.nonNull(userBackpack)) { diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/controller/ChatController.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/controller/ChatController.java index ce9c756..1bcb118 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/controller/ChatController.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/controller/ChatController.java @@ -44,13 +44,13 @@ public class ChatController { @GetMapping("/public/room/page") @ApiOperation("会话列表") - public ApiResult> getRoomPage(CursorPageBaseReq request) { + public ApiResult> getRoomPage(@Valid CursorPageBaseReq request) { return ApiResult.success(chatService.getRoomPage(request, RequestHolder.get().getUid())); } @GetMapping("/public/member/page") @ApiOperation("群成员列表") - public ApiResult> getMemberPage(CursorPageBaseReq request) { + public ApiResult> getMemberPage(@Valid CursorPageBaseReq request) { CursorPageBaseResp memberPage = chatService.getMemberPage(request); filterBlackMember(memberPage); return ApiResult.success(memberPage); @@ -72,7 +72,7 @@ public class ChatController { @GetMapping("/public/msg/page") @ApiOperation("消息列表") - public ApiResult> getMsgPage(ChatMessagePageReq request) { + public ApiResult> getMsgPage(@Valid ChatMessagePageReq request) { CursorPageBaseResp msgPage = chatService.getMsgPage(request, RequestHolder.get().getUid()); filterBlackMsg(msgPage); return ApiResult.success(msgPage); diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/AbstractMsgMarkStrategy.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/AbstractMsgMarkStrategy.java index 17cf032..847eccb 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/AbstractMsgMarkStrategy.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/AbstractMsgMarkStrategy.java @@ -31,12 +31,12 @@ public abstract class AbstractMsgMarkStrategy { @Transactional public void mark(Long uid, Long msgId) { - exec(uid, msgId, MessageMarkActTypeEnum.MARK); + doMark(uid, msgId); } @Transactional public void unMark(Long uid, Long msgId) { - exec(uid, msgId, MessageMarkActTypeEnum.UN_MARK); + doUnMark(uid, msgId); } @PostConstruct @@ -44,6 +44,14 @@ public abstract class AbstractMsgMarkStrategy { MsgMarkFactory.register(getTypeEnum().getType(), this); } + protected void doMark(Long uid, Long msgId) { + exec(uid, msgId, MessageMarkActTypeEnum.MARK); + } + + protected void doUnMark(Long uid, Long msgId) { + exec(uid, msgId, MessageMarkActTypeEnum.UN_MARK); + } + protected void exec(Long uid, Long msgId, MessageMarkActTypeEnum actTypeEnum) { Integer markType = getTypeEnum().getType(); Integer actType = actTypeEnum.getType(); diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/DisLikeStrategy.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/DisLikeStrategy.java index e6ccd05..5b13d99 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/DisLikeStrategy.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/DisLikeStrategy.java @@ -22,7 +22,7 @@ public class DisLikeStrategy extends AbstractMsgMarkStrategy { } @Override - public void mark(Long uid, Long msgId) { + public void doMark(Long uid, Long msgId) { super.mark(uid, msgId); //同时取消点赞的动作 MsgMarkFactory.getStrategyNoNull(MessageMarkTypeEnum.LIKE.getType()).unMark(uid, msgId); diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/LikeStrategy.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/LikeStrategy.java index 4f86696..90c4427 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/LikeStrategy.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/strategy/mark/LikeStrategy.java @@ -17,7 +17,7 @@ public class LikeStrategy extends AbstractMsgMarkStrategy { } @Override - public void mark(Long uid, Long msgId) { + public void doMark(Long uid, Long msgId) { super.mark(uid, msgId); //同时取消点踩的动作 MsgMarkFactory.getStrategyNoNull(MessageMarkTypeEnum.DISLIKE.getType()).unMark(uid, msgId); diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServer.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServer.java index f609c69..c639dbf 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServer.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/websocket/NettyWebSocketServer.java @@ -14,6 +14,7 @@ import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.netty.handler.stream.ChunkedWriteHandler; +import io.netty.handler.timeout.IdleStateHandler; import io.netty.util.concurrent.Future; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Configuration; @@ -65,7 +66,7 @@ public class NettyWebSocketServer { protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline pipeline = socketChannel.pipeline(); //30秒客户端没有向服务器发送心跳则关闭连接 -// pipeline.addLast(new IdleStateHandler(30, 0, 0)); + pipeline.addLast(new IdleStateHandler(30, 0, 0)); // 因为使用http协议,所以需要使用http的编码器,解码器 pipeline.addLast(new HttpServerCodec()); // 以块方式写,添加 chunkedWriter 处理器