|
@@ -0,0 +1,88 @@
|
|
|
+package com.winhc.dataworks.flow.touch.utils;
|
|
|
+
|
|
|
+import cn.hutool.crypto.digest.HMac;
|
|
|
+import cn.hutool.crypto.digest.HmacAlgorithm;
|
|
|
+import com.helospark.lightdi.annotation.Autowired;
|
|
|
+import com.helospark.lightdi.annotation.Component;
|
|
|
+import com.helospark.lightdi.annotation.Value;
|
|
|
+import com.winhc.dataworks.flow.touch.bean.Entry;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import okhttp3.*;
|
|
|
+import org.apache.commons.codec.binary.Base64;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author: XuJiakai
|
|
|
+ * @Date: 2020/6/28 14:39
|
|
|
+ * @Description:
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class DingUtils {
|
|
|
+ @Autowired
|
|
|
+ private OkHttpClient client;
|
|
|
+ private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
|
|
+ private static final String URL = "https://oapi.dingtalk.com/robot/send?access_token=2773b742b74d84599c4f05f7b42cacd6714b10c33cd4c74402649019fa7e56c8";
|
|
|
+ @Value("${ding-secret}")
|
|
|
+ private String dingSecret;
|
|
|
+
|
|
|
+
|
|
|
+ public boolean send(String msg) {
|
|
|
+ Entry<Long, String> sign = getSign();
|
|
|
+ String query = "×tamp=" + sign.getKey() + "&sign=" + sign.getValue();
|
|
|
+ try {
|
|
|
+ String post = post(URL + query, getPostBody(msg));
|
|
|
+ log.info(post);
|
|
|
+ return post.contains("ok");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private String post(String url, String json) throws IOException {
|
|
|
+ RequestBody body = RequestBody.create(JSON, json);
|
|
|
+ try (Response response = client.newCall((new Request.Builder()).url(url).post(body).build()).execute()) {
|
|
|
+ return parseBody(response);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String get(String url) throws IOException {
|
|
|
+ try (Response response = client.newCall(new Request.Builder().url(url).get().build()).execute()) {
|
|
|
+ return parseBody(response);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String parseBody(Response response) throws IOException {
|
|
|
+ return response == null ? "" : response.isSuccessful() ? Objects.requireNonNull(response.body()).string() : "";
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getPostBody(String msg) {
|
|
|
+ return "{\"msgtype\": \"text\",\"text\": {\"content\": \"" + msg + "\"}}";
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ private Entry<Long, String> getSign() {
|
|
|
+ Long timestamp = System.currentTimeMillis();
|
|
|
+ String stringToSign = timestamp + "\n" + dingSecret;
|
|
|
+
|
|
|
+
|
|
|
+ byte[] key = dingSecret.getBytes("UTF-8");
|
|
|
+ HMac mac = new HMac(HmacAlgorithm.HmacSHA256, key);
|
|
|
+ byte[] signData = mac.digest(stringToSign);
|
|
|
+
|
|
|
+ String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
|
|
|
+ Entry<Long, String> entry = new Entry<>();
|
|
|
+ entry.setKey(timestamp);
|
|
|
+ entry.setValue(sign);
|
|
|
+ return entry;
|
|
|
+ }
|
|
|
+}
|