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>
<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>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</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>

View File

@ -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<WechatMessageTemplate> {
private final AtomicReference<String> accessToken = new AtomicReference<>();
private String accessToken;
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 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<WechatMessageTemplate>
@Override
public void send(WechatMessageTemplate template, Map<String, Object> 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<HashMap> checkResult(Mono<HashMap> 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<ClientResponse> responseMono = WebClient.create().get()
.uri(UriComponentsBuilder.fromUriString(tokenApi)
.queryParam("corpId", properties.getCorpId())
.queryParam("corpSecret", properties.getCorpSecret())
.build().toUri())
.exchange();
List<NameValuePair> 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;
}
}

View File

@ -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<String, Object> config = new HashMap<>();
config.put("corpId", "wwbd49ae2419a55a9f");
config.put("corpSecret", "TXDRQw_H8gpVKX0E01EmwMXJ4VooXmj65I-mDe0wQ1k");
Map<String, Object> config = new HashMap<>();
config.put("corpId", "wwbd49ae2419a55a9f");
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>
</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>