api
/*
* 环信 接口API 用于 在线聊天功能接口
* */
public interface HuanXinApi{
/**
* 获取环信token(获取管理员权限)
* 参见:http://docs-im.easemob.com/im/server/ready/user#%E8%8E%B7%E5%8F%96%E7%AE%A1%E7%90%86%E5%91%98%E6%9D%83%E9%99%90
*
* @return
*/
String getToken();
/**
* 注册环信用户
* 参见:http://docs-im.easemob.com/im/server/ready/user#%E6%B3%A8%E5%86%8C%E5%8D%95%E4%B8%AA%E7%94%A8%E6%88%B7_%E5%BC%80%E6%94%BE
*
* @param userId 用户id
* @return
*/
Boolean register(Long userId);
/**
* 根据用户Id询环信账户信息
*
* @param userId
* @return
*/
HuanXinUser queryHuanXinUser(Long userId);
/**
* 根据环信用户名查询用户信息
*
* @param userName
* @return
*/
HuanXinUser queryUserByUserName(String userName);
/**
* 添加好友(双向好友关系)
* 参见:http://docs-im.easemob.com/im/server/ready/user#%E6%B7%BB%E5%8A%A0%E5%A5%BD%E5%8F%8B
*
* @param userId 自己的id
* @param friendId 好友的id
* @return
*/
Boolean addUserFriend(Long userId, Long friendId);
/**
* 删除好友关系(双向删除)
* 参见:http://docs-im.easemob.com/im/server/ready/user#%E7%A7%BB%E9%99%A4%E5%A5%BD%E5%8F%8B
*
* @param userId 自己的id
* @param friendId 好友的id
* @return
*/
Boolean removeUserFriend(Long userId, Long friendId);
}ApiImpl
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.Method;
import cn.hutool.json.JSONUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.server.config.HuanXinConfig;
import com.tanhua.dubbo.server.mapper.HuanXinUserMapper;
import com.tanhua.dubbo.server.pojo.HuanXinUser;
import com.tanhua.dubbo.server.service.RequestService;
import com.tanhua.dubbo.server.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Arrays;
import java.util.Date;
@Service(version = "1.0.0")
public class HuanXinApiImpl implements HuanXinApi {
@Autowired
private TokenService tokenService;
@Autowired
private HuanXinConfig huanXinConfig;
@Autowired
private RequestService requestService;
@Autowired
private HuanXinUserMapper huanXinUserMapper;
@Override
public String getToken() {
return this.tokenService.getToken();
}
@Override
public Boolean register(Long userId) {
String targetUrl = this.huanXinConfig.getUrl()
+ this.huanXinConfig.getOrgName() + "/" +
this.huanXinConfig.getAppName() + "/users";
HuanXinUser huanXinUser = new HuanXinUser();
huanXinUser.setUsername("HX_" + userId); // 用户名
huanXinUser.setPassword(IdUtil.simpleUUID()); //随机生成的密码
HttpResponse response = this.requestService.execute(targetUrl, JSONUtil.toJsonStr(Arrays.asList(huanXinUser)), Method.POST);
if (response.isOk()) {
//将环信的账号信息保存到数据库
huanXinUser.setUserId(userId);
huanXinUser.setCreated(new Date());
huanXinUser.setUpdated(huanXinUser.getCreated());
this.huanXinUserMapper.insert(huanXinUser);
return true;
}
return false;
}
@Override
public HuanXinUser queryHuanXinUser(Long userId) {
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("user_id", userId);
return this.huanXinUserMapper.selectOne(wrapper);
}
@Override
public HuanXinUser queryUserByUserName(String userName) {
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("username", userName);
return this.huanXinUserMapper.selectOne(wrapper);
}
@Override
public Boolean addUserFriend(Long userId, Long friendId) {
String targetUrl = this.huanXinConfig.getUrl()
+ this.huanXinConfig.getOrgName() + "/"
+ this.huanXinConfig.getAppName() + "/users/HX_" +
userId + "/contacts/users/HX_" + friendId;
try {
// 404 -> 对方未在环信注册
return this.requestService.execute(targetUrl, null, Method.POST).isOk();
} catch (Exception e) {
e.printStackTrace();
}
// 添加失败
return false;
}
@Override
public Boolean removeUserFriend(Long userId, Long friendId) {
String targetUrl = this.huanXinConfig.getUrl()
+ this.huanXinConfig.getOrgName() + "/"
+ this.huanXinConfig.getAppName() + "/users/HX_" +
userId + "/contacts/users/HX_" + friendId;
try {
// 404 -> 对方未在环信注册
return this.requestService.execute(targetUrl, null, Method.DELETE).isOk();
} catch (Exception e) {
e.printStackTrace();
}
// 添加失败
return false;
}
} | 留言与评论(共有 0 条评论) “” |