mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
🆕 #3404 【小程序】增加同城配送相关接口,同时为WxMaService增加了API签名支持
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
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;
|
||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||
import me.chanjar.weixin.common.bean.WxAccessTokenEntity;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
||||
|
||||
/**
|
||||
* 小程序配置
|
||||
@@ -14,9 +13,7 @@ import java.util.function.Consumer;
|
||||
*/
|
||||
public interface WxMaConfig {
|
||||
|
||||
default void setUpdateAccessTokenBefore(Consumer<WxAccessTokenEntity> updateAccessTokenBefore) {
|
||||
|
||||
}
|
||||
default void setUpdateAccessTokenBefore(Consumer<WxAccessTokenEntity> updateAccessTokenBefore) {}
|
||||
|
||||
/**
|
||||
* Gets access token.
|
||||
@@ -25,12 +22,12 @@ public interface WxMaConfig {
|
||||
*/
|
||||
String getAccessToken();
|
||||
|
||||
//region 稳定版access token
|
||||
// region 稳定版access token
|
||||
boolean isStableAccessToken();
|
||||
|
||||
void useStableAccessToken(boolean useStableAccessToken);
|
||||
//endregion
|
||||
|
||||
// endregion
|
||||
|
||||
/**
|
||||
* Gets access token lock.
|
||||
@@ -46,9 +43,7 @@ public interface WxMaConfig {
|
||||
*/
|
||||
boolean isAccessTokenExpired();
|
||||
|
||||
/**
|
||||
* 强制将access token过期掉
|
||||
*/
|
||||
/** 强制将access token过期掉 */
|
||||
void expireAccessToken();
|
||||
|
||||
/**
|
||||
@@ -63,7 +58,7 @@ public interface WxMaConfig {
|
||||
/**
|
||||
* 应该是线程安全的
|
||||
*
|
||||
* @param accessToken 新的accessToken值
|
||||
* @param accessToken 新的accessToken值
|
||||
* @param expiresInSeconds 过期时间,以秒为单位
|
||||
*/
|
||||
void updateAccessToken(String accessToken, int expiresInSeconds);
|
||||
@@ -77,10 +72,7 @@ public interface WxMaConfig {
|
||||
updateAccessToken(accessToken, expiresInSeconds);
|
||||
}
|
||||
|
||||
default void updateAccessTokenBefore(WxAccessTokenEntity wxAccessTokenEntity) {
|
||||
|
||||
}
|
||||
|
||||
default void updateAccessTokenBefore(WxAccessTokenEntity wxAccessTokenEntity) {}
|
||||
|
||||
/**
|
||||
* Gets jsapi ticket.
|
||||
@@ -103,15 +95,13 @@ public interface WxMaConfig {
|
||||
*/
|
||||
boolean isJsapiTicketExpired();
|
||||
|
||||
/**
|
||||
* 强制将jsapi ticket过期掉
|
||||
*/
|
||||
/** 强制将jsapi ticket过期掉 */
|
||||
void expireJsapiTicket();
|
||||
|
||||
/**
|
||||
* 应该是线程安全的
|
||||
*
|
||||
* @param jsapiTicket 新的jsapi ticket值
|
||||
* @param jsapiTicket 新的jsapi ticket值
|
||||
* @param expiresInSeconds 过期时间,以秒为单位
|
||||
*/
|
||||
void updateJsapiTicket(String jsapiTicket, int expiresInSeconds);
|
||||
@@ -137,15 +127,13 @@ public interface WxMaConfig {
|
||||
*/
|
||||
boolean isCardApiTicketExpired();
|
||||
|
||||
/**
|
||||
* 强制将卡券api ticket过期掉.
|
||||
*/
|
||||
/** 强制将卡券api ticket过期掉. */
|
||||
void expireCardApiTicket();
|
||||
|
||||
/**
|
||||
* 应该是线程安全的.
|
||||
*
|
||||
* @param apiTicket 新的卡券api ticket值
|
||||
* @param apiTicket 新的卡券api ticket值
|
||||
* @param expiresInSeconds 过期时间,以秒为单位
|
||||
*/
|
||||
void updateCardApiTicket(String apiTicket, int expiresInSeconds);
|
||||
@@ -236,6 +224,7 @@ public interface WxMaConfig {
|
||||
|
||||
/**
|
||||
* http 请求重试间隔
|
||||
*
|
||||
* <pre>
|
||||
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setRetrySleepMillis(int)}
|
||||
* </pre>
|
||||
@@ -244,6 +233,7 @@ public interface WxMaConfig {
|
||||
|
||||
/**
|
||||
* http 请求最大重试次数
|
||||
*
|
||||
* <pre>
|
||||
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setMaxRetryTimes(int)}
|
||||
* </pre>
|
||||
@@ -288,10 +278,35 @@ public interface WxMaConfig {
|
||||
String getAccessTokenUrl();
|
||||
|
||||
/**
|
||||
* 设置自定义的获取accessToken地址
|
||||
* 可用于设置获取accessToken的自定义服务
|
||||
* 设置自定义的获取accessToken地址 可用于设置获取accessToken的自定义服务
|
||||
*
|
||||
* @param accessTokenUrl 自定义的获取accessToken地址
|
||||
*/
|
||||
void setAccessTokenUrl(String accessTokenUrl);
|
||||
|
||||
/**
|
||||
* 服务端API签名用到的RSA私钥【pkcs8格式,会以 -----BEGIN PRIVATE KEY-----开头, 'BEGIN RSA PRIVATE
|
||||
* KEY'的是pkcs1格式,需要转换(可用openssl转换)。 设置参考:
|
||||
* https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/getting_started/api_signature.html
|
||||
*
|
||||
* @return rsa private key string
|
||||
*/
|
||||
String getApiSignatureRsaPrivateKey();
|
||||
|
||||
/**
|
||||
* 服务端API签名用到的AES密钥
|
||||
* https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/getting_started/api_signature.html
|
||||
*
|
||||
* @return aes key string
|
||||
*/
|
||||
String getApiSignatureAesKey();
|
||||
|
||||
/** 密钥对应的序号 */
|
||||
String getApiSignatureAesKeySn();
|
||||
|
||||
/** 密钥对应的序号 */
|
||||
String getApiSignatureRsaPrivateKeySn();
|
||||
|
||||
/** 密钥对应的小程序ID (普通小程序同 appId, 托管第三方平台的是 componentAppId) */
|
||||
String getWechatMpAppid();
|
||||
}
|
||||
|
||||
@@ -2,17 +2,16 @@ package cn.binarywang.wx.miniapp.config.impl;
|
||||
|
||||
import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
||||
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
|
||||
import java.io.File;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.function.Consumer;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
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,在实际生产环境中应该将这些配置持久化
|
||||
*
|
||||
@@ -23,30 +22,32 @@ public class WxMaDefaultConfigImpl implements WxMaConfig {
|
||||
protected volatile String appid;
|
||||
protected volatile String token;
|
||||
|
||||
/**
|
||||
* 是否使用稳定版获取accessToken接口
|
||||
*/
|
||||
/** 是否使用稳定版获取accessToken接口 */
|
||||
@Getter(value = AccessLevel.NONE)
|
||||
private boolean useStableAccessToken;
|
||||
|
||||
/**
|
||||
* 小程序原始ID
|
||||
*/
|
||||
/** 小程序原始ID */
|
||||
protected volatile String originalId;
|
||||
|
||||
protected Lock accessTokenLock = new ReentrantLock();
|
||||
/**
|
||||
* 临时文件目录.
|
||||
*/
|
||||
|
||||
/** 临时文件目录. */
|
||||
protected volatile File tmpDirFile;
|
||||
|
||||
private volatile String msgDataFormat;
|
||||
private volatile String secret;
|
||||
private volatile String accessToken;
|
||||
private volatile String aesKey;
|
||||
private volatile long expiresTime;
|
||||
/**
|
||||
* 云环境ID
|
||||
*/
|
||||
private volatile String apiSignatureRsaPrivateKey;
|
||||
private volatile String apiSignatureAesKey;
|
||||
private volatile String apiSignatureRsaPrivateKeySn;
|
||||
private volatile String apiSignatureAesKeySn;
|
||||
private volatile String wechatMpAppid;
|
||||
|
||||
/** 云环境ID */
|
||||
private volatile String cloudEnv;
|
||||
|
||||
private volatile String httpProxyHost;
|
||||
private volatile int httpProxyPort;
|
||||
private volatile String httpProxyUsername;
|
||||
@@ -57,10 +58,10 @@ public class WxMaDefaultConfigImpl implements WxMaConfig {
|
||||
|
||||
private volatile String jsapiTicket;
|
||||
private volatile long jsapiTicketExpiresTime;
|
||||
/**
|
||||
* 微信卡券的ticket单独缓存.
|
||||
*/
|
||||
|
||||
/** 微信卡券的ticket单独缓存. */
|
||||
private volatile String cardApiTicket;
|
||||
|
||||
private volatile long cardApiTicketExpiresTime;
|
||||
protected volatile Lock jsapiTicketLock = new ReentrantLock();
|
||||
protected volatile Lock cardApiTicketLock = new ReentrantLock();
|
||||
@@ -68,35 +69,24 @@ public class WxMaDefaultConfigImpl implements WxMaConfig {
|
||||
private String apiHostUrl;
|
||||
private String accessTokenUrl;
|
||||
|
||||
/**
|
||||
* 自定义配置token的消费者
|
||||
*/
|
||||
@Setter
|
||||
private Consumer<WxAccessTokenEntity> updateAccessTokenBefore;
|
||||
/** 自定义配置token的消费者 */
|
||||
@Setter private Consumer<WxAccessTokenEntity> updateAccessTokenBefore;
|
||||
|
||||
/**
|
||||
* 开启回调
|
||||
*/
|
||||
/** 开启回调 */
|
||||
@Getter(AccessLevel.NONE)
|
||||
private boolean enableUpdateAccessTokenBefore = true;
|
||||
|
||||
/**
|
||||
* 可临时关闭更新token回调,主要用于其他介质初始化数据时,可不进行回调
|
||||
*/
|
||||
/** 可临时关闭更新token回调,主要用于其他介质初始化数据时,可不进行回调 */
|
||||
public void enableUpdateAccessTokenBefore(boolean enableUpdateAccessTokenBefore) {
|
||||
this.enableUpdateAccessTokenBefore = enableUpdateAccessTokenBefore;
|
||||
}
|
||||
|
||||
/**
|
||||
* 会过期的数据提前过期时间,默认预留200秒的时间
|
||||
*/
|
||||
/** 会过期的数据提前过期时间,默认预留200秒的时间 */
|
||||
protected long expiresAheadInMillis(int expiresInSeconds) {
|
||||
return System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断 expiresTime 是否已经过期
|
||||
*/
|
||||
/** 判断 expiresTime 是否已经过期 */
|
||||
protected boolean isExpired(long expiresTime) {
|
||||
return System.currentTimeMillis() > expiresTime;
|
||||
}
|
||||
@@ -110,7 +100,7 @@ public class WxMaDefaultConfigImpl implements WxMaConfig {
|
||||
this.accessToken = accessToken;
|
||||
}
|
||||
|
||||
//region 使用稳定版接口获取accessToken
|
||||
// region 使用稳定版接口获取accessToken
|
||||
@Override
|
||||
public boolean isStableAccessToken() {
|
||||
return this.useStableAccessToken;
|
||||
@@ -120,8 +110,8 @@ public class WxMaDefaultConfigImpl implements WxMaConfig {
|
||||
public void useStableAccessToken(boolean useStableAccessToken) {
|
||||
this.useStableAccessToken = useStableAccessToken;
|
||||
}
|
||||
//endregion
|
||||
|
||||
// endregion
|
||||
|
||||
@Override
|
||||
public Lock getAccessTokenLock() {
|
||||
@@ -137,10 +127,10 @@ public class WxMaDefaultConfigImpl implements WxMaConfig {
|
||||
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) {
|
||||
@@ -248,6 +238,46 @@ public class WxMaDefaultConfigImpl implements WxMaConfig {
|
||||
this.aesKey = aesKey;
|
||||
}
|
||||
|
||||
public String getApiSignatureRsaPrivateKey() {
|
||||
return apiSignatureRsaPrivateKey;
|
||||
}
|
||||
|
||||
public void setApiSignatureRsaPrivateKey(String apiSignatureRsaPrivateKey) {
|
||||
this.apiSignatureRsaPrivateKey = apiSignatureRsaPrivateKey;
|
||||
}
|
||||
|
||||
public String getApiSignatureAesKey() {
|
||||
return apiSignatureAesKey;
|
||||
}
|
||||
|
||||
public void setApiSignatureAesKey(String apiSignatureAesKey) {
|
||||
this.apiSignatureAesKey = apiSignatureAesKey;
|
||||
}
|
||||
|
||||
public String getApiSignatureRsaPrivateKeySn() {
|
||||
return apiSignatureRsaPrivateKeySn;
|
||||
}
|
||||
|
||||
public void setApiSignatureRsaPrivateKeySn(String apiSignatureRsaPrivateKeySn) {
|
||||
this.apiSignatureRsaPrivateKeySn = apiSignatureRsaPrivateKeySn;
|
||||
}
|
||||
|
||||
public String getApiSignatureAesKeySn() {
|
||||
return apiSignatureAesKeySn;
|
||||
}
|
||||
|
||||
public void setApiSignatureAesKeySn(String apiSignatureAesKeySn) {
|
||||
this.apiSignatureAesKeySn = apiSignatureAesKeySn;
|
||||
}
|
||||
|
||||
public String getWechatMpAppid() {
|
||||
return wechatMpAppid == null ? appid : wechatMpAppid;
|
||||
}
|
||||
|
||||
public void setWechatMpAppid(String wechatMpAppid) {
|
||||
this.wechatMpAppid = wechatMpAppid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getOriginalId() {
|
||||
return originalId;
|
||||
|
||||
Reference in New Issue
Block a user