77 lines
3.7 KiB
Java
77 lines
3.7 KiB
Java
package com.ruoyi.cai.trd;
|
||
|
||
import cn.hutool.core.date.DateUtil;
|
||
import cn.hutool.core.lang.UUID;
|
||
import com.alibaba.fastjson2.JSON;
|
||
import com.alibaba.fastjson2.JSONObject;
|
||
import com.ruoyi.cai.util.RestTemplateUtil;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.http.HttpEntity;
|
||
import org.springframework.http.HttpHeaders;
|
||
import org.springframework.stereotype.Component;
|
||
|
||
import java.util.HashMap;
|
||
import java.util.Map;
|
||
|
||
@Component
|
||
public class Yunxin {
|
||
|
||
@Autowired
|
||
private YunxinProperties yunxinProperties;
|
||
|
||
private final static String SEND_URL = "https://api.netease.im/nimserver/msg/sendMsg.action";
|
||
private final static String SEND_ATTR_URL = "https://api.netease.im/nimserver/msg/sendAttachMsg.action";
|
||
|
||
public void sendTo(Long toUid,Object data,Long fromUid){
|
||
Map<String,Object> option = new HashMap<>();
|
||
option.put("push",true); // 该消息是否需要APNS推送或安卓系统通知栏推送
|
||
option.put("roam",false); // 该消息是否需要漫游(需要app开通漫游消息功能)
|
||
option.put("history",false); // 该消息是否存云端历史
|
||
option.put("sendersync",false); // 该消息是否需要发送方多端同步
|
||
option.put("route",false); // 该消息是否需要抄送第三方 (需要app开通消息抄送功能)
|
||
|
||
Map<String,Object> bodyData = new HashMap<>();
|
||
bodyData.put("from", fromUid == null ? yunxinProperties.getDefaultFromUid() :fromUid);
|
||
bodyData.put("ope", 0);
|
||
bodyData.put("to", toUid);
|
||
bodyData.put("type", 100);
|
||
bodyData.put("body", JSON.toJSONString(data));
|
||
bodyData.put("option", JSON.toJSONString(option));
|
||
String nonce = UUID.fastUUID().toString();
|
||
String curTime = DateUtil.currentSeconds() + "";
|
||
HttpHeaders headers = new HttpHeaders();
|
||
headers.add("AppKey",yunxinProperties.getAppKey());
|
||
headers.add("Nonce", nonce);
|
||
headers.add("CurTime", curTime);
|
||
headers.add("CheckSum", yunxinProperties.getAppSecret()+nonce+curTime);
|
||
HttpEntity httpEntity = new HttpEntity<>(bodyData, headers);
|
||
RestTemplateUtil.restTemplate.postForObject(SEND_URL,httpEntity, JSONObject.class);
|
||
}
|
||
|
||
public void sendAttachMsg(Long toUid,Object data,Long fromUid){
|
||
Map<String,Object> option = new HashMap<>();
|
||
option.put("push",true); // 该消息是否需要APNS推送或安卓系统通知栏推送
|
||
option.put("roam",false); // 该消息是否需要漫游(需要app开通漫游消息功能)
|
||
option.put("history",false); // 该消息是否存云端历史
|
||
option.put("sendersync",false); // 该消息是否需要发送方多端同步
|
||
option.put("route",false); // 该消息是否需要抄送第三方 (需要app开通消息抄送功能)
|
||
|
||
Map<String,Object> bodyData = new HashMap<>();
|
||
bodyData.put("from", fromUid == null ? yunxinProperties.getDefaultFromUid() :fromUid);
|
||
bodyData.put("ope", 0);
|
||
bodyData.put("to", toUid);
|
||
bodyData.put("type", 100);
|
||
bodyData.put("body", JSON.toJSONString(data));
|
||
bodyData.put("option", JSON.toJSONString(option));
|
||
String nonce = UUID.fastUUID().toString();
|
||
String curTime = DateUtil.currentSeconds() + "";
|
||
HttpHeaders headers = new HttpHeaders();
|
||
headers.add("AppKey",yunxinProperties.getAppKey());
|
||
headers.add("Nonce", nonce);
|
||
headers.add("CurTime", curTime);
|
||
headers.add("CheckSum", yunxinProperties.getAppSecret()+nonce+curTime);
|
||
HttpEntity httpEntity = new HttpEntity<>(bodyData, headers);
|
||
RestTemplateUtil.restTemplate.postForObject(SEND_ATTR_URL,httpEntity, JSONObject.class);
|
||
}
|
||
}
|