From 91c8b32db7e87bcd5458a77b36376ecc997223b1 Mon Sep 17 00:00:00 2001 From: xsx <825657193@qq.com> Date: Thu, 5 Oct 2023 01:55:58 +0800 Subject: [PATCH] =?UTF-8?q?uniapp=20=E6=B7=BB=E5=8A=A0=E3=80=81=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=A5=BD=E5=8F=8B=E4=BB=A5=E5=8F=8A=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../implatform/controller/UserController.java | 8 +- .../bx/implatform/service/IUserService.java | 4 +- .../service/impl/UserServiceImpl.java | 27 ++++++- im-uniapp/App.vue | 35 ++++---- im-uniapp/components/chat-item/chat-item.vue | 1 + .../components/friend-item/friend-item.vue | 1 + im-uniapp/pages.json | 48 ++--------- im-uniapp/pages/chat/chat-box.vue | 8 ++ im-uniapp/pages/chat/chat.vue | 26 +++++- im-uniapp/pages/common/user-info.vue | 79 ++++++++++++++++--- im-uniapp/pages/friend/friend-search.vue | 46 ++++++----- im-uniapp/pages/friend/friend.vue | 7 +- im-uniapp/store/chatStore.js | 6 ++ im-uniapp/store/friendStore.js | 27 ++----- 14 files changed, 205 insertions(+), 118 deletions(-) diff --git a/im-platform/src/main/java/com/bx/implatform/controller/UserController.java b/im-platform/src/main/java/com/bx/implatform/controller/UserController.java index 685fbfd..bbdeace 100644 --- a/im-platform/src/main/java/com/bx/implatform/controller/UserController.java +++ b/im-platform/src/main/java/com/bx/implatform/controller/UserController.java @@ -64,8 +64,14 @@ public class UserController { @GetMapping("/findByNickName") @ApiOperation(value = "查找用户",notes="根据昵称查找用户") - public Result findByNickName(@NotEmpty(message = "用户昵称不可为空") @RequestParam("nickName") String nickName){ + public Result> findByNickName(@NotEmpty(message = "用户昵称不可为空") @RequestParam("nickName") String nickName){ return ResultUtils.success( userService.findUserByNickName(nickName)); } + + @GetMapping("/findByName") + @ApiOperation(value = "查找用户",notes="根据用户名或昵称查找用户") + public Result> findByName(@NotEmpty(message = "用户名称不可为空") @RequestParam("name") String name){ + return ResultUtils.success( userService.findUserByName(name)); + } } diff --git a/im-platform/src/main/java/com/bx/implatform/service/IUserService.java b/im-platform/src/main/java/com/bx/implatform/service/IUserService.java index 4e48398..4ff8c40 100644 --- a/im-platform/src/main/java/com/bx/implatform/service/IUserService.java +++ b/im-platform/src/main/java/com/bx/implatform/service/IUserService.java @@ -18,12 +18,14 @@ public interface IUserService extends IService { void register(RegisterDTO dto); - User findUserByName(String username); + User findUserByUserName(String username); void update(UserVO vo); List findUserByNickName(String nickname); + List findUserByName(String name); + List checkOnline(String userIds); } diff --git a/im-platform/src/main/java/com/bx/implatform/service/impl/UserServiceImpl.java b/im-platform/src/main/java/com/bx/implatform/service/impl/UserServiceImpl.java index 4e47101..543083a 100644 --- a/im-platform/src/main/java/com/bx/implatform/service/impl/UserServiceImpl.java +++ b/im-platform/src/main/java/com/bx/implatform/service/impl/UserServiceImpl.java @@ -66,7 +66,7 @@ public class UserServiceImpl extends ServiceImpl implements IU @Override public LoginVO login(LoginDTO dto) { - User user = findUserByName(dto.getUserName()); + User user = this.findUserByUserName(dto.getUserName()); if(null == user){ throw new GlobalException(ResultCode.PROGRAM_ERROR,"用户不存在"); } @@ -119,7 +119,7 @@ public class UserServiceImpl extends ServiceImpl implements IU */ @Override public void register(RegisterDTO dto) { - User user = findUserByName(dto.getUserName()); + User user = this.findUserByUserName(dto.getUserName()); if(null != user){ throw new GlobalException(ResultCode.USERNAME_ALREADY_REGISTER); } @@ -136,12 +136,13 @@ public class UserServiceImpl extends ServiceImpl implements IU * @return */ @Override - public User findUserByName(String username) { + public User findUserByUserName(String username) { LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); queryWrapper.eq(User::getUserName,username); return this.getOne(queryWrapper); } + /** * 更新用户信息,好友昵称和群聊昵称等冗余信息也会更新 * @@ -206,6 +207,26 @@ public class UserServiceImpl extends ServiceImpl implements IU }).collect(Collectors.toList()); } + /** + * 根据用户昵称查询用户,最多返回20条数据 + * + * @param name 用户名或昵称 + * @return + */ + @Override + public List findUserByName(String name) { + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); + queryWrapper.like(User::getUserName,name) + .or() + .like(User::getNickName,name) + .last("limit 20"); + List users = this.list(queryWrapper); + return users.stream().map(u-> { + UserVO vo = BeanUtils.copyProperties(u,UserVO.class); + vo.setOnline(imClient.isOnline(u.getId())); + return vo; + }).collect(Collectors.toList()); + } /** * 判断用户是否在线,返回在线的用户id列表 diff --git a/im-uniapp/App.vue b/im-uniapp/App.vue index 2e57e5e..53b4b1b 100644 --- a/im-uniapp/App.vue +++ b/im-uniapp/App.vue @@ -1,10 +1,17 @@ diff --git a/im-uniapp/pages/chat/chat.vue b/im-uniapp/pages/chat/chat.vue index a5d8a56..c1b6692 100644 --- a/im-uniapp/pages/chat/chat.vue +++ b/im-uniapp/pages/chat/chat.vue @@ -16,7 +16,31 @@ } }, methods: { - + refreshUnreadBadge(){ + if(this.unreadCount>0){ + uni.setTabBarBadge({ + index: 0, + text: this.unreadCount+"" + }) + }else{ + uni.removeTabBarBadge({ + index:0 + }) + + } + } + }, + computed:{ + unreadCount(){ + let count = 0; + this.$store.state.chatStore.chats.forEach(chat =>{ + count += chat.unreadCount; + }) + return count; + } + }, + onLoad() { + this.refreshUnreadBadge(); } } diff --git a/im-uniapp/pages/common/user-info.vue b/im-uniapp/pages/common/user-info.vue index 551455f..929be5f 100644 --- a/im-uniapp/pages/common/user-info.vue +++ b/im-uniapp/pages/common/user-info.vue @@ -24,8 +24,9 @@ - - + + + @@ -38,7 +39,7 @@ } }, methods: { - sendMessage() { + onSendMessage() { let chat = { type: 'PRIVATE', targetId: this.userInfo.id, @@ -50,7 +51,7 @@ url:"/pages/chat/chat-box?chatIdx=0" }) }, - addFriend() { + onAddFriend() { this.$http({ url: "/friend/add?friendId=" + this.userInfo.id, method: "POST" @@ -63,27 +64,74 @@ } this.$store.commit("addFriend", friend); uni.showToast({ - title: '添加成功,对方已成为您的好友', + title: '对方已成为您的好友', icon: 'none' }) }) + }, + onDelFriend(){ + console.log(this.userInfo) + uni.showModal({ + title: "确认删除", + content: `确认要删除与 '${this.userInfo.nickName}'的好友关系吗?`, + success: ()=> { + this.$http({ + url: `/friend/delete/${this.userInfo.id}`, + method: 'delete' + }).then((data) => { + this.$store.commit("removeFriend", this.userInfo.id); + this.$store.commit("removePrivateChat", this.userInfo.id); + uni.showToast({ + title: `与 '${this.userInfo.nickName}'的好友关系已解除`, + icon: 'none' + }) + }) + } + }) + }, + updateFriendInfo() { + // store的数据不能直接修改,深拷贝一份store的数据 + let friend = JSON.parse(JSON.stringify(this.friendInfo)); + friend.headImage = this.userInfo.headImageThumb; + friend.nickName = this.userInfo.nickName; + this.$http({ + url: "/friend/update", + method: "PUT", + data: friend + }).then(() => { + // 更新好友列表中的昵称和头像 + this.$store.commit("updateFriend", friend); + // 更新会话中的头像和昵称 + this.$store.commit("updateChatFromFriend", this.userInfo); + }) + }, + loadUserInfo(id){ + this.$http({ + url: "/user/find/" + id, + method: 'GET' + }).then((user) => { + this.userInfo = user; + // 如果发现好友的头像和昵称改了,进行更新 + if (this.isFriend &&this.userInfo.headImageThumb != this.friendInfo.headImage || + this.userInfo.nickName != this.friendInfo.nickName) { + this.updateFriendInfo() + } + }) } }, computed: { isFriend() { + return this.friendInfo != undefined; + }, + friendInfo(){ let friends = this.$store.state.friendStore.friends; let friend = friends.find((f) => f.id == this.userInfo.id); - return friend != undefined; + return friend; } }, onLoad(options) { - // 查询好友信息 - const id = options.id; - this.$http({ - url: "/user/find/" + id - }).then((userInfo) => { - this.userInfo = userInfo; - }) + // 查询用户信息 + this.loadUserInfo(options.id); } } @@ -136,6 +184,11 @@ .btn-group { margin: 100rpx; + + .btn{ + margin-top: 20rpx; + } + } } \ No newline at end of file diff --git a/im-uniapp/pages/friend/friend-search.vue b/im-uniapp/pages/friend/friend-search.vue index 2a5f7e5..20aa91a 100644 --- a/im-uniapp/pages/friend/friend-search.vue +++ b/im-uniapp/pages/friend/friend-search.vue @@ -1,14 +1,15 @@ @@ -23,19 +24,28 @@ onCancel(){ uni.navigateBack(); } - - }, - onNavigationBarButtonTap(e) { - if(e.index==0){ - uni.navigateBack(); - } - }, - onNavigationBarSearchInputChanged(e){ - this.searchText = e.text; } } - + .search-bar { + background: white; + } + .friend-items{ + position: relative; + flex: 1; + overflow: hidden; + .scroll-bar { + height: 100%; + } + } + + } + \ No newline at end of file diff --git a/im-uniapp/pages/friend/friend.vue b/im-uniapp/pages/friend/friend.vue index a244465..e063b25 100644 --- a/im-uniapp/pages/friend/friend.vue +++ b/im-uniapp/pages/friend/friend.vue @@ -4,7 +4,7 @@ - + @@ -30,6 +30,11 @@ uni.navigateTo({ url: "/pages/friend/friend-search" }) + }, + onAddNewFriends(){ + uni.navigateTo({ + url: "/pages/friend/friend-add" + }) } }, onNavigationBarButtonTap(e) { diff --git a/im-uniapp/store/chatStore.js b/im-uniapp/store/chatStore.js index d63dbd9..70eb19f 100644 --- a/im-uniapp/store/chatStore.js +++ b/im-uniapp/store/chatStore.js @@ -36,6 +36,12 @@ export default { } uni.setStorageSync("chats",state.chats); }, + activeChat(state, idx) { + state.activeIndex = idx; + if(idx>=0){ + state.chats[idx].unreadCount = 0; + } + }, removeChat(state, idx) { state.chats.splice(idx, 1); uni.setStorageSync("chats",state.chats); diff --git a/im-uniapp/store/friendStore.js b/im-uniapp/store/friendStore.js index 18790e0..ce52faf 100644 --- a/im-uniapp/store/friendStore.js +++ b/im-uniapp/store/friendStore.js @@ -3,8 +3,7 @@ import request from '../common/request'; export default { state: { - friends: [], - activeIndex: -1 + friends: [] }, mutations: { setFriends(state, friends) { @@ -20,14 +19,12 @@ export default { } }) }, - activeFriend(state, index) { - state.activeIndex = index; - }, - removeFriend(state, index) { - state.friends.splice(index, 1); - if (state.activeIndex >= state.friends.length) { - state.activeIndex = state.friends.length - 1; - } + removeFriend(state, id) { + state.friends.forEach((f,idx) => { + if(f.id == id){ + state.friends.splice(idx, 1) + } + }); }, addFriend(state, friend) { state.friends.push(friend); @@ -39,7 +36,6 @@ export default { f.online = onlineFriend != undefined; }); - let activeFriend = state.friends[state.activeIndex]; state.friends.sort((f1, f2) => { if (f1.online && !f2.online) { return -1; @@ -49,15 +45,6 @@ export default { } return 0; }); - - // 重新排序后,activeIndex指向的好友可能会变化,需要重新指定 - if (state.activeIndex >= 0) { - state.friends.forEach((f, i) => { - if (f.id == activeFriend.id) { - state.activeIndex = i; - } - }) - } } }, actions: {