Files
cai-server/ruoyi-yunxin/src/main/java/com/ruoyi/yunxin/Yunxin.java
dute7liang ea111d983e init
2024-01-29 21:29:26 +08:00

122 lines
4.4 KiB
Java

package com.ruoyi.yunxin;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.yunxin.client.ImMessageClient;
import com.ruoyi.yunxin.config.YunxinProperties;
import com.ruoyi.yunxin.enums.ImTypeEnum;
import com.ruoyi.yunxin.req.*;
import com.ruoyi.yunxin.req.type.YxTextData;
import com.ruoyi.yunxin.resp.SendMsgResp;
import com.ruoyi.yunxin.resp.YxCommonR;
import com.ruoyi.yunxin.resp.YxDataR;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
@Component
public class Yunxin {
private final static String SYS_NOTICE_ID = "2";
@Autowired
private YunxinProperties yunxinProperties;
@Resource
private ImMessageClient messageClient;
/**
* 发送系统消息
* @param toUid 接收者ID
* @param data 数据
* @return
*/
public YxDataR<SendMsgResp> sendToNotice(Long toUid, Object data){
SendMsgReq req = new SendMsgReq();
req.setFrom(SYS_NOTICE_ID);
req.setTo(toUid+"");
req.setBody(JSON.toJSONString(data));
req.setOption(JSON.toJSONString(new Option()));
req.setType(100);
return messageClient.sendMsg(req);
}
/**
* 批量发送 系统消息
* @param toUid
* @return
*/
public YxDataR<YxCommonR> batchSendToNotice(List<Long> toUid, Object data){
SendBatchMsgReq req = new SendBatchMsgReq();
req.setFromAccid(SYS_NOTICE_ID);
req.setToAccids(toUid.stream().map(i -> String.valueOf(toUid)).collect(Collectors.toList()));
req.setBody(JSON.toJSONString(data));
req.setOption(JSON.toJSONString(new Option()));
req.setType(100);
return messageClient.sendBatchMsg(req);
}
public YxDataR<YxCommonR> batchSendToNotice(List<Long> toUid, Object body, Option option, ImTypeEnum type){
SendBatchMsgReq req = new SendBatchMsgReq();
req.setFromAccid(SYS_NOTICE_ID);
req.setToAccids(toUid.stream().map(i -> String.valueOf(toUid)).collect(Collectors.toList()));
req.setBody(JSON.toJSONString(body));
if(option != null){
req.setOption(JSON.toJSONString(new Option()));
}
req.setType(type.getCode());
return messageClient.sendBatchMsg(req);
}
/**
* 指定用户的自定义消息
*/
public YxDataR<SendMsgResp> sendToUserNotice(Long toUid, Long fromUid, Object data){
SendMsgReq req = new SendMsgReq();
req.setFrom(fromUid+"");
req.setTo(toUid+"");
req.setBody(JSON.toJSONString(data));
req.setOption(JSON.toJSONString(new Option()));
return messageClient.sendMsg(req);
}
public YxDataR<YxCommonR> batchSendToTextMessage(Long fromUid, List<Long> toUid, String data){
SendBatchMsgReq req = new SendBatchMsgReq();
req.setFromAccid(fromUid+"");
req.setToAccids(toUid.stream().map(i -> String.valueOf(toUid)).collect(Collectors.toList()));
req.setBody(JSON.toJSONString(new YxTextData(data)));
return messageClient.sendBatchMsg(req);
}
public YxDataR<YxCommonR> sendAttachMsg(Long fromUid, Long toUid, Object data){
SendAttachMsgReq sendAttachMsgReq = new SendAttachMsgReq();
sendAttachMsgReq.setFrom(fromUid+"");
sendAttachMsgReq.setTo(toUid+"");
sendAttachMsgReq.setAttach(JSON.toJSONString(data));
sendAttachMsgReq.setOption(JSON.toJSONString(new Option()));
return messageClient.sendAttachMsg(sendAttachMsgReq);
}
public YxDataR<YxCommonR> sendBatchAttachMsgNotice(List<Long> toUid, Object data){
SendBatchAttachMsgReq req = new SendBatchAttachMsgReq();
req.setFromAccid(SYS_NOTICE_ID);
req.setToAccids(toUid.stream().map(i -> String.valueOf(toUid)).collect(Collectors.toList()));
req.setAttach(JSON.toJSONString(data));
return messageClient.sendBatchAttachMsg(req);
}
public YxDataR<YxCommonR> sendAttachMsgNotice(Long toUid, Object data){
SendAttachMsgReq sendAttachMsgReq = new SendAttachMsgReq();
sendAttachMsgReq.setFrom(SYS_NOTICE_ID);
sendAttachMsgReq.setTo(toUid+"");
sendAttachMsgReq.setAttach(JSON.toJSONString(data));
sendAttachMsgReq.setOption(JSON.toJSONString(new Option()));
return messageClient.sendAttachMsg(sendAttachMsgReq);
}
}