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

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Tair(兼容Redis),内存型 2GB
简介: 【探花交友】day06—即时通信(二)

2.3.4、获取管理员权限

环信提供的 REST API 需要权限才能访问,权限通过发送 HTTP 请求时携带 token 来体现,下面描述获取 token 的方式。说明:API 描述的时候使用到的 {APP 的 client_id} 之类的这种参数需要替换成具体的值。

重要提醒:获取 token 时服务器会返回 token 有效期,具体值参考接口返回的 expires_in 字段值。由于网络延迟等原因,系统不保证 token 在此值表示的有效期内绝对有效,如果发现 token 使用异常请重新获取新的 token,比如“http response code”返回 401。另外,请不要频繁向服务器发送获取 token 的请求,同一账号发送此请求超过一定频率会被服务器封号,切记,切记!!

client_id 和 client_secret 可以在环信管理后台的 APP 详情页面看到。

HTTP Request

post /{org_name}/{app_name}/token

Request Headers

参数 说明
Content-Type application/json

Request Body

参数 说明
grant_type client_credentials
client_id App的client_id,可在app详情页找到
client_secret App的client_secret,可在app详情页找到

Response Body

参数 说明
access_token 有效的token字符串
expires_in token 有效时间,以秒为单位,在有效期内不需要重复获取
application 当前 App 的 UUID 值

3、抽取环信组件

抽取环信组件到tanhua-autoconfig工程中

3.1、编写HuanXinTemplate

1. @Slf4j
2. public class HuanXinTemplate {
3. 
4. private EMService service;
5. 
6. public HuanXinTemplate(HuanXinProperties properties) {
7. EMProperties emProperties = EMProperties.builder()
8.                 .setAppkey(properties.getAppkey())
9.                 .setClientId(properties.getClientId())
10.                 .setClientSecret(properties.getClientSecret())
11.                 .build();
12.         service = new EMService(emProperties);
13.     }
14. 
15. //创建环信用户
16. public Boolean createUser(String username,String password) {
17. try {
18. //创建环信用户
19.             service.user().create(username.toLowerCase(), password)
20.                     .block();
21. return true;
22.         }catch (Exception e) {
23.             e.printStackTrace();
24.             log.error("创建环信用户失败~");
25.         }
26. return false;
27.     }
28. 
29. //添加联系人
30. public Boolean addContact(String username1,String username2) {
31. try {
32. //创建环信用户
33.             service.contact().add(username1,username2)
34.                     .block();
35. return true;
36.         }catch (Exception e) {
37.             log.error("添加联系人失败~");
38.         }
39. return false;
40.     }
41. 
42. //删除联系人
43. public Boolean deleteContact(String username1,String username2) {
44. try {
45. //创建环信用户
46.             service.contact().remove(username1,username2)
47.                     .block();
48. return true;
49.         }catch (Exception e) {
50.             log.error("删除联系人失败~");
51.         }
52. return false;
53.     }
54. 
55. //发送消息
56. public Boolean sendMsg(String username,String content) {
57. try {
58. //接收人用户列表
59.             Set<String> set = CollUtil.newHashSet(username);
60. //文本消息
61. EMTextMessage message = new EMTextMessage().text(content);
62. //发送消息  from:admin是管理员发送
63.             service.message().send("admin","users",
64.                     set,message,null).block();
65. return true;
66.         }catch (Exception e) {
67.             log.error("删除联系人失败~");
68.         }
69. return false;
70.     }
71. }

3.2、编写Properties对象

1. @Configuration
2. @ConfigurationProperties(prefix = "tanhua.huanxin")
3. @Data
4. public class HuanXinProperties {
5. 
6. private String appkey;
7. private String clientId;
8. private String clientSecret;
9. 
10. }

3.3、配置

tanhua-app-server工程的application.yml文件加入配置如下

1. tanhua:
2.   huanxin:
3.     appkey: 1110201018107234#tanhua
4.     clientId: YXA6nxJJ_pdEQ_eYUlqcRicS4w
5.     clientSecret: YXA6GMUxVEZhAvxlMn4OvHSXbWuEUTE

3.4、测试

1. @RunWith(SpringRunner.class)
2. @SpringBootTest
3. public class HuanXinTest {
4. 
5. @Autowired
6. private HuanXinTemplate template;
7. 
8. @Test
9. public void testRegister() {
10.         template.createUser("user01","123456");
11.     }
12. }

4、用户体系集成

