fix: 去除无用的ImUserController,调整IM代码

This commit is contained in:
chc 2023-03-02 15:10:51 +08:00
parent 53e753c3ef
commit 07d4a2cda9
9 changed files with 150 additions and 162 deletions

View File

@ -413,6 +413,7 @@ public enum ResultCode {
STORE_NOT_OPEN(50004, "该会员未开通店铺"),
STORE_NOT_LOGIN_ERROR(50005, "未登录店铺"),
STORE_CLOSE_ERROR(50006, "店铺关闭,请联系管理员"),
STORE_DELIVER_GOODS_ADDRESS(50007,"请填写商家发货地址"),
FREIGHT_TEMPLATE_NOT_EXIST(50010, "当前模版不存在"),
STORE_STATUS_ERROR(50011, "店铺状态异常,无法申请"),
@ -491,6 +492,12 @@ public enum ResultCode {
WECHAT_ERROR(80307, "微信接口异常"),
APP_VERSION_EXIST(80307, "APP版本已存在"),
/**
* IM
*/
IM_MESSAGE_ADD_ERROR(80400,"IM消息发送错误"),
IM_MESSAGE_EDIT_ERROR(80400,"IM消息更新错误"),
/**
* 其他
*/

View File

@ -2,6 +2,8 @@ package cn.lili.modules.im.entity.dos;
import cn.lili.common.utils.SnowFlake;
import cn.lili.modules.member.entity.dos.Member;
import cn.lili.modules.store.entity.dos.Store;
import cn.lili.mybatis.BaseTenantEntity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
@ -99,4 +101,71 @@ public class ImTalk extends BaseTenantEntity {
this.name1 = name1;
this.name2 = name2;
}
public ImTalk(Member member1,Member member2){
if(Long.parseLong(member1.getId()) > Long.parseLong(member2.getId())){
this.userId1 = member2.getId();
this.userId2 = member1.getId();
this.top1 = false;
this.top2 = false;
this.disable1 = false;
this.disable2 = false;
this.storeFlag1 = false;
this.storeFlag2 = false;
this.setId(SnowFlake.getIdStr());
this.lastTalkTime = new Date();
this.face1 = member2.getFace();
this.face2 = member1.getFace();
this.name1 = member2.getNickName();
this.name2 = member1.getNickName();
}else{
this.userId1 = member1.getId();
this.userId2 = member2.getId();
this.top1 = false;
this.top2 = false;
this.disable1 = false;
this.disable2 = false;
this.storeFlag1 = false;
this.storeFlag2 = false;
this.setId(SnowFlake.getIdStr());
this.lastTalkTime = new Date();
this.face1 = member1.getFace();
this.face2 = member2.getFace();
this.name1 = member1.getNickName();
this.name2 = member2.getNickName();
}
}
public ImTalk(Member member, Store store){
if(Long.parseLong(member.getId()) > Long.parseLong(store.getId())){
this.userId1 = store.getId();
this.userId2 = member.getId();
this.top1 = false;
this.top2 = false;
this.disable1 = false;
this.disable2 = false;
this.storeFlag1 = true;
this.storeFlag2 = false;
this.setId(SnowFlake.getIdStr());
this.lastTalkTime = new Date();
this.face1 = store.getStoreLogo();
this.face2 = member.getFace();
this.name1 = store.getStoreName();
this.name2 = member.getNickName();
}else{
this.userId1 = member.getId();
this.userId2 = store.getId();
this.top1 = false;
this.top2 = false;
this.disable1 = false;
this.disable2 = false;
this.storeFlag1 = false;
this.storeFlag2 = true;
this.setId(SnowFlake.getIdStr());
this.lastTalkTime = new Date();
this.face1 = member.getFace();
this.face2 = store.getStoreLogo();
this.name1 = member.getNickName();
this.name2 = store.getStoreName();
}
}
}

View File

@ -16,29 +16,32 @@ public interface ImMessageService extends IService<ImMessage> {
/**
* 阅读消息
*
* @param talkId
* @param accessToken
* @param talkId 对话Id
* @param accessToken 验证token
*/
void read(String talkId, String accessToken);
/**
* 未读消息列表
*
* @param accessToken
* @param accessToken 验证token
* @return 未读消息列表
*/
List<ImMessage> unReadMessages(String accessToken);
/**
* 历史消息
*
* @param accessToken
* @param to
* @param accessToken 验证token
* @param to 接收人
*
* @return 历史消息列表
*/
List<ImMessage> historyMessage(String accessToken, String to);
/**
* 是否有新消息
* @param accessToken
* @param accessToken 验证token
* @return
*/
Boolean hasNewMessage(String accessToken);
@ -52,7 +55,7 @@ public interface ImMessageService extends IService<ImMessage> {
/**
* 获取所有未读消息
* @return
* @return 未读数量
*/
Long unreadMessageCount();

View File

@ -17,10 +17,9 @@ public interface ImTalkService extends IService<ImTalk> {
* 获取与某人的聊天框
*
* @param userId1
* @param userId2
* @return
*/
ImTalk getTalkByUser(String userId1, String userId2);
ImTalk getTalkByUser(String userId1);
/**
* 置顶消息
@ -41,7 +40,7 @@ public interface ImTalkService extends IService<ImTalk> {
* 获取用户聊天列表
* @return
*/
List<ImTalkVO> getUserTalkList(String userName);
List<ImTalkVO> getUserTalkList();
/**
* 获取商家聊天列表

View File

@ -4,6 +4,7 @@ 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.modules.im.entity.dos.ImMessage;
import cn.lili.modules.im.entity.dto.MessageQueryParams;
import cn.lili.modules.im.mapper.ImMessageMapper;
@ -18,9 +19,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
/**
* Im消息 业务实现
@ -130,9 +131,18 @@ public class ImMessageServiceImpl extends ServiceImpl<ImMessageMapper, ImMessage
* @param messageList 消息列表
*/
private void readMessage(List<ImMessage> messageList) {
if (messageList.size() > 0) {
if (!messageList.isEmpty()) {
//判断用户类型
AuthUser authUser = Objects.requireNonNull(UserContext.getCurrentUser());
String toUserId = "";
if(UserEnums.MEMBER.equals(authUser.getRole())){
toUserId = authUser.getId();
}else if(UserEnums.STORE.equals(authUser.getRole())){
toUserId = authUser.getStoreId();
}
//发送给自己的未读信息进行已读操作
for (ImMessage imMessage : messageList) {
if(Boolean.FALSE.equals(imMessage.getIsRead())){
if(Boolean.FALSE.equals(imMessage.getIsRead()) && imMessage.getToUser().equals(toUserId)){
imMessage.setIsRead(true);
}
}

View File

@ -21,9 +21,9 @@ import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.stringtemplate.v4.ST;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
@ -45,47 +45,44 @@ public class ImTalkServiceImpl extends ServiceImpl<ImTalkMapper, ImTalk> impleme
@Autowired
private ImMessageService imMessageService;
@Override
public ImTalk getTalkByUser(String userId1, String userId2) {
public ImTalk getTalkByUser(String userId) {
LambdaQueryWrapper<ImTalk> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(ImTalk::getUserId2, userId2);
queryWrapper.eq(ImTalk::getUserId1, userId1);
AuthUser currentUser = Objects.requireNonNull(UserContext.getCurrentUser());
//登录用户的Id
String selfId = "";
//查看当前用户角色对Id进行赋值
if(UserEnums.STORE.equals(currentUser.getRole())){
selfId = currentUser.getStoreId();
}else if(UserEnums.MEMBER.equals(currentUser.getRole())){
selfId = currentUser.getId();
}
//小数在前保证永远是同一个对话
String finalSelfId = selfId;
queryWrapper.and(wq-> wq.eq(ImTalk::getUserId2, userId).eq(ImTalk::getUserId1, finalSelfId).or().eq(ImTalk::getUserId2, finalSelfId).eq(ImTalk::getUserId1, userId));
ImTalk imTalk = this.getOne(queryWrapper);
AuthUser currentUser = UserContext.getCurrentUser();
//如果没有聊天则创建聊天
if (imTalk == null) {
// 没有登录的这个账户信息
if (currentUser == null) {
return null;
}
//当自己为店铺时
if(UserEnums.STORE.equals(currentUser.getRole())){
Store selfStore = storeService.getById(userId1);
Store selfStore = storeService.getById(selfId);
//没有这个用户信息
Member other = memberService.getById(userId2);
Member other = memberService.getById(userId);
if(other == null){
return null;
}
//自己为店铺其他人必定为用户
imTalk = new ImTalk(userId1, userId2, selfStore.getStoreLogo(), other.getFace(), selfStore.getStoreName(), other.getNickName());
imTalk.setStoreFlag1(true);
imTalk = new ImTalk(other,selfStore);
}else if(UserEnums.MEMBER.equals(currentUser.getRole())){
//没有这个店铺信息
Member self = memberService.getById(userId1);
Member otherMember = memberService.getById(userId2);
Store otherStore = storeService.getById(userId2);
if(otherStore != null){
imTalk = new ImTalk(userId1, userId2, self.getFace(), otherStore.getStoreLogo(), self.getNickName(), otherStore.getStoreName());
imTalk.setStoreFlag2(true);
}else if (otherMember != null){
imTalk = new ImTalk(userId1, userId2, self.getFace(), otherMember.getFace(), self.getNickName(), otherMember.getNickName());
}else{
return null;
}
Member self = memberService.getById(selfId);
Member otherMember = memberService.getById(userId);
Store otherStore = storeService.getById(userId);
if(otherStore != null){
imTalk = new ImTalk(self, otherStore);
}else if (otherMember != null){
imTalk = new ImTalk(self, otherMember);
}
}
this.save(imTalk);
} else {
imTalk = check(imTalk);
}
return imTalk;
}
@ -93,7 +90,7 @@ public class ImTalkServiceImpl extends ServiceImpl<ImTalkMapper, ImTalk> impleme
/**
* 发起聊天后如果聊天不可见为true则需要修正
*
* @param imTalk
* @param imTalk 对话信息
*/
private ImTalk check(ImTalk imTalk) {
if (imTalk.getDisable1() || imTalk.getDisable2()) {
@ -131,23 +128,16 @@ public class ImTalkServiceImpl extends ServiceImpl<ImTalkMapper, ImTalk> impleme
}
@Override
public List<ImTalkVO> getUserTalkList(String userName) {
public List<ImTalkVO> getUserTalkList() {
AuthUser authUser = UserContext.getCurrentUser();
if(authUser == null){
throw new ServiceException(ResultCode.USER_NOT_LOGIN);
}
LambdaQueryWrapper<ImTalk> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.and(wq->{
wq.like(ImTalk::getName1, userName).or().like(ImTalk::getName2,userName);
});
queryWrapper.and(wq->{
wq.like(ImTalk::getUserId1, authUser.getId()).or().like(ImTalk::getUserId2,authUser.getId());
});
queryWrapper.and(wq-> wq.eq(ImTalk::getUserId1, authUser.getId()).or().eq(ImTalk::getUserId2,authUser.getId()));
queryWrapper.orderByDesc(ImTalk::getLastTalkTime);
List<ImTalk> imTalks = this.list(queryWrapper);
List<ImTalkVO> imTalkVOList = imTalks.stream().map(imTalk -> {
return new ImTalkVO(imTalk, authUser.getId());
}).collect(Collectors.toList());
List<ImTalkVO> imTalkVOList = imTalks.stream().map(imTalk -> new ImTalkVO(imTalk, authUser.getId())).collect(Collectors.toList());
getUnread(imTalkVOList);
return imTalkVOList;
}
@ -160,7 +150,7 @@ public class ImTalkServiceImpl extends ServiceImpl<ImTalkMapper, ImTalk> impleme
}
LambdaQueryWrapper<ImTalk> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.and(wq->{
wq.like(ImTalk::getUserId1, authUser.getStoreId()).or().like(ImTalk::getUserId2,authUser.getStoreId());
wq.eq(ImTalk::getUserId1, authUser.getStoreId()).or().eq(ImTalk::getUserId2,authUser.getStoreId());
});
queryWrapper.orderByDesc(ImTalk::getLastTalkTime);
List<ImTalk> imTalks = this.list(queryWrapper);
@ -174,12 +164,12 @@ public class ImTalkServiceImpl extends ServiceImpl<ImTalkMapper, ImTalk> impleme
/**
* 获取未读消息数量
* @param imTalkVOList
* @param imTalkVOList 消息列表
*/
private void getUnread(List<ImTalkVO> imTalkVOList){
if(imTalkVOList.size() > 0){
if(!imTalkVOList.isEmpty()){
for (ImTalkVO imTalkVO : imTalkVOList) {
long count = imMessageService.count(new LambdaQueryWrapper<ImMessage>().eq(ImMessage::getFromUser, imTalkVO.getUserId()).eq(ImMessage::getIsRead, false));
long count = imMessageService.count(new LambdaQueryWrapper<ImMessage>().eq(ImMessage::getFromUser, imTalkVO.getUserId()).eq(ImMessage::getTalkId,imTalkVO.getId()).eq(ImMessage::getIsRead, false));
imTalkVO.setUnread(count);
}
}

View File

@ -2,6 +2,7 @@ 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.vo.ResultMessage;
import cn.lili.modules.im.entity.dos.ImMessage;
import cn.lili.modules.im.entity.dto.MessageQueryParams;
@ -32,39 +33,39 @@ public class ImMessageController {
@ApiOperation(value = "查看Im消息详情")
public ResultMessage<ImMessage> get(@PathVariable String id) {
ImMessage imMessage = imMessageService.getById(id);
return new ResultUtil<ImMessage>().setData(imMessage);
return ResultUtil.data(imMessage);
}
@GetMapping
@ApiOperation(value = "分页获取Im消息")
public ResultMessage<List<ImMessage>> historyMessage(MessageQueryParams messageQueryParams) {
List<ImMessage> data = imMessageService.getList(messageQueryParams);
return new ResultUtil<List<ImMessage>>().setData(data);
return ResultUtil.data(data);
}
@PostMapping
@ApiOperation(value = "新增Im消息")
public ResultMessage<ImMessage> save(ImMessage imMessage) {
if (imMessageService.save(imMessage)) {
return new ResultUtil<ImMessage>().setData(imMessage);
return ResultUtil.data(imMessage);
}
return new ResultUtil<ImMessage>().setErrorMsg(ResultCode.ERROR);
throw new ServiceException(ResultCode.IM_MESSAGE_ADD_ERROR);
}
@PutMapping("/{id}")
@ApiOperation(value = "更新Im消息")
public ResultMessage<ImMessage> update(@PathVariable String id, ImMessage imMessage) {
if (imMessageService.updateById(imMessage)) {
return new ResultUtil<ImMessage>().setData(imMessage);
return ResultUtil.data(imMessage);
}
return new ResultUtil<ImMessage>().setErrorMsg(ResultCode.ERROR);
throw new ServiceException(ResultCode.IM_MESSAGE_EDIT_ERROR);
}
@DeleteMapping(value = "/{ids}")
@ApiOperation(value = "删除Im消息")
public ResultMessage<Object> delAllByIds(@PathVariable List ids) {
imMessageService.removeByIds(ids);
return ResultUtil.success(ResultCode.SUCCESS);
return ResultUtil.success();
}

View File

@ -2,16 +2,11 @@ package cn.lili.controller.im;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.enums.ResultUtil;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.im.entity.dos.ImTalk;
import cn.lili.modules.im.entity.vo.ImTalkVO;
import cn.lili.modules.im.service.ImTalkService;
import cn.lili.modules.store.service.StoreService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
@ -19,7 +14,6 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
/**
@ -34,43 +28,38 @@ public class ImTalkController {
private final ImTalkService imTalkService;
@Autowired
private StoreService storeService;
@GetMapping(value = "/{id}")
@ApiOperation(value = "查看聊天详情")
public ResultMessage<ImTalk> get(@PathVariable String id) {
ImTalk imTalk = imTalkService.getById(id);
return new ResultUtil<ImTalk>().setData(imTalk);
return ResultUtil.data(imTalk);
}
@GetMapping(value = "/user/{uid}")
@ApiOperation(value = "查看与某人聊天详情")
public ResultMessage<ImTalk> getUser(@PathVariable String uid) {
AuthUser authUser = UserContext.getCurrentUser();
return ResultUtil.data(imTalkService.getTalkByUser(authUser.getId(), uid));
//通过长度判断,保证每次都是同一个聊天
return ResultUtil.data(imTalkService.getTalkByUser(uid));
}
@GetMapping(value = "/by/user/{userId}")
@ApiOperation(value = "查看与某人聊天详情")
public ResultMessage<ImTalkVO> getByUser(@PathVariable String userId) {
AuthUser authUser = UserContext.getCurrentUser();
return ResultUtil.data(new ImTalkVO(imTalkService.getTalkByUser(authUser.getId(), userId), authUser.getId()));
return ResultUtil.data(new ImTalkVO(imTalkService.getTalkByUser(userId),userId));
}
@GetMapping(value = "/top")
@ApiOperation(value = "查看与某人聊天详情")
public ResultMessage top(String id, Boolean top) {
public ResultMessage<Object> top(String id, Boolean top) {
imTalkService.top(id, top);
return ResultUtil.success();
}
@GetMapping("/list")
@ApiOperation(value = "分页获取聊天")
@ApiImplicitParam(name = "userName", value = "用户名称", paramType = "query", dataType = "String")
public ResultMessage<List<ImTalkVO>> getUserTalkList(String userName) {
return ResultUtil.data(imTalkService.getUserTalkList(userName));
@ApiOperation(value = "分页获取用户聊天")
public ResultMessage<List<ImTalkVO>> getUserTalkList() {
return ResultUtil.data(imTalkService.getUserTalkList());
}
@GetMapping("/store/list")

View File

@ -1,80 +0,0 @@
package cn.lili.controller.im;
import cn.lili.common.enums.ResultUtil;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.member.entity.dos.Member;
import cn.lili.modules.member.entity.dto.FootPrintQueryParams;
import cn.lili.modules.member.service.FootprintService;
import cn.lili.modules.member.service.MemberService;
import cn.lili.modules.search.entity.dos.EsGoodsIndex;
import cn.lili.modules.store.entity.dos.Store;
import cn.lili.modules.store.service.StoreService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Chopper
*/
@RestController
@Api(tags = "Im消息接口")
@RequestMapping("/im/user")
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ImUserController {
private final MemberService memberService;
@Autowired
private StoreService storeService;
@Autowired
private FootprintService footprintService;
@GetMapping
@ApiOperation(value = "获取用户信息")
public ResultMessage<Member> getImUser() {
AuthUser authUser = UserContext.getCurrentUser();
return ResultUtil.data(memberService.getById(authUser.getId()));
}
@GetMapping("/store")
@ApiOperation(value = "获取店铺信息")
public ResultMessage<Store> getStoreUser() {
AuthUser authUser = UserContext.getCurrentUser();
return ResultUtil.data(storeService.getById(authUser.getStoreId()));
}
@GetMapping("/{memberId}")
@ApiImplicitParam(name = "memberId", value = "店铺Id", required = true, dataType = "String", paramType = "path")
@ApiOperation(value = "获取用户信息")
public ResultMessage<Member> getImUserDetail(@PathVariable String memberId) {
return ResultUtil.data(memberService.getById(memberId));
}
@GetMapping("/store/{storeId}")
@ApiImplicitParam(name = "storeId", value = "店铺Id", required = true, dataType = "String", paramType = "path")
@ApiOperation(value = "获取店铺信息")
public ResultMessage<Store> getStoreUserDetail(@PathVariable String storeId) {
return ResultUtil.data(storeService.getById(storeId));
}
@GetMapping("/history")
@ApiOperation(value = "获取会员的历史足迹")
public ResultMessage<IPage<EsGoodsIndex>> getMemberHistory(FootPrintQueryParams params) {
return ResultUtil.data(footprintService.footPrintPage(params));
}
}