From 5065105e2bb8ced538c5f3dbc6b313a0c9d937f4 Mon Sep 17 00:00:00 2001 From: Kkuil <3024067144@qq.com> Date: Tue, 24 Oct 2023 16:50:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=91=98=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/chat/dao/GroupMemberDao.java | 54 +++++++++++++++- .../chat/domain/enums/GroupRoleEnum.java | 3 + .../chat/service/IGroupMemberService.java | 1 + .../common/exception/GroupErrorEnum.java | 34 ++++++++++ .../resources/application-test.properties | 51 ++++++++------- .../custom/chat/constant/GroupConst.java | 15 +++++ .../chat/controller/RoomController.java | 14 ++++- .../chat/domain/vo/request/AdminAddReq.java | 25 ++++++++ .../chat/service/GroupMemberService.java | 18 ++++++ .../chat/service/impl/ChatServiceImpl.java | 24 +++---- .../service/impl/GroupMemberServiceImpl.java | 63 +++++++++++++++++++ .../mallchat/custom/ac/CreateTokenTest.java | 1 - 12 files changed, 261 insertions(+), 42 deletions(-) create mode 100644 mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/GroupErrorEnum.java create mode 100644 mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/constant/GroupConst.java create mode 100644 mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/domain/vo/request/AdminAddReq.java create mode 100644 mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/GroupMemberService.java create mode 100644 mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/GroupMemberServiceImpl.java diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/chat/dao/GroupMemberDao.java b/mallchat-common/src/main/java/com/abin/mallchat/common/chat/dao/GroupMemberDao.java index 307521f..9cbcbe7 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/chat/dao/GroupMemberDao.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/chat/dao/GroupMemberDao.java @@ -1,20 +1,24 @@ package com.abin.mallchat.common.chat.dao; +import cn.hutool.core.util.ObjectUtil; import com.abin.mallchat.common.chat.domain.entity.GroupMember; import com.abin.mallchat.common.chat.domain.enums.GroupRoleEnum; import com.abin.mallchat.common.chat.mapper.GroupMemberMapper; import com.abin.mallchat.common.chat.service.cache.GroupMemberCache; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; +import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import io.swagger.models.auth.In; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; -import java.util.HashMap; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import static com.abin.mallchat.common.chat.domain.enums.GroupRoleEnum.ADMIN_LIST; + /** *

* 群成员表 服务实现类 @@ -58,6 +62,7 @@ public class GroupMemberDao extends ServiceImpl List list = lambdaQuery() .eq(GroupMember::getGroupId, groupId) .in(GroupMember::getUid, uidList) + .in(GroupMember::getRole, ADMIN_LIST) .select(GroupMember::getUid, GroupMember::getRole) .list(); return list.stream().collect(Collectors.toMap(GroupMember::getUid, GroupMember::getRole)); @@ -88,4 +93,49 @@ public class GroupMemberDao extends ServiceImpl List memberUidList = groupMemberCache.getMemberUidList(roomId); return memberUidList.containsAll(uidList); } + + /** + * 是否是群主 + * + * @param id 群组ID + * @param uid 用户ID + * @return 是否是群主 + */ + public Boolean isLord(Long id, Long uid) { + GroupMember groupMember = this.lambdaQuery() + .eq(GroupMember::getGroupId, id) + .eq(GroupMember::getUid, uid) + .one(); + return ObjectUtil.isNotNull(groupMember); + } + + /** + * 获取管理员uid列表 + * + * @param id 群主ID + * @return 管理员uid列表 + */ + public List getManageUidList(Long id) { + return this.lambdaQuery() + .eq(GroupMember::getGroupId, id) + .eq(GroupMember::getRole, GroupRoleEnum.MANAGER.getType()) + .list() + .stream() + .map(GroupMember::getUid) + .collect(Collectors.toList()); + } + + /** + * 增加管理员 + * + * @param id 群主ID + * @param uidList 用户列表 + */ + public void addAdmin(Long id, List uidList) { + LambdaUpdateWrapper wrapper = new UpdateWrapper().lambda() + .eq(GroupMember::getGroupId, id) + .in(GroupMember::getUid, uidList) + .set(GroupMember::getRole, GroupRoleEnum.MANAGER.getType()); + this.update(wrapper); + } } diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/chat/domain/enums/GroupRoleEnum.java b/mallchat-common/src/main/java/com/abin/mallchat/common/chat/domain/enums/GroupRoleEnum.java index ccb8eea..b12feb9 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/chat/domain/enums/GroupRoleEnum.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/chat/domain/enums/GroupRoleEnum.java @@ -4,6 +4,7 @@ import lombok.AllArgsConstructor; import lombok.Getter; import java.util.Arrays; +import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; @@ -23,6 +24,8 @@ public enum GroupRoleEnum { private final Integer type; private final String desc; + + public static final List ADMIN_LIST = Arrays.asList(GroupRoleEnum.LEADER.getType(), GroupRoleEnum.MANAGER.getType()); private static Map cache; diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/chat/service/IGroupMemberService.java b/mallchat-common/src/main/java/com/abin/mallchat/common/chat/service/IGroupMemberService.java index 65190db..f61136b 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/chat/service/IGroupMemberService.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/chat/service/IGroupMemberService.java @@ -13,4 +13,5 @@ import com.baomidou.mybatisplus.extension.service.IService; */ public interface IGroupMemberService extends IService { + } diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/GroupErrorEnum.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/GroupErrorEnum.java new file mode 100644 index 0000000..f03a996 --- /dev/null +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/GroupErrorEnum.java @@ -0,0 +1,34 @@ +package com.abin.mallchat.common.common.exception; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * @Author Kkuil + * @Date 2023/10/24 15:50 + * @Description 群异常码 + */ +@AllArgsConstructor +@Getter +public enum GroupErrorEnum implements ErrorEnum { + /** + * + */ + GROUP_NOT_EXIST(9001, "该群不存在~"), + NOT_ALLOWED_OPERATION(9002, "您无权操作~"), + MANAGE_COUNT_EXCEED(9003, "群管理员数量达到上限,请先删除后再操作~"), + USER_NOT_IN_GROUP(9004, "非法操作,用户不存在群聊中~"), + ; + private final Integer code; + private final String msg; + + @Override + public Integer getErrorCode() { + return this.code; + } + + @Override + public String getErrorMsg() { + return this.msg; + } +} diff --git a/mallchat-common/src/main/resources/application-test.properties b/mallchat-common/src/main/resources/application-test.properties index 88a485c..e1a6fcf 100644 --- a/mallchat-common/src/main/resources/application-test.properties +++ b/mallchat-common/src/main/resources/application-test.properties @@ -1,40 +1,39 @@ -#todo ???????????? ##################mysql??################## -mallchat.mysql.ip=127.0.0.1 -mallchat.mysql.port=3306 +mallchat.mysql.ip=101.33.251.36 +mallchat.mysql.port=3307 mallchat.mysql.db=mallchat mallchat.mysql.username=root -mallchat.mysql.password=123456 +mallchat.mysql.password=123321@ ##################redis??################## -mallchat.redis.host=127.0.0.1 -mallchat.redis.port=6379 -mallchat.redis.password=123456 +mallchat.redis.host=101.33.251.36 +mallchat.redis.port=6378 +mallchat.redis.password=123321@ ##################jwt################## mallchat.jwt.secret=dsfsdfsdfsdfsd -##################???????################## -mallchat.wx.callback=http://limeng-test.nat300.top/ -mallchat.wx.appId=wx6cb0974bff30cce0 -mallchat.wx.secret=072c615d9704d2e9bf0a048407928258 +##################???????(????)################## +mallchat.wx.callback=http://709dj99328.goho.co:14042 +mallchat.wx.appId=wx53f618ad95af90b4 +mallchat.wx.secret=5f17417b434f5442ace0a101c38d5dda # ??????Token? -mallchat.wx.token=sdfsf -# ??????EncodingAESKey? -mallchat.wx.aesKey=sha1 +mallchat.wx.token=dssdfsdfs +# ??????EncodingAESKey???????????? +mallchat.wx.aesKey=TPn0rewrkQL1E2t ##################OSS??################## oss.enabled=true -oss.type=minio -oss.endpoint=http://localhost:9000 -oss.access-key=BEZ213 -oss.secret-key=Ii4vCMIXuFe241dsfEZ8e7RXI2342342kV -oss.bucketName=default +oss.type= minio +oss.endpoint=http://101.33.251.36:9002 +oss.access-key=cWAjkmXd9BL9XWlf +oss.secret-key=JZSOH7NLZJ7qmM8OEV0L4Cj49nJAa0Z1 +oss.bucketName=mallchat ##################rocketmq################## -rocketmq.name-server=127.0.0.1:9876 -rocketmq.access-key=root -rocketmq.secret-key=123456 -##################gpt??################## +rocketmq.name-server=101.33.251.36:9876 +rocketmq.access-key=mallchat +rocketmq.secret-key=12345678 +##################gpt??(????)################## mallchat.chatgpt.use=false mallchat.chatgpt.uid=10001 -mallchat.chatgpt.key=sk-wvWM0xGcxFfsddfsgxixbXK5tHovM -mallchat.chatgpt.proxyUrl=https://123.cc +mallchat.chatgpt.key=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 +mallchat.chatgpt.proxyUrl=https://test.mallchat.top/v1/completions mallchat.chatglm2.use=false -mallchat.chatglm2.url=http://v32134.cc +mallchat.chatglm2.url=http://vaso.ntap1.cc mallchat.chatglm2.uid=10002 diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/constant/GroupConst.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/constant/GroupConst.java new file mode 100644 index 0000000..7e76ba1 --- /dev/null +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/constant/GroupConst.java @@ -0,0 +1,15 @@ +package com.abin.mallchat.custom.chat.constant; + +/** + * @Author Kkuil + * @Date 2023/10/24 16:06 + * @Description 群常量 + */ +public class GroupConst { + + /** + * 最大群管理员数量 + */ + public static final int MAX_MANAGE_COUNT = 3; + +} diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/controller/RoomController.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/controller/RoomController.java index fe280e5..1b439ad 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/controller/RoomController.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/controller/RoomController.java @@ -1,6 +1,7 @@ package com.abin.mallchat.custom.chat.controller; +import com.abin.mallchat.common.chat.service.IGroupMemberService; import com.abin.mallchat.common.common.domain.vo.request.IdReqVO; import com.abin.mallchat.common.common.domain.vo.response.ApiResult; import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp; @@ -10,6 +11,7 @@ import com.abin.mallchat.common.user.domain.vo.response.ws.ChatMemberResp; import com.abin.mallchat.custom.chat.domain.vo.request.*; import com.abin.mallchat.custom.chat.domain.vo.response.ChatMemberListResp; import com.abin.mallchat.custom.chat.domain.vo.response.MemberResp; +import com.abin.mallchat.custom.chat.service.GroupMemberService; import com.abin.mallchat.custom.chat.service.RoomAppService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -35,6 +37,8 @@ import java.util.List; public class RoomController { @Autowired private RoomAppService roomService; + @Autowired + private GroupMemberService groupMemberService; @GetMapping("/public/group") @ApiOperation("群组详情") @@ -62,7 +66,6 @@ public class RoomController { roomService.delMember(uid, request); return ApiResult.success(); } - @PostMapping("/group") @ApiOperation("新增群组") public ApiResult addGroup(@Valid @RequestBody GroupAddReq request) { @@ -78,5 +81,12 @@ public class RoomController { roomService.addMember(uid, request); return ApiResult.success(); } -} + @PutMapping("/group/admin") + @ApiOperation("添加管理员") + public ApiResult addAdmin(@Valid @RequestBody AdminAddReq request) { + Long uid = RequestHolder.get().getUid(); + groupMemberService.addAdmin(uid, request); + return ApiResult.success(); + } +} diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/domain/vo/request/AdminAddReq.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/domain/vo/request/AdminAddReq.java new file mode 100644 index 0000000..3609881 --- /dev/null +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/domain/vo/request/AdminAddReq.java @@ -0,0 +1,25 @@ +package com.abin.mallchat.custom.chat.domain.vo.request; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; +import java.util.List; + +/** + * @Author Kkuil + * @Date 2023/10/24 12:46 + * @Description 添加管理员请求信息 + */ +@Data +public class AdminAddReq { + @NotNull + @ApiModelProperty("房间号") + private Long roomId; + + @NotNull + @Size(min = 1, max = 3) + @ApiModelProperty("需要添加管理的列表") + private List uidList; +} diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/GroupMemberService.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/GroupMemberService.java new file mode 100644 index 0000000..3ee309e --- /dev/null +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/GroupMemberService.java @@ -0,0 +1,18 @@ +package com.abin.mallchat.custom.chat.service; + +import com.abin.mallchat.custom.chat.domain.vo.request.AdminAddReq; + +/** + * @Author Kkuil + * @Date 2023/10/24 15:45 + * @Description 群成员服务接口 + */ +public interface GroupMemberService { + /** + * 增加管理员 + * + * @param uid 用户ID + * @param request 请求信息 + */ + void addAdmin(Long uid, AdminAddReq request); +} diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/ChatServiceImpl.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/ChatServiceImpl.java index d25861e..fa4b659 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/ChatServiceImpl.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/ChatServiceImpl.java @@ -151,28 +151,30 @@ public class ChatServiceImpl implements ChatService { String timeCursor = pair.getValue(); List resultList = new ArrayList<>();// 最终列表 Boolean isLast = Boolean.FALSE; - if (activeStatusEnum == ChatActiveStatusEnum.ONLINE) {//在线列表 + if (activeStatusEnum == ChatActiveStatusEnum.ONLINE) {// 在线列表 CursorPageBaseResp cursorPage = userDao.getCursorPage(memberUidList, new CursorPageBaseReq(request.getPageSize(), timeCursor), ChatActiveStatusEnum.ONLINE); - resultList.addAll(MemberAdapter.buildMember(request.getRoomId(), cursorPage.getList()));//添加在线列表 - if (cursorPage.getIsLast()) {//如果是最后一页,从离线列表再补点数据 + resultList.addAll(MemberAdapter.buildMember(request.getRoomId(), cursorPage.getList()));// 添加在线列表 + if (cursorPage.getIsLast()) {// 如果是最后一页,从离线列表再补点数据 activeStatusEnum = ChatActiveStatusEnum.OFFLINE; Integer leftSize = request.getPageSize() - cursorPage.getList().size(); cursorPage = userDao.getCursorPage(memberUidList, new CursorPageBaseReq(leftSize, null), ChatActiveStatusEnum.OFFLINE); - resultList.addAll(MemberAdapter.buildMember(request.getRoomId(), cursorPage.getList()));//添加离线线列表 + resultList.addAll(MemberAdapter.buildMember(request.getRoomId(), cursorPage.getList()));// 添加离线线列表 } timeCursor = cursorPage.getCursor(); isLast = cursorPage.getIsLast(); - } else if (activeStatusEnum == ChatActiveStatusEnum.OFFLINE) {//离线列表 + } else if (activeStatusEnum == ChatActiveStatusEnum.OFFLINE) {// 离线列表 CursorPageBaseResp cursorPage = userDao.getCursorPage(memberUidList, new CursorPageBaseReq(request.getPageSize(), timeCursor), ChatActiveStatusEnum.OFFLINE); - resultList.addAll(MemberAdapter.buildMember(request.getRoomId(), cursorPage.getList()));//添加离线线列表 + resultList.addAll(MemberAdapter.buildMember(request.getRoomId(), cursorPage.getList()));// 添加离线线列表 timeCursor = cursorPage.getCursor(); isLast = cursorPage.getIsLast(); } - // 获取群成员角色ID - List uidList = resultList.stream().map(ChatMemberResp::getUid).collect(Collectors.toList()); - RoomGroup roomGroup = roomGroupDao.getByRoomId(request.getRoomId()); - Map uidMapRole = groupMemberDao.getMemberMapRole(roomGroup.getId(), uidList); - resultList.forEach(member -> member.setRoleId(uidMapRole.get(member.getUid()))); + if (!roomDao.getById(request.getRoomId()).isHotRoom()) { + // 获取群成员角色ID + List uidList = resultList.stream().map(ChatMemberResp::getUid).collect(Collectors.toList()); + RoomGroup roomGroup = roomGroupDao.getByRoomId(request.getRoomId()); + Map uidMapRole = groupMemberDao.getMemberMapRole(roomGroup.getId(), uidList); + resultList.forEach(member -> member.setRoleId(uidMapRole.get(member.getUid()))); + } // 组装结果 return new CursorPageBaseResp<>(ChatMemberHelper.generateCursor(activeStatusEnum, timeCursor), isLast, resultList); } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/GroupMemberServiceImpl.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/GroupMemberServiceImpl.java new file mode 100644 index 0000000..322b86d --- /dev/null +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/chat/service/impl/GroupMemberServiceImpl.java @@ -0,0 +1,63 @@ +package com.abin.mallchat.custom.chat.service.impl; + +import com.abin.mallchat.common.chat.dao.GroupMemberDao; +import com.abin.mallchat.common.chat.dao.RoomGroupDao; +import com.abin.mallchat.common.chat.domain.entity.RoomGroup; +import com.abin.mallchat.common.common.exception.GroupErrorEnum; +import com.abin.mallchat.common.common.utils.AssertUtil; +import com.abin.mallchat.custom.chat.domain.vo.request.AdminAddReq; +import com.abin.mallchat.custom.chat.service.GroupMemberService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.HashSet; +import java.util.List; + +import static com.abin.mallchat.custom.chat.constant.GroupConst.MAX_MANAGE_COUNT; + +/** + * @Author Kkuil + * @Date 2023/10/24 15:45 + * @Description 群成员服务类 + */ +@Service +public class GroupMemberServiceImpl implements GroupMemberService { + + @Autowired + private GroupMemberDao groupMemberDao; + + @Autowired + private RoomGroupDao roomGroupDao; + + /** + * 增加管理员 + * + * @param uid 用户ID + * @param request 请求信息 + */ + @Override + public void addAdmin(Long uid, AdminAddReq request) { + // 1. 判断群聊是否存在 + RoomGroup roomGroup = roomGroupDao.getByRoomId(request.getRoomId()); + AssertUtil.isNotEmpty(roomGroup, GroupErrorEnum.GROUP_NOT_EXIST); + + // 2. 判断该用户是否是群主 + Boolean isLord = groupMemberDao.isLord(roomGroup.getId(), uid); + AssertUtil.isTrue(isLord, GroupErrorEnum.NOT_ALLOWED_OPERATION); + + // 3. 判断群成员是否在群中 + Boolean isGroupShip = groupMemberDao.isGroupShip(roomGroup.getRoomId(), request.getUidList()); + AssertUtil.isTrue(isGroupShip, GroupErrorEnum.USER_NOT_IN_GROUP); + + // 4. 判断管理员数量是否达到上限 + // 4.1 查询现有管理员数量 + List manageUidList = groupMemberDao.getManageUidList(roomGroup.getId()); + // 4.2 去重 + HashSet manageUidSet = new HashSet<>(manageUidList); + manageUidSet.addAll(request.getUidList()); + AssertUtil.isFalse(manageUidSet.size() > MAX_MANAGE_COUNT, GroupErrorEnum.MANAGE_COUNT_EXCEED); + + // 5. 增加管理员 + groupMemberDao.addAdmin(roomGroup.getId(), request.getUidList()); + } +} diff --git a/mallchat-custom-server/src/test/java/com/abin/mallchat/custom/ac/CreateTokenTest.java b/mallchat-custom-server/src/test/java/com/abin/mallchat/custom/ac/CreateTokenTest.java index aace46f..4fcc4b4 100644 --- a/mallchat-custom-server/src/test/java/com/abin/mallchat/custom/ac/CreateTokenTest.java +++ b/mallchat-custom-server/src/test/java/com/abin/mallchat/custom/ac/CreateTokenTest.java @@ -22,7 +22,6 @@ public class CreateTokenTest { .sign(Algorithm.HMAC256("dsfsdfsdfsdfsd")); // signature log.info("生成的token为 {}",token); - try { JWTVerifier verifier = JWT.require(Algorithm.HMAC256("dsfsdfsdfsdfsd")).build(); DecodedJWT jwt = verifier.verify(token);