deps: 添加编译使用的文件编码

This commit is contained in:
椰子 2021-07-02 20:23:06 +08:00
parent f69eeb502c
commit 2c880a5f05
4 changed files with 120 additions and 92 deletions

View File

@ -10,35 +10,46 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>notify-wechat</artifactId> <artifactId>notify-wechat</artifactId>
<dependencies>
<dependency>
<groupId>com.cicdi</groupId>
<artifactId>notify-core</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.3.11.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties> <properties>
<maven.compiler.source>8</maven.compiler.source> <maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target> <maven.compiler.target>8</maven.compiler.target>
</properties> </properties>
<dependencies>
<dependency>
<groupId>com.cicdi</groupId>
<artifactId>notify-core</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.3.11.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
</dependencies>
</project> </project>

View File

@ -1,26 +1,33 @@
package com.cicdi.notify.wechat; package com.cicdi.notify.wechat;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cicdi.notify.AbstractNotifier; import com.cicdi.notify.AbstractNotifier;
import com.cicdi.notify.DefaultNotifyType; import com.cicdi.notify.DefaultNotifyType;
import com.cicdi.notify.NotifyType; import com.cicdi.notify.NotifyType;
import com.cicdi.notify.Provider; import com.cicdi.notify.Provider;
import com.cicdi.notify.template.TemplateManager; import com.cicdi.notify.template.TemplateManager;
import org.springframework.http.MediaType; import org.apache.http.NameValuePair;
import org.springframework.web.reactive.function.BodyInserters; import org.apache.http.client.methods.CloseableHttpResponse;
import org.springframework.web.reactive.function.client.ClientResponse; import org.apache.http.client.methods.HttpGet;
import org.springframework.web.reactive.function.client.WebClient; import org.apache.http.client.methods.HttpPost;
import org.springframework.web.util.UriComponentsBuilder; import org.apache.http.client.utils.URIBuilder;
import reactor.core.publisher.Mono; 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.time.Duration;
import java.util.HashMap; import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
public class WeixinCorpNotifier extends AbstractNotifier<WechatMessageTemplate> { public class WeixinCorpNotifier extends AbstractNotifier<WechatMessageTemplate> {
private final AtomicReference<String> accessToken = new AtomicReference<>(); private String accessToken;
private long refreshTokenTime; private long refreshTokenTime;
@ -28,7 +35,7 @@ public class WeixinCorpNotifier extends AbstractNotifier<WechatMessageTemplate>
private static final String tokenApi = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"; 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; private final WechatCorpProperties properties;
@ -58,67 +65,64 @@ public class WeixinCorpNotifier extends AbstractNotifier<WechatMessageTemplate>
@Override @Override
public void send(WechatMessageTemplate template, Map<String, Object> context) { public void send(WechatMessageTemplate template, Map<String, Object> context) {
String token1 = getToken(); CloseableHttpClient httpClient = HttpClientBuilder.create().build();
WebClient.create().post() HttpPost httpPost = new HttpPost(notifyApi + "?access_token=" + getToken());
.uri(UriComponentsBuilder.fromUriString(notify).queryParam("access_token", token1).toUriString()) httpPost.setHeader("Content-Type", "application/json;charset=utf8");
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(template.createJsonRequest(context)))
.exchange()
.flatMap(clientResponse -> clientResponse.bodyToMono(HashMap.class))
.as(this::checkResult)
.then();
}
private Mono<HashMap> checkResult(Mono<HashMap> msg) { StringEntity stringEntity = new StringEntity(template.createJsonRequest(context), "UTF-8");
return msg.doOnNext(map -> { httpPost.setEntity(stringEntity);
String code = String.valueOf(map.get("errcode"));
if ("0".equals(code)) { 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("发送微信企业通知成功"); // log.info("发送微信企业通知成功");
} else { } else {
// log.warn("发送微信企业通知失败:{}", map); // log.warn("发送微信企业通知失败:{}", map);
// throw new BusinessException("发送微信企业通知失败:" + map.get("errmsg"), code); // throw new BusinessException("发送微信企业通知失败:" + map.get("errmsg"), code);
} }
}); } catch (IOException e) {
e.printStackTrace();
}
} }
private String getToken() { private String getToken() {
if (System.currentTimeMillis() - refreshTokenTime > tokenTimeOut || accessToken.get() == null) { if (System.currentTimeMillis() - refreshTokenTime > tokenTimeOut || accessToken == null) {
return requestToken(); return requestToken();
} }
return accessToken.get(); return accessToken;
} }
private String requestToken() { private String requestToken() {
Mono<ClientResponse> responseMono = WebClient.create().get() List<NameValuePair> params = new ArrayList<>();
.uri(UriComponentsBuilder.fromUriString(tokenApi) params.add(new BasicNameValuePair("corpId", properties.getCorpId()));
.queryParam("corpId", properties.getCorpId()) params.add(new BasicNameValuePair("corpSecret", properties.getCorpSecret()));
.queryParam("corpSecret", properties.getCorpSecret())
.build().toUri())
.exchange();
responseMono.flatMap(resp -> { CloseableHttpClient httpClient = HttpClientBuilder.create().build();
System.out.println(resp); try {
return resp.bodyToMono(HashMap.class); HttpGet httpGet = new HttpGet(new URIBuilder(tokenApi).setParameters(params).build());
}) CloseableHttpResponse response = httpClient.execute(httpGet);
.map(map -> { JSONObject responseJson = JSON.parseObject(EntityUtils.toString(response.getEntity()));
if (map.containsKey("access_token")) { if (responseJson.containsKey("access_token")) {
return map.get("access_token"); String access_token = responseJson.get("access_token").toString();
} accessToken = access_token;
// TODO 自定义异常 refreshTokenTime = System.currentTimeMillis();
// throw new BusinessException("获取Token失败:" + map.get("errmsg"), String.valueOf(map.get("errcode"))); return access_token;
return Mono.empty(); } else {
}) // TODO 自定义异常
.cast(String.class) // throw new BusinessException("获取Token失败:" + map.get("errmsg"), String.valueOf(map.get("errcode")));
.doOnNext(r -> { }
refreshTokenTime = System.currentTimeMillis(); } catch (Exception e) {
accessToken.set(r); e.printStackTrace();
}); }
return accessToken.get(); return null;
} }
@Override @Override
public void close() { public void close() {
accessToken.set(null); this.accessToken = null;
refreshTokenTime = 0; this.refreshTokenTime = 0;
} }
} }

