package com.ruoyi.yunxin; import com.alibaba.fastjson2.JSON; import com.ruoyi.yunxin.client.ImMessageClient; import com.ruoyi.yunxin.config.YunxinExecutorConstant; import com.ruoyi.yunxin.config.YunxinProperties; import com.ruoyi.yunxin.req.Option; import com.ruoyi.yunxin.req.SendAttachMsgReq; import com.ruoyi.yunxin.req.SendBatchMsgReq; import com.ruoyi.yunxin.req.SendMsgReq; 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 { @Autowired private YunxinProperties yunxinProperties; @Resource private ImMessageClient messageClient; public void sendToSync(Long toUid,Long fromUid,Object data){ YunxinExecutorConstant.COMMON_EXECUTOR.execute(() -> { this.sendTo(toUid,fromUid,data); }); } public YxDataR sendTo(Long toUid,Long fromUid,Object data){ SendMsgReq req = new SendMsgReq(); req.setFrom(fromUid == null ? yunxinProperties.getDefaultFromUid() : 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(toUid.stream().map(i -> String.valueOf(toUid)).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); } }