将用户体系集成的逻辑写入到tanhua-server系统中。

  • 探花用户注册时需要将用户信息注册到环信系统中
  • 对于老数据:编写单元测试方法批量的注册到环信
  • 对于新用户:改造代码(用户注册的时候,自动注册到环信)
  • APP从服务端获取当前用户的环信用户密码,自动登入环信系统
  • 编写一个接口,获取当前用户在环信的用户名密码
  • APP自动获取环信服务器发送的信息数据

4.1、注册环信用户

在用户登录逻辑中,当第一次注册时,将用户信息注册到环信

1. /**
2.  * 验证登录
3.  * @param phone
4.  * @param code
5.  */
6. public Map loginVerification(String phone, String code) {
7. //1、从redis中获取下发的验证码
8. String redisCode = redisTemplate.opsForValue().get("CHECK_CODE_" + phone);
9. //2、对验证码进行校验(验证码是否存在,是否和输入的验证码一致)
10. if(StringUtils.isEmpty(redisCode) || !redisCode.equals(code)) {
11. //验证码无效
12. throw new BusinessException(ErrorResult.loginError());
13.     }
14. //3、删除redis中的验证码
15.     redisTemplate.delete("CHECK_CODE_" + phone);
16. //4、通过手机号码查询用户
17. User user = userApi.findByMobile(phone);
18. boolean isNew = false;
19. //5、如果用户不存在,创建用户保存到数据库中
20. if(user == null) {
21.         user = new User();
22.         user.setMobile(phone);
23.         user.setPassword(DigestUtils.md5Hex("123456"));
24. Long userId = userApi.save(user);
25.         user.setId(userId);
26.         isNew = true;
27. 
28. //注册环信用户
29. String hxUser = "hx"+user.getId();
30. Boolean create = huanXinTemplate.createUser(hxUser, Constants.INIT_PASSWORD);
31. if(create) {
32.             user.setHxUser(hxUser);
33.             user.setHxPassword(Constants.INIT_PASSWORD);
34.             userApi.update(user);
35.         }
36.     }
37. //6、通过JWT生成token(存入id和手机号码)
38. Map tokenMap = new HashMap();
39.     tokenMap.put("id",user.getId());
40.     tokenMap.put("mobile",phone);
41. String token = JwtUtils.getToken(tokenMap);
42. //7、构造返回值
43. Map retMap = new HashMap();
44.     retMap.put("token",token);
45.     retMap.put("isNew",isNew);
46. 
47. return retMap;
48. }

4.2、查询环信用户信息

在app中,用户登录后需要根据用户名密码登录环信,由于用户名密码保存在后台,所以需要提供接口进行返回。

4.2.1 API接口

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

4.2.2 vo对象

1. import lombok.AllArgsConstructor;
2. import lombok.Data;
3. import lombok.NoArgsConstructor;
4. 
5. @Data
6. @NoArgsConstructor
7. @AllArgsConstructor
8. public class HuanXinUserVo {
9. private String username;
10. private String password;
11. }

4.2.3 代码实现

编写HuanXinController实现:

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
存储 前端开发 API
【探花交友】day02—完善个人信息(三)
【探花交友】day02—完善个人信息(三)
105 0
|
11月前
|
运维 算法 安全
相亲交友/婚恋交友/红娘相亲/社交软件/语音视频聊天平台系统开发指南详细丨源码版
系统设计:设计系统的数据库结构、用户界面、算法等。需要考虑用户友好性、安全性和灵活性。
|
NoSQL 程序员 API
【探花交友】day06—即时通信(三)
【探花交友】day06—即时通信(三)
110 0
|
消息中间件 NoSQL 前端开发
【探花交友】day06—即时通信(一)
【探花交友】day06—即时通信
165 0
|
Dubbo NoSQL 应用服务中间件
【探花交友】day06—即时通信(四)
【探花交友】day06—即时通信(四)
170 0
|
存储 NoSQL 搜索推荐
【探花交友】day05—圈子互动(上)
【探花交友】day05—圈子互动
109 0
|
NoSQL API Redis
【探花交友】day05—圈子互动(下)
【探花交友】day05—圈子互动(下)
124 0
|
存储 NoSQL API
【探花交友】day04—圈子功能实现(二)
【探花交友】day04—圈子功能实现(二)
134 0
|
API
【探花交友】day04—圈子功能实现(三)
【探花交友】day04—圈子功能实现(三)
75 0
|
存储 SQL 缓存
【探花交友】day04—圈子功能实现(一)
【探花交友】day04—圈子功能实现
108 0