View File

@ -18,24 +18,20 @@ import java.util.Map;
public class WechatTest { public class WechatTest {
@Test @Test
public void test() { public void test() {
Hooks.onOperatorDebug();
// 通知器配置管理器 // 通知器配置管理器
NotifyConfigManager notifyConfigManager = new NotifyConfigManager() { NotifyConfigManager notifyConfigManager = (notifyType, configId) -> {
@Override NotifierProperties notifierProperties = new NotifierProperties();
public NotifierProperties getNotifyConfig(NotifyType notifyType, String configId) { notifierProperties.setType(DefaultNotifyType.wechat.getId());
NotifierProperties notifierProperties = new NotifierProperties(); notifierProperties.setProvider(WechatProvider.corpMessage.getId());
notifierProperties.setType(DefaultNotifyType.wechat.getId()); notifierProperties.setId("12");
notifierProperties.setProvider(WechatProvider.corpMessage.getId());
notifierProperties.setId("12");
Map<String, Object> config = new HashMap<>(); Map<String, Object> config = new HashMap<>();
config.put("corpId", "wwbd49ae2419a55a9f"); config.put("corpId", "wwbd49ae2419a55a9f");
config.put("corpSecret", "TXDRQw_H8gpVKX0E01EmwMXJ4VooXmj65I-mDe0wQ1k"); config.put("corpSecret", "TXDRQw_H8gpVKX0E01EmwMXJ4VooXmj65I-mDe0wQ1k");
notifierProperties.setConfiguration(config); notifierProperties.setConfiguration(config);
return notifierProperties; return notifierProperties;
}
}; };
// 模板管理器 // 模板管理器

17
pom.xml
View File

@ -20,4 +20,21 @@
<maven.compiler.target>8</maven.compiler.target> <maven.compiler.target>8</maven.compiler.target>
</properties> </properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project> </project>