This commit is contained in:
007
2017-12-04 16:26:19 +08:00
22 changed files with 317 additions and 408 deletions

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-parent</artifactId>
<version>2.8.8.BETA</version>
<version>2.9.0</version>
</parent>
<artifactId>weixin-java-mp</artifactId>
<name>WeiXin Java Tools - MP</name>

View File

@@ -5,7 +5,10 @@ import redis.clients.jedis.JedisPool;
/**
* 基于Redis的微信配置provider
*
* <pre>
* 使用说明:本实现仅供参考,并不完整,
* 比如为减少项目依赖未加入redis分布式锁的实现如有需要请自行实现。
* </pre>
* @author nickwong
*/
@SuppressWarnings("hiding")

View File

@@ -5,8 +5,11 @@ import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.mp.bean.*;
import me.chanjar.weixin.mp.bean.result.*;
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery;
import me.chanjar.weixin.mp.bean.result.WxMpCurrentAutoReplyInfo;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
/**
* 微信API的Service
@@ -62,6 +65,11 @@ public interface WxMpService {
*/
String GET_CURRENT_AUTOREPLY_INFO_URL = "https://api.weixin.qq.com/cgi-bin/get_current_autoreply_info";
/**
* 公众号调用或第三方平台帮公众号调用对公众号的所有api调用包括第三方帮其调用次数进行清零
*/
String CLEAR_QUOTA_URL = "https://api.weixin.qq.com/cgi-bin/clear_quota";
/**
* <pre>
* 验证消息的确来自微信服务器
@@ -219,6 +227,18 @@ public interface WxMpService {
*/
WxMpCurrentAutoReplyInfo getCurrentAutoReplyInfo() throws WxErrorException;
/**
* <pre>
* 公众号调用或第三方平台帮公众号调用对公众号的所有api调用包括第三方帮其调用次数进行清零
* HTTP调用https://api.weixin.qq.com/cgi-bin/clear_quota?access_token=ACCESS_TOKEN
* 接口文档地址https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433744592
*
* </pre>
*
* @param appid 公众号的APPID
*/
void clearQuota(String appid) throws WxErrorException;
/**
* 当本Service没有实现某个API的时候可以用这个针对所有微信API中的GET请求
*/
@@ -376,6 +396,7 @@ public interface WxMpService {
/**
* 返回群发消息相关接口方法的实现类对象,以方便调用其各个接口
*
* @return WxMpMassMessageService
*/
WxMpMassMessageService getMassMessageService();

View File

@@ -207,6 +207,13 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
return WxMpCurrentAutoReplyInfo.fromJson(this.get(GET_CURRENT_AUTOREPLY_INFO_URL, null));
}
@Override
public void clearQuota(String appid) throws WxErrorException {
JsonObject o = new JsonObject();
o.addProperty("appid", appid);
this.post(CLEAR_QUOTA_URL, o.toString());
}
@Override
public String get(String url, String queryParam) throws WxErrorException {
return execute(SimpleGetRequestExecutor.create(this), url, queryParam);
@@ -252,7 +259,7 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
throw new RuntimeException("微信服务端异常,超出重试次数");
}
public synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
public <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
if (uri.contains("access_token=")) {
throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
}

View File

@@ -1,5 +1,6 @@
package me.chanjar.weixin.mp.util.http.apache;
import com.google.common.collect.ImmutableMap;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp;
@@ -43,9 +44,7 @@ public class ApacheMaterialNewsInfoRequestExecutor
httpPost.setConfig(config);
}
Map<String, String> params = new HashMap<>();
params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId))));
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
this.logger.debug("响应原始数据:{}", responseContent);

View File

