mirror of
https://github.com/zongzibinbin/MallChat.git
synced 2026-04-05 23:27:33 +00:00
项目基础搭建
This commit is contained in:
114
mallchat-common/pom.xml
Normal file
114
mallchat-common/pom.xml
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>mallchat</artifactId>
|
||||||
|
<groupId>com.abin.mallchat</groupId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>mallchat-common</artifactId>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.swagger</groupId>
|
||||||
|
<artifactId>swagger-annotations</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- MyBatis-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis</groupId>
|
||||||
|
<artifactId>mybatis</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!--代码生成器-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-generator</artifactId>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<artifactId>mybatis-plus-extension</artifactId>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.velocity</groupId>
|
||||||
|
<artifactId>velocity-engine-core</artifactId>
|
||||||
|
<version>2.0</version>
|
||||||
|
</dependency>
|
||||||
|
<!--Mysql数据库驱动-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!--JWT(Json Web Token)登录支持-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- 阿里云OSS -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun.oss</groupId>
|
||||||
|
<artifactId>aliyun-sdk-oss</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- netty -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.netty</groupId>
|
||||||
|
<artifactId>netty-all</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.binarywang</groupId>
|
||||||
|
<artifactId>weixin-java-mp</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.xiaoymin</groupId>
|
||||||
|
<!--使用Swagger2-->
|
||||||
|
<artifactId>knife4j-spring-boot-starter</artifactId>
|
||||||
|
<version>2.0.9</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate</groupId>
|
||||||
|
<artifactId>hibernate-validator</artifactId>
|
||||||
|
<version>6.0.1.Final</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.auth0</groupId>
|
||||||
|
<artifactId>java-jwt</artifactId>
|
||||||
|
<version>3.19.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||||
|
<artifactId>caffeine</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package com.abin.mallchat.common.chat.dao;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.abin.mallchat.common.chat.domain.entity.Message;
|
||||||
|
import com.abin.mallchat.common.chat.domain.enums.MessageStatusEnum;
|
||||||
|
import com.abin.mallchat.common.chat.mapper.MessageMapper;
|
||||||
|
import com.abin.mallchat.common.common.domain.vo.request.CursorPageBaseReq;
|
||||||
|
import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp;
|
||||||
|
import com.abin.mallchat.common.common.utils.CursorUtils;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 消息表 服务实现类
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-03-25
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class MessageDao extends ServiceImpl<MessageMapper, Message> {
|
||||||
|
@Autowired
|
||||||
|
private CursorUtils cursorUtils;
|
||||||
|
|
||||||
|
public CursorPageBaseResp<Message> getCursorPage(Long roomId, CursorPageBaseReq request) {
|
||||||
|
return cursorUtils.getCursorPageByMysql(this, request, wrapper -> {
|
||||||
|
wrapper.eq(Message::getRoomId, roomId);
|
||||||
|
wrapper.eq(Message::getStatus, MessageStatusEnum.NORMAL.getStatus());
|
||||||
|
}, Message::getId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 乐观更新消息类型
|
||||||
|
*/
|
||||||
|
public boolean riseOptimistic(Long id, Integer oldType, Integer newType) {
|
||||||
|
return lambdaUpdate()
|
||||||
|
.eq(Message::getId, id)
|
||||||
|
.eq(Message::getType, oldType)
|
||||||
|
.set(Message::getType, newType)
|
||||||
|
.update();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getGapCount(Long roomId, Long fromId, Long toId) {
|
||||||
|
return lambdaQuery()
|
||||||
|
.eq(Message::getRoomId, roomId)
|
||||||
|
.gt(Message::getId, fromId)
|
||||||
|
.le(Message::getId, toId)
|
||||||
|
.count();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateGapCount(Long id, Integer gapCount) {
|
||||||
|
lambdaUpdate()
|
||||||
|
.eq(Message::getId, id)
|
||||||
|
.set(Message::getGapCount, gapCount);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package com.abin.mallchat.common.chat.dao;
|
||||||
|
|
||||||
|
import com.abin.mallchat.common.chat.domain.entity.MessageMark;
|
||||||
|
import com.abin.mallchat.common.chat.mapper.MessageMarkMapper;
|
||||||
|
import com.abin.mallchat.common.chat.service.IMessageMarkService;
|
||||||
|
import com.abin.mallchat.common.common.domain.enums.YesOrNoEnum;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 消息标记表 服务实现类
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-04-08
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class MessageMarkDao extends ServiceImpl<MessageMarkMapper, MessageMark> {
|
||||||
|
|
||||||
|
public MessageMark get(Long uid, Long msgId, Integer markType) {
|
||||||
|
return lambdaQuery().eq(MessageMark::getUid, uid)
|
||||||
|
.eq(MessageMark::getMsgId, msgId)
|
||||||
|
.eq(MessageMark::getType, markType)
|
||||||
|
.one();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getMarkCount(Long msgId, Integer markType) {
|
||||||
|
return lambdaQuery().eq(MessageMark::getMsgId, msgId)
|
||||||
|
.eq(MessageMark::getType, markType)
|
||||||
|
.eq(MessageMark::getStatus, YesOrNoEnum.NO.getStatus())
|
||||||
|
.count();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<MessageMark> getValidMarkByMsgIdBatch(List<Long> msgIds) {
|
||||||
|
return lambdaQuery()
|
||||||
|
.in(MessageMark::getMsgId, msgIds)
|
||||||
|
.eq(MessageMark::getStatus, YesOrNoEnum.NO.getStatus())
|
||||||
|
.list();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.abin.mallchat.common.chat.dao;
|
||||||
|
|
||||||
|
import com.abin.mallchat.common.chat.domain.entity.Message;
|
||||||
|
import com.abin.mallchat.common.chat.domain.entity.Room;
|
||||||
|
import com.abin.mallchat.common.chat.domain.enums.MessageStatusEnum;
|
||||||
|
import com.abin.mallchat.common.chat.domain.enums.RoomTypeEnum;
|
||||||
|
import com.abin.mallchat.common.chat.mapper.RoomMapper;
|
||||||
|
import com.abin.mallchat.common.common.domain.vo.request.CursorPageBaseReq;
|
||||||
|
import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp;
|
||||||
|
import com.abin.mallchat.common.common.utils.CursorUtils;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 会话表 服务实现类
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-03-25
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class RoomDao extends ServiceImpl<RoomMapper, Room> {
|
||||||
|
@Autowired
|
||||||
|
private CursorUtils cursorUtils;
|
||||||
|
|
||||||
|
public CursorPageBaseResp<Room> getCursorPage(CursorPageBaseReq request) {
|
||||||
|
return cursorUtils.getCursorPageByMysql(this, request, wrapper -> {
|
||||||
|
wrapper.ne(Room::getType, RoomTypeEnum.GROUP.getStatus());
|
||||||
|
}, Room::getActiveTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
package com.abin.mallchat.common.chat.domain.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 消息表
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-03-25
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = false)
|
||||||
|
@TableName("message")
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class Message implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* id
|
||||||
|
*/
|
||||||
|
@TableId(value = "id", type = IdType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 会话表id
|
||||||
|
*/
|
||||||
|
@TableField("room_id")
|
||||||
|
private Long roomId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息发送者uid
|
||||||
|
*/
|
||||||
|
@TableField("from_uid")
|
||||||
|
private Long fromUid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息内容
|
||||||
|
*/
|
||||||
|
@TableField("content")
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 回复的消息内容
|
||||||
|
*/
|
||||||
|
@TableField("reply_msg_id")
|
||||||
|
private Long replyMsgId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息状态 0正常 1删除
|
||||||
|
* @see com.abin.mallchat.common.chat.domain.enums.MessageStatusEnum
|
||||||
|
*/
|
||||||
|
@TableField("status")
|
||||||
|
private Integer status;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 与回复消息的间隔条数
|
||||||
|
*/
|
||||||
|
@TableField("gap_count")
|
||||||
|
private Integer gapCount;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息类型 1正常文本 2.爆赞 (点赞超过10)3.危险发言(举报超5)
|
||||||
|
* @see com.abin.mallchat.common.chat.domain.enums.MessageTypeEnum
|
||||||
|
*/
|
||||||
|
@TableField("type")
|
||||||
|
private Integer type;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建时间
|
||||||
|
*/
|
||||||
|
@TableField("create_time")
|
||||||
|
private Date createTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改时间
|
||||||
|
*/
|
||||||
|
@TableField("update_time")
|
||||||
|
private Date updateTime;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
package com.abin.mallchat.common.chat.domain.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 消息标记表
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-04-08
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = false)
|
||||||
|
@TableName("message_mark")
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class MessageMark implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* id
|
||||||
|
*/
|
||||||
|
@TableId(value = "id", type = IdType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息表id
|
||||||
|
*/
|
||||||
|
@TableField("msg_id")
|
||||||
|
private Long msgId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 标记人uid
|
||||||
|
*/
|
||||||
|
@TableField("uid")
|
||||||
|
private Long uid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 标记类型 1点赞 2举报
|
||||||
|
* @see com.abin.mallchat.common.chat.domain.enums.MessageMarkTypeEnum
|
||||||
|
*/
|
||||||
|
@TableField("type")
|
||||||
|
private Integer type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息状态 0正常 1取消
|
||||||
|
*/
|
||||||
|
@TableField("status")
|
||||||
|
private Integer status;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建时间
|
||||||
|
*/
|
||||||
|
@TableField("create_time")
|
||||||
|
private LocalDateTime createTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改时间
|
||||||
|
*/
|
||||||
|
@TableField("update_time")
|
||||||
|
private LocalDateTime updateTime;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package com.abin.mallchat.common.chat.domain.entity;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 会话表
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-03-25
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = false)
|
||||||
|
@TableName("room")
|
||||||
|
public class Room implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* id
|
||||||
|
*/
|
||||||
|
@TableId(value = "id", type = IdType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 会话名
|
||||||
|
*/
|
||||||
|
@TableField("name")
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 会话类型 1大群聊 2沸点
|
||||||
|
* @see com.abin.mallchat.common.chat.domain.enums.RoomTypeEnum
|
||||||
|
*/
|
||||||
|
@TableField("type")
|
||||||
|
private Integer type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 最后活跃时间-排序
|
||||||
|
*/
|
||||||
|
@TableField("active_time")
|
||||||
|
private Date activeTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建时间
|
||||||
|
*/
|
||||||
|
@TableField("create_time")
|
||||||
|
private Date createTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改时间
|
||||||
|
*/
|
||||||
|
@TableField("update_time")
|
||||||
|
private Date updateTime;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package com.abin.mallchat.common.chat.domain.enums;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description: 消息标记类型
|
||||||
|
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* Date: 2023-03-19
|
||||||
|
*/
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Getter
|
||||||
|
public enum MessageMarkTypeEnum {
|
||||||
|
LIKE(1, "点赞",10,MessageTypeEnum.LIKE),
|
||||||
|
DISLIKE(2, "点踩",5,MessageTypeEnum.DISLIKE),
|
||||||
|
;
|
||||||
|
|
||||||
|
private final Integer type;
|
||||||
|
private final String desc;
|
||||||
|
private final Integer riseNum;//需要多少个标记升级
|
||||||
|
private final MessageTypeEnum riseEnum;//消息升级成什么类型的消息
|
||||||
|
|
||||||
|
private static Map<Integer, MessageMarkTypeEnum> cache;
|
||||||
|
|
||||||
|
static {
|
||||||
|
cache = Arrays.stream(MessageMarkTypeEnum.values()).collect(Collectors.toMap(MessageMarkTypeEnum::getType, Function.identity()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MessageMarkTypeEnum of(Integer type) {
|
||||||
|
return cache.get(type);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package com.abin.mallchat.common.chat.domain.enums;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description: 消息状态
|
||||||
|
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* Date: 2023-03-19
|
||||||
|
*/
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Getter
|
||||||
|
public enum MessageStatusEnum {
|
||||||
|
NORMAL(0, "正常"),
|
||||||
|
DELETE(1, "删除"),
|
||||||
|
;
|
||||||
|
|
||||||
|
private final Integer status;
|
||||||
|
private final String desc;
|
||||||
|
|
||||||
|
private static Map<Integer, MessageStatusEnum> cache;
|
||||||
|
|
||||||
|
static {
|
||||||
|
cache = Arrays.stream(MessageStatusEnum.values()).collect(Collectors.toMap(MessageStatusEnum::getStatus, Function.identity()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MessageStatusEnum of(Integer type) {
|
||||||
|
return cache.get(type);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.abin.mallchat.common.chat.domain.enums;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description: 消息状态
|
||||||
|
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* Date: 2023-03-19
|
||||||
|
*/
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Getter
|
||||||
|
public enum MessageTypeEnum {
|
||||||
|
NORMAL(1, "正常"),
|
||||||
|
LIKE(2, "爆赞"),
|
||||||
|
DISLIKE(3, "危险发言"),
|
||||||
|
;
|
||||||
|
|
||||||
|
private final Integer type;
|
||||||
|
private final String desc;
|
||||||
|
|
||||||
|
private static Map<Integer, MessageTypeEnum> cache;
|
||||||
|
|
||||||
|
static {
|
||||||
|
cache = Arrays.stream(MessageTypeEnum.values()).collect(Collectors.toMap(MessageTypeEnum::getType, Function.identity()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MessageTypeEnum of(Integer type) {
|
||||||
|
return cache.get(type);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package com.abin.mallchat.common.chat.domain.enums;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description: 消息状态
|
||||||
|
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* Date: 2023-03-19
|
||||||
|
*/
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Getter
|
||||||
|
public enum RoomTypeEnum {
|
||||||
|
GROUP(1, "大群聊"),
|
||||||
|
BOILING(2, "沸点"),
|
||||||
|
;
|
||||||
|
|
||||||
|
private final Integer status;
|
||||||
|
private final String desc;
|
||||||
|
|
||||||
|
private static Map<Integer, RoomTypeEnum> cache;
|
||||||
|
|
||||||
|
static {
|
||||||
|
cache = Arrays.stream(RoomTypeEnum.values()).collect(Collectors.toMap(RoomTypeEnum::getStatus, Function.identity()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RoomTypeEnum of(Integer type) {
|
||||||
|
return cache.get(type);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.abin.mallchat.common.chat.mapper;
|
||||||
|
|
||||||
|
import com.abin.mallchat.common.chat.domain.entity.Message;
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 消息表 Mapper 接口
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-03-25
|
||||||
|
*/
|
||||||
|
public interface MessageMapper extends BaseMapper<Message> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.abin.mallchat.common.chat.mapper;
|
||||||
|
|
||||||
|
import com.abin.mallchat.common.chat.domain.entity.MessageMark;
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 消息标记表 Mapper 接口
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-04-08
|
||||||
|
*/
|
||||||
|
public interface MessageMarkMapper extends BaseMapper<MessageMark> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.abin.mallchat.common.chat.mapper;
|
||||||
|
|
||||||
|
|
||||||
|
import com.abin.mallchat.common.chat.domain.entity.Room;
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 会话表 Mapper 接口
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-03-25
|
||||||
|
*/
|
||||||
|
public interface RoomMapper extends BaseMapper<Room> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.abin.mallchat.common.chat.service;
|
||||||
|
|
||||||
|
import com.abin.mallchat.common.chat.domain.entity.MessageMark;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 消息标记表 服务类
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-04-08
|
||||||
|
*/
|
||||||
|
public interface IMessageMarkService extends IService<MessageMark> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.abin.mallchat.common.chat.service;
|
||||||
|
|
||||||
|
import com.abin.mallchat.common.chat.domain.entity.Message;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 消息表 服务类
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-03-25
|
||||||
|
*/
|
||||||
|
public interface IMessageService extends IService<Message> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.abin.mallchat.common.chat.service;
|
||||||
|
|
||||||
|
import com.abin.mallchat.common.chat.domain.entity.Room;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 会话表 服务类
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-03-25
|
||||||
|
*/
|
||||||
|
public interface IRoomService extends IService<Room> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package com.abin.mallchat.common.common.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 频控注解
|
||||||
|
*/
|
||||||
|
@Repeatable(FrequencyControlContainer.class)//可重复
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)//运行时生效
|
||||||
|
@Target(ElementType.METHOD)//作用在方法上
|
||||||
|
public @interface FrequencyControl {
|
||||||
|
/**
|
||||||
|
* key的前缀,默认取方法全限定名,除非我们在不同方法上对同一个资源做频控,就自己指定
|
||||||
|
*
|
||||||
|
* @return key的前缀
|
||||||
|
*/
|
||||||
|
String prefixKey() default "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 频控对象,默认el表达指定具体的频控对象
|
||||||
|
* 对于ip 和uid模式,需要是http入口的对象,保证RequestHolder里有值
|
||||||
|
*
|
||||||
|
* @return 对象
|
||||||
|
*/
|
||||||
|
Target target() default Target.EL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* springEl 表达式,target=EL必填
|
||||||
|
*
|
||||||
|
* @return 表达式
|
||||||
|
*/
|
||||||
|
String spEl() default "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 频控时间范围,默认单位秒
|
||||||
|
*
|
||||||
|
* @return 时间范围
|
||||||
|
*/
|
||||||
|
int time();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 频控时间单位,默认秒
|
||||||
|
*
|
||||||
|
* @return 单位
|
||||||
|
*/
|
||||||
|
TimeUnit unit() default TimeUnit.SECONDS;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单位时间内最大访问次数
|
||||||
|
*
|
||||||
|
* @return 次数
|
||||||
|
*/
|
||||||
|
int count();
|
||||||
|
|
||||||
|
enum Target {
|
||||||
|
UID, IP, EL
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.abin.mallchat.common.common.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)//运行时生效
|
||||||
|
@Target(ElementType.METHOD)//作用在方法上
|
||||||
|
public @interface FrequencyControlContainer {
|
||||||
|
FrequencyControl[] value();
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
package com.abin.mallchat.common.common.aspect;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.abin.mallchat.common.common.annotation.FrequencyControl;
|
||||||
|
import com.abin.mallchat.common.common.exception.BusinessException;
|
||||||
|
import com.abin.mallchat.common.common.exception.CommonErrorEnum;
|
||||||
|
import com.abin.mallchat.common.common.utils.RedisUtils;
|
||||||
|
import com.abin.mallchat.common.common.utils.RequestHolder;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.aspectj.lang.reflect.MethodSignature;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.core.DefaultParameterNameDiscoverer;
|
||||||
|
import org.springframework.expression.EvaluationContext;
|
||||||
|
import org.springframework.expression.Expression;
|
||||||
|
import org.springframework.expression.ExpressionParser;
|
||||||
|
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||||
|
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description: 频控实现
|
||||||
|
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* Date: 2023-04-20
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
public class FrequencyControlAspect {
|
||||||
|
@Autowired
|
||||||
|
private RedisUtils redisUtils;
|
||||||
|
private final ExpressionParser parser = new SpelExpressionParser();
|
||||||
|
private final DefaultParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
|
||||||
|
|
||||||
|
@Around("@annotation(com.abin.mallchat.common.common.annotation.FrequencyControl)||@annotation(com.abin.mallchat.common.common.annotation.FrequencyControlContainer)")
|
||||||
|
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||||
|
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
|
||||||
|
FrequencyControl[] annotationsByType = method.getAnnotationsByType(FrequencyControl.class);
|
||||||
|
Map<String, FrequencyControl> keyMap = new HashMap<>();
|
||||||
|
for (int i = 0; i < annotationsByType.length; i++) {
|
||||||
|
FrequencyControl frequencyControl = annotationsByType[i];
|
||||||
|
String prefix = StrUtil.isBlank(frequencyControl.prefixKey()) ? method.toGenericString() + ":index:" + i : frequencyControl.prefixKey();//默认方法限定名+注解排名(可能多个)
|
||||||
|
String key = "";
|
||||||
|
switch (frequencyControl.target()) {
|
||||||
|
case EL:
|
||||||
|
key = parseSpEl(method, joinPoint.getArgs(), frequencyControl.spEl());
|
||||||
|
break;
|
||||||
|
case IP:
|
||||||
|
key = RequestHolder.get().getIp();
|
||||||
|
break;
|
||||||
|
case UID:
|
||||||
|
key = RequestHolder.get().getUid().toString();
|
||||||
|
}
|
||||||
|
keyMap.put(prefix + ":" + key, frequencyControl);
|
||||||
|
}
|
||||||
|
//批量获取redis统计的值
|
||||||
|
ArrayList<String> keyList = new ArrayList<>(keyMap.keySet());
|
||||||
|
List<Integer> countList = redisUtils.mget(keyList, Integer.class);
|
||||||
|
for (int i = 0; i < keyList.size(); i++) {
|
||||||
|
String key = keyList.get(i);
|
||||||
|
Integer count = countList.get(i);
|
||||||
|
FrequencyControl frequencyControl = keyMap.get(key);
|
||||||
|
if (Objects.nonNull(count) && count >= frequencyControl.count()) {//频率超过了
|
||||||
|
log.warn("frequencyControl limit key:{},count:{}", key, count);
|
||||||
|
throw new BusinessException(CommonErrorEnum.FREQUENCY_LIMIT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return joinPoint.proceed();
|
||||||
|
} finally {
|
||||||
|
//不管成功还是失败,都增加次数
|
||||||
|
keyMap.forEach((k, v) -> {
|
||||||
|
redisUtils.inc(k,v.time(),v.unit());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String parseSpEl(Method method, Object[] args, String spEl) {
|
||||||
|
String[] params = parameterNameDiscoverer.getParameterNames(method);//解析参数名
|
||||||
|
EvaluationContext context = new StandardEvaluationContext();//el解析需要的上下文对象
|
||||||
|
for (int i = 0; i < params.length; i++) {
|
||||||
|
context.setVariable(params[i], args[i]);//所有参数都作为原材料扔进去
|
||||||
|
}
|
||||||
|
Expression expression = parser.parseExpression(spEl);
|
||||||
|
return expression.getValue(context, String.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.abin.mallchat.common.common.config;
|
||||||
|
|
||||||
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
|
import org.springframework.cache.CacheManager;
|
||||||
|
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
||||||
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
|
import org.springframework.cache.caffeine.CaffeineCacheManager;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@EnableCaching
|
||||||
|
@Configuration
|
||||||
|
public class CacheConfig extends CachingConfigurerSupport {
|
||||||
|
|
||||||
|
@Bean("caffeineCacheManager")
|
||||||
|
@Primary
|
||||||
|
public CacheManager caffeineCacheManager() {
|
||||||
|
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
|
||||||
|
// 方案一(常用):定制化缓存Cache
|
||||||
|
cacheManager.setCaffeine(Caffeine.newBuilder()
|
||||||
|
.expireAfterWrite(10, TimeUnit.MINUTES)
|
||||||
|
.initialCapacity(100)
|
||||||
|
.maximumSize(200));
|
||||||
|
return cacheManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.abin.mallchat.common.common.config;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.DbType;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class MybatisPlusConfig {
|
||||||
|
/**
|
||||||
|
* 新增分页拦截器,并设置数据库类型为mysql
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||||
|
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||||
|
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||||
|
return interceptor;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package com.abin.mallchat.common.common.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
||||||
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||||
|
import org.springframework.data.redis.serializer.SerializationException;
|
||||||
|
import org.springframework.data.redis.serializer.SerializationUtils;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class RedisConfig {
|
||||||
|
@Bean
|
||||||
|
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||||
|
// 创建模板
|
||||||
|
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
||||||
|
// 设置连接工厂
|
||||||
|
redisTemplate.setConnectionFactory(redisConnectionFactory);
|
||||||
|
// 设置序列化工具
|
||||||
|
MyRedisSerializerCustomized jsonRedisSerializer =
|
||||||
|
new MyRedisSerializerCustomized();
|
||||||
|
// key和 hashKey采用 string序列化
|
||||||
|
redisTemplate.setKeySerializer(RedisSerializer.string());
|
||||||
|
redisTemplate.setHashKeySerializer(RedisSerializer.string());
|
||||||
|
// value和 hashValue采用 JSON序列化
|
||||||
|
redisTemplate.setValueSerializer(jsonRedisSerializer);
|
||||||
|
redisTemplate.setHashValueSerializer(jsonRedisSerializer);
|
||||||
|
return redisTemplate;
|
||||||
|
}
|
||||||
|
public class MyRedisSerializerCustomized extends GenericJackson2JsonRedisSerializer {
|
||||||
|
@Override
|
||||||
|
public byte[] serialize(Object source) throws SerializationException {
|
||||||
|
if (Objects.nonNull(source)) {
|
||||||
|
if (source instanceof String || source instanceof Character) {
|
||||||
|
return source.toString().getBytes();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return super.serialize(source);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public <T> T deserialize(byte[] source, Class<T> type) throws SerializationException {
|
||||||
|
Assert.notNull(type,
|
||||||
|
"Deserialization type must not be null! Please provide Object.class to make use of Jackson2 default typing.");
|
||||||
|
if (source == null || source.length == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (type.isInstance(String.class) || type.isInstance(Character.class)) {
|
||||||
|
return (T) new String(source);
|
||||||
|
}
|
||||||
|
return super.deserialize(source, type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package com.abin.mallchat.common.common.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.scheduling.annotation.AsyncConfigurer;
|
||||||
|
import org.springframework.scheduling.annotation.EnableAsync;
|
||||||
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description: 线程池配置
|
||||||
|
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* Date: 2023-04-09
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@EnableAsync
|
||||||
|
public class ThreadPoolConfig implements AsyncConfigurer {
|
||||||
|
/**
|
||||||
|
* 项目共用线程池
|
||||||
|
*/
|
||||||
|
public static final String MALLCHAT_EXECUTOR = "mallchatExecutor";
|
||||||
|
/**
|
||||||
|
* websocket通信线程池
|
||||||
|
*/
|
||||||
|
public static final String WS_EXECUTOR = "websocketExecutor";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Executor getAsyncExecutor() {
|
||||||
|
return mallchatExecutor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(MALLCHAT_EXECUTOR)
|
||||||
|
@Primary
|
||||||
|
public ThreadPoolTaskExecutor mallchatExecutor() {
|
||||||
|
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||||
|
executor.setCorePoolSize(10);
|
||||||
|
executor.setMaxPoolSize(10);
|
||||||
|
executor.setQueueCapacity(200);
|
||||||
|
executor.setThreadNamePrefix("mallchat-executor-");
|
||||||
|
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());//满了调用线程执行,认为重要任务
|
||||||
|
executor.initialize();
|
||||||
|
return executor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(WS_EXECUTOR)
|
||||||
|
public ThreadPoolTaskExecutor websocketExecutor() {
|
||||||
|
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||||
|
executor.setCorePoolSize(16);
|
||||||
|
executor.setMaxPoolSize(16);
|
||||||
|
executor.setQueueCapacity(1000);//支持同时推送1000人
|
||||||
|
executor.setThreadNamePrefix("websocket-executor-");
|
||||||
|
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());//满了直接丢弃,默认为不重要消息推送
|
||||||
|
executor.initialize();
|
||||||
|
return executor;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.abin.mallchat.common.common.constant;
|
||||||
|
|
||||||
|
public interface MDCKey {
|
||||||
|
String TID = "tid";
|
||||||
|
String UID = "uid";
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.abin.mallchat.common.common.constant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhongzb create on 2021/06/10
|
||||||
|
*/
|
||||||
|
public class RedisKey {
|
||||||
|
private static final String BASE_KEY = "mallchat:";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在线用户列表
|
||||||
|
*/
|
||||||
|
public static final String ONLINE_UID_ZET = "online";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 离线用户列表
|
||||||
|
*/
|
||||||
|
public static final String OFFLINE_UID_ZET = "offline";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户信息
|
||||||
|
*/
|
||||||
|
public static final String USER_INFO_STRING = "userInfo:uid_%d";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户token存放
|
||||||
|
*/
|
||||||
|
public static final String USER_TOKEN_STRING = "userToken:uid_%d";
|
||||||
|
|
||||||
|
public static String getKey(String key, Object... objects) {
|
||||||
|
return BASE_KEY + String.format(key, objects);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.abin.mallchat.common.common.domain.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description: web请求信息收集类
|
||||||
|
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* Date: 2023-04-05
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class RequestInfo {
|
||||||
|
private Long uid;
|
||||||
|
private String ip;
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package com.abin.mallchat.common.common.domain.enums;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description: 幂等类型
|
||||||
|
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* Date: 2023-03-19
|
||||||
|
*/
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Getter
|
||||||
|
public enum IdempotentEnum {
|
||||||
|
UID(1, "uid"),
|
||||||
|
MSG_ID(2, "消息id"),
|
||||||
|
;
|
||||||
|
|
||||||
|
private final Integer type;
|
||||||
|
private final String desc;
|
||||||
|
|
||||||
|
private static Map<Integer, IdempotentEnum> cache;
|
||||||
|
|
||||||
|
static {
|
||||||
|
cache = Arrays.stream(IdempotentEnum.values()).collect(Collectors.toMap(IdempotentEnum::getType, Function.identity()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IdempotentEnum of(Integer type) {
|
||||||
|
return cache.get(type);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package com.abin.mallchat.common.common.domain.enums;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description: ws前端请求类型枚举
|
||||||
|
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* Date: 2023-03-19
|
||||||
|
*/
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Getter
|
||||||
|
public enum YesOrNoEnum {
|
||||||
|
NO(0, "否"),
|
||||||
|
YES(1, "是"),
|
||||||
|
;
|
||||||
|
|
||||||
|
private final Integer status;
|
||||||
|
private final String desc;
|
||||||
|
|
||||||
|
private static Map<Integer, YesOrNoEnum> cache;
|
||||||
|
|
||||||
|
static {
|
||||||
|
cache = Arrays.stream(YesOrNoEnum.values()).collect(Collectors.toMap(YesOrNoEnum::getStatus, Function.identity()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static YesOrNoEnum of(Integer type) {
|
||||||
|
return cache.get(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Integer toStatus(Boolean bool){
|
||||||
|
return bool?YES.getStatus():NO.getStatus();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.abin.mallchat.common.common.domain.vo.request;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.baomidou.mybatisplus.generator.config.querys.XuguQuery;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-03-19
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ApiModel("游标翻页请求")
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class CursorPageBaseReq {
|
||||||
|
|
||||||
|
@ApiModelProperty("页面大小")
|
||||||
|
private Integer pageSize = 10;
|
||||||
|
|
||||||
|
@ApiModelProperty("游标(初始为null,后续请求附带上次翻页的游标)")
|
||||||
|
private String cursor;
|
||||||
|
|
||||||
|
public Page plusPage() {
|
||||||
|
return new Page(1, this.pageSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonIgnore
|
||||||
|
public Boolean isFirstPage() {
|
||||||
|
return Objects.isNull(cursor);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.abin.mallchat.common.common.domain.vo.request;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-03-19
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ApiModel("基础翻页请求")
|
||||||
|
public class PageBaseReq{
|
||||||
|
|
||||||
|
@ApiModelProperty("页面大小")
|
||||||
|
private Integer pageSize = 10;
|
||||||
|
|
||||||
|
@ApiModelProperty("页面索引(从1开始)")
|
||||||
|
private Integer pageNo = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取mybatisPlus的page
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Page plusPage() {
|
||||||
|
return new Page(pageNo, pageSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package com.abin.mallchat.common.common.domain.vo.response;
|
||||||
|
|
||||||
|
import com.abin.mallchat.common.common.exception.ErrorEnum;
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description: 通用返回体
|
||||||
|
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* Date: 2023-03-23
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ApiModel("基础返回体")
|
||||||
|
public class ApiResult<T> {
|
||||||
|
@ApiModelProperty("成功标识true or false")
|
||||||
|
private Boolean success;
|
||||||
|
@ApiModelProperty("错误码")
|
||||||
|
private Integer errCode;
|
||||||
|
@ApiModelProperty("错误消息")
|
||||||
|
private String errMsg;
|
||||||
|
@ApiModelProperty("返回对象")
|
||||||
|
private T data;
|
||||||
|
|
||||||
|
public static <T> ApiResult<T> success() {
|
||||||
|
ApiResult<T> result = new ApiResult<T>();
|
||||||
|
result.setData(null);
|
||||||
|
result.setSuccess(Boolean.TRUE);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> ApiResult<T> success(T data) {
|
||||||
|
ApiResult<T> result = new ApiResult<T>();
|
||||||
|
result.setData(data);
|
||||||
|
result.setSuccess(Boolean.TRUE);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> ApiResult<T> fail(Integer code, String msg) {
|
||||||
|
ApiResult<T> result = new ApiResult<T>();
|
||||||
|
result.setSuccess(Boolean.FALSE);
|
||||||
|
result.setErrCode(code);
|
||||||
|
result.setErrMsg(msg);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> ApiResult<T> fail(ErrorEnum errorEnum) {
|
||||||
|
ApiResult<T> result = new ApiResult<T>();
|
||||||
|
result.setSuccess(Boolean.FALSE);
|
||||||
|
result.setErrCode(errorEnum.getErrorCode());
|
||||||
|
result.setErrMsg(errorEnum.getErrorMsg());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSuccess() {
|
||||||
|
return this.success;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package com.abin.mallchat.common.common.domain.vo.response;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
|
import com.abin.mallchat.common.chat.domain.entity.Message;
|
||||||
|
import com.abin.mallchat.common.common.domain.vo.request.CursorPageBaseReq;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-03-19
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ApiModel("游标翻页返回")
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class CursorPageBaseResp<T> {
|
||||||
|
|
||||||
|
@ApiModelProperty("游标(下次翻页带上这参数)")
|
||||||
|
private String cursor;
|
||||||
|
|
||||||
|
@ApiModelProperty("是否最后一页")
|
||||||
|
private Boolean isLast = Boolean.FALSE;
|
||||||
|
|
||||||
|
@ApiModelProperty("数据列表")
|
||||||
|
private List<T> list;
|
||||||
|
|
||||||
|
public static CursorPageBaseResp init(CursorPageBaseResp cursorPage, List list) {
|
||||||
|
CursorPageBaseResp cursorPageBaseResp = new CursorPageBaseResp();
|
||||||
|
cursorPageBaseResp.setIsLast(cursorPage.getIsLast());
|
||||||
|
cursorPageBaseResp.setList(list);
|
||||||
|
cursorPageBaseResp.setCursor(cursorPage.getCursor());
|
||||||
|
return cursorPageBaseResp;
|
||||||
|
}
|
||||||
|
@JsonIgnore
|
||||||
|
public Boolean isEmpty() {
|
||||||
|
return CollectionUtil.isEmpty(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CursorPageBaseResp empty() {
|
||||||
|
CursorPageBaseResp cursorPageBaseResp = new CursorPageBaseResp();
|
||||||
|
cursorPageBaseResp.setIsLast(true);
|
||||||
|
cursorPageBaseResp.setList(new ArrayList());
|
||||||
|
return cursorPageBaseResp;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.abin.mallchat.common.common.domain.vo.response;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zhongzb create on 2021/05/31
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class IdRespVO {
|
||||||
|
@ApiModelProperty("id")
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
public static IdRespVO id(Long id) {
|
||||||
|
IdRespVO idRespVO = new IdRespVO();
|
||||||
|
idRespVO.setId(id);
|
||||||
|
return idRespVO;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
package com.abin.mallchat.common.common.domain.vo.response;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-03-19
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@ApiModel("基础翻页返回")
|
||||||
|
public class PageBaseResp<T> {
|
||||||
|
|
||||||
|
@ApiModelProperty("当前页数")
|
||||||
|
private Integer pageNo;
|
||||||
|
|
||||||
|
@ApiModelProperty("每页查询数量")
|
||||||
|
private Integer pageSize;
|
||||||
|
|
||||||
|
@ApiModelProperty("总记录数")
|
||||||
|
private Long totalRecords;
|
||||||
|
|
||||||
|
@ApiModelProperty("是否最后一页")
|
||||||
|
private Boolean isLast = Boolean.FALSE;
|
||||||
|
|
||||||
|
@ApiModelProperty("数据列表")
|
||||||
|
private List<T> list;
|
||||||
|
|
||||||
|
|
||||||
|
public static PageBaseResp empty() {
|
||||||
|
PageBaseResp r = new PageBaseResp();
|
||||||
|
r.setPageNo(1);
|
||||||
|
r.setPageSize(0);
|
||||||
|
r.setIsLast(true);
|
||||||
|
r.setTotalRecords(0L);
|
||||||
|
r.setList(new ArrayList());
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> PageBaseResp<T> init(Integer pageNo, Integer pageSize, Long totalRecords, Boolean isLast, List<T> list) {
|
||||||
|
return new PageBaseResp<>(pageNo, pageSize, totalRecords, isLast, list);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> PageBaseResp<T> init(Integer pageNo, Integer pageSize, Long totalRecords, List<T> list) {
|
||||||
|
return new PageBaseResp<>(pageNo, pageSize, totalRecords, isLastPage(totalRecords, pageNo, pageSize), list);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> PageBaseResp<T> init(IPage<T> page) {
|
||||||
|
return init((int) page.getCurrent(), (int) page.getSize(), page.getTotal(), page.getRecords());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> PageBaseResp<T> init(IPage page, List<T> list) {
|
||||||
|
return init((int) page.getCurrent(), (int) page.getSize(), page.getTotal(), list);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> PageBaseResp<T> init(PageBaseResp resp, List<T> list) {
|
||||||
|
return init(resp.getPageNo(), resp.getPageSize(), resp.getTotalRecords(), resp.getIsLast(), list);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否是最后一页
|
||||||
|
*/
|
||||||
|
public static Boolean isLastPage(long totalRecords, int pageNo, int pageSize) {
|
||||||
|
if (pageSize == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
long pageTotal = totalRecords / pageSize + (totalRecords % pageSize == 0 ? 0 : 1);
|
||||||
|
return pageNo >= pageTotal ? true : false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.abin.mallchat.common.common.exception;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description: 业务校验异常码
|
||||||
|
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* Date: 2023-03-26
|
||||||
|
*/
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Getter
|
||||||
|
public enum BusinessErrorEnum implements ErrorEnum {
|
||||||
|
//==================================common==================================
|
||||||
|
BUSINESS_ERROR(1001, "{}"),
|
||||||
|
//==================================user==================================
|
||||||
|
//==================================chat==================================
|
||||||
|
SYSTEM_ERROR(1001, "系统出小差了,请稍后再试哦~~"),
|
||||||
|
;
|
||||||
|
private Integer code;
|
||||||
|
private String msg;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer getErrorCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getErrorMsg() {
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package com.abin.mallchat.common.common.exception;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class BusinessException extends RuntimeException {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 错误码
|
||||||
|
*/
|
||||||
|
protected Integer errorCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 错误信息
|
||||||
|
*/
|
||||||
|
protected String errorMsg;
|
||||||
|
|
||||||
|
public BusinessException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public BusinessException(String errorMsg) {
|
||||||
|
super(errorMsg);
|
||||||
|
this.errorMsg = errorMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BusinessException(Integer errorCode, String errorMsg) {
|
||||||
|
super(errorMsg);
|
||||||
|
this.errorCode = errorCode;
|
||||||
|
this.errorMsg = errorMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BusinessException(Integer errorCode, String errorMsg, Throwable cause) {
|
||||||
|
super(errorMsg, cause);
|
||||||
|
this.errorCode = errorCode;
|
||||||
|
this.errorMsg = errorMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BusinessException(ErrorEnum error) {
|
||||||
|
super(error.getErrorMsg());
|
||||||
|
this.errorCode = error.getErrorCode();
|
||||||
|
this.errorMsg = error.getErrorMsg();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMessage() {
|
||||||
|
return errorMsg;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized Throwable fillInStackTrace() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package com.abin.mallchat.common.common.exception;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description: 通用异常码
|
||||||
|
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* Date: 2023-03-26
|
||||||
|
*/
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Getter
|
||||||
|
public enum CommonErrorEnum implements ErrorEnum {
|
||||||
|
|
||||||
|
SYSTEM_ERROR(-1, "系统出小差了,请稍后再试哦~~"),
|
||||||
|
PARAM_VALID(-2, "参数校验失败"),
|
||||||
|
FREQUENCY_LIMIT(-3, "请求太频繁了,请稍后再试哦~~"),
|
||||||
|
;
|
||||||
|
private Integer code;
|
||||||
|
private String msg;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer getErrorCode() {
|
||||||
|
return this.code;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getErrorMsg() {
|
||||||
|
return this.msg;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.abin.mallchat.common.common.exception;
|
||||||
|
|
||||||
|
public interface ErrorEnum {
|
||||||
|
|
||||||
|
Integer getErrorCode();
|
||||||
|
|
||||||
|
String getErrorMsg();
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
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.web.bind.MethodArgumentNotValidException;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
|
||||||
|
@RestControllerAdvice
|
||||||
|
@Slf4j
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* validation参数校验异常
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
||||||
|
public ApiResult methodArgumentNotValidExceptionExceptionHandler(MethodArgumentNotValidException 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));
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 处理空指针的异常
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(value = NullPointerException.class)
|
||||||
|
public ApiResult exceptionHandler( NullPointerException e) {
|
||||||
|
log.error("null point exception!The reason is:{}", e.getMessage(), e);
|
||||||
|
return ApiResult.fail(CommonErrorEnum.SYSTEM_ERROR);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 未知异常
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(value = Exception.class)
|
||||||
|
public ApiResult systemExceptionHandler( Exception e) {
|
||||||
|
log.error("system exception!The reason is:{}", e.getMessage(), e);
|
||||||
|
return ApiResult.fail(CommonErrorEnum.SYSTEM_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义校验异常(如参数校验等)
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(value = BusinessException.class)
|
||||||
|
public ApiResult businessExceptionHandler(BusinessException e) {
|
||||||
|
log.info("business exception!The reason is:{}", e.getMessage(), e);
|
||||||
|
return ApiResult.fail(e.getErrorCode(), e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package com.abin.mallchat.common.common.exception;
|
||||||
|
|
||||||
|
import cn.hutool.http.ContentType;
|
||||||
|
import cn.hutool.http.HttpUtil;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.abin.mallchat.common.common.domain.vo.response.ApiResult;
|
||||||
|
import com.google.common.base.Charsets;
|
||||||
|
import io.netty.handler.codec.http.HttpContent;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.apache.commons.lang3.CharSet;
|
||||||
|
import org.apache.commons.lang3.CharSetUtils;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import sun.awt.CharsetString;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description: 业务校验异常码
|
||||||
|
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* Date: 2023-03-26
|
||||||
|
*/
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Getter
|
||||||
|
public enum HttpErrorEnum implements ErrorEnum {
|
||||||
|
ACCESS_DENIED(401, "登录失效,请重新登录"),
|
||||||
|
;
|
||||||
|
private Integer httpCode;
|
||||||
|
private String msg;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer getErrorCode() {
|
||||||
|
return httpCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getErrorMsg() {
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendHttpError(HttpServletResponse response) throws IOException {
|
||||||
|
response.setStatus(this.getErrorCode());
|
||||||
|
ApiResult responseData = ApiResult.fail(this);
|
||||||
|
response.setContentType(ContentType.JSON.toString(Charsets.UTF_8));
|
||||||
|
response.getWriter().write(JSONUtil.toJsonStr(responseData));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
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.ErrorEnum;
|
||||||
|
import com.abin.mallchat.common.user.domain.entity.UserBackpack;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 校验工具类
|
||||||
|
*/
|
||||||
|
public class AssertUtil {
|
||||||
|
|
||||||
|
//如果不是true,则抛异常
|
||||||
|
public static void isTrue(boolean expression, String msg) {
|
||||||
|
if (!expression) {
|
||||||
|
throwException(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void isTrue(boolean expression, ErrorEnum errorEnum, Object... args) {
|
||||||
|
if (!expression) {
|
||||||
|
throwException(errorEnum, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//如果是true,则抛异常
|
||||||
|
public static void isFalse(boolean expression, String msg) {
|
||||||
|
if (expression) {
|
||||||
|
throwException(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//如果是true,则抛异常
|
||||||
|
public static void isFalse(boolean expression, ErrorEnum errorEnum, Object... args) {
|
||||||
|
if (expression) {
|
||||||
|
throwException(errorEnum, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//如果不是非空对象,则抛异常
|
||||||
|
public static void isNotEmpty(Object obj, String msg) {
|
||||||
|
if (isEmpty(obj)) {
|
||||||
|
throwException(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//如果不是非空对象,则抛异常
|
||||||
|
public static void isEmpty(Object obj, String msg) {
|
||||||
|
if (!isEmpty(obj)) {
|
||||||
|
throwException(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static void equal(Object o1,Object o2, String msg) {
|
||||||
|
if (!ObjectUtil.equal(o1,o2)) {
|
||||||
|
throwException(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isEmpty(Object obj) {
|
||||||
|
return ObjectUtil.isEmpty(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void throwException(String msg) {
|
||||||
|
throwException(null, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void throwException(ErrorEnum errorEnum, Object... arg) {
|
||||||
|
if (Objects.isNull(errorEnum)) {
|
||||||
|
errorEnum = BusinessErrorEnum.BUSINESS_ERROR;
|
||||||
|
}
|
||||||
|
throw new BusinessException(errorEnum.getErrorCode(), String.format(errorEnum.getErrorMsg(), arg));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
package com.abin.mallchat.common.common.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
|
import cn.hutool.core.lang.Pair;
|
||||||
|
import cn.hutool.core.lang.Tuple;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.abin.mallchat.common.chat.dao.MessageDao;
|
||||||
|
import com.abin.mallchat.common.chat.domain.entity.Message;
|
||||||
|
import com.abin.mallchat.common.common.domain.vo.request.CursorPageBaseReq;
|
||||||
|
import com.abin.mallchat.common.common.domain.vo.request.PageBaseReq;
|
||||||
|
import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp;
|
||||||
|
import com.abin.mallchat.common.common.domain.vo.response.PageBaseResp;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.google.errorprone.annotations.Var;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.ZSetOperations;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import reactor.util.function.Tuple2;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description: 游标分页工具类
|
||||||
|
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* Date: 2023-03-28
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class CursorUtils {
|
||||||
|
@Autowired
|
||||||
|
private RedisUtils redisUtils;
|
||||||
|
|
||||||
|
public <T> CursorPageBaseResp<Pair<T, Double>> getCursorPageByRedis(CursorPageBaseReq cursorPageBaseReq, String redisKey, Function<String, T> typeConvert) {
|
||||||
|
Set<ZSetOperations.TypedTuple<String>> typedTuples;
|
||||||
|
if (StrUtil.isBlank(cursorPageBaseReq.getCursor())) {//第一次
|
||||||
|
typedTuples = redisUtils.zReverseRangeWithScores(redisKey, cursorPageBaseReq.getPageSize());
|
||||||
|
} else {
|
||||||
|
typedTuples = redisUtils.zReverseRangeByScoreWithScores(redisKey, Double.parseDouble(cursorPageBaseReq.getCursor()), cursorPageBaseReq.getPageSize());
|
||||||
|
}
|
||||||
|
List<Pair<T, Double>> result = typedTuples
|
||||||
|
.stream()
|
||||||
|
.map(t -> Pair.of(typeConvert.apply(t.getValue()), t.getScore()))
|
||||||
|
.sorted((o1, o2) -> o2.getValue().compareTo(o1.getValue()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
String cursor = Optional.ofNullable(CollectionUtil.getLast(result))
|
||||||
|
.map(Pair::getValue)
|
||||||
|
.map(String::valueOf)
|
||||||
|
.orElse(null);
|
||||||
|
Boolean isLast = result.size() != cursorPageBaseReq.getPageSize();
|
||||||
|
return new CursorPageBaseResp(cursor, isLast, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> CursorPageBaseResp<T> getCursorPageByMysql(IService<T> mapper, CursorPageBaseReq request, Consumer<LambdaQueryWrapper<T>> initWrapper, SFunction<T, ?> cursorColumn) {
|
||||||
|
LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
|
||||||
|
initWrapper.accept(wrapper);
|
||||||
|
if (StrUtil.isNotBlank(request.getCursor())) {
|
||||||
|
wrapper.lt(cursorColumn, request.getCursor());
|
||||||
|
}
|
||||||
|
wrapper.orderByDesc(cursorColumn);
|
||||||
|
Page<T> page = mapper.page(request.plusPage(), wrapper);
|
||||||
|
String cursor = Optional.ofNullable(CollectionUtil.getLast(page.getRecords()))
|
||||||
|
.map(cursorColumn)
|
||||||
|
.map(String::valueOf)
|
||||||
|
.orElse(null);
|
||||||
|
Boolean isLast = page.getRecords().size() != request.getPageSize();
|
||||||
|
return new CursorPageBaseResp(cursor, isLast, page.getRecords());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
package com.abin.mallchat.common.common.utils;
|
||||||
|
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.auth0.jwt.JWT;
|
||||||
|
import com.auth0.jwt.algorithms.Algorithm;
|
||||||
|
import com.auth0.jwt.interfaces.Claim;
|
||||||
|
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||||
|
import com.auth0.jwt.interfaces.JWTVerifier;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description: jwt的token生成与解析
|
||||||
|
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* Date: 2023-04-03
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class JwtUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* token秘钥,请勿泄露,请勿随便修改
|
||||||
|
*/
|
||||||
|
@Value("jwt.secret")
|
||||||
|
private String secret;
|
||||||
|
|
||||||
|
private static final String UID_CLAIM = "uid";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JWT生成Token.<br/>
|
||||||
|
* <p>
|
||||||
|
* JWT构成: header, payload, signature
|
||||||
|
*/
|
||||||
|
public String createToken(Long uid) {
|
||||||
|
// build token
|
||||||
|
String token = JWT.create()
|
||||||
|
.withClaim(UID_CLAIM, uid) // 只存一个uid信息,其他的自己去redis查
|
||||||
|
.sign(Algorithm.HMAC256(secret)); // signature
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
JwtUtils jwtUtils = new JwtUtils();
|
||||||
|
String token = jwtUtils.createToken(123L);
|
||||||
|
System.out.println(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解密Token
|
||||||
|
*
|
||||||
|
* @param token
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Map<String, Claim> verifyToken(String token) {
|
||||||
|
if (Objects.isNull(token)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
JWTVerifier verifier = JWT.require(Algorithm.HMAC256(secret)).build();
|
||||||
|
DecodedJWT jwt = verifier.verify(token);
|
||||||
|
return jwt.getClaims();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.info("decode error,token:{}", token, e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据Token获取uid
|
||||||
|
*
|
||||||
|
* @param token
|
||||||
|
* @return uid
|
||||||
|
*/
|
||||||
|
public Long getUidOrNull(String token) {
|
||||||
|
return Optional.ofNullable(verifyToken(token))
|
||||||
|
.map(map -> map.get(UID_CLAIM))
|
||||||
|
.map(Claim::asLong)
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,25 @@
|
|||||||
|
package com.abin.mallchat.common.common.utils;
|
||||||
|
|
||||||
|
import com.abin.mallchat.common.common.domain.dto.RequestInfo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description: 请求上下文
|
||||||
|
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* Date: 2023-04-05
|
||||||
|
*/
|
||||||
|
public class RequestHolder {
|
||||||
|
|
||||||
|
private static final ThreadLocal<RequestInfo> threadLocal = new ThreadLocal<>();
|
||||||
|
|
||||||
|
public static void set(RequestInfo requestInfo) {
|
||||||
|
threadLocal.set(requestInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RequestInfo get() {
|
||||||
|
return threadLocal.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void remove() {
|
||||||
|
threadLocal.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package com.abin.mallchat.common.user.dao;
|
||||||
|
|
||||||
|
import com.abin.mallchat.common.user.domain.entity.ItemConfig;
|
||||||
|
import com.abin.mallchat.common.user.mapper.ItemConfigMapper;
|
||||||
|
import com.abin.mallchat.common.user.service.IItemConfigService;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 功能物品配置表 服务实现类
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-03-19
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class ItemConfigDao extends ServiceImpl<ItemConfigMapper, ItemConfig> {
|
||||||
|
|
||||||
|
public List<ItemConfig> getByType(Integer type) {
|
||||||
|
return lambdaQuery().eq(ItemConfig::getType, type).list();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package com.abin.mallchat.common.user.dao;
|
||||||
|
|
||||||
|
import com.abin.mallchat.common.common.domain.enums.YesOrNoEnum;
|
||||||
|
import com.abin.mallchat.common.user.domain.entity.User;
|
||||||
|
import com.abin.mallchat.common.user.domain.entity.UserBackpack;
|
||||||
|
import com.abin.mallchat.common.user.mapper.UserBackpackMapper;
|
||||||
|
import com.abin.mallchat.common.user.service.IUserBackpackService;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 用户背包表 服务实现类
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-03-19
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class UserBackpackDao extends ServiceImpl<UserBackpackMapper, UserBackpack> {
|
||||||
|
|
||||||
|
public Integer getCountByValidItemId(Long uid, Long itemId) {
|
||||||
|
LambdaQueryWrapper<UserBackpack> wrapper = new QueryWrapper<UserBackpack>().lambda()
|
||||||
|
.eq(UserBackpack::getUid, uid)
|
||||||
|
.eq(UserBackpack::getItemId, itemId)
|
||||||
|
.eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus());
|
||||||
|
return count(wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserBackpack getFirstValidItem(Long uid, Long itemId) {
|
||||||
|
LambdaQueryWrapper<UserBackpack> wrapper = new QueryWrapper<UserBackpack>().lambda()
|
||||||
|
.eq(UserBackpack::getUid, uid)
|
||||||
|
.eq(UserBackpack::getItemId, itemId)
|
||||||
|
.eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
|
||||||
|
.last("limit 1");
|
||||||
|
return getOne(wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean invalidItem(Long itemId) {
|
||||||
|
UserBackpack update = new UserBackpack();
|
||||||
|
update.setItemId(itemId);
|
||||||
|
update.setStatus(YesOrNoEnum.YES.getStatus());
|
||||||
|
return updateById(update);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<UserBackpack> getByItemIds(Long uid, List<Long> itemIds) {
|
||||||
|
return lambdaQuery().eq(UserBackpack::getUid, uid)
|
||||||
|
.in(UserBackpack::getItemId, itemIds)
|
||||||
|
.eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
|
||||||
|
.list();
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserBackpack getByIdp(String idempotent) {
|
||||||
|
return lambdaQuery().eq(UserBackpack::getIdempotent, idempotent).one();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package com.abin.mallchat.common.user.dao;
|
||||||
|
|
||||||
|
import com.abin.mallchat.common.user.domain.entity.User;
|
||||||
|
import com.abin.mallchat.common.user.mapper.UserMapper;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 用户表 服务实现类
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* @since 2023-03-19
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class UserDao extends ServiceImpl<UserMapper, User> {
|
||||||
|
|
||||||
|
public User getByOpenId(String openId) {
|
||||||
|
LambdaQueryWrapper<User> wrapper = new QueryWrapper<User>().lambda().eq(User::getOpenId, openId);
|
||||||
|
return getOne(wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void modifyName(Long uid, String name) {
|
||||||
|
User update = new User();
|
||||||
|
update.setId(uid);
|
||||||
|
update.setName(name);
|
||||||
|
updateById(update);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void wearingBadge(Long uid, Long badgeId) {
|
||||||
|
User update = new User();
|
||||||
|
update.setId(uid);
|
||||||
|
update.setItemId(badgeId);
|
||||||
|
updateById(update);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package com.abin.mallchat.common.user.domain.dto;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description:
|
||||||
|
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||||
|
* Date: 2023-04-18
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class IpResult<T> implements Serializable {
|
||||||
|
@ApiModelProperty("错误码")
|
||||||
|
private Integer code;
|
||||||
|
@ApiModelProperty("错误消息")
|
||||||
|
private String msg;
|
||||||
|
@ApiModelProperty("返回对象")
|
||||||
|
private T data;
|
||||||
|
|
||||||
|
public boolean isSuccess() {
|
||||||
|
return Objects.nonNull(this.code) && this.code == 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user