diff --git a/notify-wechat/pom.xml b/notify-wechat/pom.xml
index 58036b5..0b52f15 100644
--- a/notify-wechat/pom.xml
+++ b/notify-wechat/pom.xml
@@ -10,35 +10,46 @@
4.0.0
notify-wechat
-
-
- com.cicdi
- notify-core
- 1.0-SNAPSHOT
- compile
-
-
- org.springframework.boot
- spring-boot-starter-webflux
- 2.3.11.RELEASE
-
-
- com.alibaba
- fastjson
- 1.2.73
- compile
-
-
- junit
- junit
- 4.12
- test
-
-
8
8
+
+
+
+ com.cicdi
+ notify-core
+ 1.0-SNAPSHOT
+ compile
+
+
+
+ org.springframework.boot
+ spring-boot-starter-webflux
+ 2.3.11.RELEASE
+
+
+
+ com.alibaba
+ fastjson
+ 1.2.73
+ compile
+
+
+
+ junit
+ junit
+ 4.12
+ test
+
+
+
+ org.apache.httpcomponents
+ httpclient
+ 4.5.13
+
+
+
\ No newline at end of file
diff --git a/notify-wechat/src/main/java/com/cicdi/notify/wechat/WeixinCorpNotifier.java b/notify-wechat/src/main/java/com/cicdi/notify/wechat/WeixinCorpNotifier.java
index c2a1645..d2e3225 100644
--- a/notify-wechat/src/main/java/com/cicdi/notify/wechat/WeixinCorpNotifier.java
+++ b/notify-wechat/src/main/java/com/cicdi/notify/wechat/WeixinCorpNotifier.java
@@ -1,26 +1,33 @@
package com.cicdi.notify.wechat;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
import com.cicdi.notify.AbstractNotifier;
import com.cicdi.notify.DefaultNotifyType;
import com.cicdi.notify.NotifyType;
import com.cicdi.notify.Provider;
import com.cicdi.notify.template.TemplateManager;
-import org.springframework.http.MediaType;
-import org.springframework.web.reactive.function.BodyInserters;
-import org.springframework.web.reactive.function.client.ClientResponse;
-import org.springframework.web.reactive.function.client.WebClient;
-import org.springframework.web.util.UriComponentsBuilder;
-import reactor.core.publisher.Mono;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.utils.URIBuilder;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.util.EntityUtils;
+import java.io.IOException;
import java.time.Duration;
-import java.util.HashMap;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Map;
-import java.util.concurrent.atomic.AtomicReference;
public class WeixinCorpNotifier extends AbstractNotifier {
- private final AtomicReference accessToken = new AtomicReference<>();
+ private String accessToken;
private long refreshTokenTime;
@@ -28,7 +35,7 @@ public class WeixinCorpNotifier extends AbstractNotifier
private static final String tokenApi = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
- private static final String notify = "https://qyapi.weixin.qq.com/cgi-bin/message/send";
+ private static final String notifyApi = "https://qyapi.weixin.qq.com/cgi-bin/message/send";
private final WechatCorpProperties properties;
@@ -58,67 +65,64 @@ public class WeixinCorpNotifier extends AbstractNotifier
@Override
public void send(WechatMessageTemplate template, Map context) {
- String token1 = getToken();
- WebClient.create().post()
- .uri(UriComponentsBuilder.fromUriString(notify).queryParam("access_token", token1).toUriString())
- .contentType(MediaType.APPLICATION_JSON)
- .body(BodyInserters.fromValue(template.createJsonRequest(context)))
- .exchange()
- .flatMap(clientResponse -> clientResponse.bodyToMono(HashMap.class))
- .as(this::checkResult)
- .then();
- }
+ CloseableHttpClient httpClient = HttpClientBuilder.create().build();
+ HttpPost httpPost = new HttpPost(notifyApi + "?access_token=" + getToken());
+ httpPost.setHeader("Content-Type", "application/json;charset=utf8");
- private Mono checkResult(Mono msg) {
- return msg.doOnNext(map -> {
- String code = String.valueOf(map.get("errcode"));
- if ("0".equals(code)) {
+ StringEntity stringEntity = new StringEntity(template.createJsonRequest(context), "UTF-8");
+ httpPost.setEntity(stringEntity);
+
+ try {
+ CloseableHttpResponse response = httpClient.execute(httpPost);
+ String result = EntityUtils.toString(response.getEntity());
+ System.out.println(result);
+ JSONObject jsonObject = JSON.parseObject(result);
+ if ("0".equals(jsonObject.get("errcode").toString())) {
// log.info("发送微信企业通知成功");
} else {
// log.warn("发送微信企业通知失败:{}", map);
// throw new BusinessException("发送微信企业通知失败:" + map.get("errmsg"), code);
}
- });
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
}
private String getToken() {
- if (System.currentTimeMillis() - refreshTokenTime > tokenTimeOut || accessToken.get() == null) {
+ if (System.currentTimeMillis() - refreshTokenTime > tokenTimeOut || accessToken == null) {
return requestToken();
}
- return accessToken.get();
+ return accessToken;
}
private String requestToken() {
- Mono responseMono = WebClient.create().get()
- .uri(UriComponentsBuilder.fromUriString(tokenApi)
- .queryParam("corpId", properties.getCorpId())
- .queryParam("corpSecret", properties.getCorpSecret())
- .build().toUri())
- .exchange();
+ List params = new ArrayList<>();
+ params.add(new BasicNameValuePair("corpId", properties.getCorpId()));
+ params.add(new BasicNameValuePair("corpSecret", properties.getCorpSecret()));
- responseMono.flatMap(resp -> {
- System.out.println(resp);
- return resp.bodyToMono(HashMap.class);
- })
- .map(map -> {
- if (map.containsKey("access_token")) {
- return map.get("access_token");
- }
- // TODO 自定义异常
-// throw new BusinessException("获取Token失败:" + map.get("errmsg"), String.valueOf(map.get("errcode")));
- return Mono.empty();
- })
- .cast(String.class)
- .doOnNext(r -> {
- refreshTokenTime = System.currentTimeMillis();
- accessToken.set(r);
- });
- return accessToken.get();
+ CloseableHttpClient httpClient = HttpClientBuilder.create().build();
+ try {
+ HttpGet httpGet = new HttpGet(new URIBuilder(tokenApi).setParameters(params).build());
+ CloseableHttpResponse response = httpClient.execute(httpGet);
+ JSONObject responseJson = JSON.parseObject(EntityUtils.toString(response.getEntity()));
+ if (responseJson.containsKey("access_token")) {
+ String access_token = responseJson.get("access_token").toString();
+ accessToken = access_token;
+ refreshTokenTime = System.currentTimeMillis();
+ return access_token;
+ } else {
+ // TODO 自定义异常
+ // throw new BusinessException("获取Token失败:" + map.get("errmsg"), String.valueOf(map.get("errcode")));
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return null;
}
@Override
public void close() {
- accessToken.set(null);
- refreshTokenTime = 0;
+ this.accessToken = null;
+ this.refreshTokenTime = 0;
}
}
diff --git a/notify-wechat/src/test/java/com/cicdi/notify/wechat/WechatTest.java b/notify-wechat/src/test/java/com/cicdi/notify/wechat/WechatTest.java
index 4a3843d..b3d8bf8 100644
--- a/notify-wechat/src/test/java/com/cicdi/notify/wechat/WechatTest.java
+++ b/notify-wechat/src/test/java/com/cicdi/notify/wechat/WechatTest.java
@@ -18,24 +18,20 @@ import java.util.Map;
public class WechatTest {
@Test
public void test() {
- Hooks.onOperatorDebug();
// 通知器配置管理器
- NotifyConfigManager notifyConfigManager = new NotifyConfigManager() {
- @Override
- public NotifierProperties getNotifyConfig(NotifyType notifyType, String configId) {
- NotifierProperties notifierProperties = new NotifierProperties();
- notifierProperties.setType(DefaultNotifyType.wechat.getId());
- notifierProperties.setProvider(WechatProvider.corpMessage.getId());
- notifierProperties.setId("12");
+ NotifyConfigManager notifyConfigManager = (notifyType, configId) -> {
+ NotifierProperties notifierProperties = new NotifierProperties();
+ notifierProperties.setType(DefaultNotifyType.wechat.getId());
+ notifierProperties.setProvider(WechatProvider.corpMessage.getId());
+ notifierProperties.setId("12");
- Map config = new HashMap<>();
- config.put("corpId", "wwbd49ae2419a55a9f");
- config.put("corpSecret", "TXDRQw_H8gpVKX0E01EmwMXJ4VooXmj65I-mDe0wQ1k");
+ Map config = new HashMap<>();
+ config.put("corpId", "wwbd49ae2419a55a9f");
+ config.put("corpSecret", "TXDRQw_H8gpVKX0E01EmwMXJ4VooXmj65I-mDe0wQ1k");
- notifierProperties.setConfiguration(config);
+ notifierProperties.setConfiguration(config);
- return notifierProperties;
- }
+ return notifierProperties;
};
// 模板管理器
diff --git a/pom.xml b/pom.xml
index 44a1023..f37e772 100644
--- a/pom.xml
+++ b/pom.xml
@@ -20,4 +20,21 @@
8
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.8.1
+
+
+ 8
+ UTF-8
+
+
+
+
+
+
\ No newline at end of file