diff --git a/im-platform/src/main/java/com/lx/implatform/controller/FriendsController.java b/im-platform/src/main/java/com/lx/implatform/controller/FriendController.java similarity index 56% rename from im-platform/src/main/java/com/lx/implatform/controller/FriendsController.java rename to im-platform/src/main/java/com/lx/implatform/controller/FriendController.java index 1a812b3..eb25d50 100644 --- a/im-platform/src/main/java/com/lx/implatform/controller/FriendsController.java +++ b/im-platform/src/main/java/com/lx/implatform/controller/FriendController.java @@ -4,10 +4,10 @@ package com.lx.implatform.controller; import com.lx.common.result.Result; import com.lx.common.result.ResultUtils; import com.lx.common.util.BeanUtils; -import com.lx.implatform.entity.Friends; -import com.lx.implatform.service.IFriendsService; +import com.lx.implatform.entity.Friend; +import com.lx.implatform.service.IFriendService; import com.lx.implatform.session.SessionContext; -import com.lx.implatform.vo.FriendsVO; +import com.lx.implatform.vo.FriendVO; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; @@ -20,18 +20,18 @@ import java.util.stream.Collectors; @Api(tags = "好友") @RestController -@RequestMapping("/friends") -public class FriendsController { +@RequestMapping("/friend") +public class FriendController { @Autowired - private IFriendsService friendsService; + private IFriendService friendService; @GetMapping("/list") @ApiOperation(value = "好友列表",notes="获取好友列表") - public Result< List> findFriends(){ - List friendsList = friendsService.findFriendsByUserId(SessionContext.getSession().getId()); - List vos = friendsList.stream().map(f->{ - FriendsVO vo = BeanUtils.copyProperties(f,FriendsVO.class); + public Result< List> findFriends(){ + List friends = friendService.findFriendByUserId(SessionContext.getSession().getId()); + List vos = friends.stream().map(f->{ + FriendVO vo = BeanUtils.copyProperties(f, FriendVO.class); return vo; }).collect(Collectors.toList()); return ResultUtils.success(vos); @@ -41,22 +41,22 @@ public class FriendsController { @PostMapping("/add") @ApiOperation(value = "添加好友",notes="双方建立好友关系") - public Result addFriends(@NotEmpty(message = "好友id不可为空") @RequestParam("friendId") Long friendId){ - friendsService.addFriends(friendId); + public Result addFriend(@NotEmpty(message = "好友id不可为空") @RequestParam("friendId") Long friendId){ + friendService.addFriend(friendId); return ResultUtils.success(); } @DeleteMapping("/delete") @ApiOperation(value = "删除好友",notes="解除好友关系") - public Result delFriends(@NotEmpty(message = "好友id不可为空") @RequestParam("friendId") Long friendId){ - friendsService.delFriends(friendId); + public Result delFriend(@NotEmpty(message = "好友id不可为空") @RequestParam("friendId") Long friendId){ + friendService.delFriend(friendId); return ResultUtils.success(); } @PutMapping("/update") @ApiOperation(value = "更新好友信息",notes="更新好友头像或昵称") - public Result modifyFriends(@Valid @RequestBody FriendsVO vo){ - friendsService.update(vo); + public Result modifyFriend(@Valid @RequestBody FriendVO vo){ + friendService.update(vo); return ResultUtils.success(); } diff --git a/im-platform/src/main/java/com/lx/implatform/entity/Friends.java b/im-platform/src/main/java/com/lx/implatform/entity/Friend.java similarity index 94% rename from im-platform/src/main/java/com/lx/implatform/entity/Friends.java rename to im-platform/src/main/java/com/lx/implatform/entity/Friend.java index d75cc9a3..b4d29eb 100644 --- a/im-platform/src/main/java/com/lx/implatform/entity/Friends.java +++ b/im-platform/src/main/java/com/lx/implatform/entity/Friend.java @@ -21,8 +21,8 @@ import java.util.Date; */ @Data @EqualsAndHashCode(callSuper = false) -@TableName("im_friends") -public class Friends extends Model { +@TableName("im_friend") +public class Friend extends Model { private static final long serialVersionUID=1L; diff --git a/im-platform/src/main/java/com/lx/implatform/mapper/FriendsMapper.java b/im-platform/src/main/java/com/lx/implatform/mapper/FriendMapper.java similarity index 63% rename from im-platform/src/main/java/com/lx/implatform/mapper/FriendsMapper.java rename to im-platform/src/main/java/com/lx/implatform/mapper/FriendMapper.java index 978fa27..9ed8a93 100644 --- a/im-platform/src/main/java/com/lx/implatform/mapper/FriendsMapper.java +++ b/im-platform/src/main/java/com/lx/implatform/mapper/FriendMapper.java @@ -1,7 +1,7 @@ package com.lx.implatform.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; -import com.lx.implatform.entity.Friends; +import com.lx.implatform.entity.Friend; /** *

@@ -11,6 +11,6 @@ import com.lx.implatform.entity.Friends; * @author blue * @since 2022-10-22 */ -public interface FriendsMapper extends BaseMapper { +public interface FriendMapper extends BaseMapper { } diff --git a/im-platform/src/main/java/com/lx/implatform/service/IFriendService.java b/im-platform/src/main/java/com/lx/implatform/service/IFriendService.java new file mode 100644 index 0000000..591c204 --- /dev/null +++ b/im-platform/src/main/java/com/lx/implatform/service/IFriendService.java @@ -0,0 +1,29 @@ +package com.lx.implatform.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.lx.implatform.entity.Friend; +import com.lx.implatform.vo.FriendVO; + +import java.util.List; + +/** + *

+ * 服务类 + *

+ * + * @author blue + * @since 2022-10-22 + */ +public interface IFriendService extends IService { + + Boolean isFriend(Long userId1, long userId2); + + List findFriendByUserId(long UserId); + + void addFriend(long friendId); + + void delFriend(long friendId); + + void update(FriendVO vo); + +} diff --git a/im-platform/src/main/java/com/lx/implatform/service/IFriendsService.java b/im-platform/src/main/java/com/lx/implatform/service/IFriendsService.java deleted file mode 100644 index 52b0676..0000000 --- a/im-platform/src/main/java/com/lx/implatform/service/IFriendsService.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.lx.implatform.service; - -import com.baomidou.mybatisplus.extension.service.IService; -import com.lx.implatform.entity.Friends; -import com.lx.implatform.vo.FriendsVO; - -import java.util.List; - -/** - *

- * 服务类 - *

- * - * @author blue - * @since 2022-10-22 - */ -public interface IFriendsService extends IService { - - Boolean isFriends(Long userId1,long userId2); - - List findFriendsByUserId(long UserId); - - void addFriends(long friendId); - - void delFriends(long friendId); - - void update(FriendsVO vo); - -} diff --git a/im-platform/src/main/java/com/lx/implatform/service/impl/FriendServiceImpl.java b/im-platform/src/main/java/com/lx/implatform/service/impl/FriendServiceImpl.java new file mode 100644 index 0000000..73b2906 --- /dev/null +++ b/im-platform/src/main/java/com/lx/implatform/service/impl/FriendServiceImpl.java @@ -0,0 +1,124 @@ +package com.lx.implatform.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.lx.common.enums.ResultCode; +import com.lx.implatform.entity.Friend; +import com.lx.implatform.entity.User; +import com.lx.implatform.exception.GlobalException; +import com.lx.implatform.mapper.FriendMapper; +import com.lx.implatform.service.IFriendService; +import com.lx.implatform.service.IUserService; +import com.lx.implatform.session.SessionContext; +import com.lx.implatform.vo.FriendVO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +/** + *

+ * 好友服务实现类 + *

+ * + * @author blue + * @since 2022-10-22 + */ +@Service +public class FriendServiceImpl extends ServiceImpl implements IFriendService { + + @Autowired + private IUserService userService; + + @Override + public List findFriendByUserId(long UserId) { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.lambda().eq(Friend::getUserId,UserId); + List friends = this.list(queryWrapper); + return friends; + } + + + @Transactional + @Override + public void addFriend(long friendId) { + long userId = SessionContext.getSession().getId(); + if(userId == friendId){ + throw new GlobalException(ResultCode.PROGRAM_ERROR,"不允许添加自己为好友"); + } + // 互相绑定好友关系 + bindFriend(userId,friendId); + bindFriend(friendId,userId); + } + + + @Transactional + @Override + public void delFriend(long friendId) { + long userId = SessionContext.getSession().getId(); + // 互相解除好友关系 + unbindFriend(userId,friendId); + unbindFriend(friendId,userId); + } + + + + @Override + public Boolean isFriend(Long userId1, long userId2) { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.lambda() + .eq(Friend::getUserId,userId1) + .eq(Friend::getFriendId,userId2); + return this.count(queryWrapper) > 0; + } + + + @Override + public void update(FriendVO vo) { + long userId = SessionContext.getSession().getId(); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.lambda() + .eq(Friend::getUserId,userId) + .eq(Friend::getFriendId,vo.getFriendId()); + + Friend f = this.getOne(queryWrapper); + if(f == null){ + throw new GlobalException(ResultCode.PROGRAM_ERROR,"对方不是您的好友"); + } + + f.setFriendHeadImage(vo.getFriendHeadImage()); + f.setFriendNickName(vo.getFriendNickName()); + this.updateById(f); + } + + private void bindFriend(long userId, long friendId) { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.lambda() + .eq(Friend::getUserId,userId) + .eq(Friend::getFriendId,friendId); + if(this.count(queryWrapper)==0){ + Friend friend = new Friend(); + friend.setUserId(userId); + friend.setFriendId(friendId); + User friendInfo = userService.getById(friendId); + friend.setFriendHeadImage(friendInfo.getHeadImage()); + friend.setFriendNickName(friendInfo.getNickName()); + this.save(friend); + } + } + + + private void unbindFriend(long userId, long friendId) { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.lambda() + .eq(Friend::getUserId,userId) + .eq(Friend::getFriendId,friendId); + List friends = this.list(queryWrapper); + friends.stream().forEach(friend -> { + this.removeById(friend.getId()); + }); + } + + +} diff --git a/im-platform/src/main/java/com/lx/implatform/service/impl/FriendsServiceImpl.java b/im-platform/src/main/java/com/lx/implatform/service/impl/FriendsServiceImpl.java deleted file mode 100644 index 71d01a1..0000000 --- a/im-platform/src/main/java/com/lx/implatform/service/impl/FriendsServiceImpl.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.lx.implatform.service.impl; - -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.lx.common.enums.ResultCode; -import com.lx.implatform.entity.Friends; -import com.lx.implatform.entity.User; -import com.lx.implatform.exception.GlobalException; -import com.lx.implatform.mapper.FriendsMapper; -import com.lx.implatform.service.IFriendsService; -import com.lx.implatform.service.IUserService; -import com.lx.implatform.session.SessionContext; -import com.lx.implatform.vo.FriendsVO; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import java.util.List; - -/** - *

- * 好友服务实现类 - *

- * - * @author blue - * @since 2022-10-22 - */ -@Service -public class FriendsServiceImpl extends ServiceImpl implements IFriendsService { - - @Autowired - private IUserService userService; - - @Override - public List findFriendsByUserId(long UserId) { - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.lambda().eq(Friends::getUserId,UserId); - List friendsList = this.list(queryWrapper); - return friendsList; - } - - - @Transactional - @Override - public void addFriends(long friendId) { - long userId = SessionContext.getSession().getId(); - if(userId == friendId){ - throw new GlobalException(ResultCode.PROGRAM_ERROR,"不允许添加自己为好友"); - } - // 互相绑定好友关系 - bindFriends(userId,friendId); - bindFriends(friendId,userId); - } - - - @Transactional - @Override - public void delFriends(long friendId) { - long userId = SessionContext.getSession().getId(); - // 互相解除好友关系 - unbindFriends(userId,friendId); - unbindFriends(friendId,userId); - } - - - - @Override - public Boolean isFriends(Long userId1, long userId2) { - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.lambda() - .eq(Friends::getUserId,userId1) - .eq(Friends::getFriendId,userId2); - return this.count(queryWrapper) > 0; - } - - - @Override - public void update(FriendsVO vo) { - long userId = SessionContext.getSession().getId(); - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.lambda() - .eq(Friends::getUserId,userId) - .eq(Friends::getFriendId,vo.getFriendId()); - - Friends f = this.getOne(queryWrapper); - if(f == null){ - throw new GlobalException(ResultCode.PROGRAM_ERROR,"对方不是您的好友"); - } - - f.setFriendHeadImage(vo.getFriendHeadImage()); - f.setFriendNickName(vo.getFriendNickName()); - this.updateById(f); - } - - private void bindFriends(long userId, long friendsId) { - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.lambda() - .eq(Friends::getUserId,userId) - .eq(Friends::getFriendId,friendsId); - if(this.count(queryWrapper)==0){ - Friends friends = new Friends(); - friends.setUserId(userId); - friends.setFriendId(friendsId); - User friendsInfo = userService.getById(friendsId); - friends.setFriendHeadImage(friendsInfo.getHeadImage()); - friends.setFriendNickName(friendsInfo.getNickName()); - this.save(friends); - } - } - - - private void unbindFriends(long userId, long friendsId) { - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.lambda() - .eq(Friends::getUserId,userId) - .eq(Friends::getFriendId,friendsId); - List friendsList = this.list(queryWrapper); - friendsList.stream().forEach(friends -> { - this.removeById(friends.getId()); - }); - } - - -} diff --git a/im-platform/src/main/java/com/lx/implatform/service/impl/GroupServiceImpl.java b/im-platform/src/main/java/com/lx/implatform/service/impl/GroupServiceImpl.java index e263250..ced8e53 100644 --- a/im-platform/src/main/java/com/lx/implatform/service/impl/GroupServiceImpl.java +++ b/im-platform/src/main/java/com/lx/implatform/service/impl/GroupServiceImpl.java @@ -3,13 +3,13 @@ package com.lx.implatform.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.lx.common.enums.ResultCode; import com.lx.common.util.BeanUtils; -import com.lx.implatform.entity.Friends; +import com.lx.implatform.entity.Friend; import com.lx.implatform.entity.Group; import com.lx.implatform.entity.GroupMember; import com.lx.implatform.entity.User; import com.lx.implatform.exception.GlobalException; import com.lx.implatform.mapper.GroupMapper; -import com.lx.implatform.service.IFriendsService; +import com.lx.implatform.service.IFriendService; import com.lx.implatform.service.IGroupMemberService; import com.lx.implatform.service.IGroupService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; @@ -36,7 +36,7 @@ public class GroupServiceImpl extends ServiceImpl implements private IGroupMemberService groupMemberService; @Autowired - private IFriendsService friendsService; + private IFriendService friendsService; /** * 创建新群聊 @@ -154,8 +154,8 @@ public class GroupServiceImpl extends ServiceImpl implements throw new GlobalException(ResultCode.PROGRAM_ERROR, "部分用户已经在群中,邀请失败"); } // 找出好友信息 - List friends = friendsService.findFriendsByUserId(session.getId()); - List friendsList = vo.getFriendIds().stream().map(id -> + List friends = friendsService.findFriendByUserId(session.getId()); + List friendsList = vo.getFriendIds().stream().map(id -> friends.stream().filter(f -> f.getFriendId().equals(id)).findFirst().get()).collect(Collectors.toList()); if (friendsList.size() != vo.getFriendIds().size()) { throw new GlobalException(ResultCode.PROGRAM_ERROR, "部分用户不是您的好友,邀请失败"); diff --git a/im-platform/src/main/java/com/lx/implatform/service/impl/SingleMessageServiceImpl.java b/im-platform/src/main/java/com/lx/implatform/service/impl/SingleMessageServiceImpl.java index bb7ddb7..3a29fae 100644 --- a/im-platform/src/main/java/com/lx/implatform/service/impl/SingleMessageServiceImpl.java +++ b/im-platform/src/main/java/com/lx/implatform/service/impl/SingleMessageServiceImpl.java @@ -10,7 +10,7 @@ import com.lx.common.util.BeanUtils; import com.lx.implatform.entity.SingleMessage; import com.lx.implatform.exception.GlobalException; import com.lx.implatform.mapper.SingleMessageMapper; -import com.lx.implatform.service.IFriendsService; +import com.lx.implatform.service.IFriendService; import com.lx.implatform.service.ISingleMessageService; import com.lx.implatform.session.SessionContext; import com.lx.implatform.vo.SingleMessageVO; @@ -28,14 +28,14 @@ import java.util.stream.Collectors; public class SingleMessageServiceImpl extends ServiceImpl implements ISingleMessageService { @Autowired - private IFriendsService friendsService; + private IFriendService friendService; @Autowired private RedisTemplate redisTemplate; @Override public void sendMessage(SingleMessageVO vo) { Long userId = SessionContext.getSession().getId(); - Boolean isFriends = friendsService.isFriends(userId,vo.getRecvUserId()); + Boolean isFriends = friendService.isFriend(userId,vo.getRecvUserId()); if(!isFriends){ throw new GlobalException(ResultCode.PROGRAM_ERROR,"您已不是对方好友,无法发送消息"); } diff --git a/im-platform/src/main/java/com/lx/implatform/vo/FriendsVO.java b/im-platform/src/main/java/com/lx/implatform/vo/FriendVO.java similarity index 95% rename from im-platform/src/main/java/com/lx/implatform/vo/FriendsVO.java rename to im-platform/src/main/java/com/lx/implatform/vo/FriendVO.java index b9049f4..d4c739b 100644 --- a/im-platform/src/main/java/com/lx/implatform/vo/FriendsVO.java +++ b/im-platform/src/main/java/com/lx/implatform/vo/FriendVO.java @@ -9,7 +9,7 @@ import javax.validation.constraints.NotNull; @Data @ApiModel("好友信息VO") -public class FriendsVO { +public class FriendVO { @NotNull(message = "好友id不可为空") @ApiModelProperty(value = "好友id") diff --git a/im-platform/src/main/resources/db/db.sql b/im-platform/src/main/resources/db/db.sql index d8c2c9c..e6f2be7 100644 --- a/im-platform/src/main/resources/db/db.sql +++ b/im-platform/src/main/resources/db/db.sql @@ -14,12 +14,12 @@ create table `im_user`( key `idx_nick_name`(nick_name) ) ENGINE=InnoDB CHARSET=utf8mb3 comment '用户'; -create table `im_friends`( +create table `im_friend`( `id` bigint not null auto_increment primary key comment 'id', `user_id` bigint not null comment '用户id', `friend_id` bigint not null comment '好友id', - `friend_nick_name` varchar(255) not null comment '用户昵称', - `friend_head_image` varchar(255) default '' comment '用户头像', + `friend_nick_name` varchar(255) not null comment '好友昵称', + `friend_head_image` varchar(255) default '' comment '好友头像', `created_time` datetime DEFAULT CURRENT_TIMESTAMP comment '创建时间', key `idx_user_id` (`user_id`), key `idx_friend_id` (`friend_id`) diff --git a/im-server/src/main/java/com/lx/implatform/imserver/IMServerApp.java b/im-server/src/main/java/com/lx/implatform/imserver/IMServerApp.java index 0babca3..123804a 100644 --- a/im-server/src/main/java/com/lx/implatform/imserver/IMServerApp.java +++ b/im-server/src/main/java/com/lx/implatform/imserver/IMServerApp.java @@ -15,12 +15,18 @@ import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling @ComponentScan(basePackages={"com.lx"}) @SpringBootApplication -public class IMServerApp { +public class IMServerApp implements CommandLineRunner { + + @Value("${websocket.port}") + private int port; public static void main(String[] args) { SpringApplication.run(IMServerApp.class); } -} + public void run(String... args) throws Exception { + new WebsocketServer().start(port); + } +} diff --git a/im-server/src/main/java/com/lx/implatform/imserver/websocket/WebsocketServer.java b/im-server/src/main/java/com/lx/implatform/imserver/websocket/WebsocketServer.java index 3305226..c083859 100644 --- a/im-server/src/main/java/com/lx/implatform/imserver/websocket/WebsocketServer.java +++ b/im-server/src/main/java/com/lx/implatform/imserver/websocket/WebsocketServer.java @@ -27,7 +27,7 @@ public class WebsocketServer { private int port; @PostConstruct public void init(){ - this.start(port); + //this.start(port); } public void start(int port) { diff --git a/im-ui/src/components/friend/AddFriends.vue b/im-ui/src/components/friend/AddFriends.vue deleted file mode 100644 index f74c6ed..0000000 --- a/im-ui/src/components/friend/AddFriends.vue +++ /dev/null @@ -1,119 +0,0 @@ - - - - - diff --git a/im-ui/src/components/friend/FriendsItem.vue b/im-ui/src/components/friend/FriendsItem.vue deleted file mode 100644 index 349f697..0000000 --- a/im-ui/src/components/friend/FriendsItem.vue +++ /dev/null @@ -1,114 +0,0 @@ - - - - - diff --git a/im-ui/src/router/index.js b/im-ui/src/router/index.js index da1917a..0925f66 100644 --- a/im-ui/src/router/index.js +++ b/im-ui/src/router/index.js @@ -34,8 +34,8 @@ export default new VueRouter({ }, { name: "Friends", - path: "/home/friends", - component: () => import("../view/Friends"), + path: "/home/friend", + component: () => import("../view/Friend"), }, { name: "Friends", diff --git a/im-ui/src/store/friendsStore.js b/im-ui/src/store/friendsStore.js deleted file mode 100644 index 80f4b30..0000000 --- a/im-ui/src/store/friendsStore.js +++ /dev/null @@ -1,90 +0,0 @@ -import httpRequest from '../api/httpRequest.js' - -export default { - - state: { - friendsList: [], - activeIndex: -1, - timer: null - }, - mutations: { - initFriendsStore(state) { - httpRequest({ - url: '/api/friends/list', - method: 'get' - }).then((friendsList) => { - this.commit("setFriendsList",friendsList); - this.commit("refreshOnlineStatus"); - }) - }, - - setFriendsList(state, friendsList) { - state.friendsList = friendsList; - }, - updateFriends(state,friendsInfo){ - console.log(friendsInfo) - state.friendsList.forEach((f,index)=>{ - if(f.friendId==friendsInfo.friendId){ - state.friendsList[index] = friendsInfo; - } - }) - }, - activeFriends(state, index) { - state.activeIndex = index; - }, - removeFriends(state, index) { - state.friendsList.splice(index, 1); - }, - addFriends(state, friendsInfo) { - state.friendsList.push(friendsInfo); - }, - refreshOnlineStatus(state){ - let userIds = []; - if(state.friendsList.length ==0){ - return; - } - state.friendsList.forEach((f)=>{userIds.push(f.friendId)}); - httpRequest({ - url: '/api/user/online', - method: 'get', - params: {userIds: userIds.join(',')} - }).then((onlineIds) => { - this.commit("setOnlineStatus",onlineIds); - }) - - // 30s后重新拉取 - clearTimeout(state.timer); - state.timer = setTimeout(()=>{ - this.commit("refreshOnlineStatus"); - },30000) - }, - setOnlineStatus(state,onlineIds){ - state.friendsList.forEach((f)=>{ - let onlineFriend = onlineIds.find((id)=> f.friendId==id); - f.online = onlineFriend != undefined; - console.log(f.friendNickName+":"+f.online); - }); - - let activeFriend = state.friendsList[state.activeIndex]; - state.friendsList.sort((f1,f2)=>{ - if(f1.online&&!f2.online){ - return -1; - } - if(f2.online&&!f1.online){ - return 1; - } - return 0; - }); - - // 重新排序后,activeIndex指向的好友可能会变化,需要重新指定 - if(state.activeIndex >=0){ - state.friendsList.forEach((f,i)=>{ - if(f.friendId == activeFriend.friendId){ - state.activeIndex = i; - } - }) - } - } - - } -} diff --git a/im-ui/src/store/index.js b/im-ui/src/store/index.js index b01db4c..40752ca 100644 --- a/im-ui/src/store/index.js +++ b/im-ui/src/store/index.js @@ -1,7 +1,7 @@ import Vue from 'vue'; import Vuex from 'vuex'; import chatStore from './chatStore.js'; -import friendsStore from './friendsStore.js'; +import friendStore from './friendStore.js'; import userStore from './userStore.js'; import VuexPersistence from 'vuex-persist' @@ -14,7 +14,7 @@ const vuexLocal = new VuexPersistence({ Vue.use(Vuex) export default new Vuex.Store({ - modules: {chatStore,friendsStore,userStore}, + modules: {chatStore,friendStore,userStore}, state: { userInfo: {} }, @@ -22,7 +22,7 @@ export default new Vuex.Store({ mutations: { initStore(state){ - this.commit("initFriendsStore"); + this.commit("initFriendStore"); } }, diff --git a/im-ui/src/view/Chat.vue b/im-ui/src/view/Chat.vue index dc9549f..9e05a6d 100644 --- a/im-ui/src/view/Chat.vue +++ b/im-ui/src/view/Chat.vue @@ -10,7 +10,7 @@
- +
@@ -54,10 +54,10 @@
- +
- 发送 + 发送
@@ -87,7 +87,7 @@ } }, methods: { - onClickItem(index) { + handleActiveItem(index) { this.$store.commit("activeChat", index); // 获取对方 let userId = this.chatStore.chats[index].targetId; @@ -103,7 +103,7 @@ } }) }, - onSendMessage() { + handleSendMessage() { let msgInfo = { recvUserId: this.activeChat.targetId, content: this.messageContent, @@ -112,7 +112,7 @@ this.sendMessage(msgInfo); }, - onDelItem(chat, index) { + handleDelItem(chat, index) { this.$store.commit("removeChat", index); }, diff --git a/im-ui/src/view/Friends.vue b/im-ui/src/view/Friends.vue deleted file mode 100644 index 3f19260..0000000 --- a/im-ui/src/view/Friends.vue +++ /dev/null @@ -1,184 +0,0 @@ - - - - - diff --git a/im-ui/src/view/Home.vue b/im-ui/src/view/Home.vue index ccec3e8..57b6e58 100644 --- a/im-ui/src/view/Home.vue +++ b/im-ui/src/view/Home.vue @@ -12,7 +12,7 @@ - + @@ -73,7 +73,7 @@ }, handleSingleMessage(msg){ // 插入私聊消息 - let f = this.$store.state.friendsStore.friendsList.find((f)=>f.friendId==msg.sendUserId); + let f = this.$store.state.friendStore.friends.find((f)=>f.friendId==msg.sendUserId); let chatInfo = { type: 'single', targetId: f.friendId,