diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/user/domain/enums/ApplyReadStatusEnum.java b/mallchat-common/src/main/java/com/abin/mallchat/common/user/domain/enums/ApplyReadStatusEnum.java new file mode 100644 index 0000000..5ee359c --- /dev/null +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/user/domain/enums/ApplyReadStatusEnum.java @@ -0,0 +1,22 @@ +package com.abin.mallchat.common.user.domain.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * @author : limeng + * @description : 申请阅读状态枚举 + * @date : 2023/07/20 + */ +@Getter +@AllArgsConstructor +public enum ApplyReadStatusEnum { + + UNREAD(1, "未读"), + + READ(2, "已读"); + + private final Integer code; + + private final String desc; +} diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/user/domain/enums/ApplyStatusEnum.java b/mallchat-common/src/main/java/com/abin/mallchat/common/user/domain/enums/ApplyStatusEnum.java new file mode 100644 index 0000000..21e7ab5 --- /dev/null +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/user/domain/enums/ApplyStatusEnum.java @@ -0,0 +1,22 @@ +package com.abin.mallchat.common.user.domain.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * @author : limeng + * @description : 申请状态枚举 + * @date : 2023/07/20 + */ +@Getter +@AllArgsConstructor +public enum ApplyStatusEnum { + + WAIT_APPROVAL(1, "待审批"), + + AGREE(2, "同意"); + + private final Integer code; + + private final String desc; +} diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/user/domain/enums/ApplyTypeEnum.java b/mallchat-common/src/main/java/com/abin/mallchat/common/user/domain/enums/ApplyTypeEnum.java new file mode 100644 index 0000000..54e5307 --- /dev/null +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/user/domain/enums/ApplyTypeEnum.java @@ -0,0 +1,21 @@ +package com.abin.mallchat.common.user.domain.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * @author : limeng + * @description : 申请类型枚举 + * @date : 2023/07/20 + */ +@Getter +@AllArgsConstructor +public enum ApplyTypeEnum { + + ADD_FRIEND(1, "加好友"); + + + private final Integer code; + + private final String desc; +} diff --git a/mallchat-common/src/main/resources/application-test.properties b/mallchat-common/src/main/resources/application-test.properties index a4967ae..dabc77d 100644 --- a/mallchat-common/src/main/resources/application-test.properties +++ b/mallchat-common/src/main/resources/application-test.properties @@ -12,9 +12,9 @@ mallchat.redis.password=123456 ##################jwt################## mallchat.jwt.secret=dsfsdfsdfsdfsd ##################微信公众号信息################## -mallchat.wx.callback=http://127.0.0.1:8080 -mallchat.wx.appId=appid -mallchat.wx.secret=380bfc1c9147fdsf4sf07 +mallchat.wx.callback=http://limeng-test.nat300.top/ +mallchat.wx.appId=wx6cb0974bff30cce0 +mallchat.wx.secret=072c615d9704d2e9bf0a048407928258 # 接口配置里的Token值 mallchat.wx.token=sdfsf # 接口配置里的EncodingAESKey值 diff --git a/mallchat-common/src/main/resources/application.yml b/mallchat-common/src/main/resources/application.yml index ae8591f..fc98e64 100644 --- a/mallchat-common/src/main/resources/application.yml +++ b/mallchat-common/src/main/resources/application.yml @@ -12,7 +12,7 @@ mybatis-plus: spring: profiles: #运行的环境 - active: my-prod + active: test application: name: mallchat datasource: diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/domain/vo/response/ChatMessageResp.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/domain/vo/response/ChatMessageResp.java index 096eaf7..9d40c8d 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/domain/vo/response/ChatMessageResp.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/domain/vo/response/ChatMessageResp.java @@ -5,7 +5,9 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import org.springframework.format.annotation.DateTimeFormat; +import java.time.LocalDateTime; import java.util.Date; /** @@ -24,6 +26,9 @@ public class ChatMessageResp { @ApiModelProperty("消息详情") private Message message; + @ApiModelProperty("申请消息") + private ApplyMessage applyMessage; + @Data public static class UserInfo { @ApiModelProperty("用户id") @@ -55,4 +60,18 @@ public class ChatMessageResp { @ApiModelProperty("该用户是否已经举报 0否 1是") private Integer userDislike; } + + @Data + public static class ApplyMessage { + @ApiModelProperty("申请id") + private Long applyId; + + @ApiModelProperty("申请信息") + private String applyMsg; + + @ApiModelProperty("申请发送时间") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") + private LocalDateTime sendTime; + + } } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/adapter/MessageAdapter.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/adapter/MessageAdapter.java index 38f0a3c..b1bdf8d 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/adapter/MessageAdapter.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/adapter/MessageAdapter.java @@ -6,12 +6,19 @@ import com.abin.mallchat.common.chat.domain.entity.MessageMark; import com.abin.mallchat.common.chat.domain.enums.MessageMarkTypeEnum; import com.abin.mallchat.common.chat.domain.enums.MessageStatusEnum; import com.abin.mallchat.common.common.domain.enums.YesOrNoEnum; +import com.abin.mallchat.common.user.domain.entity.UserApply; import com.abin.mallchat.custom.chat.domain.vo.request.ChatMessageReq; import com.abin.mallchat.custom.chat.domain.vo.response.ChatMessageResp; import com.abin.mallchat.custom.chat.service.strategy.msg.AbstractMsgHandler; import com.abin.mallchat.custom.chat.service.strategy.msg.MsgHandlerFactory; +import com.abin.mallchat.custom.user.domain.vo.response.ws.WSApplyMessage; -import java.util.*; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; import java.util.stream.Collectors; /** @@ -76,5 +83,20 @@ public class MessageAdapter { return userInfo; } + public static WSApplyMessage buildApplyResp(UserApply userApply) { + WSApplyMessage wsApplyMessage = new WSApplyMessage(); + wsApplyMessage.setApplyMessage(buildApplyMessage(userApply)); + wsApplyMessage.setFromUser(buildFromUser(userApply.getUid())); + return wsApplyMessage; + } + + private static ChatMessageResp.ApplyMessage buildApplyMessage(UserApply userApply) { + ChatMessageResp.ApplyMessage applyMessage = new ChatMessageResp.ApplyMessage(); + applyMessage.setApplyId(userApply.getId()); + applyMessage.setApplyMsg(userApply.getMsg()); + applyMessage.setSendTime(userApply.getCreateTime()); + return applyMessage; + } + } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/adapter/RoomAdapter.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/adapter/RoomAdapter.java index b91a308..58a69c4 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/adapter/RoomAdapter.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/adapter/RoomAdapter.java @@ -1,6 +1,7 @@ package com.abin.mallchat.custom.chat.service.adapter; import cn.hutool.core.bean.BeanUtil; +import com.abin.mallchat.common.chat.domain.entity.Room; import com.abin.mallchat.custom.chat.domain.vo.response.ChatRoomResp; import java.util.List; diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/controller/FriendController.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/controller/FriendController.java index b18126e..f3ae8fe 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/controller/FriendController.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/controller/FriendController.java @@ -17,12 +17,14 @@ import com.abin.mallchat.custom.user.domain.vo.request.friend.FriendDeleteReq; import com.abin.mallchat.custom.user.domain.vo.response.friend.FriendApplyResp; import com.abin.mallchat.custom.user.domain.vo.response.friend.FriendCheckResp; import com.abin.mallchat.custom.user.domain.vo.response.friend.FriendUnreadResp; +import com.abin.mallchat.custom.user.service.FriendService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import javax.annotation.Resource; import javax.validation.Valid; /** @@ -43,50 +45,57 @@ public class FriendController { @Autowired private UserCache userCache; + @Resource + private FriendService friendService; + @GetMapping("/check") @ApiOperation("批量判断是否是自己好友") - public ApiResult check(@Valid FriendCheckReq request) {//todo - return ApiResult.success(); + public ApiResult check(@Valid FriendCheckReq request) { + Long uid = RequestHolder.get().getUid(); + return ApiResult.success(friendService.check(uid, request)); } @PostMapping("/apply") @ApiOperation("申请好友") - public ApiResult apply(@Valid @RequestBody FriendApplyReq request) {//todo + public ApiResult apply(@Valid @RequestBody FriendApplyReq request) { Long uid = RequestHolder.get().getUid(); + friendService.apply(uid, request); return ApiResult.success(); } @DeleteMapping() @ApiOperation("删除好友") - public ApiResult delete(@Valid @RequestBody FriendDeleteReq request) {//todo + public ApiResult delete(@Valid @RequestBody FriendDeleteReq request) { + //todo Long uid = RequestHolder.get().getUid(); return ApiResult.success(); } @GetMapping("/apply/page") @ApiOperation("好友申请列表") - public ApiResult> page(@Valid PageBaseReq request) {//todo + public ApiResult> page(@Valid PageBaseReq request) { Long uid = RequestHolder.get().getUid(); - return ApiResult.success(); + return ApiResult.success(friendService.pageApplyFriend(uid, request)); } @GetMapping("/apply/unread") @ApiOperation("申请未读数") - public ApiResult unread() {//todo + public ApiResult unread() { Long uid = RequestHolder.get().getUid(); - return ApiResult.success(); + return ApiResult.success(friendService.unread(uid)); } @PutMapping("/apply") @ApiOperation("申请审批") - public ApiResult applyApprove(@Valid @RequestBody FriendApproveReq request) {//todo - Long uid = RequestHolder.get().getUid(); + public ApiResult applyApprove(@Valid @RequestBody FriendApproveReq request) { + friendService.applyApprove(request); return ApiResult.success(); } @PutMapping("/page") @ApiOperation("联系人列表") - public ApiResult> applyApprove(@Valid CursorPageBaseReq request) {//todo + public ApiResult> applyApprove(@Valid CursorPageBaseReq request) { + //todo Long uid = RequestHolder.get().getUid(); return ApiResult.success(); } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/enums/WSRespTypeEnum.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/enums/WSRespTypeEnum.java index 0ccfa2d..e8ab2fe 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/enums/WSRespTypeEnum.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/enums/WSRespTypeEnum.java @@ -26,6 +26,7 @@ public enum WSRespTypeEnum { BLACK(7, "拉黑用户", WSBlack.class), MARK(8, "消息标记", WSMsgMark.class), RECALL(9, "消息撤回", WSMsgRecall.class), + APPLY(10,"好友申请", WSApplyMessage.class), ; private final Integer type; diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/request/friend/FriendApproveReq.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/request/friend/FriendApproveReq.java index 3658c11..94a9b74 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/request/friend/FriendApproveReq.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/request/friend/FriendApproveReq.java @@ -21,7 +21,7 @@ import javax.validation.constraints.NotNull; public class FriendApproveReq { @NotNull - @ApiModelProperty("审批动作 2同意") - private Integer approveStatus; + @ApiModelProperty("申请id") + private Long applyId; } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/response/friend/FriendApplyResp.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/response/friend/FriendApplyResp.java index f2c701d..ed2bedb 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/response/friend/FriendApplyResp.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/response/friend/FriendApplyResp.java @@ -27,6 +27,6 @@ public class FriendApplyResp { @ApiModelProperty("申请信息") private String msg; - @ApiModelProperty("申请状态 1待审批 2同意")//todo 自己去加枚举 + @ApiModelProperty("申请状态 1待审批 2同意") private Integer status; } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/response/ws/WSApplyMessage.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/response/ws/WSApplyMessage.java new file mode 100644 index 0000000..192d33f --- /dev/null +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/domain/vo/response/ws/WSApplyMessage.java @@ -0,0 +1,14 @@ +package com.abin.mallchat.custom.user.domain.vo.response.ws; + +import com.abin.mallchat.custom.chat.domain.vo.response.ChatMessageResp; +import lombok.AllArgsConstructor; + +/** + * @author : limeng + * @description : 好友申请消息推送 + * @date : 2023/07/21 + */ +@AllArgsConstructor +public class WSApplyMessage extends ChatMessageResp { + +} diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/FriendService.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/FriendService.java new file mode 100644 index 0000000..dc8b0b5 --- /dev/null +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/FriendService.java @@ -0,0 +1,60 @@ +package com.abin.mallchat.custom.user.service; + +import com.abin.mallchat.common.common.domain.vo.request.PageBaseReq; +import com.abin.mallchat.common.common.domain.vo.response.PageBaseResp; +import com.abin.mallchat.custom.user.domain.vo.request.friend.FriendApplyReq; +import com.abin.mallchat.custom.user.domain.vo.request.friend.FriendApproveReq; +import com.abin.mallchat.custom.user.domain.vo.request.friend.FriendCheckReq; +import com.abin.mallchat.custom.user.domain.vo.response.friend.FriendApplyResp; +import com.abin.mallchat.custom.user.domain.vo.response.friend.FriendCheckResp; +import com.abin.mallchat.custom.user.domain.vo.response.friend.FriendUnreadResp; + +/** + * @author : limeng + * @description : 好友 + * @date : 2023/07/19 + */ +public interface FriendService { + + /** + * 检查 + * 检查是否是自己好友 + * + * @param request 请求 + * @param uid uid + * @return {@link FriendCheckResp} + */ + FriendCheckResp check(Long uid, FriendCheckReq request); + + /** + * 应用 + * 申请好友 + * + * @param request 请求 + * @param uid uid + */ + void apply(Long uid, FriendApplyReq request); + + /** + * 分页查询好友申请 + * + * @param request 请求 + * @return {@link PageBaseResp}<{@link FriendApplyResp}> + */ + PageBaseResp pageApplyFriend(Long uid, PageBaseReq request); + + /** + * 申请未读数 + * + * @return {@link FriendUnreadResp} + */ + FriendUnreadResp unread(Long uid); + + /** + * 同意好友申请 + * + * @param uid uid + * @param request 请求 + */ + void applyApprove(FriendApproveReq request); +} diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/WebSocketService.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/WebSocketService.java index 43b925a..f33571a 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/WebSocketService.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/WebSocketService.java @@ -66,5 +66,7 @@ public interface WebSocketService { */ void sendToAllOnline(WSBaseResp wsBaseResp); + void sendToFriend(WSBaseResp wsBaseResp, Long friendUid); + } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/adapter/WSAdapter.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/adapter/WSAdapter.java index 2a51379..7739026 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/adapter/WSAdapter.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/adapter/WSAdapter.java @@ -132,4 +132,11 @@ public class WSAdapter { wsBaseResp.setData(mark); return wsBaseResp; } + + public static WSBaseResp buildApplySend(WSApplyMessage resp) { + WSBaseResp wsBaseResp = new WSBaseResp<>(); + wsBaseResp.setType(WSRespTypeEnum.APPLY.getType()); + wsBaseResp.setData(resp); + return wsBaseResp; + } } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/FriendServiceImpl.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/FriendServiceImpl.java new file mode 100644 index 0000000..5c5343b --- /dev/null +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/FriendServiceImpl.java @@ -0,0 +1,172 @@ +package com.abin.mallchat.custom.user.service.impl; + +import com.abin.mallchat.common.common.domain.vo.request.PageBaseReq; +import com.abin.mallchat.common.common.domain.vo.response.PageBaseResp; +import com.abin.mallchat.common.user.domain.entity.UserApply; +import com.abin.mallchat.common.user.domain.entity.UserFriend; +import com.abin.mallchat.common.user.service.IUserApplyService; +import com.abin.mallchat.common.user.service.IUserFriendService; +import com.abin.mallchat.custom.chat.service.adapter.MessageAdapter; +import com.abin.mallchat.custom.user.domain.vo.request.friend.FriendApplyReq; +import com.abin.mallchat.custom.user.domain.vo.request.friend.FriendApproveReq; +import com.abin.mallchat.custom.user.domain.vo.request.friend.FriendCheckReq; +import com.abin.mallchat.custom.user.domain.vo.response.friend.FriendApplyResp; +import com.abin.mallchat.custom.user.domain.vo.response.friend.FriendCheckResp; +import com.abin.mallchat.custom.user.domain.vo.response.friend.FriendUnreadResp; +import com.abin.mallchat.custom.user.domain.vo.response.ws.WSApplyMessage; +import com.abin.mallchat.custom.user.service.FriendService; +import com.abin.mallchat.custom.user.service.WebSocketService; +import com.abin.mallchat.custom.user.service.adapter.WSAdapter; +import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; +import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper; +import com.google.common.collect.Lists; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + +import static com.abin.mallchat.common.user.domain.enums.ApplyReadStatusEnum.UNREAD; +import static com.abin.mallchat.common.user.domain.enums.ApplyStatusEnum.AGREE; +import static com.abin.mallchat.common.user.domain.enums.ApplyStatusEnum.WAIT_APPROVAL; +import static com.abin.mallchat.common.user.domain.enums.ApplyTypeEnum.ADD_FRIEND; + +/** + * @author : limeng + * @description : 好友 + * @date : 2023/07/19 + */ +@Slf4j +@Service +public class FriendServiceImpl implements FriendService { + + @Resource + private IUserFriendService friendService; + + @Resource + private IUserApplyService applyService; + + @Resource + private WebSocketService webSocketService; + + /** + * 检查 + * 检查是否是自己好友 + * + * @param uid uid + * @param request 请求 + * @return {@link FriendCheckResp} + */ + @Override + public FriendCheckResp check(Long uid, FriendCheckReq request) { + LambdaQueryChainWrapper wrapper = friendService.lambdaQuery(); + wrapper.eq(UserFriend::getUid, uid) + .in(UserFriend::getFriendUid, request.getUidList()); + List friendList = friendService.list(wrapper); + Map friendMap = friendList.stream().collect(Collectors.toMap(UserFriend::getFriendUid, friend -> friend)); + List friendCheckList = request.getUidList().stream().map(friendUid -> { + FriendCheckResp.FriendCheck friendCheck = new FriendCheckResp.FriendCheck(); + friendCheck.setUid(friendUid); + friendCheck.setIsFriend(friendMap.containsKey(friendUid)); + return friendCheck; + }).collect(Collectors.toList()); + return new FriendCheckResp(friendCheckList); + } + + /** + * 申请好友 + * + * @param request 请求 + */ + @Override + public void apply(Long uid, FriendApplyReq request) { + LambdaQueryChainWrapper wrapper = applyService.lambdaQuery(); + wrapper.eq(UserApply::getUid, uid) + .eq(UserApply::getTargetId, request.getTargetUid()); + UserApply userApply = applyService.getOne(wrapper); + if (Objects.nonNull(userApply)) { + log.info("已有好友申请记录,uid:{}, targetId:{}", uid, request.getTargetUid()); + return; + } + UserApply userApplyNew = new UserApply(); + userApplyNew.setUid(uid); + userApplyNew.setMsg(request.getMsg()); + userApplyNew.setType(ADD_FRIEND.getCode()); + userApplyNew.setTargetId(request.getTargetUid()); + userApplyNew.setStatus(WAIT_APPROVAL.getCode()); + userApplyNew.setReadStatus(UNREAD.getCode()); + applyService.save(userApplyNew); + + WSApplyMessage applyMessage = MessageAdapter.buildApplyResp(userApplyNew); + webSocketService.sendToFriend(WSAdapter.buildApplySend(applyMessage), request.getTargetUid()); + } + + /** + * 分页查询好友申请 + * + * @param request 请求 + * @return {@link PageBaseResp}<{@link FriendApplyResp}> + */ + @Override + public PageBaseResp pageApplyFriend(Long uid, PageBaseReq request) { + // todo 分页 + LambdaQueryChainWrapper wrapper = applyService.lambdaQuery(); + wrapper.eq(UserApply::getUid, uid) + .or() + .eq(UserApply::getTargetId, uid); + List userApplyList = applyService.list(wrapper); + List friendApplyResps = userApplyList.stream().map(userApply -> { + FriendApplyResp friendApplyResp = new FriendApplyResp(); + friendApplyResp.setUid(userApply.getUid()); + friendApplyResp.setType(userApply.getType()); + friendApplyResp.setMsg(userApply.getMsg()); + friendApplyResp.setStatus(userApply.getStatus()); + return friendApplyResp; + }).collect(Collectors.toList()); + PageBaseResp pageBaseResp = new PageBaseResp<>(); + pageBaseResp.setList(friendApplyResps); + return pageBaseResp; + } + + /** + * 申请未读数 + * + * @return {@link FriendUnreadResp} + */ + @Override + public FriendUnreadResp unread(Long uid) { + LambdaQueryChainWrapper wrapper = applyService.lambdaQuery(); + wrapper.eq(UserApply::getTargetId, uid) + .eq(UserApply::getReadStatus, UNREAD.getCode()); + return new FriendUnreadResp(applyService.count(wrapper)); + } + + @Override + @Transactional + public void applyApprove(FriendApproveReq request) { + UserApply userApply = applyService.getById(request.getApplyId()); + if (Objects.isNull(userApply)) { + log.error("不存在申请记录:{}", request.getApplyId()); + return; + } + if (Objects.equals(userApply.getStatus(), AGREE.getCode())) { + log.error("已同意好友申请:{}", request.getApplyId()); + return; + } + LambdaUpdateChainWrapper updateWrapper = applyService.lambdaUpdate(); + updateWrapper.set(UserApply::getStatus, AGREE.getCode()) + .eq(UserApply::getId, request.getApplyId()); + applyService.update(updateWrapper); + UserFriend userFriend1 = new UserFriend(); + userFriend1.setUid(userApply.getUid()); + userFriend1.setFriendUid(userApply.getTargetId()); + UserFriend userFriend2 = new UserFriend(); + userFriend2.setUid(userApply.getTargetId()); + userFriend2.setFriendUid(userApply.getUid()); + friendService.saveBatch(Lists.newArrayList(userFriend1, userFriend2)); + } +} diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/WebSocketServiceImpl.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/WebSocketServiceImpl.java index 64861e0..a27a64e 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/WebSocketServiceImpl.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/WebSocketServiceImpl.java @@ -257,6 +257,16 @@ public class WebSocketServiceImpl implements WebSocketService { sendToAllOnline(wsBaseResp, null); } + @Override + public void sendToFriend(WSBaseResp wsBaseResp, Long friendUid) { + CopyOnWriteArrayList channels = ONLINE_UID_MAP.get(friendUid); + if (CollectionUtil.isEmpty(channels)) { + log.info("用户:{}不在线", friendUid); + return; + } + threadPoolTaskExecutor.execute(() -> sendMsg(channels.get(0), wsBaseResp)); + } + private void sendMsg(Channel channel, WSBaseResp wsBaseResp) { channel.writeAndFlush(new TextWebSocketFrame(JSONUtil.toJsonStr(wsBaseResp))); }