目录
1:自动装配
在项目中集成环信API,完成即时通信等
环信官方文档地址:Java Server SDK [IM 开发文档]
自动装配模块:
pom文件相关依赖
<dependency>
<groupId>com.easemob.im</groupId>
<artifactId>im-sdk-core</artifactId>
<version>0.5.2</version>
</dependency>
yml文件中配置相关信息
读取yml文件中的配置
@ConfigurationProperties(prefix = "tanhua.huanxin")
@Data
public class HuanXinProperties {
private String appkey;
private String clientId;
private String clientSecret;
}
创建封装类以及需要用到的方法
package com.czxy.tanhua.autoconfig.template;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.czxy.tanhua.autoconfig.properties.HuanXinProperties;
import com.czxy.tanhua.autoconfig.vo.HuanxinMessageVo;
import com.easemob.im.server.EMProperties;
import com.easemob.im.server.EMService;
import com.easemob.im.server.model.EMTextMessage;
import lombok.extern.log4j.Log4j;
import java.util.Set;
@Log4j
public class HuanXinTemplate {
private EMService service;
public HuanXinTemplate(HuanXinProperties properties) {
EMProperties emProperties = EMProperties.builder()
.setAppkey(properties.getAppkey())
.setClientId(properties.getClientId())
.setClientSecret(properties.getClientSecret())
.build();
service = new EMService(emProperties);
}
/**
* 创建环信用户
* @param username
* @param password
* @return
*/
public Boolean createUser(String username,String password) {
try {
//创建环信用户
service.user().create(username.toLowerCase(), password)
.block();
return true;
}catch (Exception e) {
e.printStackTrace();
log.error("创建环信用户失败~");
}
return false;
}
/**
* 添加联系人
* @param username1
* @param username2
* @return
*/
public Boolean addContact(String username1,String username2) {
try {
//创建环信用户
service.contact().add(username1,username2)
.block();
return true;
}catch (Exception e) {
log.error("添加联系人失败~");
}
return false;
}
/**
* 删除联系人
* @param username1
* @param username2
* @return
*/
public Boolean deleteContact(String username1,String username2) {
try {
//创建环信用户
service.contact().remove(username1,username2)
.block();
return true;
}catch (Exception e) {
log.error("删除联系人失败~");
}
return false;
}
/**
* 发送消息
* @param username
* @param huanxinMessageVo
* @return
*/
public Boolean sendMsg(String username, HuanxinMessageVo huanxinMessageVo) {
try {
//接收人用户列表
Set<String> set = CollUtil.newHashSet(username);
// 将消息内容转换成json
String content = JSON.toJSONString(huanxinMessageVo);
//文本消息
EMTextMessage message = new EMTextMessage().text(content);
//发送消息 from:admin是管理员发送
service.message().send("admin","users",
set,message,null).block();
return true;
}catch (Exception e) {
log.error("删除联系人失败~");
}
return false;
}
}
在引导类中进行声明封装类,将相关的工具类交给IOC容器管理
/** 自动装配类
* @description
*/
@EnableConfigurationProperties({
SmsProperties.class,
OssProperties.class,
AipFaceProperties.class,
HuanXinProperties.class
})
public class TanhuaAutoConfiguration {
@Bean
public SmsTemplate smsTemplate(SmsProperties smsProperties) {
return new SmsTemplate(smsProperties);
}
@Bean
public OssTemplate ossTemplate(OssProperties ossProperties) {
return new OssTemplate(ossProperties);
}
@Bean
public AipFaceTemplate aipFaceTemplate() {
return new AipFaceTemplate();
}
@Bean
public HuanXinTemplate huanXinTemplate(HuanXinProperties properties) {
return new HuanXinTemplate(properties);
}
}
在resources目录下创建META-INF文件夹,在此文件夹下创建spring.factories文件,该文件声明引导类位置,springboot启动时会加载该文件中声明的引导类完成工具类的自动装配。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.czxy.tanhua.autoconfig.TanhuaAutoConfiguration
2:查询用户环信账户
接口地址: http://192.168.136.160:3000/project/19/interface/api/85
app-server模块:
controller层:
@RestController
@RequestMapping("/huanxin")
public class HuanxinController {
@Resource
private UserService userService;
@GetMapping("/user")
public ResponseEntity<HuanXinUserVo> user() {
HuanXinUserVo huanXinUserVo = userService.findHuanXinUser();
return ResponseEntity.ok(huanXinUserVo);
}
}
service层:
public HuanXinUserVo findHuanXinUser() {
// 查询
User user = userApi.findById(UserHolder.getUserId());
if(user == null) {
return null;
}
// 封装
return new HuanXinUserVo(user.getHxUser() ,user.getHxPassword());
}
3:环信ID查询用户信息
app-server模块:
controller层
@GetMapping("/userinfo")
public ResponseEntity userinfo(String huanxinId) {
//查询
UserInfoVo userInfoVo = messageService.findUserInfoByHuanxin(huanxinId);
//返回
return ResponseEntity.ok(userInfoVo);
}
service层:
/**
* 通过环信id查询详情
* @param huanxinId
* @return
*/
public UserInfoVo findUserInfoByHuanxin(String huanxinId) {
//1 通过huanxinId查询用户信息
User user = userApi.findByHuanxin(huanxinId);
//2 查询UserInfo信息
UserInfo userInfo = userInfoApi.findById(user.getId());
//3 封装数据UserInfoVo
UserInfoVo userInfoVo = new UserInfoVo();
BeanUtils.copyProperties(userInfo, userInfoVo);
if(userInfo.getAge() != null){
userInfoVo.setAge(userInfo.getAge().toString());
}
return userInfoVo;
}
通过huanxinId查询用户信息的接口
@Override
public User findByHuanxin(String huanxinId) {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("hx_user", huanxinId);
return userMapper.selectOne(queryWrapper);
}
通过userid查询用户的接口
@Override
public UserInfo findById(Long userInfoId) {
return userInfoMapper.selectById(userInfoId);
}