mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-12-13 21:49:32 +08:00
🆕 #3404 【小程序】增加同城配送相关接口,同时为WxMaService增加了API签名支持
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
package me.chanjar.weixin.open.api;
|
||||
|
||||
import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
||||
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
|
||||
import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken;
|
||||
import me.chanjar.weixin.open.bean.WxOpenComponentAccessToken;
|
||||
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
/**
|
||||
* The interface Wx open config storage.
|
||||
*
|
||||
@@ -99,9 +98,7 @@ public interface WxOpenConfigStorage {
|
||||
*/
|
||||
boolean isComponentAccessTokenExpired();
|
||||
|
||||
/**
|
||||
* Expire component access token.
|
||||
*/
|
||||
/** Expire component access token. */
|
||||
void expireComponentAccessToken();
|
||||
|
||||
/**
|
||||
@@ -141,6 +138,7 @@ public interface WxOpenConfigStorage {
|
||||
|
||||
/**
|
||||
* http 请求重试间隔
|
||||
*
|
||||
* <pre>
|
||||
* {@link me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl#setRetrySleepMillis(int)}
|
||||
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setRetrySleepMillis(int)}
|
||||
@@ -150,6 +148,7 @@ public interface WxOpenConfigStorage {
|
||||
|
||||
/**
|
||||
* http 请求最大重试次数
|
||||
*
|
||||
* <pre>
|
||||
* {@link me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl#setMaxRetryTimes(int)}
|
||||
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setMaxRetryTimes(int)}
|
||||
@@ -199,7 +198,7 @@ public interface WxOpenConfigStorage {
|
||||
* 应该是线程安全的
|
||||
*
|
||||
* @param componentAccessToken 新的accessToken值
|
||||
* @param expiresInSeconds 过期时间,以秒为单位
|
||||
* @param expiresInSeconds 过期时间,以秒为单位
|
||||
*/
|
||||
void updateComponentAccessToken(String componentAccessToken, int expiresInSeconds);
|
||||
|
||||
@@ -221,7 +220,7 @@ public interface WxOpenConfigStorage {
|
||||
/**
|
||||
* Sets authorizer refresh token.
|
||||
*
|
||||
* @param appId the app id
|
||||
* @param appId the app id
|
||||
* @param authorizerRefreshToken the authorizer refresh token
|
||||
*/
|
||||
void setAuthorizerRefreshToken(String appId, String authorizerRefreshToken);
|
||||
@@ -229,7 +228,7 @@ public interface WxOpenConfigStorage {
|
||||
/**
|
||||
* setAuthorizerRefreshToken(String appId, String authorizerRefreshToken) 方法重载方法
|
||||
*
|
||||
* @param appId the app id
|
||||
* @param appId the app id
|
||||
* @param authorizerRefreshToken the authorizer refresh token
|
||||
*/
|
||||
void updateAuthorizerRefreshToken(String appId, String authorizerRefreshToken);
|
||||
@@ -260,7 +259,7 @@ public interface WxOpenConfigStorage {
|
||||
/**
|
||||
* 应该是线程安全的
|
||||
*
|
||||
* @param appId the app id
|
||||
* @param appId the app id
|
||||
* @param authorizerAccessToken 要更新的WxAccessToken对象
|
||||
*/
|
||||
void updateAuthorizerAccessToken(String appId, WxOpenAuthorizerAccessToken authorizerAccessToken);
|
||||
@@ -268,11 +267,12 @@ public interface WxOpenConfigStorage {
|
||||
/**
|
||||
* 应该是线程安全的
|
||||
*
|
||||
* @param appId the app id
|
||||
* @param appId the app id
|
||||
* @param authorizerAccessToken 新的accessToken值
|
||||
* @param expiresInSeconds 过期时间,以秒为单位
|
||||
* @param expiresInSeconds 过期时间,以秒为单位
|
||||
*/
|
||||
void updateAuthorizerAccessToken(String appId, String authorizerAccessToken, int expiresInSeconds);
|
||||
void updateAuthorizerAccessToken(
|
||||
String appId, String authorizerAccessToken, int expiresInSeconds);
|
||||
|
||||
/**
|
||||
* Gets jsapi ticket.
|
||||
@@ -300,8 +300,8 @@ public interface WxOpenConfigStorage {
|
||||
/**
|
||||
* 应该是线程安全的
|
||||
*
|
||||
* @param appId the app id
|
||||
* @param jsapiTicket 新的jsapi ticket值
|
||||
* @param appId the app id
|
||||
* @param jsapiTicket 新的jsapi ticket值
|
||||
* @param expiresInSeconds 过期时间,以秒为单位
|
||||
*/
|
||||
void updateJsapiTicket(String appId, String jsapiTicket, int expiresInSeconds);
|
||||
@@ -314,7 +314,6 @@ public interface WxOpenConfigStorage {
|
||||
*/
|
||||
String getCardApiTicket(String appId);
|
||||
|
||||
|
||||
/**
|
||||
* Is card api ticket expired boolean.
|
||||
*
|
||||
@@ -333,8 +332,8 @@ public interface WxOpenConfigStorage {
|
||||
/**
|
||||
* 应该是线程安全的
|
||||
*
|
||||
* @param appId the app id
|
||||
* @param cardApiTicket 新的cardApi ticket值
|
||||
* @param appId the app id
|
||||
* @param cardApiTicket 新的cardApi ticket值
|
||||
* @param expiresInSeconds 过期时间,以秒为单位
|
||||
*/
|
||||
void updateCardApiTicket(String appId, String cardApiTicket, int expiresInSeconds);
|
||||
@@ -342,10 +341,34 @@ public interface WxOpenConfigStorage {
|
||||
/**
|
||||
* 设置第三方平台基础信息
|
||||
*
|
||||
* @param componentAppId 第三方平台 appid
|
||||
* @param componentAppId 第三方平台 appid
|
||||
* @param componentAppSecret 第三方平台 appsecret
|
||||
* @param componentToken 消息校验Token
|
||||
* @param componentAesKey 消息加解密Key
|
||||
* @param componentToken 消息校验Token
|
||||
* @param componentAesKey 消息加解密Key
|
||||
*/
|
||||
void setWxOpenInfo(String componentAppId, String componentAppSecret, String componentToken, String componentAesKey);
|
||||
void setWxOpenInfo(
|
||||
String componentAppId,
|
||||
String componentAppSecret,
|
||||
String componentToken,
|
||||
String componentAesKey);
|
||||
|
||||
/** 第三方平台设置API签名 RSA 私钥 */
|
||||
String getComponentApiSignatureRsaPrivateKey();
|
||||
|
||||
void setComponentApiSignatureRsaPrivateKey(String apiSignatureRsaPrivateKey);
|
||||
|
||||
/** 第三方平台设置API签名 AES KEY */
|
||||
String getComponentApiSignatureAesKey();
|
||||
|
||||
void setComponentApiSignatureAesKey(String apiSignatureAesKey);
|
||||
|
||||
/** 第三方平台设置API签名 RSA 私钥 序号 */
|
||||
String getComponentApiSignatureRsaPrivateKeySn();
|
||||
|
||||
void setComponentApiSignatureRsaPrivateKeySn(String apiSignatureRsaPrivateKeySn);
|
||||
|
||||
/** 第三方平台设置API签名 AES key 序号 */
|
||||
String getComponentApiSignatureAesKeySn();
|
||||
|
||||
void setComponentApiSignatureAesKeySn(String apiSignatureAesKeySn);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
package me.chanjar.weixin.open.api.impl;
|
||||
|
||||
|
||||
import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
@@ -16,12 +20,6 @@ import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken;
|
||||
import me.chanjar.weixin.open.bean.WxOpenComponentAccessToken;
|
||||
import me.chanjar.weixin.open.util.json.WxOpenGsonBuilder;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
* 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化
|
||||
*
|
||||
@@ -37,26 +35,36 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
|
||||
private String componentAccessToken;
|
||||
private long componentExpiresTime;
|
||||
|
||||
private String componentApiSignatureRsaPrivateKey;
|
||||
private String componentApiSignatureAesKey;
|
||||
private String componentApiSignatureRsaPrivateKeySn;
|
||||
private String componentApiSignatureAesKeySn;
|
||||
|
||||
private String httpProxyHost;
|
||||
private int httpProxyPort;
|
||||
private String httpProxyUsername;
|
||||
private String httpProxyPassword;
|
||||
|
||||
/**
|
||||
* http 请求重试间隔
|
||||
*
|
||||
* <pre>
|
||||
* {@link me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl#setRetrySleepMillis(int)}
|
||||
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setRetrySleepMillis(int)}
|
||||
* </pre>
|
||||
*/
|
||||
private int retrySleepMillis = 1000;
|
||||
|
||||
/**
|
||||
* http 请求最大重试次数
|
||||
*
|
||||
* <pre>
|
||||
* {@link me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl#setMaxRetryTimes(int)}
|
||||
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setMaxRetryTimes(int)}
|
||||
* </pre>
|
||||
*/
|
||||
private int maxRetryTimes = 5;
|
||||
|
||||
private ApacheHttpClientBuilder apacheHttpClientBuilder;
|
||||
|
||||
private Map<String, Token> authorizerRefreshTokens = new ConcurrentHashMap<>();
|
||||
@@ -77,7 +85,8 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
|
||||
|
||||
@Override
|
||||
public void updateComponentAccessToken(WxOpenComponentAccessToken componentAccessToken) {
|
||||
updateComponentAccessToken(componentAccessToken.getComponentAccessToken(), componentAccessToken.getExpiresIn());
|
||||
updateComponentAccessToken(
|
||||
componentAccessToken.getComponentAccessToken(), componentAccessToken.getExpiresIn());
|
||||
}
|
||||
|
||||
private Lock accessTokenLockInstance;
|
||||
@@ -126,8 +135,11 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setWxOpenInfo(String componentAppId, String componentAppSecret, String componentToken,
|
||||
String componentAesKey) {
|
||||
public void setWxOpenInfo(
|
||||
String componentAppId,
|
||||
String componentAppSecret,
|
||||
String componentToken,
|
||||
String componentAesKey) {
|
||||
setComponentAppId(componentAppId);
|
||||
setComponentAppSecret(componentAppSecret);
|
||||
setComponentToken(componentToken);
|
||||
@@ -141,7 +153,8 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
|
||||
|
||||
private String getTokenString(Map<String, Token> map, String key) {
|
||||
Token token = map.get(key);
|
||||
if (token == null || (token.expiresTime != null && System.currentTimeMillis() > token.expiresTime)) {
|
||||
if (token == null
|
||||
|| (token.expiresTime != null && System.currentTimeMillis() > token.expiresTime)) {
|
||||
return null;
|
||||
}
|
||||
return token.token;
|
||||
@@ -154,7 +167,8 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
|
||||
}
|
||||
}
|
||||
|
||||
private void updateToken(Map<String, Token> map, String key, String tokenString, Integer expiresInSeconds) {
|
||||
private void updateToken(
|
||||
Map<String, Token> map, String key, String tokenString, Integer expiresInSeconds) {
|
||||
Token token = map.get(key);
|
||||
if (token == null) {
|
||||
token = new Token();
|
||||
@@ -186,7 +200,6 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
|
||||
return getTokenString(authorizerAccessTokens, appId);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isAuthorizerAccessTokenExpired(String appId) {
|
||||
return getTokenString(authorizerAccessTokens, appId) == null;
|
||||
@@ -198,13 +211,17 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateAuthorizerAccessToken(String appId, WxOpenAuthorizerAccessToken authorizerAccessToken) {
|
||||
updateAuthorizerAccessToken(appId, authorizerAccessToken.getAuthorizerAccessToken(),
|
||||
authorizerAccessToken.getExpiresIn());
|
||||
public void updateAuthorizerAccessToken(
|
||||
String appId, WxOpenAuthorizerAccessToken authorizerAccessToken) {
|
||||
updateAuthorizerAccessToken(
|
||||
appId,
|
||||
authorizerAccessToken.getAuthorizerAccessToken(),
|
||||
authorizerAccessToken.getExpiresIn());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateAuthorizerAccessToken(String appId, String authorizerAccessToken, int expiresInSeconds) {
|
||||
public void updateAuthorizerAccessToken(
|
||||
String appId, String authorizerAccessToken, int expiresInSeconds) {
|
||||
updateToken(authorizerAccessTokens, appId, authorizerAccessToken, expiresInSeconds);
|
||||
}
|
||||
|
||||
@@ -261,21 +278,18 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
|
||||
private WxMpHostConfig hostConfig;
|
||||
private String apiHostUrl;
|
||||
private String accessTokenUrl;
|
||||
/**
|
||||
* 是否使用稳定版获取accessToken接口
|
||||
*/
|
||||
|
||||
/** 是否使用稳定版获取accessToken接口 */
|
||||
@Getter(value = AccessLevel.NONE)
|
||||
@Setter(value = AccessLevel.NONE)
|
||||
private boolean useStableAccessToken;
|
||||
|
||||
/**
|
||||
* 小程序原始ID
|
||||
*/
|
||||
/** 小程序原始ID */
|
||||
private volatile String originalId;
|
||||
/**
|
||||
* 云环境ID
|
||||
*/
|
||||
|
||||
/** 云环境ID */
|
||||
private volatile String cloudEnv;
|
||||
|
||||
private final Lock accessTokenLock;
|
||||
private final Lock jsapiTicketLock;
|
||||
private final Lock cardApiTicketLock;
|
||||
@@ -326,15 +340,18 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
|
||||
@Override
|
||||
public String getTicket(TicketType type) {
|
||||
switch (type) {
|
||||
case JSAPI: {
|
||||
return wxOpenConfigStorage.getJsapiTicket(appId);
|
||||
}
|
||||
case WX_CARD: {
|
||||
return wxOpenConfigStorage.getCardApiTicket(appId);
|
||||
}
|
||||
default: {
|
||||
// do nothing
|
||||
}
|
||||
case JSAPI:
|
||||
{
|
||||
return wxOpenConfigStorage.getJsapiTicket(appId);
|
||||
}
|
||||
case WX_CARD:
|
||||
{
|
||||
return wxOpenConfigStorage.getCardApiTicket(appId);
|
||||
}
|
||||
default:
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -342,15 +359,18 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
|
||||
@Override
|
||||
public Lock getTicketLock(TicketType type) {
|
||||
switch (type) {
|
||||
case JSAPI: {
|
||||
return this.jsapiTicketLock;
|
||||
}
|
||||
case WX_CARD: {
|
||||
return this.cardApiTicketLock;
|
||||
}
|
||||
default: {
|
||||
// do nothing
|
||||
}
|
||||
case JSAPI:
|
||||
{
|
||||
return this.jsapiTicketLock;
|
||||
}
|
||||
case WX_CARD:
|
||||
{
|
||||
return this.cardApiTicketLock;
|
||||
}
|
||||
default:
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -358,15 +378,18 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
|
||||
@Override
|
||||
public boolean isTicketExpired(TicketType type) {
|
||||
switch (type) {
|
||||
case JSAPI: {
|
||||
return wxOpenConfigStorage.isJsapiTicketExpired(appId);
|
||||
}
|
||||
case WX_CARD: {
|
||||
return wxOpenConfigStorage.isCardApiTicketExpired(appId);
|
||||
}
|
||||
default: {
|
||||
// do nothing
|
||||
}
|
||||
case JSAPI:
|
||||
{
|
||||
return wxOpenConfigStorage.isJsapiTicketExpired(appId);
|
||||
}
|
||||
case WX_CARD:
|
||||
{
|
||||
return wxOpenConfigStorage.isCardApiTicketExpired(appId);
|
||||
}
|
||||
default:
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -375,36 +398,41 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
|
||||
@Override
|
||||
public void expireTicket(TicketType type) {
|
||||
switch (type) {
|
||||
case JSAPI: {
|
||||
wxOpenConfigStorage.expireJsapiTicket(appId);
|
||||
break;
|
||||
}
|
||||
case WX_CARD: {
|
||||
wxOpenConfigStorage.expireCardApiTicket(appId);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
// do nothing
|
||||
}
|
||||
case JSAPI:
|
||||
{
|
||||
wxOpenConfigStorage.expireJsapiTicket(appId);
|
||||
break;
|
||||
}
|
||||
case WX_CARD:
|
||||
{
|
||||
wxOpenConfigStorage.expireCardApiTicket(appId);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateTicket(TicketType type, String ticket, int expiresInSeconds) {
|
||||
switch (type) {
|
||||
case JSAPI: {
|
||||
wxOpenConfigStorage.updateJsapiTicket(appId, ticket, expiresInSeconds);
|
||||
break;
|
||||
}
|
||||
case WX_CARD: {
|
||||
wxOpenConfigStorage.updateCardApiTicket(appId, ticket, expiresInSeconds);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
// do nothing
|
||||
}
|
||||
case JSAPI:
|
||||
{
|
||||
wxOpenConfigStorage.updateJsapiTicket(appId, ticket, expiresInSeconds);
|
||||
break;
|
||||
}
|
||||
case WX_CARD:
|
||||
{
|
||||
wxOpenConfigStorage.updateCardApiTicket(appId, ticket, expiresInSeconds);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -510,12 +538,35 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getAesKey() {
|
||||
return wxOpenConfigStorage.getComponentAesKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getApiSignatureRsaPrivateKey() {
|
||||
return wxOpenConfigStorage.getComponentApiSignatureRsaPrivateKey();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getApiSignatureAesKey() {
|
||||
return wxOpenConfigStorage.getComponentApiSignatureAesKey();
|
||||
}
|
||||
|
||||
public String getApiSignatureRsaPrivateKeySn() {
|
||||
return wxOpenConfigStorage.getComponentApiSignatureRsaPrivateKeySn();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getApiSignatureAesKeySn() {
|
||||
return wxOpenConfigStorage.getComponentApiSignatureAesKeySn();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getWechatMpAppid() {
|
||||
return wxOpenConfigStorage.getComponentAppId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMsgDataFormat() {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user