|
@@ -0,0 +1,225 @@
|
|
|
+package com.nokia.pushmessage.service;
|
|
|
+
|
|
|
+import java.text.DateFormat;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.lang.Nullable;
|
|
|
+import org.springframework.util.DigestUtils;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.nokia.pushmessage.config.PushMessageProperties;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 使用RestTemplate调用能力商店钉钉消息推送服务API接口
|
|
|
+ *
|
|
|
+ * 此接口实际调用webhook接口的机器人实现
|
|
|
+ *
|
|
|
+ * markdown方式使用<font color='red'>内容</font>方式修改字体颜色,在pc端钉钉可生效,在安卓端钉钉上不生效。
|
|
|
+ */
|
|
|
+public class PushMessageService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PushMessageProperties properties;
|
|
|
+
|
|
|
+ private RestTemplate restTemplate = new RestTemplate();
|
|
|
+
|
|
|
+ private DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
|
|
|
+
|
|
|
+ public PushMessageService() {
|
|
|
+ System.out.println("===PushMessageService===");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送文本消息
|
|
|
+ *
|
|
|
+ * @param message
|
|
|
+ */
|
|
|
+ public void sendTextMessage(String message) {
|
|
|
+ sendTextMessage(message, null, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送文本消息
|
|
|
+ *
|
|
|
+ * @param message
|
|
|
+ * @param atList
|
|
|
+ */
|
|
|
+ public void sendTextMessage(String message, @Nullable List<Integer> atList) {
|
|
|
+ sendTextMessage(message, atList, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送文本消息
|
|
|
+ *
|
|
|
+ * @param message
|
|
|
+ * @param atAll
|
|
|
+ */
|
|
|
+ public void sendTextMessage(String message, @Nullable Boolean atAll) {
|
|
|
+ sendTextMessage(message, null, atAll);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送文本消息
|
|
|
+ *
|
|
|
+ * @param messge
|
|
|
+ * @param atList
|
|
|
+ * @param atAll
|
|
|
+ */
|
|
|
+ public void sendTextMessage(String messge, @Nullable List<Integer> atList, @Nullable Boolean atAll) {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("MSG_TYPE", "text");
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ map.put("DATA", data);
|
|
|
+ Map<String, Object> text = new HashMap<>();
|
|
|
+ data.put("TEXT", text);
|
|
|
+ messge = messge.startsWith(properties.getPrefix()) ? messge : properties.getPrefix() + messge;
|
|
|
+ text.put("CONTENT", messge);
|
|
|
+ if (atList != null) {
|
|
|
+ data.put("AT_LIST", atList);
|
|
|
+ }
|
|
|
+ if (atAll != null) {
|
|
|
+ data.put("AT_ALL", atAll);
|
|
|
+ }
|
|
|
+ sendMessage(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送markdown消息
|
|
|
+ *
|
|
|
+ * @param messge
|
|
|
+ */
|
|
|
+ public void sendMarkdownMessage(String messge) {
|
|
|
+ sendMarkdownMessage(messge, null, null, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送markdown消息
|
|
|
+ *
|
|
|
+ * @param messge
|
|
|
+ * @param title
|
|
|
+ */
|
|
|
+ public void sendMarkdownMessage(String messge, String title) {
|
|
|
+ sendMarkdownMessage(messge, title, null, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送markdown消息
|
|
|
+ *
|
|
|
+ * @param messge
|
|
|
+ * @param atList
|
|
|
+ */
|
|
|
+ public void sendMarkdownMessage(String messge, List<Integer> atList) {
|
|
|
+ sendMarkdownMessage(messge, null, atList, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送markdown消息
|
|
|
+ *
|
|
|
+ * @param messge
|
|
|
+ * @param atAll
|
|
|
+ */
|
|
|
+ public void sendMarkdownMessage(String messge, @Nullable Boolean atAll) {
|
|
|
+ sendMarkdownMessage(messge, null, null, atAll);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送markdown消息
|
|
|
+ *
|
|
|
+ * @param messge
|
|
|
+ * @param title
|
|
|
+ * @param atList
|
|
|
+ * @param atAll
|
|
|
+ */
|
|
|
+ public void sendMarkdownMessage(String messge, @Nullable String title, @Nullable List<Integer> atList,
|
|
|
+ @Nullable Boolean atAll) {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("MSG_TYPE", "markdown");
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ map.put("DATA", data);
|
|
|
+ Map<String, Object> markdown = new HashMap<>();
|
|
|
+ data.put("MARK_DOWN", markdown);
|
|
|
+ title = title == null || "".equals(title) ? properties.getPrefix() : title;
|
|
|
+ title = title.startsWith(properties.getPrefix()) ? title : properties.getPrefix() + title;
|
|
|
+ markdown.put("TITLE", title);
|
|
|
+ markdown.put("TEXT", messge);
|
|
|
+ if (atList != null) {
|
|
|
+ data.put("AT_LIST", atList);
|
|
|
+ }
|
|
|
+ if (atAll != null) {
|
|
|
+ data.put("AT_ALL", atAll);
|
|
|
+ }
|
|
|
+ sendMessage(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void sendMessage(Map<String, Object> reqMap) {
|
|
|
+ JsonNode respon = restTemplate.postForObject(properties.getUrl(), getRequest(reqMap), JsonNode.class);
|
|
|
+ if (respon == null) {
|
|
|
+ throw new RuntimeException("调用结果为空");
|
|
|
+ }
|
|
|
+ JsonNode node = respon.get("UNI_BSS_HEAD");
|
|
|
+ if ("00000".equals(node.get("RESP_CODE").textValue())) {
|
|
|
+ node = respon.get("UNI_BSS_BODY").get("PUSH_MESSAGE_RSP");
|
|
|
+ if (node != null && "0000".equals(node.get("STATUS").textValue())) {
|
|
|
+ node = node.get("RSP");
|
|
|
+ if (node != null && "0000".equals(node.get("RSP_CODE").textValue())) {
|
|
|
+ // 只有到这里才说明调用成功了
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException(respon.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> getRequest(Map<String, Object> reqMap) {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("UNI_BSS_HEAD", getUniBssHead());
|
|
|
+ map.put("UNI_BSS_BODY", getUniBssBody(reqMap));
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> getUniBssBody(Map<String, Object> reqMap) {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("PUSH_MESSAGE_REQ", reqMap);
|
|
|
+ reqMap.put("ACCESS_TOKEN", properties.getAccessToken());
|
|
|
+ reqMap.put("BUSI_CODE", properties.getBusiCode());
|
|
|
+ reqMap.put("MODULE_ID", properties.getModuleId());
|
|
|
+ reqMap.put("SYSTEM_ID", properties.getSystemId());
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> getUniBssHead() {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ Date date = new Date();
|
|
|
+ String timeStamp = getTimeStamp(date);
|
|
|
+ String transId = getTransId(date);
|
|
|
+ map.put("APP_ID", properties.getAppId());
|
|
|
+ map.put("TIMESTAMP", timeStamp);
|
|
|
+ map.put("TRANS_ID", transId);
|
|
|
+ map.put("TOKEN", getToken(timeStamp, transId));
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getToken(String timeStamp, String transId) {
|
|
|
+ String beforeEncode = "APP_ID" + properties.getAppId() + "TIMESTAMP" + timeStamp + "TRANS_ID" + transId
|
|
|
+ + properties.getAppSecret();
|
|
|
+ return DigestUtils.md5DigestAsHex(beforeEncode.getBytes());
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getTransId(Date date) {
|
|
|
+ return (new SimpleDateFormat("yyyyMMddHHmmssSSS" + getRandomNumber())).format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getTimeStamp(Date date) {
|
|
|
+ return dateFormat1.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ private int getRandomNumber() {
|
|
|
+ return (int) ((Math.random() * 9D + 1D) * 100000D);
|
|
|
+ }
|
|
|
+}
|