【探花交友】day06—即时通信(四)

简介: 【探花交友】day06—即时通信(四)

5.3、回复陌生人消息

需求:

  • 通过服务器端,给目标用户发送一条陌生人消息

5.3.1、mock接口

地址:http://192.168.136.160:3000/project/19/interface/api/106

5.3.2、TanhuaController

1. /**
2.  * 回复陌生人问题
3.  */
4. @PostMapping("/strangerQuestions")
5. public ResponseEntity replyQuestions(@RequestBody Map map) {
6. //前端传递的userId:是Integer类型的
7. String obj = map.get("userId").toString();
8. Long userId = Long.valueOf(obj);
9. String reply = map.get("reply").toString();
10.     tanhuaService.replyQuestions(userId,reply);
11. return ResponseEntity.ok(null);
12. }

5.3.3、TanhuaService

创建TanhuaService并编写方法,完成回复陌生人消息功能

{"userId":106,"huanXinId":"hx106","nickname":"黑马小妹","strangerQuestion":"你喜欢去看蔚蓝的大海还是去爬巍峨的高山?","reply":"我喜欢秋天的落叶,夏天的泉水,冬天的雪地,只要有你一切皆可~"}

1. //回复陌生人问题
2. public void replyQuestions(Long userId, String reply) {
3. //1、构造消息数据
4. Long currentUserId = UserHolder.getUserId();
5. UserInfo userInfo = userInfoApi.findById(currentUserId);
6. Map map = new HashMap();
7.     map.put("userId",currentUserId);
8.     map.put("huanXinId", Constants.HX_USER_PREFIX+currentUserId);
9.     map.put("nickname",userInfo.getNickname());
10.     map.put("strangerQuestion",strangerQuestions(userId));
11.     map.put("reply",reply);
12. String message = JSON.toJSONString(map);
13. //2、调用template对象,发送消息
14. Boolean aBoolean = template.sendMsg(Constants.HX_USER_PREFIX + userId, message);//1、接受方的环信id,2、消息
15. if(!aBoolean) {
16. throw  new BusinessException(ErrorResult.error());
17.     }
18. }

5.4、添加联系人

用户获取陌生人消息后,点击“聊一下”,就会成为联系人(好友)。

实现:

  • 将好友写入到MongoDB中
  • 将好友关系注册到环信

5.4.1、mock接口

地址: http://192.168.136.160:3000/project/19/interface/api/205

5.4.2、定义MessagesController

1. /**
2.  * 添加好友
3.  */
4. @PostMapping("/contacts")
5. public ResponseEntity contacts(@RequestBody Map map) {
6. Long friendId = Long.valueOf(map.get("userId").toString());
7.     messagesService.contacts(friendId);
8. return ResponseEntity.ok(null);
9. }

5.4.3、编写Service方法

MessageService补充添加联系人方法

1. //添加好友关系
2. public void contacts(Long friendId) {
3. //1、将好友关系注册到环信
4. Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + UserHolder.getUserId(),
5.             Constants.HX_USER_PREFIX + friendId);
6. if(!aBoolean) {
7. throw new BusinessException(ErrorResult.error());
8.     }
9. //2、如果注册成功,记录好友关系到mongodb
10.     friendApi.save(UserHolder.getUserId(),friendId);
11. }

5.4.4、dubbo服务

创建FriendApiFriendApiImpl并编写添加好友的方法

FriendApiImpl实现类

1. @Override
2. public void save(Long userId, Long friendId) {
3. //1、保存自己的好友数据
4. Query query1 = Query.query(Criteria.where("userId").is(userId).and("frinedId").is(friendId));
5. //1.1 判断好友关系是否存在
6. if(!mongoTemplate.exists(query1, Friend.class)) {
7. //1.2 如果不存在,保存
8. Friend friend1 = new Friend();
9.         friend1.setUserId(userId);
10.         friend1.setFriendId(friendId);
11.         friend1.setCreated(System.currentTimeMillis());
12.         mongoTemplate.save(friend1);
13.     }
14. //2、保存好友的数据
15. Query query2 = Query.query(Criteria.where("userId").is(friendId).and("frinedId").is(userId));
16. //2.1 判断好友关系是否存在
17. if(!mongoTemplate.exists(query2, Friend.class)) {
18. //2.2 如果不存在,保存
19. Friend friend1 = new Friend();
20.         friend1.setUserId(friendId);
21.         friend1.setFriendId(userId);
22.         friend1.setCreated(System.currentTimeMillis());
23.         mongoTemplate.save(friend1);
24.     }
25. }

5.4.5、测试

可以看到好友已经添加成功。

5.5、联系人列表

联系人列表:分页查询好友列表数据 (tanhua-users:好友关系表)

