From 5dcd06b620cffa65c39b8e9574ed4f8a44ca8dd6 Mon Sep 17 00:00:00 2001 From: 007gzs <007gzs@gmail.com> Date: Thu, 4 Jan 2018 22:44:27 +0800 Subject: [PATCH] =?UTF-8?q?#420=20=E5=BC=80=E6=94=BE=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E5=A2=9E=E5=8A=A0=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- weixin-java-open/pom.xml | 5 +++ .../open/api/WxOpenComponentService.java | 11 +++--- .../weixin/open/api/WxOpenConfigStorage.java | 2 ++ .../api/impl/WxOpenComponentServiceImpl.java | 34 +++++++++++++++--- .../api/impl/WxOpenInMemoryConfigStorage.java | 23 +++++++++--- .../open/api/impl/WxOpenMaServiceImpl.java | 36 +++++++++++++++++++ .../open/bean/auth/WxOpenAuthorizerInfo.java | 1 - .../open/bean/auth/WxOpenMiniProgramInfo.java | 14 ++++++++ .../result/WxOpenAuthorizerInfoResult.java | 3 ++ .../json/WxOpenAuthorizerInfoGsonAdapter.java | 13 ++++--- 10 files changed, 123 insertions(+), 19 deletions(-) create mode 100644 weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenMaServiceImpl.java create mode 100644 weixin-java-open/src/main/java/me/chanjar/weixin/open/bean/auth/WxOpenMiniProgramInfo.java diff --git a/weixin-java-open/pom.xml b/weixin-java-open/pom.xml index 7554315a0d..3c669bb57c 100644 --- a/weixin-java-open/pom.xml +++ b/weixin-java-open/pom.xml @@ -31,6 +31,11 @@ weixin-java-mp ${project.version} + + com.github.binarywang + weixin-java-miniapp + ${project.version} + org.jodd diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenComponentService.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenComponentService.java index 39d1b4e645..b5ac105845 100644 --- a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenComponentService.java +++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenComponentService.java @@ -1,6 +1,7 @@ package me.chanjar.weixin.open.api; -import me.chanjar.weixin.common.bean.result.WxError; +import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; @@ -35,8 +36,10 @@ public interface WxOpenComponentService { */ String OAUTH2_REFRESH_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/component/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s&component_appid=%s"; - WxMpService getWxMpServiceByAppid(String appid); + String MINIAPP_JSCODE_2_SESSION = "https://api.weixin.qq.com/sns/component/jscode2session?appid=%s&js_code=%s&grant_type=authorization_code&component_appid=%s"; + WxMpService getWxMpServiceByAppid(String appid); + WxMaService getWxMaServiceByAppid(String appid); WxOpenConfigStorage getWxOpenConfigStorage(); boolean checkSignature(String timestamp, String nonce, String signature); @@ -49,7 +52,6 @@ public interface WxOpenComponentService { String getPreAuthUrl(String redirectURI) throws WxErrorException; String route(WxOpenXmlMessage wxMessage) throws WxErrorException; - /** * 使用授权码换取公众号或小程序的接口调用凭据和授权信息 */ @@ -68,7 +70,7 @@ public interface WxOpenComponentService { /** * 设置授权方的选项信息 */ - WxError setAuthorizerOption(String authorizerAppid, String optionName, String optionValue) throws WxErrorException; + void setAuthorizerOption(String authorizerAppid, String optionName, String optionValue) throws WxErrorException; String getAuthorizerAccessToken(String appid, boolean forceRefresh) throws WxErrorException; @@ -79,5 +81,6 @@ public interface WxOpenComponentService { WxMpOAuth2AccessToken oauth2refreshAccessToken(String appid, String refreshToken) throws WxErrorException; String oauth2buildAuthorizationUrl(String appid, String redirectURI, String scope, String state); + WxMaJscode2SessionResult miniappJscode2Session(String appid, String jsCode, String appId) throws WxErrorException; } diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenConfigStorage.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenConfigStorage.java index 7e548253e8..57eccd1390 100644 --- a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenConfigStorage.java +++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenConfigStorage.java @@ -1,5 +1,6 @@ package me.chanjar.weixin.open.api; +import cn.binarywang.wx.miniapp.config.WxMaConfig; import me.chanjar.weixin.mp.api.WxMpConfigStorage; import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken; import me.chanjar.weixin.open.bean.WxOpenComponentAccessToken; @@ -36,6 +37,7 @@ public interface WxOpenConfigStorage { void updateComponentAccessTokent(WxOpenComponentAccessToken componentAccessToken); WxMpConfigStorage getWxMpConfigStorage(String appId); + WxMaConfig getWxMaConfig(String appId); /** * 应该是线程安全的 diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenComponentServiceImpl.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenComponentServiceImpl.java index 5e8fc81bf8..e7e070d5d4 100644 --- a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenComponentServiceImpl.java +++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenComponentServiceImpl.java @@ -1,7 +1,8 @@ package me.chanjar.weixin.open.api.impl; +import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import com.google.gson.JsonObject; -import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.util.crypto.SHA1; import me.chanjar.weixin.common.util.http.URIUtil; @@ -30,7 +31,10 @@ * @author 007 */ public class WxOpenComponentServiceImpl implements WxOpenComponentService { + + private static final Map WX_OPEN_MA_SERVICE_MAP = new Hashtable<>(); private static final Map WX_OPEN_MP_SERVICE_MAP = new Hashtable<>(); + protected final Logger log = LoggerFactory.getLogger(this.getClass()); private WxOpenService wxOpenService; @@ -54,6 +58,20 @@ public WxMpService getWxMpServiceByAppid(String appId) { return wxMpService; } + @Override + public WxMaService getWxMaServiceByAppid(String appId) { + WxMaService wxMaService = WX_OPEN_MA_SERVICE_MAP.get(appId); + if (wxMaService == null) { + synchronized (WX_OPEN_MA_SERVICE_MAP) { + wxMaService = WX_OPEN_MA_SERVICE_MAP.get(appId); + if (wxMaService == null) { + wxMaService = new WxOpenMaServiceImpl(this, appId, getWxOpenConfigStorage().getWxMaConfig(appId)); + WX_OPEN_MA_SERVICE_MAP.put(appId, wxMaService); + } + } + } + return wxMaService; + } public WxOpenService getWxOpenService() { return wxOpenService; } @@ -137,7 +155,7 @@ public String route(final WxOpenXmlMessage wxMessage) throws WxErrorException { } return "success"; } - return null; + return ""; } @Override @@ -169,14 +187,13 @@ public WxOpenAuthorizerOptionResult getAuthorizerOption(String authorizerAppid, } @Override - public WxError setAuthorizerOption(String authorizerAppid, String optionName, String optionValue) throws WxErrorException { + public void setAuthorizerOption(String authorizerAppid, String optionName, String optionValue) throws WxErrorException { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("component_appid", getWxOpenConfigStorage().getComponentAppId()); jsonObject.addProperty("authorizer_appid", authorizerAppid); jsonObject.addProperty("option_name", optionName); jsonObject.addProperty("option_value", optionValue); - String responseContent = post(API_SET_AUTHORIZER_OPTION_URL, jsonObject.toString()); - return WxGsonBuilder.create().fromJson(responseContent, WxError.class); + post(API_SET_AUTHORIZER_OPTION_URL, jsonObject.toString()); } @Override @@ -220,4 +237,11 @@ public String oauth2buildAuthorizationUrl(String appId, String redirectURI, Stri appId, URIUtil.encodeURIComponent(redirectURI), scope, StringUtils.trimToEmpty(state), getWxOpenConfigStorage().getComponentAppId()); } + @Override + public WxMaJscode2SessionResult miniappJscode2Session(String appid, String jsCode, String appId) throws WxErrorException { + String url = String.format(MINIAPP_JSCODE_2_SESSION, appId, jsCode, getWxOpenConfigStorage().getComponentAppId()); + String responseContent = get(url); + return WxMaJscode2SessionResult.fromJson(responseContent); + } + } diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInMemoryConfigStorage.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInMemoryConfigStorage.java index 50cf8b6e74..3ec6d5cee1 100644 --- a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInMemoryConfigStorage.java +++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInMemoryConfigStorage.java @@ -1,6 +1,7 @@ package me.chanjar.weixin.open.api.impl; +import cn.binarywang.wx.miniapp.config.WxMaConfig; import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.util.ToStringUtils; import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; @@ -101,7 +102,12 @@ public void updateComponentAccessTokent(WxOpenComponentAccessToken componentAcce @Override public WxMpConfigStorage getWxMpConfigStorage(String appId) { - return new WxOpenMpConfigStorage(this, appId); + return new WxOpenInnerConfigStorage(this, appId); + } + + @Override + public WxMaConfig getWxMaConfig(String appId) { + return new WxOpenInnerConfigStorage(this, appId); } @Override @@ -222,14 +228,13 @@ private static class Token { private String token; private Long expiresTime; } - - private static class WxOpenMpConfigStorage implements WxMpConfigStorage { + private static class WxOpenInnerConfigStorage implements WxMpConfigStorage, WxMaConfig { private WxOpenConfigStorage wxOpenConfigStorage; private String appId; private Lock accessTokenLock = new ReentrantLock(); private Lock jsapiTicketLock = new ReentrantLock(); private Lock cardApiTicketLock = new ReentrantLock(); - private WxOpenMpConfigStorage(WxOpenConfigStorage wxOpenConfigStorage, String appId) { + private WxOpenInnerConfigStorage(WxOpenConfigStorage wxOpenConfigStorage, String appId) { this.wxOpenConfigStorage = wxOpenConfigStorage; this.appId = appId; } @@ -259,6 +264,11 @@ public synchronized void updateAccessToken(String accessToken, int expiresInSeco wxOpenConfigStorage.updateAuthorizerAccessToken(appId, accessToken, expiresInSeconds); } + @Override + public String getAppid() { + return this.appId; + } + @Override public void expireAccessToken() { wxOpenConfigStorage.expireAuthorizerAccessToken(appId); @@ -343,6 +353,11 @@ public String getAesKey() { return wxOpenConfigStorage.getComponentAesKey(); } + @Override + public String getMsgDataFormat() { + return null; + } + @Override public String getOauth2redirectUri() { return null; diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenMaServiceImpl.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenMaServiceImpl.java new file mode 100644 index 0000000000..1dc5f8accf --- /dev/null +++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenMaServiceImpl.java @@ -0,0 +1,36 @@ +package me.chanjar.weixin.open.api.impl; + +import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; +import cn.binarywang.wx.miniapp.config.WxMaConfig; +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.mp.api.WxMpConfigStorage; +import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; +import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; +import me.chanjar.weixin.open.api.WxOpenComponentService; + +/** + * @author 007 + */ +/* package */ class WxOpenMaServiceImpl extends WxMaServiceImpl { + private WxOpenComponentService wxOpenComponentService; + private WxMaConfig wxMaConfig; + private String appId; + + public WxOpenMaServiceImpl(WxOpenComponentService wxOpenComponentService, String appId, WxMaConfig wxMaConfig) { + this.wxOpenComponentService = wxOpenComponentService; + this.appId = appId; + this.wxMaConfig = wxMaConfig; + initHttp(); + } + + @Override + public WxMaConfig getWxMaConfig() { + return wxMaConfig; + } + + @Override + public String getAccessToken(boolean forceRefresh) throws WxErrorException { + return wxOpenComponentService.getAuthorizerAccessToken(appId, forceRefresh); + } + +} diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/bean/auth/WxOpenAuthorizerInfo.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/bean/auth/WxOpenAuthorizerInfo.java index 0fe7a18823..a80dab0307 100644 --- a/weixin-java-open/src/main/java/me/chanjar/weixin/open/bean/auth/WxOpenAuthorizerInfo.java +++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/bean/auth/WxOpenAuthorizerInfo.java @@ -23,7 +23,6 @@ public class WxOpenAuthorizerInfo implements Serializable { private Map businessInfo; private String alias; private String qrcodeUrl; - /** * 账号介绍 */ diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/bean/auth/WxOpenMiniProgramInfo.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/bean/auth/WxOpenMiniProgramInfo.java new file mode 100644 index 0000000000..44f56e6f2b --- /dev/null +++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/bean/auth/WxOpenMiniProgramInfo.java @@ -0,0 +1,14 @@ +package me.chanjar.weixin.open.bean.auth; + +import lombok.Data; +import org.apache.commons.lang3.tuple.Pair; + +import java.util.List; +import java.util.Map; + +@Data +public class WxOpenMiniProgramInfo { + private Map> network; + private List> categories; + private Integer visitStatus; +} diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/bean/result/WxOpenAuthorizerInfoResult.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/bean/result/WxOpenAuthorizerInfoResult.java index 7bccff5c3c..e7734a1115 100644 --- a/weixin-java-open/src/main/java/me/chanjar/weixin/open/bean/result/WxOpenAuthorizerInfoResult.java +++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/bean/result/WxOpenAuthorizerInfoResult.java @@ -15,4 +15,7 @@ public class WxOpenAuthorizerInfoResult implements Serializable { private WxOpenAuthorizationInfo authorizationInfo; private WxOpenAuthorizerInfo authorizerInfo; + public boolean isMiniProgram(){ + return authorizerInfo != null && authorizerInfo.getMiniProgramInfo() != null; + } } diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/util/json/WxOpenAuthorizerInfoGsonAdapter.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/util/json/WxOpenAuthorizerInfoGsonAdapter.java index e1260c1785..2fb4e7957e 100644 --- a/weixin-java-open/src/main/java/me/chanjar/weixin/open/util/json/WxOpenAuthorizerInfoGsonAdapter.java +++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/util/json/WxOpenAuthorizerInfoGsonAdapter.java @@ -23,6 +23,8 @@ public WxOpenAuthorizerInfo deserialize(JsonElement jsonElement, Type type, Json authorizationInfo.setPrincipalName(GsonHelper.getString(jsonObject, "principal_name")); authorizationInfo.setAlias(GsonHelper.getString(jsonObject, "alias")); authorizationInfo.setQrcodeUrl(GsonHelper.getString(jsonObject, "qrcode_url")); + authorizationInfo.setSignature(GsonHelper.getString(jsonObject, "signature")); + if (jsonObject.has("service_type_info")) { authorizationInfo.setServiceTypeInfo(GsonHelper.getInteger(jsonObject.getAsJsonObject("service_type_info"), "id")); } @@ -33,11 +35,12 @@ public WxOpenAuthorizerInfo deserialize(JsonElement jsonElement, Type type, Json new TypeToken>() { }.getType()); authorizationInfo.setBusinessInfo(businessInfo); - - WxOpenAuthorizerInfo.MiniProgramInfo miniProgramInfo = WxOpenGsonBuilder.create().fromJson(jsonObject.get("MiniProgramInfo"), - new TypeToken() { - }.getType()); - authorizationInfo.setMiniProgramInfo(miniProgramInfo); + if (jsonObject.has("MiniProgramInfo")) { + WxOpenAuthorizerInfo.MiniProgramInfo miniProgramInfo = WxOpenGsonBuilder.create().fromJson(jsonObject.get("MiniProgramInfo"), + new TypeToken() { + }.getType()); + authorizationInfo.setMiniProgramInfo(miniProgramInfo); + } return authorizationInfo; } }