|
@@ -0,0 +1,76 @@
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import org.apache.commons.codec.binary.Base64;
|
|
|
+import org.apache.log4j.Logger;
|
|
|
+
|
|
|
+import javax.crypto.Mac;
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class AlarmUtil {
|
|
|
+
|
|
|
+ private final static Logger log = Logger.getLogger(AlarmUtil.class);
|
|
|
+
|
|
|
+ private static final String DING_DING_URL = "https://oapi.dingtalk.com/robot/send?access_token=fc68871c0fd75e3921f370a7f0b936fd78d879cf2e36c94a1dcb07994fc25202";
|
|
|
+
|
|
|
+ private static final String LOCAL_SECRET = "SECb3651d77a3baffb27e1c492c0df786262ce52aca6ba6cc15c83795f56ea9281f";
|
|
|
+
|
|
|
+ private static final String SHA = "HmacSHA256";
|
|
|
+
|
|
|
+ private static final String CHARSET_NAME = "UTF-8";
|
|
|
+
|
|
|
+ private static final String SUCCESS_CODE = "0";
|
|
|
+
|
|
|
+ public static boolean alarmByDingDing(String content, boolean isAtAll, List<String> mobileList) {
|
|
|
+ String wholeUrl = generateWholeUrl();
|
|
|
+ if (null == wholeUrl) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ String reqStr = buildReqStr(content, isAtAll, mobileList);
|
|
|
+ String result = HttpUtil.post(wholeUrl, reqStr);
|
|
|
+ JSONObject jsonObject = JSONUtil.parseObj(result);
|
|
|
+ if (null == jsonObject.get("errcode")) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return String.valueOf(jsonObject.get("errcode")).equals(SUCCESS_CODE) ? true : false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String generateWholeUrl() {
|
|
|
+ try {
|
|
|
+ Long timestamp = System.currentTimeMillis();
|
|
|
+ String stringToSign = timestamp + "\n" + LOCAL_SECRET;
|
|
|
+ Mac mac = Mac.getInstance(SHA);
|
|
|
+ mac.init(new SecretKeySpec(LOCAL_SECRET.getBytes(CHARSET_NAME), SHA));
|
|
|
+ byte[] signData = mac.doFinal(stringToSign.getBytes(CHARSET_NAME));
|
|
|
+ String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), CHARSET_NAME);
|
|
|
+ return DING_DING_URL + "×tamp=" + timestamp + "&sign=" + sign;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("生成钉钉请求全路径失败", e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String buildReqStr(String content, boolean isAtAll, List<String> mobileList) {
|
|
|
+ //消息内容
|
|
|
+ Map<String, String> contentMap = new HashMap<>();
|
|
|
+ contentMap.put("content", content);
|
|
|
+
|
|
|
+ //通知人
|
|
|
+ Map<String, Object> atMap = new HashMap<>();
|
|
|
+ //是否通知所有人
|
|
|
+ atMap.put("isAtAll", isAtAll);
|
|
|
+ //通知具体人的手机号码列表
|
|
|
+ atMap.put("atMobiles", mobileList);
|
|
|
+
|
|
|
+ Map<String, Object> reqMap = new HashMap<>();
|
|
|
+ reqMap.put("msgtype", "text");
|
|
|
+ reqMap.put("text", contentMap);
|
|
|
+ reqMap.put("at", atMap);
|
|
|
+
|
|
|
+ return JSONUtil.toJsonStr(reqMap);
|
|
|
+ }
|
|
|
+}
|