@@ -1,5 +1,6 @@
package me.chanjar.weixin.mp.util.http.jodd;
import com.google.common.collect.ImmutableMap;
import jodd.http.HttpConnectionProvider;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
@@ -9,6 +10,7 @@ import jodd.util.StringPool;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;
import me.chanjar.weixin.mp.util.http.MaterialNewsInfoRequestExecutor;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
@@ -28,13 +30,13 @@ public class JoddMaterialNewsInfoRequestExecutor extends MaterialNewsInfoRequest
@Override
public WxMpMaterialNews execute(String uri, String materialId) throws WxErrorException, IOException {
HttpRequest request = HttpRequest.post(uri);
if (requestHttp.getRequestHttpProxy() != null) {
requestHttp.getRequestHttpClient().useProxy(requestHttp.getRequestHttpProxy());
}
request.withConnectionProvider(requestHttp.getRequestHttpClient());
request.query("media_id", materialId);
HttpRequest request = HttpRequest.post(uri)
.withConnectionProvider(requestHttp.getRequestHttpClient())
.body(WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId)));
HttpResponse response = request.send();
response.charset(StringPool.UTF_8);

View File

@@ -1,9 +1,11 @@
package me.chanjar.weixin.mp.util.http.okhttp;
import com.google.common.collect.ImmutableMap;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;
import me.chanjar.weixin.mp.util.http.MaterialNewsInfoRequestExecutor;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
@@ -28,7 +30,8 @@ public class OkhttpMaterialNewsInfoRequestExecutor extends MaterialNewsInfoReque
//得到httpClient
OkHttpClient client = requestHttp.getRequestHttpClient();
RequestBody requestBody = new FormBody.Builder().add("media_id", materialId).build();
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),
WxGsonBuilder.create().toJson(ImmutableMap.of("media_id", materialId)));
Request request = new Request.Builder().url(uri).post(requestBody).build();
Response response = client.newCall(request).execute();

View File

@@ -15,7 +15,6 @@ import static org.testng.Assert.*;
@Test
@Guice(modules = ApiTestModule.class)
public class WxMpServiceImplTest {
@Inject
private WxMpService wxService;
@@ -28,88 +27,8 @@ public class WxMpServiceImplTest {
}
@Test
public void testCheckSignature() {
Assert.fail("Not yet implemented");
}
@Test
public void testGetAccessToken() {
Assert.fail("Not yet implemented");
}
@Test
public void testGetAccessTokenBoolean() {
Assert.fail("Not yet implemented");
}
@Test
public void testGetJsapiTicket() {
Assert.fail("Not yet implemented");
}
@Test
public void testGetJsapiTicketBoolean() {
Assert.fail("Not yet implemented");
}
@Test
public void testCreateJsapiSignature() {
Assert.fail("Not yet implemented");
}
@Test
public void testCustomMessageSend() {
Assert.fail("Not yet implemented");
}
@Test
public void testMassNewsUpload() {
Assert.fail("Not yet implemented");
}
@Test
public void testMassVideoUpload() {
Assert.fail("Not yet implemented");
}
@Test
public void testMassGroupMessageSend() {
Assert.fail("Not yet implemented");
}
@Test
public void testMassOpenIdsMessageSend() {
Assert.fail("Not yet implemented");
}
@Test
public void testMassMessagePreview() {
Assert.fail("Not yet implemented");
}
@Test
public void testShortUrl() {
Assert.fail("Not yet implemented");
}
@Test
public void testSetIndustry() {
Assert.fail("Not yet implemented");
}
@Test
public void testGetIndustry() {
Assert.fail("Not yet implemented");
}
@Test
public void testSemanticQuery() {
Assert.fail("Not yet implemented");
}
@Test
public void testOauth2buildAuthorizationUrl() {
Assert.fail("Not yet implemented");
public void testClearQuota() throws WxErrorException {
this.wxService.clearQuota(wxService.getWxMpConfigStorage().getAppId());
}
@Test
@@ -121,44 +40,4 @@ public class WxMpServiceImplTest {
System.out.println(qrConnectUrl);
}
@Test
public void testOauth2getAccessToken() {
Assert.fail("Not yet implemented");
}
@Test
public void testOauth2refreshAccessToken() {
Assert.fail("Not yet implemented");
}
@Test
public void testOauth2getUserInfo() {
Assert.fail("Not yet implemented");
}
@Test
public void testOauth2validateAccessToken() {
Assert.fail("Not yet implemented");
}
@Test
public void testGetCallbackIP() {
Assert.fail("Not yet implemented");
}
@Test
public void testGet() {
Assert.fail("Not yet implemented");
}
@Test
public void testPost() {
Assert.fail("Not yet implemented");
}
@Test
public void testExecute() {
Assert.fail("Not yet implemented");
}
}