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.YxImTypeEnum; 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 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 batchSendToNotice(List toUid, Object data){ SendBatchMsgReq req = new SendBatchMsgReq(); req.setFromAccid(SYS_NOTICE_ID); req.setToAccids(JSON.toJSONString(toUid.stream().map(String::valueOf).collect(Collectors.toList()))); req.setBody(JSON.toJSONString(data)); req.setOption(JSON.toJSONString(new Option())); req.setType(100); return messageClient.sendBatchMsg(req); } public YxDataR batchSendToNotice(List toUid, Object body, Option option, YxImTypeEnum type){ SendBatchMsgReq req = new SendBatchMsgReq(); req.setFromAccid(SYS_NOTICE_ID); req.setToAccids(JSON.toJSONString(toUid.stream().map(String::valueOf).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 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 batchSendToTextMessage(Long fromUid, List toUid, String data){ SendBatchMsgReq req = new SendBatchMsgReq(); req.setFromAccid(fromUid+""); req.setToAccids(JSON.toJSONString(toUid.stream().map(String::valueOf).collect(Collectors.toList()))); req.setBody(JSON.toJSONString(new YxTextData(data))); return messageClient.sendBatchMsg(req); } public YxDataR 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 sendBatchAttachMsgNotice(List toUid, Object data){ SendBatchAttachMsgReq req = new SendBatchAttachMsgReq(); req.setFromAccid(SYS_NOTICE_ID); req.setToAccids(JSON.toJSONString(toUid.stream().map(String::valueOf).collect(Collectors.toList()))); req.setAttach(JSON.toJSONString(data)); return messageClient.sendBatchAttachMsg(req); } public YxDataR 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); } }