#1157 增加网络检测接口

This commit is contained in:
billytomato
2019-08-15 13:26:43 +08:00
committed by Binary Wang
parent 6e1d7fcef0
commit d1586aec25
8 changed files with 197 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package me.chanjar.weixin.mp.api;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.WxNetCheckResult;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestExecutor;
@@ -178,6 +179,20 @@ public interface WxMpService {
*/
String[] getCallbackIP() throws WxErrorException;
/**
* <pre>
* 网络检测
* https://mp.weixin.qq.com/wiki?t=resource/res_main&id=21541575776DtsuT
* 为了帮助开发者排查回调连接失败的问题提供这个网络检测的API。它可以对开发者URL做域名解析然后对所有IP进行一次ping操作得到丢包率和耗时。
* </pre>
*
* @param action 执行的检测动作
* @param operator 指定平台从某个运营商进行检测
* @throws WxErrorException
*/
WxNetCheckResult netCheck(String action, String operator) throws WxErrorException;
/**
* <pre>
* 获取公众号的自动回复规则.

View File

@@ -8,6 +8,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.WxNetCheckResult;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.StandardSessionManager;
@@ -236,6 +237,15 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
return ipArray;
}
@Override
public WxNetCheckResult netCheck(String action, String operator) throws WxErrorException {
JsonObject o = new JsonObject();
o.addProperty("action", action);
o.addProperty("check_operator", operator);
String responseContent = this.post(NETCHECK_URL, o.toString());
return WxNetCheckResult.fromJson(responseContent);
}
@Override
public WxMpCurrentAutoReplyInfo getCurrentAutoReplyInfo() throws WxErrorException {
return WxMpCurrentAutoReplyInfo.fromJson(this.get(GET_CURRENT_AUTOREPLY_INFO_URL, null));

View File

@@ -110,6 +110,10 @@ public interface WxMpApiUrl {
* 获取微信服务器IP地址.
*/
GET_CALLBACK_IP_URL(API_DEFAULT_HOST_URL, "/cgi-bin/getcallbackip"),
/**
* 网络检测.
*/
NETCHECK_URL(API_DEFAULT_HOST_URL, "/cgi-bin/callback/check"),
/**
* 第三方使用网站应用授权登录的url.
*/

View File

@@ -1,10 +1,13 @@
package me.chanjar.weixin.mp.api.impl;
import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxNetCheckResult;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.test.ApiTestModule;
import me.chanjar.weixin.mp.util.WxMpConfigStorageHolder;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
@@ -37,4 +40,57 @@ public class BaseWxMpServiceImplTest {
assertThat(this.wxService.switchoverTo("another").getAccessToken()).isNotEmpty();
assertThat(WxMpConfigStorageHolder.get()).isEqualTo("another");
}
@Test
public void testNetCheck() throws WxErrorException {
WxNetCheckResult result = this.wxService.netCheck(WxConsts.NetCheckArgs.ACTIONALL, WxConsts.NetCheckArgs.OPERATORDEFAULT);
Assert.assertNotNull(result);
}
@Test
public void testNectCheckResult() {
String json = "{\n" +
" \"dns\": [\n" +
" {\n" +
" \"ip\": \"111.161.64.40\", \n" +
" \"real_operator\": \"UNICOM\"\n" +
" }, \n" +
" {\n" +
" \"ip\": \"111.161.64.48\", \n" +
" \"real_operator\": \"UNICOM\"\n" +
" }\n" +
" ], \n" +
" \"ping\": [\n" +
" {\n" +
" \"ip\": \"111.161.64.40\", \n" +
" \"from_operator\": \"UNICOM\"," +
" \"package_loss\": \"0%\", \n" +
" \"time\": \"23.079ms\"\n" +
" }, \n" +
" {\n" +
" \"ip\": \"111.161.64.48\", \n" +
" \"from_operator\": \"UNICOM\", \n" +
" \"package_loss\": \"0%\", \n" +
" \"time\": \"21.434ms\"\n" +
" }\n" +
" ]\n" +
"}";
WxNetCheckResult result = WxNetCheckResult.fromJson(json);
Assert.assertNotNull(result);
Assert.assertNotNull(result.getDnsInfos());
WxNetCheckResult.WxNetCheckDnsInfo dnsInfo = new WxNetCheckResult.WxNetCheckDnsInfo();
dnsInfo.setIp("111.161.64.40");
dnsInfo.setRealOperator("UNICOM");
Assert.assertEquals(result.getDnsInfos().get(0), dnsInfo);
WxNetCheckResult.WxNetCheckPingInfo pingInfo = new WxNetCheckResult.WxNetCheckPingInfo();
pingInfo.setTime("21.434ms");
pingInfo.setFromOperator("UNICOM");
pingInfo.setIp("111.161.64.48");
pingInfo.setPackageLoss("0%");
Assert.assertEquals(result.getPingInfos().get(1), pingInfo);
}
}