Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【小程序/公众号】提供了更新access_token的消费接口 #3005

Merged
merged 9 commits into from
May 11, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package me.chanjar.weixin.common.bean;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
* @author cn
binarywang marked this conversation as resolved.
Show resolved Hide resolved
* @version 1.0
* @description
* @date 2023/3/27 18:34
*/
@Getter
@Setter
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class WxAccessTokenEntity extends WxAccessToken {
private String appid;
}
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ protected String extractAccessToken(String resultContent) throws WxErrorExceptio
throw new WxErrorException(error);
}
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
config.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
config.updateAccessTokenProcessor(accessToken.getAccessToken(), accessToken.getExpiresIn());
return accessToken.getAccessToken();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cn.binarywang.wx.miniapp.config;

import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.bean.WxAccessTokenEntity;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;

import java.util.concurrent.locks.Lock;
import java.util.function.Consumer;

/**
* 小程序配置
Expand All @@ -12,6 +14,10 @@
*/
public interface WxMaConfig {

default void setUpdateAccessTokenBefore(Consumer<WxAccessTokenEntity> updateAccessTokenBefore) {

}

/**
* Gets access token.
*
Expand Down Expand Up @@ -43,7 +49,9 @@ public interface WxMaConfig {
*
* @param accessToken 要更新的WxAccessToken对象
*/
void updateAccessToken(WxAccessToken accessToken);
default void updateAccessToken(WxAccessToken accessToken) {
updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
}

/**
* 应该是线程安全的
Expand All @@ -53,6 +61,20 @@ public interface WxMaConfig {
*/
void updateAccessToken(String accessToken, int expiresInSeconds);

default void updateAccessTokenProcessor(String accessToken, int expiresInSeconds) {
WxAccessTokenEntity wxAccessTokenEntity = new WxAccessTokenEntity();
wxAccessTokenEntity.setAppid(getAppid());
wxAccessTokenEntity.setAccessToken(accessToken);
wxAccessTokenEntity.setExpiresIn(expiresInSeconds);
updateAccessTokenBefore(wxAccessTokenEntity);
updateAccessToken(accessToken, expiresInSeconds);
}

default void updateAccessTokenBefore(WxAccessTokenEntity wxAccessTokenEntity) {

}


/**
* Gets jsapi ticket.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
import lombok.AccessLevel;
import lombok.Getter;
import me.chanjar.weixin.common.bean.WxAccessToken;
import lombok.Setter;
import me.chanjar.weixin.common.bean.WxAccessTokenEntity;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;

import java.io.File;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;

/**
* 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化
Expand Down Expand Up @@ -58,6 +61,23 @@ public class WxMaDefaultConfigImpl implements WxMaConfig {
private String apiHostUrl;
private String accessTokenUrl;

/**
* 自定义配置token的消费者
*/
@Setter
private Consumer<WxAccessTokenEntity> updateAccessTokenBefore;
@Getter(AccessLevel.NONE)
private boolean enableUpdateAccessTokenBefore = true;

/**
* 可临时关闭更新token回调,主要用于其他介质初始化数据时,可不进行回调
*
* @param enableUpdateAccessTokenBefore
binarywang marked this conversation as resolved.
Show resolved Hide resolved
*/
public void enableUpdateAccessTokenBefore(boolean enableUpdateAccessTokenBefore) {
this.enableUpdateAccessTokenBefore = enableUpdateAccessTokenBefore;
}

/**
* 会过期的数据提前过期时间,默认预留200秒的时间
*/
Expand Down Expand Up @@ -95,17 +115,24 @@ public boolean isAccessTokenExpired() {
return isExpired(this.expiresTime);
}

@Override
public synchronized void updateAccessToken(WxAccessToken accessToken) {
updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
}
// @Override
// public synchronized void updateAccessToken(WxAccessToken accessToken) {
// updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
// }

@Override
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
setAccessToken(accessToken);
setExpiresTime(expiresAheadInMillis(expiresInSeconds));
}

@Override
public void updateAccessTokenBefore(WxAccessTokenEntity wxAccessTokenEntity) {
if (updateAccessTokenBefore != null && enableUpdateAccessTokenBefore) {
updateAccessTokenBefore.accept(wxAccessTokenEntity);
}
}

@Override
public String getJsapiTicket() {
return this.jsapiTicket;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public boolean isAccessTokenExpired() {

@Override
public void updateAccessToken(WxAccessToken accessToken) {
redisOps.setValue(this.accessTokenKey, accessToken.getAccessToken(), accessToken.getExpiresIn(), TimeUnit.SECONDS);
updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ public void testRefreshAccessToken() throws WxErrorException {
assertTrue(StringUtils.isNotBlank(after));
}

public void testTokenCallBack() throws WxErrorException {
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
WxMaConfig configStorage = this.wxService.getWxMaConfig();
config.setAppid(configStorage.getAppid());
config.setSecret(configStorage.getSecret());
config.setUpdateAccessTokenBefore(e -> {
System.out.println("token:" + e.toString());
});
this.wxService.setWxMaConfig(config);

String before = config.getAccessToken();
this.wxService.getAccessToken(true);
String after = config.getAccessToken();
assertNotEquals(before, after);
assertTrue(StringUtils.isNotBlank(after));
config.enableUpdateAccessTokenBefore(false);
this.wxService.getAccessToken(true);
after = config.getAccessToken();
System.out.println(after);
}
@Test(expectedExceptions = {WxErrorException.class})
public void testGetPaidUnionId() throws WxErrorException {
final String unionId = this.wxService.getPaidUnionId("1", null, "3", "4");
Expand Down Expand Up @@ -134,7 +154,7 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
});
try {
Object execute = service.execute(re, "http://baidu.com", new HashMap<>());
Assert.assertTrue(false, "代码应该不会执行到这里");
Assert.fail("代码应该不会执行到这里");
} catch (WxErrorException e) {
Assert.assertEquals(WxMpErrorMsgEnum.CODE_40001.getCode(), e.getError().getErrorCode());
Assert.assertEquals(2, counter.get());
Expand Down