【探花交友】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服务

创建FriendApi和FriendApiImpl并编写添加好友的方法

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服务

在FriendApi和FriendApiImpl中添加分页查询方法

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. }
相关文章
|
存储 SQL 缓存
【探花交友】day04—圈子功能实现(一)
【探花交友】day04—圈子功能实现
1512 0
|
程序员 开发工具 开发者
|
9天前
|
域名解析 人工智能 Linux
|
19天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17713 63
|
17天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
3天前
|
人工智能 安全 网络安全
|
7天前
|
云安全 数据采集 人工智能
|
8天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1399 5
|
14天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2446 8
|
19天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3970 22

热门文章

最新文章