5.5.1、mock接口

地址:http://192.168.136.160:3000/project/19/interface/api/202

响应数据结构:

5.5.2、定义ContactVo

1. package com.tanhua.domain.vo;
2. 
3. import lombok.AllArgsConstructor;
4. import lombok.Data;
5. import lombok.NoArgsConstructor;
6. 
7. import java.io.Serializable;
8. 
9. @Data
10. @NoArgsConstructor
11. @AllArgsConstructor
12. public class ContactVo implements Serializable {
13. 
14. private Long id;
15. private String userId;
16. private String avatar;
17. private String nickname;
18. private String gender;
19. private Integer age;
20. private String city;
21. 
22. public static ContactVo init(UserInfo userInfo) {
23. ContactVo vo = new ContactVo();
24. if(userInfo != null) {
25.           BeanUtils.copyProperties(userInfo,vo);
26.           vo.setUserId("hx"+userInfo.getId().toString());
27.         }
28. return vo;
29.     }
30. }

5.5.3、编写MessagesController

MessagesController中添加查询联系人列表方法

1. /**
2.  * 分页查询联系人列表
3.  */
4. @GetMapping("/contacts")
5. public ResponseEntity contacts(@RequestParam(defaultValue = "1") Integer page,
6.                                @RequestParam(defaultValue = "10") Integer pagesize,
7.                                String keyword) {
8. PageResult pr = messagesService.findFriends(page,pagesize,keyword);
9. return ResponseEntity.ok(pr);
10. }

5.5.4、编写Service

MessageService中添加查询联系人方法

1. //分页查询联系人列表
2. public PageResult findFriends(Integer page, Integer pagesize, String keyword) {
3. //1、调用API查询当前用户的好友数据 -- List<Friend>
4.     List<Friend> list = friendApi.findByUserId(UserHolder.getUserId(),page,pagesize);
5. if(CollUtil.isEmpty(list)) {
6. return new PageResult();
7.     }
8. //2、提取数据列表中的好友id
9.     List<Long> userIds = CollUtil.getFieldValues(list, "friendId", Long.class);
10. //3、调用UserInfoAPI查询好友的用户详情
11. UserInfo info = new UserInfo();
12.     info.setNickname(keyword);
13.     Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, info);
14. //4、构造VO对象
15.     List<ContactVo> vos = new ArrayList<>();
16. for (Friend friend : list) {
17. UserInfo userInfo = map.get(friend.getFriendId());
18. if(userInfo != null) {
19. ContactVo vo = ContactVo.init(userInfo);
20.             vos.add(vo);
21.         }
22.     }
23. return new PageResult(page,pagesize,0l,vos);
24. }

5.5.5、dubbo服务

FriendApiFriendApiImpl中添加分页查询方法

1. @Override
2. public List<Friend> findByUserId(Long userId, Integer page, Integer pagesize) {
3. Criteria criteria = Criteria.where("userId").is(userId);
4. Query query = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize)
5.             .with(Sort.by(Sort.Order.desc("created")));
6. return mongoTemplate.find(query,Friend.class);
7. }
相关文章
|
8月前
|
运维 算法 安全
相亲交友/婚恋交友/红娘相亲/社交软件/语音视频聊天平台系统开发指南详细丨源码版
系统设计:设计系统的数据库结构、用户界面、算法等。需要考虑用户友好性、安全性和灵活性。
|
11月前
|
NoSQL 程序员 API
【探花交友】day06—即时通信(三)
【探花交友】day06—即时通信(三)
87 0
|
11月前
|
消息中间件 NoSQL 前端开发
【探花交友】day06—即时通信(一)
【探花交友】day06—即时通信
142 0
|
11月前
|
JSON 测试技术 API
【探花交友】day06—即时通信(二)
【探花交友】day06—即时通信(二)
84 0
|
11月前
|
NoSQL API Redis
【探花交友】day05—圈子互动(下)
【探花交友】day05—圈子互动(下)
100 0
|
11月前
|
存储 NoSQL 搜索推荐
【探花交友】day05—圈子互动(上)
【探花交友】day05—圈子互动
90 0
|
11月前
|
存储 NoSQL API
【探花交友】day04—圈子功能实现(二)
【探花交友】day04—圈子功能实现(二)
109 0
|
11月前
|
API
【探花交友】day04—圈子功能实现(三)
【探花交友】day04—圈子功能实现(三)
63 0
|
11月前
|
存储 SQL 缓存
【探花交友】day04—圈子功能实现(一)
【探花交友】day04—圈子功能实现
88 0
|
算法 前端开发
微信严打刷量后 自媒体的出路在何方?
作为一个极为稳定的自媒体生态,微信公众号很少有大动作。
微信严打刷量后 自媒体的出路在何方?