im
This commit is contained in:
parent
e72b35c7eb
commit
340b5b7248
@ -0,0 +1,34 @@
|
||||
package cn.lili.im.config;
|
||||
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
|
||||
import javax.websocket.server.ServerEndpointConfig;
|
||||
|
||||
/**
|
||||
* CustomSpringConfigurator
|
||||
*
|
||||
* @author Chopper
|
||||
* @version v1.0
|
||||
* 2021-12-31 11:53
|
||||
*/
|
||||
public class CustomSpringConfigurator extends ServerEndpointConfig.Configurator implements ApplicationContextAware {
|
||||
|
||||
/**
|
||||
* Spring application context.
|
||||
*/
|
||||
private static volatile BeanFactory context;
|
||||
|
||||
@Override
|
||||
public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException {
|
||||
return context.getBean(clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
CustomSpringConfigurator.context = applicationContext;
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package cn.lili.im.config;
|
||||
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* WebSocketConfigurator
|
||||
*
|
||||
* @author Chopper
|
||||
* @version v1.0
|
||||
* 2021-12-31 11:53
|
||||
*/
|
||||
@ConditionalOnWebApplication
|
||||
@Configuration
|
||||
public class WebSocketConfigurator {
|
||||
|
||||
@Bean
|
||||
public CustomSpringConfigurator customSpringConfigurator() {
|
||||
// This is just to get context
|
||||
return new CustomSpringConfigurator();
|
||||
}
|
||||
}
|
49
framework/src/main/java/cn/lili/im/entity/ImMessage.java
Normal file
49
framework/src/main/java/cn/lili/im/entity/ImMessage.java
Normal file
@ -0,0 +1,49 @@
|
||||
package cn.lili.im.entity;
|
||||
|
||||
import cn.lili.im.entity.enums.MessageType;
|
||||
import cn.lili.mybatis.BaseEntity;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author Chopper
|
||||
*/
|
||||
@Data
|
||||
@TableName("li_im_message")
|
||||
@ApiModel(value = "Im消息")
|
||||
public class ImMessage extends BaseEntity {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 发送者
|
||||
*/
|
||||
private String fromUser;
|
||||
|
||||
/**
|
||||
* 接收者
|
||||
*/
|
||||
private String toUser;
|
||||
|
||||
/**
|
||||
* 已阅
|
||||
*/
|
||||
private Boolean isRead;
|
||||
|
||||
/**
|
||||
* 消息类型
|
||||
*/
|
||||
private MessageType messageType;
|
||||
|
||||
/**
|
||||
* 聊天id
|
||||
*/
|
||||
private String talkId;
|
||||
|
||||
/**
|
||||
* 消息实体
|
||||
*/
|
||||
private String text;
|
||||
|
||||
}
|
97
framework/src/main/java/cn/lili/im/entity/ImTalk.java
Normal file
97
framework/src/main/java/cn/lili/im/entity/ImTalk.java
Normal file
@ -0,0 +1,97 @@
|
||||
package cn.lili.im.entity;
|
||||
|
||||
|
||||
import cn.lili.common.utils.SnowFlake;
|
||||
import cn.lili.mybatis.BaseTenantEntity;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author Chopper
|
||||
*/
|
||||
@Data
|
||||
@TableName("li_im_talk")
|
||||
@ApiModel(value = "聊天")
|
||||
public class ImTalk extends BaseTenantEntity {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 用户1 id小的排在1
|
||||
*/
|
||||
private String userId1;
|
||||
/**
|
||||
* 用户1 id大的排在2
|
||||
*/
|
||||
private String userId2;
|
||||
|
||||
/**
|
||||
* 用户1置顶
|
||||
*/
|
||||
private Boolean top1;
|
||||
|
||||
/**
|
||||
* 用户2置顶
|
||||
*/
|
||||
private Boolean top2;
|
||||
/**
|
||||
* 用户1 不可见
|
||||
*/
|
||||
private Boolean disable1;
|
||||
|
||||
/**
|
||||
* 用户2 不可见
|
||||
*/
|
||||
private Boolean disable2;
|
||||
/**
|
||||
* 用户1名字
|
||||
*/
|
||||
private String name1;
|
||||
|
||||
/**
|
||||
* 用户2名字
|
||||
*/
|
||||
private String name2;
|
||||
/**
|
||||
* 用户1头像
|
||||
*/
|
||||
private String face1;
|
||||
|
||||
/**
|
||||
* 用户2头像
|
||||
*/
|
||||
private String face2;
|
||||
|
||||
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@ApiModelProperty(value = "最后聊天时间", hidden = true)
|
||||
private Date lastTalkTime;
|
||||
|
||||
public ImTalk() {
|
||||
|
||||
}
|
||||
|
||||
public ImTalk(String userId1, String userId2,
|
||||
String face1, String face2,
|
||||
String name1, String name2
|
||||
) {
|
||||
this.userId1 = userId1;
|
||||
this.userId2 = userId2;
|
||||
this.top1 = false;
|
||||
this.top2 = false;
|
||||
this.disable1 = false;
|
||||
this.disable2 = false;
|
||||
this.setId(SnowFlake.getIdStr());
|
||||
this.lastTalkTime = new Date();
|
||||
this.face1 = face1;
|
||||
this.face2 = face2;
|
||||
this.name1 = name1;
|
||||
this.name2 = name2;
|
||||
}
|
||||
}
|
26
framework/src/main/java/cn/lili/im/entity/ImUser.java
Normal file
26
framework/src/main/java/cn/lili/im/entity/ImUser.java
Normal file
@ -0,0 +1,26 @@
|
||||
package cn.lili.im.entity;
|
||||
|
||||
|
||||
import cn.lili.mybatis.BaseTenantEntity;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author Chopper
|
||||
*/
|
||||
@Data
|
||||
@TableName("li_im_users")
|
||||
@ApiModel(value = "Im消息")
|
||||
public class ImUser extends BaseTenantEntity {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
/**
|
||||
* 头像
|
||||
*/
|
||||
private String face;
|
||||
/**
|
||||
* 昵称
|
||||
*/
|
||||
private String name;
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package cn.lili.im.entity.dto;
|
||||
|
||||
import cn.lili.common.enums.ResultCode;
|
||||
import cn.lili.common.exception.ServiceException;
|
||||
import cn.lili.common.utils.StringUtils;
|
||||
import cn.lili.im.entity.ImMessage;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* MessageQueryParams
|
||||
*
|
||||
* @author Chopper
|
||||
* @version v1.0
|
||||
* 2022-01-20 17:16
|
||||
*/
|
||||
@Data
|
||||
public class MessageQueryParams {
|
||||
/**
|
||||
* 聊天窗口
|
||||
*/
|
||||
private String talkId;
|
||||
/**
|
||||
* 最后一个消息
|
||||
*/
|
||||
private String lastMessageId;
|
||||
/**
|
||||
* 获取消息数量
|
||||
*/
|
||||
private Integer num;
|
||||
|
||||
public LambdaQueryWrapper<ImMessage> initQueryWrapper() {
|
||||
if (StringUtils.isEmpty(talkId)) {
|
||||
throw new ServiceException(ResultCode.ERROR);
|
||||
}
|
||||
if (num == null || num > 50) {
|
||||
num = 50;
|
||||
}
|
||||
|
||||
LambdaQueryWrapper<ImMessage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
lambdaQueryWrapper.eq(ImMessage::getTalkId, talkId);
|
||||
if (StringUtils.isNotEmpty(lastMessageId)) {
|
||||
lambdaQueryWrapper.lt(ImMessage::getId, lastMessageId);
|
||||
}
|
||||
lambdaQueryWrapper.orderByDesc(ImMessage::getId);
|
||||
lambdaQueryWrapper.last("limit " + num);
|
||||
return lambdaQueryWrapper;
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package cn.lili.im.entity.enums;
|
||||
|
||||
/**
|
||||
* 返回消息类型枚举
|
||||
*
|
||||
* @author liushuai
|
||||
*/
|
||||
public enum MessageResultType {
|
||||
/**
|
||||
* 返回消息类型枚举
|
||||
* <p>
|
||||
* 好友列表
|
||||
* 增加好友
|
||||
* 消息
|
||||
* 阅读消息
|
||||
* 未读消息
|
||||
* 历史消息
|
||||
* 系统提示
|
||||
*/
|
||||
FRIENDS,
|
||||
ADD_FRIENDS,
|
||||
MESSAGE,
|
||||
READ_MESSAGE,
|
||||
UN_READ,
|
||||
HISTORY,
|
||||
SYSTEM_TIPS
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package cn.lili.im.entity.enums;
|
||||
|
||||
/**
|
||||
* 消息类型
|
||||
*
|
||||
* @author liushuai
|
||||
*/
|
||||
public enum MessageType {
|
||||
/**
|
||||
* 消息类型枚举
|
||||
* <p>
|
||||
* 普通消息
|
||||
* 图片
|
||||
* 语音
|
||||
* 视频
|
||||
*/
|
||||
MESSAGE,
|
||||
PICTURE,
|
||||
VOICE,
|
||||
VIDEO
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package cn.lili.im.entity.enums;
|
||||
|
||||
/**
|
||||
* 操作类型枚举
|
||||
*
|
||||
* @author liushuai
|
||||
*/
|
||||
public enum OperationType {
|
||||
/**
|
||||
* 消息类型枚举
|
||||
* <p>
|
||||
* 心跳检测
|
||||
* 发起聊天
|
||||
* 发起消息
|
||||
* 查询历史消息
|
||||
* 阅读消息
|
||||
* 查询未读消息
|
||||
*/
|
||||
PING,
|
||||
CREATE,
|
||||
MESSAGE,
|
||||
HISTORY,
|
||||
READ,
|
||||
UNREAD,
|
||||
|
||||
}
|
78
framework/src/main/java/cn/lili/im/entity/vo/ImTalkVO.java
Normal file
78
framework/src/main/java/cn/lili/im/entity/vo/ImTalkVO.java
Normal file
@ -0,0 +1,78 @@
|
||||
package cn.lili.im.entity.vo;
|
||||
|
||||
import cn.lili.im.entity.ImTalk;
|
||||
import cn.lili.mybatis.BaseTenantEntity;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author Chopper
|
||||
*/
|
||||
@Data
|
||||
@ApiModel(value = "聊天")
|
||||
public class ImTalkVO extends BaseTenantEntity {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
private String id;
|
||||
/**
|
||||
* 用户 id
|
||||
*/
|
||||
private String userId;
|
||||
|
||||
/**
|
||||
* 置顶
|
||||
*/
|
||||
private Boolean top;
|
||||
|
||||
/**
|
||||
* 用户 不可见
|
||||
*/
|
||||
private Boolean disable;
|
||||
|
||||
/**
|
||||
* 用户名字
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 用户头像
|
||||
*/
|
||||
private String face;
|
||||
|
||||
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@ApiModelProperty(value = "最后聊天时间", hidden = true)
|
||||
private Date lastTalkTime;
|
||||
|
||||
public ImTalkVO() {
|
||||
|
||||
}
|
||||
|
||||
public ImTalkVO(ImTalk imTalk, String currentUser) {
|
||||
if (imTalk.getUserId2().equals(currentUser)) {
|
||||
userId = imTalk.getUserId1();
|
||||
top = imTalk.getTop1();
|
||||
disable = imTalk.getDisable1();
|
||||
name = imTalk.getName1();
|
||||
face = imTalk.getFace1();
|
||||
} else {
|
||||
userId = imTalk.getUserId2();
|
||||
top = imTalk.getTop2();
|
||||
disable = imTalk.getDisable2();
|
||||
name = imTalk.getName2();
|
||||
face = imTalk.getFace2();
|
||||
}
|
||||
|
||||
lastTalkTime = imTalk.getLastTalkTime();
|
||||
id = imTalk.getId();
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package cn.lili.im.entity.vo;
|
||||
|
||||
import cn.lili.common.utils.StringUtils;
|
||||
import cn.lili.im.entity.enums.MessageType;
|
||||
import cn.lili.im.entity.enums.OperationType;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author liushuai
|
||||
*/
|
||||
@Data
|
||||
public class MessageOperation {
|
||||
|
||||
/**
|
||||
* 消息类型
|
||||
*/
|
||||
private OperationType operationType;
|
||||
/**
|
||||
* 与某人聊天记录
|
||||
*/
|
||||
private String to;
|
||||
|
||||
/**
|
||||
* 聊天id
|
||||
*/
|
||||
private String talkId;
|
||||
|
||||
/**
|
||||
* 消息类型
|
||||
*/
|
||||
private MessageType messageType;
|
||||
/**
|
||||
* 消息内容
|
||||
*/
|
||||
private String context;
|
||||
|
||||
public void setOperationType(String operationType) {
|
||||
if (!StringUtils.isEmpty(operationType)) {
|
||||
this.operationType = OperationType.valueOf(operationType);
|
||||
}
|
||||
}
|
||||
|
||||
public void setMessageType(String messageType) {
|
||||
if (!StringUtils.isEmpty(messageType)) {
|
||||
this.messageType = MessageType.valueOf(messageType);
|
||||
}
|
||||
}
|
||||
}
|
26
framework/src/main/java/cn/lili/im/entity/vo/MessageVO.java
Normal file
26
framework/src/main/java/cn/lili/im/entity/vo/MessageVO.java
Normal file
@ -0,0 +1,26 @@
|
||||
package cn.lili.im.entity.vo;
|
||||
|
||||
import cn.lili.im.entity.enums.MessageResultType;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* MessageVO
|
||||
*
|
||||
* @author Chopper
|
||||
* @version v1.0
|
||||
* 2021-12-30 15:51
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class MessageVO {
|
||||
|
||||
/**
|
||||
* 消息类型
|
||||
*/
|
||||
private MessageResultType messageResultType;
|
||||
/**
|
||||
* 消息内容
|
||||
*/
|
||||
private Object result;
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package cn.lili.im.entity.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* ReadMessage
|
||||
*
|
||||
* @author Chopper
|
||||
* @version v1.0
|
||||
* 2021-12-31 11:13
|
||||
*/
|
||||
@Data
|
||||
public class ReadMessage {
|
||||
private List<String> readMessageList;
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package cn.lili.im.mapper;
|
||||
|
||||
import cn.lili.im.entity.ImMessage;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* Im消息 Dao层
|
||||
* @author Chopper
|
||||
*/
|
||||
public interface ImMessageMapper extends BaseMapper<ImMessage> {
|
||||
|
||||
}
|
12
framework/src/main/java/cn/lili/im/mapper/ImTalkMapper.java
Normal file
12
framework/src/main/java/cn/lili/im/mapper/ImTalkMapper.java
Normal file
@ -0,0 +1,12 @@
|
||||
package cn.lili.im.mapper;
|
||||
|
||||
import cn.lili.im.entity.ImTalk;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* 聊天 Dao层
|
||||
* @author Chopper
|
||||
*/
|
||||
public interface ImTalkMapper extends BaseMapper<ImTalk> {
|
||||
|
||||
}
|
13
framework/src/main/java/cn/lili/im/mapper/ImUserMapper.java
Normal file
13
framework/src/main/java/cn/lili/im/mapper/ImUserMapper.java
Normal file
@ -0,0 +1,13 @@
|
||||
package cn.lili.im.mapper;
|
||||
|
||||
import cn.lili.im.entity.ImUser;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* Im消息 Dao层
|
||||
*
|
||||
* @author Chopper
|
||||
*/
|
||||
public interface ImUserMapper extends BaseMapper<ImUser> {
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package cn.lili.im.service;
|
||||
|
||||
import cn.lili.im.entity.ImMessage;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Im消息 业务层
|
||||
*
|
||||
* @author Chopper
|
||||
*/
|
||||
public interface ImMessageService extends IService<ImMessage> {
|
||||
|
||||
/**
|
||||
* 阅读消息
|
||||
*
|
||||
* @param talkId
|
||||
* @param accessToken
|
||||
*/
|
||||
void read(String talkId, String accessToken);
|
||||
|
||||
/**
|
||||
* 未读消息列表
|
||||
*
|
||||
* @param accessToken
|
||||
*/
|
||||
List<ImMessage> unReadMessages(String accessToken);
|
||||
|
||||
/**
|
||||
* 历史消息
|
||||
*
|
||||
* @param accessToken
|
||||
* @param to
|
||||
*/
|
||||
List<ImMessage> historyMessage(String accessToken, String to);
|
||||
|
||||
/**
|
||||
* 是否有新消息
|
||||
* @param accessToken
|
||||
* @return
|
||||
*/
|
||||
Boolean hasNewMessage(String accessToken);
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package cn.lili.im.service;
|
||||
|
||||
import cn.lili.im.entity.ImTalk;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
/**
|
||||
* 聊天 业务层
|
||||
*
|
||||
* @author Chopper
|
||||
*/
|
||||
public interface ImTalkService extends IService<ImTalk> {
|
||||
|
||||
/**
|
||||
* 获取与某人的聊天框
|
||||
*
|
||||
* @param userId1
|
||||
* @param userId2
|
||||
* @return
|
||||
*/
|
||||
ImTalk getTalkByUser(String userId1, String userId2);
|
||||
|
||||
/**
|
||||
* 置顶消息
|
||||
*
|
||||
* @param id
|
||||
* @param top
|
||||
*/
|
||||
void top(String id, Boolean top);
|
||||
|
||||
/**
|
||||
* 禁用(前端不做展示)聊天
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
void disable(String id);
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package cn.lili.im.service;
|
||||
|
||||
import cn.lili.im.entity.ImUser;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
/**
|
||||
* Im消息 业务层
|
||||
*
|
||||
* @author Chopper
|
||||
*/
|
||||
public interface ImUserService extends IService<ImUser> {
|
||||
|
||||
/**
|
||||
* 注册用户
|
||||
*
|
||||
* @param accessToken
|
||||
* @return
|
||||
*/
|
||||
ImUser register(String accessToken);
|
||||
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package cn.lili.im.serviceimpl;
|
||||
|
||||
import cn.lili.common.security.context.UserContext;
|
||||
import cn.lili.im.entity.ImMessage;
|
||||
import cn.lili.im.mapper.ImMessageMapper;
|
||||
import cn.lili.im.service.ImMessageService;
|
||||
import cn.lili.im.service.ImTalkService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Im消息 业务实现
|
||||
*
|
||||
* @author Chopper
|
||||
*/
|
||||
@Service
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||
public class ImMessageServiceImpl extends ServiceImpl<ImMessageMapper, ImMessage> implements ImMessageService {
|
||||
|
||||
@Autowired
|
||||
private ImTalkService imTalkService;
|
||||
|
||||
@Override
|
||||
public void read(String talkId, String accessToken) {
|
||||
LambdaUpdateWrapper<ImMessage> updateWrapper = new LambdaUpdateWrapper<>();
|
||||
String userId = UserContext.getAuthUser(accessToken).getId();
|
||||
updateWrapper.eq(ImMessage::getTalkId, talkId);
|
||||
updateWrapper.eq(ImMessage::getToUser, userId);
|
||||
updateWrapper.set(ImMessage::getIsRead, true);
|
||||
this.update(updateWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ImMessage> unReadMessages(String accessToken) {
|
||||
String userId = UserContext.getAuthUser(accessToken).getId();
|
||||
LambdaQueryWrapper<ImMessage> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(ImMessage::getToUser, userId);
|
||||
queryWrapper.eq(ImMessage::getIsRead, false);
|
||||
return this.list(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ImMessage> historyMessage(String accessToken, String to) {
|
||||
String userId = UserContext.getAuthUser(accessToken).getId();
|
||||
LambdaQueryWrapper<ImMessage> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.and(i -> i.eq(ImMessage::getToUser, userId).and(j -> j.eq(ImMessage::getFromUser, to)));
|
||||
queryWrapper.or(i -> i.eq(ImMessage::getToUser, to).and(j -> j.eq(ImMessage::getFromUser, userId)));
|
||||
queryWrapper.orderByDesc(ImMessage::getCreateTime);
|
||||
return this.list(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean hasNewMessage(String accessToken) {
|
||||
String userId = UserContext.getAuthUser(accessToken).getId();
|
||||
LambdaQueryWrapper<ImMessage> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(ImMessage::getIsRead, false);
|
||||
queryWrapper.eq(ImMessage::getToUser, userId);
|
||||
return this.list(queryWrapper).size() > 0;
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
package cn.lili.im.serviceimpl;
|
||||
|
||||
import cn.lili.common.enums.ResultCode;
|
||||
import cn.lili.common.exception.ServiceException;
|
||||
import cn.lili.common.security.context.UserContext;
|
||||
import cn.lili.im.entity.ImTalk;
|
||||
import cn.lili.im.entity.ImUser;
|
||||
import cn.lili.im.mapper.ImTalkMapper;
|
||||
import cn.lili.im.service.ImTalkService;
|
||||
import cn.lili.im.service.ImUserService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* 聊天 业务实现
|
||||
*
|
||||
* @author Chopper
|
||||
*/
|
||||
@Service
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||
public class ImTalkServiceImpl extends ServiceImpl<ImTalkMapper, ImTalk> implements ImTalkService {
|
||||
|
||||
@Autowired
|
||||
private ImUserService imUserService;
|
||||
|
||||
@Override
|
||||
public ImTalk getTalkByUser(String userId1, String userId2) {
|
||||
LambdaQueryWrapper<ImTalk> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(ImTalk::getUserId2, userId2);
|
||||
queryWrapper.eq(ImTalk::getUserId1, userId1);
|
||||
ImTalk imTalk = this.getOne(queryWrapper);
|
||||
ImUser imUser1 = imUserService.getById(userId1);
|
||||
ImUser imUser2 = imUserService.getById(userId2);
|
||||
|
||||
//如果没有聊天,则创建聊天
|
||||
if (imTalk == null) {
|
||||
if (imUser1 == null || imUser2 == null) {
|
||||
return null;
|
||||
}
|
||||
imTalk = new ImTalk(userId1, userId2, imUser1.getFace(), imUser2.getFace(), imUser1.getName(), imUser2.getName());
|
||||
this.save(imTalk);
|
||||
} else {
|
||||
imTalk = check(imTalk);
|
||||
}
|
||||
return imTalk;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起聊天后,如果聊天不可见为true,则需要修正
|
||||
*
|
||||
* @param imTalk
|
||||
*/
|
||||
private ImTalk check(ImTalk imTalk) {
|
||||
if (imTalk.getDisable1() || imTalk.getDisable2()) {
|
||||
imTalk.setDisable1(false);
|
||||
imTalk.setDisable2(false);
|
||||
this.updateById(imTalk);
|
||||
|
||||
}
|
||||
return imTalk;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void top(String id, Boolean top) {
|
||||
ImTalk imTalk = this.getById(id);
|
||||
if (imTalk.getUserId1().equals(UserContext.getCurrentUser().getId())) {
|
||||
imTalk.setTop1(top);
|
||||
} else if (imTalk.getUserId2().equals(UserContext.getCurrentUser().getId())) {
|
||||
imTalk.setTop2(top);
|
||||
} else {
|
||||
throw new ServiceException(ResultCode.ERROR);
|
||||
}
|
||||
this.updateById(imTalk);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disable(String id) {
|
||||
ImTalk imTalk = this.getById(id);
|
||||
if (imTalk.getUserId1().equals(UserContext.getCurrentUser().getId())) {
|
||||
imTalk.setDisable1(true);
|
||||
this.updateById(imTalk);
|
||||
} else if (imTalk.getUserId2().equals(UserContext.getCurrentUser().getId())) {
|
||||
imTalk.setDisable2(true);
|
||||
this.updateById(imTalk);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package cn.lili.im.serviceimpl;
|
||||
|
||||
import cn.lili.common.security.AuthUser;
|
||||
import cn.lili.common.security.context.UserContext;
|
||||
import cn.lili.im.entity.ImUser;
|
||||
import cn.lili.im.mapper.ImUserMapper;
|
||||
import cn.lili.im.service.ImUserService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* Im消息 业务实现
|
||||
*
|
||||
* @author Chopper
|
||||
*/
|
||||
@Service
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||
public class ImUserServiceImpl extends ServiceImpl<ImUserMapper, ImUser> implements ImUserService {
|
||||
|
||||
@Override
|
||||
public ImUser register(String accessToken) {
|
||||
AuthUser authUser = UserContext.getAuthUser(accessToken);
|
||||
ImUser imUser;
|
||||
//如果用户存在
|
||||
imUser = this.getById(authUser.getId());
|
||||
if (imUser == null) {
|
||||
imUser = new ImUser();
|
||||
imUser.setId(authUser.getId());
|
||||
imUser.setName(authUser.getNickName());
|
||||
this.save(imUser);
|
||||
}
|
||||
return imUser;
|
||||
}
|
||||
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package cn.lili.modules.permission.service;
|
||||
|
||||
|
||||
import cn.lili.common.security.enums.UserEnums;
|
||||
import cn.lili.common.security.token.Token;
|
||||
import cn.lili.modules.permission.entity.dos.AdminUser;
|
||||
import cn.lili.modules.permission.entity.dto.AdminUserDTO;
|
||||
@ -99,4 +100,11 @@ public interface AdminUserService extends IService<AdminUser> {
|
||||
*/
|
||||
Token refreshToken(String refreshToken);
|
||||
|
||||
/**
|
||||
* 登出
|
||||
*
|
||||
* @param userEnums token角色类型
|
||||
*/
|
||||
void logout(UserEnums userEnums);
|
||||
|
||||
}
|
||||
|
@ -1,10 +1,13 @@
|
||||
package cn.lili.modules.permission.serviceimpl;
|
||||
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.lili.cache.Cache;
|
||||
import cn.lili.cache.CachePrefix;
|
||||
import cn.lili.common.enums.ResultCode;
|
||||
import cn.lili.common.exception.ServiceException;
|
||||
import cn.lili.common.security.AuthUser;
|
||||
import cn.lili.common.security.context.UserContext;
|
||||
import cn.lili.common.security.enums.UserEnums;
|
||||
import cn.lili.common.security.token.Token;
|
||||
import cn.lili.common.utils.BeanUtil;
|
||||
import cn.lili.common.utils.StringUtils;
|
||||
@ -53,6 +56,10 @@ public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser
|
||||
private MenuService menuService;
|
||||
@Autowired
|
||||
private ManagerTokenGenerate managerTokenGenerate;
|
||||
|
||||
@Autowired
|
||||
private Cache cache;
|
||||
|
||||
/**
|
||||
* 角色长度
|
||||
*/
|
||||
@ -132,6 +139,14 @@ public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser
|
||||
return managerTokenGenerate.refreshToken(refreshToken);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logout(UserEnums userEnums) {
|
||||
String currentUserToken = UserContext.getCurrentUserToken();
|
||||
if (CharSequenceUtil.isNotEmpty(currentUserToken)) {
|
||||
cache.remove(CachePrefix.ACCESS_TOKEN.getPrefix(userEnums) + currentUserToken);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public AdminUser findByUsername(String username) {
|
||||
|
@ -0,0 +1,24 @@
|
||||
package cn.lili.mybatis;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
|
||||
/**
|
||||
* 租户超级类
|
||||
*
|
||||
* @author Chopper
|
||||
* @version v1.0
|
||||
* @since 2020/8/20 14:34
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public abstract class BaseTenantEntity extends BaseEntity {
|
||||
|
||||
@ApiModelProperty(value = "租户id", hidden = true)
|
||||
private String tenantId;
|
||||
|
||||
}
|
@ -0,0 +1,175 @@
|
||||
package cn.lili.controller.im;
|
||||
|
||||
import cn.lili.common.enums.ResultCode;
|
||||
import cn.lili.common.enums.ResultUtil;
|
||||
import cn.lili.common.exception.ServiceException;
|
||||
import cn.lili.common.security.AuthUser;
|
||||
import cn.lili.common.security.context.UserContext;
|
||||
import cn.lili.common.security.enums.UserEnums;
|
||||
import cn.lili.common.security.token.Token;
|
||||
import cn.lili.common.utils.StringUtils;
|
||||
import cn.lili.common.vo.PageVO;
|
||||
import cn.lili.common.vo.ResultMessage;
|
||||
import cn.lili.common.vo.SearchVO;
|
||||
import cn.lili.modules.permission.entity.dos.AdminUser;
|
||||
import cn.lili.modules.permission.entity.dto.AdminUserDTO;
|
||||
import cn.lili.modules.permission.entity.vo.AdminUserVO;
|
||||
import cn.lili.modules.permission.service.AdminUserService;
|
||||
import cn.lili.modules.permission.service.DepartmentService;
|
||||
import cn.lili.mybatis.util.PageUtil;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@Api(tags = "管理员")
|
||||
@RequestMapping("/manager/passport/user")
|
||||
@Validated
|
||||
public class ImManagerController {
|
||||
@Autowired
|
||||
private AdminUserService adminUserService;
|
||||
@Autowired
|
||||
private DepartmentService departmentService;
|
||||
|
||||
@PostMapping(value = "/login")
|
||||
@ApiOperation(value = "登录管理员")
|
||||
public ResultMessage<Token> login(@NotNull(message = "用户名不能为空") String username,
|
||||
@NotNull(message = "密码不能为空") String password) {
|
||||
return ResultUtil.data(adminUserService.login(username, password));
|
||||
}
|
||||
|
||||
@ApiOperation(value = "注销接口")
|
||||
@PostMapping("/logout")
|
||||
public ResultMessage<Object> logout() {
|
||||
this.adminUserService.logout(UserEnums.MANAGER);
|
||||
return ResultUtil.success();
|
||||
}
|
||||
|
||||
@ApiOperation(value = "刷新token")
|
||||
@GetMapping("/refresh/{refreshToken}")
|
||||
public ResultMessage<Object> refreshToken(@NotNull(message = "刷新token不能为空") @PathVariable String refreshToken) {
|
||||
return ResultUtil.data(this.adminUserService.refreshToken(refreshToken));
|
||||
}
|
||||
|
||||
|
||||
@GetMapping(value = "/info")
|
||||
@ApiOperation(value = "获取当前登录用户接口")
|
||||
public ResultMessage<AdminUserVO> getUserInfo() {
|
||||
AuthUser tokenUser = UserContext.getCurrentUser();
|
||||
if (tokenUser != null) {
|
||||
AdminUserVO adminUser = new AdminUserVO(adminUserService.findByUsername(tokenUser.getUsername()));
|
||||
if (StringUtils.isNotEmpty(adminUser.getDepartmentId())) {
|
||||
adminUser.setDepartmentTitle(departmentService.getById(adminUser.getDepartmentId()).getTitle());
|
||||
}
|
||||
adminUser.setPassword(null);
|
||||
return ResultUtil.data(adminUser);
|
||||
}
|
||||
throw new ServiceException(ResultCode.USER_NOT_LOGIN);
|
||||
}
|
||||
|
||||
@PutMapping(value = "/edit")
|
||||
@ApiOperation(value = "修改用户自己资料", notes = "用户名密码不会修改")
|
||||
public ResultMessage<Object> editOwner(AdminUser adminUser) {
|
||||
|
||||
AuthUser tokenUser = UserContext.getCurrentUser();
|
||||
if (tokenUser != null) {
|
||||
//查询当前管理员
|
||||
AdminUser oldAdminUser = adminUserService.findByUsername(tokenUser.getUsername());
|
||||
oldAdminUser.setAvatar(adminUser.getAvatar());
|
||||
oldAdminUser.setNickName(adminUser.getNickName());
|
||||
if (!adminUserService.updateById(oldAdminUser)) {
|
||||
throw new ServiceException(ResultCode.USER_EDIT_ERROR);
|
||||
}
|
||||
return ResultUtil.success(ResultCode.USER_EDIT_SUCCESS);
|
||||
}
|
||||
throw new ServiceException(ResultCode.USER_NOT_LOGIN);
|
||||
}
|
||||
|
||||
@PutMapping(value = "/admin/edit")
|
||||
@ApiOperation(value = "超级管理员修改其他管理员资料")
|
||||
public ResultMessage<Object> edit(@Valid AdminUser adminUser,
|
||||
@RequestParam(required = false) List<String> roles) {
|
||||
if (!adminUserService.updateAdminUser(adminUser, roles)) {
|
||||
throw new ServiceException(ResultCode.USER_EDIT_ERROR);
|
||||
}
|
||||
return ResultUtil.success(ResultCode.USER_EDIT_SUCCESS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改密码
|
||||
*
|
||||
* @param password
|
||||
* @param newPassword
|
||||
* @return
|
||||
*/
|
||||
@PutMapping(value = "/editPassword")
|
||||
@ApiOperation(value = "修改密码")
|
||||
public ResultMessage<Object> editPassword(String password, String newPassword) {
|
||||
adminUserService.editPassword(password, newPassword);
|
||||
return ResultUtil.success(ResultCode.USER_EDIT_SUCCESS);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/resetPassword/{ids}")
|
||||
@ApiOperation(value = "重置密码")
|
||||
public ResultMessage<Object> resetPassword(@PathVariable List ids) {
|
||||
adminUserService.resetPassword(ids);
|
||||
return ResultUtil.success(ResultCode.USER_EDIT_SUCCESS);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@ApiOperation(value = "多条件分页获取用户列表")
|
||||
public ResultMessage<IPage<AdminUserVO>> getByCondition(AdminUserDTO user,
|
||||
SearchVO searchVo,
|
||||
PageVO pageVo) {
|
||||
IPage<AdminUserVO> page = adminUserService.adminUserPage(PageUtil.initPage(pageVo), PageUtil.initWrapper(user, searchVo));
|
||||
|
||||
return ResultUtil.data(page);
|
||||
}
|
||||
|
||||
|
||||
@PostMapping
|
||||
@ApiOperation(value = "添加用户")
|
||||
public ResultMessage<Object> register(@Valid AdminUserDTO adminUser,
|
||||
@RequestParam(required = false) List<String> roles) {
|
||||
int rolesMaxSize = 10;
|
||||
try {
|
||||
if (roles != null && roles.size() >= rolesMaxSize) {
|
||||
throw new ServiceException(ResultCode.PERMISSION_BEYOND_TEN);
|
||||
}
|
||||
adminUserService.saveAdminUser(adminUser, roles);
|
||||
} catch (Exception e) {
|
||||
log.error("添加用户错误", e);
|
||||
}
|
||||
return ResultUtil.success();
|
||||
}
|
||||
|
||||
@PutMapping(value = "/enable/{userId}")
|
||||
@ApiOperation(value = "禁/启 用 用户")
|
||||
public ResultMessage<Object> disable(@ApiParam("用户唯一id标识") @PathVariable String userId, Boolean status) {
|
||||
AdminUser user = adminUserService.getById(userId);
|
||||
if (user == null) {
|
||||
throw new ServiceException(ResultCode.USER_NOT_EXIST);
|
||||
}
|
||||
user.setStatus(status);
|
||||
adminUserService.updateById(user);
|
||||
return ResultUtil.success();
|
||||
}
|
||||
|
||||
@DeleteMapping(value = "/{ids}")
|
||||
@ApiOperation(value = "批量通过ids删除")
|
||||
public ResultMessage<Object> delAllByIds(@PathVariable List<String> ids) {
|
||||
adminUserService.deleteCompletely(ids);
|
||||
return ResultUtil.success();
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user