package cn.stylefeng.guns.modular.business.service.impl;
|
|
import cn.hutool.core.util.BooleanUtil;
|
import cn.hutool.core.util.ObjUtil;
|
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.json.JSONUtil;
|
import cn.stylefeng.guns.modular.business.dto.ImPushDataDTO;
|
import cn.stylefeng.guns.modular.business.entity.ImNotice;
|
import cn.stylefeng.guns.modular.business.service.IImNoticeService;
|
import cn.stylefeng.roses.kernel.customer.api.pojo.CustomerInfo;
|
import cn.stylefeng.roses.kernel.customer.modular.entity.Customer;
|
import cn.stylefeng.roses.kernel.customer.modular.service.CustomerService;
|
import cn.stylefeng.roses.kernel.im.api.ImServerApi;
|
import cn.stylefeng.roses.kernel.rule.enums.ImUserTypeEnum;
|
import cn.stylefeng.roses.kernel.rule.enums.PostIdEnum;
|
import io.rong.messages.BaseMessage;
|
import io.rong.messages.TxtMessage;
|
import io.rong.models.response.ResponseResult;
|
import io.rong.models.response.TokenResult;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
|
@Slf4j
|
@Service
|
public class ImBizService {
|
|
@Resource
|
private ImServerApi imServerApi;
|
|
@Resource
|
private IImNoticeService imNoticeService;
|
|
@Resource
|
private CustomerService customerService;
|
|
public String getImToken(Long customerId) {
|
// 获取用户的详细信息
|
CustomerInfo customerInfo = customerService.getCustomerInfoById(customerId);
|
String imToken = customerInfo.getImToken();
|
if (StrUtil.isEmpty(imToken)) {
|
// IM用户注册
|
TokenResult tokenResult = imServerApi.userRegister(customerInfo.getCustomerId().toString(), customerInfo.getNickName(), customerInfo.getAvatarObjectUrl());
|
if (tokenResult.getCode() == 200) {
|
imToken = tokenResult.getToken();
|
|
// 更新用户IM通讯token
|
Customer customer = new Customer();
|
customer.setCustomerId(customerId);
|
customer.setImToken(imToken);
|
customerService.updateCustomerRemoveCache(customer);
|
}
|
}
|
|
return imToken;
|
}
|
|
public <T extends BaseMessage> ResponseResult messageSendSystem(String fromUserId, String[] toUserIds, ImPushDataDTO pushData, ImUserTypeEnum userTypeEnum, PostIdEnum postIdEnum, Boolean isSaveImNotice) {
|
// 推送json消息
|
String pushDataStr = JSONUtil.toJsonStr(pushData);
|
try {
|
// 是否保存IM通知
|
if (BooleanUtil.isTrue(isSaveImNotice)) {
|
for (String userId : toUserIds) {
|
// 保存IM通知
|
imNoticeService.save(
|
ImNotice.builder()
|
.type(pushData.getType())
|
.userType(userTypeEnum == null ? null : userTypeEnum.getCode())
|
.postId(postIdEnum == null ? null : postIdEnum.getCode())
|
.userId(Long.valueOf(userId))
|
.title(pushData.getTitle())
|
.content(pushData.getContent())
|
.pushJson(pushDataStr)
|
.businessId(ObjUtil.toString(pushData.getObjId()))
|
.build()
|
);
|
}
|
}
|
} catch (Exception e) {
|
log.error("保存IM通知异常");
|
e.printStackTrace();
|
}
|
if (toUserIds.length > 0) {
|
log.info("融云发送群消息:{}", pushDataStr);
|
// 融云发送通知
|
return imServerApi.messageSendSystem(fromUserId, toUserIds, new TxtMessage(pushDataStr, pushData.getExtra()));
|
}
|
return null;
|
}
|
|
public <T extends BaseMessage> ResponseResult messageSendPrivate(String fromUserId, String[] toUserIds, ImPushDataDTO pushData) {
|
// 推送json消息
|
String pushDataStr = JSONUtil.toJsonStr(pushData);
|
if (toUserIds.length > 0) {
|
log.info("融云发送单聊消息:{}", pushDataStr);
|
// 融云发送单聊消息
|
return imServerApi.messageSendPrivate(fromUserId, toUserIds, new TxtMessage(pushDataStr, pushData.getExtra()));
|
}
|
return null;
|
}
|
|
public <T extends BaseMessage> ResponseResult messageSendGroup(String fromUserId, String[] toGroupIds, ImPushDataDTO pushData) {
|
// 推送json消息
|
String pushDataStr = JSONUtil.toJsonStr(pushData);
|
if (toGroupIds.length > 0) {
|
log.info("融云发送群消息:{}", pushDataStr);
|
// 融云发送群消息
|
return imServerApi.messageSendGroup(fromUserId, toGroupIds, new TxtMessage(pushDataStr, pushData.getExtra()));
|
}
|
return null;
|
}
|
|
}
|