mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-08-23 22:11:40 +08:00
🎨 #3005【小程序/公众号】提供更新access_token的消费接口
This commit is contained in:
parent
077f828019
commit
899ea653be
@ -0,0 +1,19 @@
|
|||||||
|
package me.chanjar.weixin.common.bean;
|
||||||
|
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* token
|
||||||
|
*
|
||||||
|
* @author cn
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@ToString(callSuper = true)
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
public class WxAccessTokenEntity extends WxAccessToken {
|
||||||
|
private String appid;
|
||||||
|
}
|
@ -339,7 +339,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
|
|||||||
throw new WxErrorException(error);
|
throw new WxErrorException(error);
|
||||||
}
|
}
|
||||||
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
||||||
config.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
config.updateAccessTokenProcessor(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||||
return accessToken.getAccessToken();
|
return accessToken.getAccessToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package cn.binarywang.wx.miniapp.config;
|
package cn.binarywang.wx.miniapp.config;
|
||||||
|
|
||||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||||
|
import me.chanjar.weixin.common.bean.WxAccessTokenEntity;
|
||||||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
||||||
|
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 小程序配置
|
* 小程序配置
|
||||||
@ -12,6 +14,10 @@ import java.util.concurrent.locks.Lock;
|
|||||||
*/
|
*/
|
||||||
public interface WxMaConfig {
|
public interface WxMaConfig {
|
||||||
|
|
||||||
|
default void setUpdateAccessTokenBefore(Consumer<WxAccessTokenEntity> updateAccessTokenBefore) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets access token.
|
* Gets access token.
|
||||||
*
|
*
|
||||||
@ -50,7 +56,9 @@ public interface WxMaConfig {
|
|||||||
*
|
*
|
||||||
* @param accessToken 要更新的WxAccessToken对象
|
* @param accessToken 要更新的WxAccessToken对象
|
||||||
*/
|
*/
|
||||||
void updateAccessToken(WxAccessToken accessToken);
|
default void updateAccessToken(WxAccessToken accessToken) {
|
||||||
|
updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 应该是线程安全的
|
* 应该是线程安全的
|
||||||
@ -60,6 +68,20 @@ public interface WxMaConfig {
|
|||||||
*/
|
*/
|
||||||
void updateAccessToken(String accessToken, int expiresInSeconds);
|
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.
|
* Gets jsapi ticket.
|
||||||
*
|
*
|
||||||
|
@ -4,12 +4,14 @@ import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
|||||||
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
|
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
|
||||||
import lombok.AccessLevel;
|
import lombok.AccessLevel;
|
||||||
import lombok.Getter;
|
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 me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化
|
* 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化
|
||||||
@ -66,6 +68,25 @@ public class WxMaDefaultConfigImpl implements WxMaConfig {
|
|||||||
private String apiHostUrl;
|
private String apiHostUrl;
|
||||||
private String accessTokenUrl;
|
private String accessTokenUrl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义配置token的消费者
|
||||||
|
*/
|
||||||
|
@Setter
|
||||||
|
private Consumer<WxAccessTokenEntity> updateAccessTokenBefore;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开启回调
|
||||||
|
*/
|
||||||
|
@Getter(AccessLevel.NONE)
|
||||||
|
private boolean enableUpdateAccessTokenBefore = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 可临时关闭更新token回调,主要用于其他介质初始化数据时,可不进行回调
|
||||||
|
*/
|
||||||
|
public void enableUpdateAccessTokenBefore(boolean enableUpdateAccessTokenBefore) {
|
||||||
|
this.enableUpdateAccessTokenBefore = enableUpdateAccessTokenBefore;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 会过期的数据提前过期时间,默认预留200秒的时间
|
* 会过期的数据提前过期时间,默认预留200秒的时间
|
||||||
*/
|
*/
|
||||||
@ -116,10 +137,10 @@ public class WxMaDefaultConfigImpl implements WxMaConfig {
|
|||||||
return isExpired(this.expiresTime);
|
return isExpired(this.expiresTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
// @Override
|
||||||
public synchronized void updateAccessToken(WxAccessToken accessToken) {
|
// public synchronized void updateAccessToken(WxAccessToken accessToken) {
|
||||||
updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
// updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||||
}
|
// }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
|
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
|
||||||
@ -127,6 +148,13 @@ public class WxMaDefaultConfigImpl implements WxMaConfig {
|
|||||||
setExpiresTime(expiresAheadInMillis(expiresInSeconds));
|
setExpiresTime(expiresAheadInMillis(expiresInSeconds));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateAccessTokenBefore(WxAccessTokenEntity wxAccessTokenEntity) {
|
||||||
|
if (updateAccessTokenBefore != null && enableUpdateAccessTokenBefore) {
|
||||||
|
updateAccessTokenBefore.accept(wxAccessTokenEntity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getJsapiTicket() {
|
public String getJsapiTicket() {
|
||||||
return this.jsapiTicket;
|
return this.jsapiTicket;
|
||||||
|
@ -91,7 +91,7 @@ public class WxMaRedissonConfigImpl extends WxMaDefaultConfigImpl {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateAccessToken(WxAccessToken accessToken) {
|
public void updateAccessToken(WxAccessToken accessToken) {
|
||||||
redisOps.setValue(this.accessTokenKey, accessToken.getAccessToken(), accessToken.getExpiresIn(), TimeUnit.SECONDS);
|
updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -5,6 +5,7 @@ import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
|||||||
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
|
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
|
||||||
import cn.binarywang.wx.miniapp.test.ApiTestModule;
|
import cn.binarywang.wx.miniapp.test.ApiTestModule;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
|
import me.chanjar.weixin.common.bean.WxAccessTokenEntity;
|
||||||
import me.chanjar.weixin.common.error.WxError;
|
import me.chanjar.weixin.common.error.WxError;
|
||||||
import me.chanjar.weixin.common.error.WxErrorException;
|
import me.chanjar.weixin.common.error.WxErrorException;
|
||||||
import me.chanjar.weixin.common.error.WxMpErrorMsgEnum;
|
import me.chanjar.weixin.common.error.WxMpErrorMsgEnum;
|
||||||
@ -46,6 +47,35 @@ public class WxMaServiceImplTest {
|
|||||||
assertTrue(StringUtils.isNotBlank(after));
|
assertTrue(StringUtils.isNotBlank(after));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void updateAccessTokenBefore(WxAccessTokenEntity wxAccessTokenEntity) {
|
||||||
|
System.out.println("token:" + wxAccessTokenEntity.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
|
// });
|
||||||
|
//第二种方式
|
||||||
|
config.setUpdateAccessTokenBefore(this::updateAccessTokenBefore);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
public void testStableRefreshAccessToken() throws WxErrorException {
|
public void testStableRefreshAccessToken() throws WxErrorException {
|
||||||
WxMaConfig configStorage = this.wxMaServiceOkHttp.getWxMaConfig();
|
WxMaConfig configStorage = this.wxMaServiceOkHttp.getWxMaConfig();
|
||||||
configStorage.useStableAccessToken(true);
|
configStorage.useStableAccessToken(true);
|
||||||
@ -56,6 +86,7 @@ public class WxMaServiceImplTest {
|
|||||||
assertTrue(StringUtils.isNotBlank(after));
|
assertTrue(StringUtils.isNotBlank(after));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test(expectedExceptions = {WxErrorException.class})
|
@Test(expectedExceptions = {WxErrorException.class})
|
||||||
public void testGetPaidUnionId() throws WxErrorException {
|
public void testGetPaidUnionId() throws WxErrorException {
|
||||||
final String unionId = this.wxService.getPaidUnionId("1", null, "3", "4");
|
final String unionId = this.wxService.getPaidUnionId("1", null, "3", "4");
|
||||||
|
Loading…
Reference in New Issue
Block a user