From bca839bfcf5122d47dc07ca7c6cfd7bab6732d4b Mon Sep 17 00:00:00 2001 From: wanggeng888 <450292408@qq.com> Date: Wed, 20 Jan 2021 12:37:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EIM=E7=BE=A4=E6=88=90=E5=91=98?= =?UTF-8?q?=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apis/user/UserImController.java | 8 ++ .../oauth/service/user/IUserImService.java | 2 + .../service/user/impl/UserImServiceImpl.java | 134 ++++++++++++++---- 3 files changed, 118 insertions(+), 26 deletions(-) diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/user/UserImController.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/user/UserImController.java index a9c6e47..e8a7528 100644 --- a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/user/UserImController.java +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/user/UserImController.java @@ -8,6 +8,7 @@ import io.swagger.annotations.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** @@ -35,4 +36,11 @@ public class UserImController { return userImService.getInitData(); } + @ApiOperation(value = "获取组成员数据", notes = "获取组成员数据接口") + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @GetMapping("getmemberdata") + public SuccessResultLayImData getMemberData(@RequestParam(name = "id", required = true) String departmentId) { + return userImService.getMemberData(departmentId); + } + } diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/user/IUserImService.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/user/IUserImService.java index 030a160..44e024f 100644 --- a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/user/IUserImService.java +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/user/IUserImService.java @@ -31,4 +31,6 @@ public interface IUserImService { * @return */ List listDepartmentUser(Map params); + + SuccessResultLayImData getMemberData(String departmentId); } diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/user/impl/UserImServiceImpl.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/user/impl/UserImServiceImpl.java index 6e90ce5..03bcd3d 100644 --- a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/user/impl/UserImServiceImpl.java +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/user/impl/UserImServiceImpl.java @@ -9,6 +9,7 @@ import com.cm.common.plugin.oauth.service.department.IDepartmentService; import com.cm.common.plugin.oauth.service.user.IUserImService; import com.cm.common.plugin.oauth.service.user.IUserService; import com.cm.common.plugin.oauth.token.ClientTokenManager; +import com.cm.common.plugin.pojo.bos.user.UserDepartmentResourceBO; import com.cm.common.plugin.utils.RestTemplateUtil; import com.cm.common.pojo.bos.UserInfoBO; import com.cm.common.result.SuccessResultLayImData; @@ -16,9 +17,7 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; +import java.util.*; /** * When you feel like quitting. Think about why you started @@ -44,17 +43,80 @@ public class UserImServiceImpl extends AbstractService implements IUserImService @Override public SuccessResultLayImData getInitData() { - UserInfoBO currentUser = securityComponent.getCurrentUser(); - SuccessResultLayImData.User mine = new SuccessResultLayImData.User(); - mine.setId(currentUser.getUserId()); - mine.setUsername(currentUser.getUserName()); - mine.setAvatar("assets/images/random-avatar1.jpg"); - mine.setStatus("online"); - mine.setSign("暂无签名"); + try { + UserInfoBO currentUser = securityComponent.getCurrentUser(); + SuccessResultLayImData.User mine = new SuccessResultLayImData.User(); + mine.setId(currentUser.getUserId()); + mine.setUsername(currentUser.getUserName()); + mine.setAvatar("assets/images/random-avatar1.jpg"); + mine.setStatus("online"); + mine.setSign("暂无签名"); - Map params = getHashMap(2); + Map params = getHashMap(2); - List friends = listDepartmentUser(params); + // 用户所在部门ID集合 + Set userFriendIdSet = new HashSet<>(); + List friends = listDepartmentUser(params); + setFriends(currentUser, mine, friends, userFriendIdSet); + + List groups = new ArrayList<>(); + setGroups(currentUser, friends, groups, userFriendIdSet); + + SuccessResultLayImData.InitData initData = new SuccessResultLayImData.InitData(); + initData.setMine(mine); + initData.setFriend(friends); + initData.setGroup(groups); + return new SuccessResultLayImData<>(0, "", initData); + } catch (Exception e) { + LOG.error(e.getMessage(), e); + return new SuccessResultLayImData<>(400, e.getMessage(), null); + } + } + + @Override + public List listDepartmentUser(Map params) { + params.put(IApiConsts.ACCESS_TOKEN, ClientTokenManager.getInstance().getClientToken().getAccessToken()); + String result = restTemplateUtil.doGetFormNormal(String.format(IApiConsts.LIST_LAYIM_DEPARTMENT_USER, apiPathProperties.getUserCenter()), params); + searchResourceResult(result, "获取人员列表失败"); + return JSONArray.parseArray(result, SuccessResultLayImData.Friend.class); + } + + @Override + public SuccessResultLayImData getMemberData(String departmentId) { + try { + List departmentIds = new ArrayList<>(); + departmentIds.add(departmentId); + List userDepartmentResourceBOS = userService.listUserDepartmentResourceByDepartmentIds(departmentIds); + SuccessResultLayImData.MemberData memberData = new SuccessResultLayImData.MemberData(); + List list = new ArrayList<>(); + memberData.setList(list); + for (UserDepartmentResourceBO userDepartmentResourceBO : userDepartmentResourceBOS) { + SuccessResultLayImData.User user = new SuccessResultLayImData.User(); + user.setId(userDepartmentResourceBO.getUserId()); + user.setUsername(userDepartmentResourceBO.getUserName()); + user.setSign("暂无签名"); + if (StringUtils.isBlank(userDepartmentResourceBO.getUserAvatar())) { + user.setAvatar("assets/images/chat_user.png"); + } else { + user.setAvatar(apiPathProperties.getUserCenter() + "/route/file/downloadfile/true/" + user.getAvatar()); + } + list.add(user); + } + return new SuccessResultLayImData<>(0, "", memberData); + } catch (Exception e) { + LOG.error(e.getMessage(), e); + return new SuccessResultLayImData<>(400, e.getMessage(), null); + } + } + + /** + * 设置朋友列表 + * + * @param currentUser + * @param mine + * @param friends + */ + private void setFriends(UserInfoBO currentUser, SuccessResultLayImData.User mine, List friends, Set userFriendIdSet) { // 非管理员添加系统管理员 if (!StringUtils.equals(ISystemConstant.ADMIN, currentUser.getUserUsername())) { // 加载管理员 @@ -76,15 +138,17 @@ public class UserImServiceImpl extends AbstractService implements IUserImService SuccessResultLayImData.User user = friend.getList().get(j); // 删除自己 if (StringUtils.equals(user.getId(), mine.getId())) { + userFriendIdSet.add(friend.getId()); friend.getList().remove(j); j--; continue; } if (StringUtils.isBlank(user.getAvatar())) { - user.setAvatar("assets/images/random-avatar1.jpg"); + user.setAvatar("assets/images/chat_user.png"); } else { user.setAvatar(apiPathProperties.getUserCenter() + "/route/file/downloadfile/true/" + user.getAvatar()); } + // 默认离线 user.setStatus("offline"); user.setSign("暂无签名"); } @@ -94,20 +158,38 @@ public class UserImServiceImpl extends AbstractService implements IUserImService i--; } } - - SuccessResultLayImData.InitData initData = new SuccessResultLayImData.InitData(); - initData.setMine(mine); - initData.setFriend(friends); - initData.setGroup(new ArrayList<>()); - return new SuccessResultLayImData<>(0, "", initData); } - @Override - public List listDepartmentUser(Map params) { - params.put(IApiConsts.ACCESS_TOKEN, ClientTokenManager.getInstance().getClientToken().getAccessToken()); - String result = restTemplateUtil.doGetFormNormal(String.format(IApiConsts.LIST_LAYIM_DEPARTMENT_USER, apiPathProperties.getUserCenter()), params); - searchResourceResult(result, "获取人员列表失败"); - List friends = JSONArray.parseArray(result, SuccessResultLayImData.Friend.class); - return friends; + /** + * 设置组 + * + * @param currentUser + * @param friends + * @param groups + * @param userFriendIdSet + */ + private void setGroups(UserInfoBO currentUser, List friends, List groups, Set userFriendIdSet) { + // 管理员添加所有组 + if (StringUtils.equals(ISystemConstant.ADMIN, currentUser.getUserUsername())) { + for (SuccessResultLayImData.Friend friend : friends) { + SuccessResultLayImData.Group group = new SuccessResultLayImData.Group(); + group.setId(friend.getId()); + group.setGroupname(friend.getGroupname()); + group.setAvatar("assets/images/chat_group.png"); + groups.add(group); + } + return; + } + // 非管理员添加自己所在部门 + for (SuccessResultLayImData.Friend friend : friends) { + if (!userFriendIdSet.contains(friend.getId())) { + continue; + } + SuccessResultLayImData.Group group = new SuccessResultLayImData.Group(); + group.setId(friend.getId()); + group.setGroupname(friend.getGroupname()); + group.setAvatar("assets/images/chat_group.png"); + groups.add(group); + } } }