10BC0 增加查询小程序可回退的版本 接口 by hywr · Pull Request #2163 · binarywang/WxJava · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f14b88c
添加对话能力(原导购)相关结果 均通过单元测试
May 14, 2021
1c049e4
Merge remote-tracking branch 'origin/develop' into develop
hywr May 14, 2021
32695c7
Merge remote-tracking branch 'origin/develop' into develop
hywr May 17, 2021
aec9387
修改公众号、小程序、企业微信的access token自动刷新逻辑,用于避免当使用secrete误调用第三方平台专属接口的情况下出现循环递…
hywr May 17, 2021
edc935a
1.修复公众号在由第三方平台管理时OAuth2Service授权相关报错问题
hywr May 21, 2021
32028de
Merge remote-tracking branch 'origin/develop' into develop
hywr May 21, 2021
41791eb
1.增加查询小程序可回退的版本 接口
hywr Jun 23, 2021
1611b41
:memo: 更新版本信息
binarywang Jun 2, 2021
5d276b7
:memo: 更新部分信息
binarywang Jun 2, 2021
9279156
:art: 修复单元测试代码
binarywang Jun 3, 2021
933e058
:arrow_up: upgrade guava version
binarywang Jun 3, 2021
1905871
:new: #2142 【企业微信】被动回复消息内容新增任务卡片格式
arthur0201 Jun 4, 2021
d47d687
:arrow_up: Bump httpclient from 4.5 to 4.5.13 (#2143)
dependabot[bot] Jun 4, 2021
953b5f9
:art: 优化部分代码
binarywang Jun 4, 2021
a736621
:art: #2144【企业微信】更新任务卡片消息状态接口参数跟文档保持一致
arthur0201 Jun 7, 2021
d1e8fe3
:new: #2135 【小程序】实现获取 URL Link接口 以及微信电子发票报销方相关接口
mr-xiaoyu Jun 11, 2021
d6d3625
:art: #2155 【企业微信】发送新客户欢迎语接口增加对视频类型的支持,同时修复结构不正确的问题
chutian0124 Jun 21, 2021
4f5be60
:new: #2150 【企业微信】补充完善部分客户联系接口,以及服务商模式外部联系人openid转换接口
chutian0124 Jun 22, 2021
b6092db
:new: #2161 【公众号】微信推送消息类增加群发接口事件相关字段
arthur0201 Jun 23, 2021
e56eb91
1.增加查询小程序可回退的版本 接口
hywr Jun 23, 2021
a6cd1e3
Merge branch 'develop' into develop2
hywr Jun 23, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
修改公众号、小程序、企业微信的access token自动刷新逻辑,用于避免当使用secrete误调用第三方平台专属接口的情况下出现循环递…
…归调用甚至被微信限制qps。
  • Loading branch information
hywr committed May 17, 2021
commit aec9387e2fcf11dfce17a6dab21e9c7f9c3b2b29
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
int retryTimes = 0;
do {
try {
return this.executeInternal(executor, uri, data);
return this.executeInternal(executor, uri, data, false);
} catch (WxErrorException e) {
if (retryTimes + 1 > this.maxRetryTimes) {
log.warn("重试达到最大次数【{}】", this.maxRetryTimes);
Expand Down Expand Up @@ -271,7 +271,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
throw new WxRuntimeException("微信服务端异常,超出重试次数");
}

protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data, boolean doNotAutoRefresh) throws WxErrorException {
E dataForLog = DataUtils.handleDataWithSecret(data);

if (uri.contains("access_token=")) {
Expand All @@ -291,9 +291,11 @@ protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E
if (WxConsts.ACCESS_TOKEN_ERROR_CODES.contains(error.getErrorCode())) {
// 强制设置wxCpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
this.configStorage.expireAccessToken();
if (this.getWxCpConfigStorage().autoRefreshToken()) {
if (this.getWxCpConfigStorage().autoRefreshToken() && !doNotAutoRefresh) {
log.warn("即将重新获取新的access_token,错误代码:{},错误信息:{}", error.getErrorCode(), error.getErrorMsg());
return this.execute(executor, uri, data);
//下一次不再自动重试
//当小程序误调用第三方平台专属接口时,第三方无法使用小程序的access token,如果可以继续自动获取token会导致无限循环重试,直到栈溢出
return this.executeInternal(executor, uri, data, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Object[][] getService() {

@Override
public synchronized <T, E> T executeInternal(
RequestExecutor<T, E> executor, String uri, E data)
RequestExecutor<T, E> executor, String uri, E data, boolean doNotAutoRefresh)
throws WxErrorException {
log.info("Executed");
throw new WxErrorException("something");
Expand Down
10BC0
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package me.chanjar.weixin.cp.api.impl;

import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.error.WxMpErrorMsgEnum;
import me.chanjar.weixin.common.util.http.HttpType;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.cp.api.ApiTestModule;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

/**
* <pre>
Expand Down Expand Up @@ -35,6 +48,61 @@ public void testJsCode2Session() throws WxErrorException {

@Test
public void testGetProviderToken() throws WxErrorException {
assertThat(this.wxService.getProviderToken("111","123")).isNotNull();
assertThat(this.wxService.getProviderToken("111", "123")).isNotNull();
}


@Test
public void testExecuteAutoRefreshToken() throws WxErrorException, IOException {
//测试access token获取时的重试机制
WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
BaseWxCpServiceImpl service = new BaseWxCpServiceImpl() {
@Override
public Object getRequestHttpClient() {
return null;
}

@Override
public Object getRequestHttpProxy() {
return null;
}

@Override
public HttpType getRequestType() {
return null;
}

@Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
return "模拟一个过期的access token:" + System.currentTimeMillis();
}

@Override
public void initHttp() {

}

@Override
public WxCpConfigStorage getWxCpConfigStorage() {
return config;
}
};
config.setAgentId(1);
service.setWxCpConfigStorage(config);
RequestExecutor<Object, Object> re = mock(RequestExecutor.class);

AtomicInteger counter = new AtomicInteger();
Mockito.when(re.execute(Mockito.anyString(), Mockito.any(), Mockito.any())).thenAnswer(invocation -> {
counter.incrementAndGet();
WxError error = WxError.builder().errorCode(WxMpErrorMsgEnum.CODE_40001.getCode()).errorMsg(WxMpErrorMsgEnum.CODE_40001.getMsg()).build();
throw new WxErrorException(error);
});
try {
Object execute = service.execute(re, "http://baidu.com", new HashMap<>());
Assert.assertTrue(false, "代码应该不会执行到这里");
} catch (WxErrorException e) {
Assert.assertEquals(WxMpErrorMsgEnum.CODE_40001.getCode(), e.getError().getErrorCode());
Assert.assertEquals(2, counter.get());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
int retryTimes = 0;
do {
try {
return this.executeInternal(executor, uri, data);
return this.executeInternal(executor, uri, data, false);
} catch (WxErrorException e) {
if (retryTimes + 1 > this.maxRetryTimes) {
log.warn("重试达到最大次数【{}】", maxRetryTimes);
Expand Down Expand Up @@ -238,7 +238,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
throw new WxRuntimeException("微信服务端异常,超出重试次数");
}

private <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
private <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data, boolean doNotAutoRefreshToken) throws WxErrorException {
E dataForLog = DataUtils.handleDataWithSecret(data);

if (uri.contains("access_token=")) {
Expand Down Expand Up @@ -271,9 +271,11 @@ private <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E d
} finally {
lock.unlock();
}
if (this.getWxMaConfig().autoRefreshToken()) {
if (this.getWxMaConfig().autoRefreshToken() && !doNotAutoRefreshToken) {
log.warn("即将重新获取新的access_token,错误代码:{},错误信息:{}", error.getErrorCode(), error.getErrorMsg());
return this.execute(executor, uri, data);
//下一次不再自动重试
//当小程序误调用第三方平台专属接口时,第三方无法使用小程序的access token,如果可以继续自动获取token会导致无限循环重试,直到栈溢出
return this.executeInternal(executor, uri, data, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.binarywang.wx.miniapp.test.ApiTestModule;
import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.error.WxMpErrorMsgEnum;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import org.apache.commons.lang3.StringUtils;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertTrue;

Expand Down Expand Up @@ -101,6 +112,35 @@ public void testGet() {
public void testExecute() {
}

@Test
public void testExecuteAutoRefreshToken() throws WxErrorException, IOException {
//测试access token获取时的重试机制
WxMaServiceImpl service = new WxMaServiceImpl() {
@Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
return "模拟一个过期的access token:" + System.currentTimeMillis();
}
};
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
config.setAppid("1");
service.setWxMaConfig(config);
RequestExecutor<Object, Object> re = mock(RequestExecutor.class);

AtomicInteger counter = new AtomicInteger();
Mockito.when(re.execute(Mockito.anyString(), Mockito.any(), Mockito.any())).thenAnswer(invocation -> {
counter.incrementAndGet();
WxError error = WxError.builder().errorCode(WxMpErrorMsgEnum.CODE_40001.getCode()).errorMsg(WxMpErrorMsgEnum.CODE_40001.getMsg()).build();
throw new WxErrorException(error);
});
try {
Object execute = service.execute(re, "http://baidu.com", new HashMap<>());
Assert.assertTrue(false, "代码应该不会执行到这里");
} catch (WxErrorException e) {
Assert.assertEquals(WxMpErrorMsgEnum.CODE_40001.getCode(), e.getError().getErrorCode());
Assert.assertEquals(2, counter.get());
}
}

@Test
public void testGetWxMaConfig() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
int retryTimes = 0;
do {
try {
return this.executeInternal(executor, uri, data);
return this.executeInternal(executor, uri, data, false);
} catch (WxErrorException e) {
WxError error = e.getError();
// -1 系统繁忙, 1000ms后重试
Expand Down Expand Up @@ -370,7 +370,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
throw new WxRuntimeException("微信服务端异常,超出重试次数");
}

protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data, boolean doNotAutoRefresh) throws WxErrorException {
E dataForLog = DataUtils.handleDataWithSecret(data);

if (uri.contains("access_token=")) {
Expand Down Expand Up @@ -399,9 +399,11 @@ protected <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E
} finally {
lock.unlock();
}
if (this.getWxMpConfigStorage().autoRefreshToken()) {
if (this.getWxMpConfigStorage().autoRefreshToken() && !doNotAutoRefresh) {
log.warn("即将重新获取新的access_token,错误代码:{},错误信息:{}", error.getErrorCode(), error.getErrorMsg());
return this.execute(executor, uri, data);
//下一次不再自动重试
//当小程序误调用第三方平台专属接口时,第三方无法使用小程序的access token,如果可以继续自动获取token会导致无限循环重试,直到栈溢出
return this.executeInternal(executor, uri, data, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package me.chanjar.weixin.mp.api;

import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.error.WxRuntimeException;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl;
import org.testng.annotations.*;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
Expand All @@ -23,7 +23,7 @@ public Object[][] getService() {

@Override
public synchronized <T, E> T executeInternal(
RequestExecutor<T, E> executor, String uri, E data)
RequestExecutor<T, E> executor, String uri, E data, boolean doNotAutoRefresh)
throws WxErrorException {
log.info("Executed");
throw new WxErrorException("something");
Expand All @@ -37,7 +37,7 @@ public synchronized <T, E> T executeInternal(

@Test(dataProvider = "getService", expectedExceptions = RuntimeException.class)
public void testRetry(WxMpService service) throws WxErrorException {
service.execute(null, (String)null, null);
service.execute(null, (String) null, null);
}

@Test(dataProvider = "getService")
Expand All @@ -48,7 +48,7 @@ public void testRetryInThreadPool(final WxMpService service) throws InterruptedE
try {
System.out.println("=====================");
System.out.println(Thread.currentThread().getName() + ": testRetry");
service.execute(null, (String)null, null);
service.execute(null, (String) null, null);
} catch (WxErrorException e) {
throw new WxRuntimeException(e);
} catch (RuntimeException e) {
Expand Down
Loading
0