优化#718所提交的代码

This commit is contained in:
Binary Wang
2018-08-20 12:11:31 +08:00
parent 9cdff1d010
commit a81a8b2b69
2 changed files with 28 additions and 20 deletions

View File

@@ -1,5 +1,7 @@
package me.chanjar.weixin.open.api; package me.chanjar.weixin.open.api;
import java.util.List;
import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
@@ -11,8 +13,6 @@ import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerInfoResult;
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerOptionResult; import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerOptionResult;
import me.chanjar.weixin.open.bean.result.WxOpenQueryAuthResult; import me.chanjar.weixin.open.bean.result.WxOpenQueryAuthResult;
import java.util.List;
/** /**
* @author <a href="https://github.com/007gzs">007</a> * @author <a href="https://github.com/007gzs">007</a>
*/ */
@@ -53,12 +53,13 @@ public interface WxOpenComponentService {
* 获取用户授权页URL来路URL和成功跳转URL 的域名都需要为三方平台设置的 登录授权的发起页域名) * 获取用户授权页URL来路URL和成功跳转URL 的域名都需要为三方平台设置的 登录授权的发起页域名)
*/ */
String getPreAuthUrl(String redirectURI) throws WxErrorException; String getPreAuthUrl(String redirectURI) throws WxErrorException;
/** /**
* authType 要授权的帐号类型1则商户点击链接后手机端仅展示公众号、2表示仅展示小程序3表示公众号和小程序都展示。如果为未指定则默认小程序和公众号都展示。第三方平台开发者可以使用本字段来控制授权的帐号类型。 * authType 要授权的帐号类型1则商户点击链接后手机端仅展示公众号、2表示仅展示小程序3表示公众号和小程序都展示。如果为未指定则默认小程序和公众号都展示。第三方平台开发者可以使用本字段来控制授权的帐号类型。
* bizAppid 指定授权唯一的小程序或公众号 * bizAppid 指定授权唯一的小程序或公众号
* 注auth_type、biz_appid两个字段互斥。 * 注auth_type、biz_appid两个字段互斥。
*/ */
String getPreAuthUrl(String redirectURI,String authType, String bizAppid) throws WxErrorException; String getPreAuthUrl(String redirectURI, String authType, String bizAppid) throws WxErrorException;
String route(WxOpenXmlMessage wxMessage) throws WxErrorException; String route(WxOpenXmlMessage wxMessage) throws WxErrorException;

View File

@@ -1,5 +1,13 @@
package me.chanjar.weixin.open.api.impl; package me.chanjar.weixin.open.api.impl;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@@ -24,13 +32,6 @@ import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerInfoResult;
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerOptionResult; import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerOptionResult;
import me.chanjar.weixin.open.bean.result.WxOpenQueryAuthResult; import me.chanjar.weixin.open.bean.result.WxOpenQueryAuthResult;
import me.chanjar.weixin.open.util.json.WxOpenGsonBuilder; import me.chanjar.weixin.open.util.json.WxOpenGsonBuilder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
/** /**
* @author <a href="https://github.com/007gzs">007</a> * @author <a href="https://github.com/007gzs">007</a>
@@ -148,6 +149,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService {
private String get(String uri) throws WxErrorException { private String get(String uri) throws WxErrorException {
return get(uri, "component_access_token"); return get(uri, "component_access_token");
} }
private String get(String uri, String accessTokenKey) throws WxErrorException { private String get(String uri, String accessTokenKey) throws WxErrorException {
String componentAccessToken = getComponentAccessToken(false); String componentAccessToken = getComponentAccessToken(false);
String uriWithComponentAccessToken = uri + (uri.contains("?") ? "&" : "?") + accessTokenKey + "=" + componentAccessToken; String uriWithComponentAccessToken = uri + (uri.contains("?") ? "&" : "?") + accessTokenKey + "=" + componentAccessToken;
@@ -177,23 +179,28 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService {
@Override @Override
public String getPreAuthUrl(String redirectURI) throws WxErrorException { public String getPreAuthUrl(String redirectURI) throws WxErrorException {
return getPreAuthUrl(redirectURI,null, null); return getPreAuthUrl(redirectURI, null, null);
} }
@Override
public String getPreAuthUrl(String redirectURI,String authType, String bizAppid) throws WxErrorException {
@Override
public String getPreAuthUrl(String redirectURI, String authType, String bizAppid) throws WxErrorException {
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("component_appid", getWxOpenConfigStorage().getComponentAppId()); jsonObject.addProperty("component_appid", getWxOpenConfigStorage().getComponentAppId());
String responseContent = post(API_CREATE_PREAUTHCODE_URL, jsonObject.toString()); String responseContent = post(API_CREATE_PREAUTHCODE_URL, jsonObject.toString());
jsonObject = WxGsonBuilder.create().fromJson(responseContent, JsonObject.class); jsonObject = WxGsonBuilder.create().fromJson(responseContent, JsonObject.class);
String preAuthUrl = String.format(COMPONENT_LOGIN_PAGE_URL, getWxOpenConfigStorage().getComponentAppId(), jsonObject.get("pre_auth_code").getAsString(), URIUtil.encodeURIComponent(redirectURI));
if(StringUtils.isNotEmpty(authType)){ StringBuilder preAuthUrl = new StringBuilder(String.format(COMPONENT_LOGIN_PAGE_URL,
preAuthUrl = preAuthUrl + "&auth_type=" + authType; getWxOpenConfigStorage().getComponentAppId(),
jsonObject.get("pre_auth_code").getAsString(),
URIUtil.encodeURIComponent(redirectURI)));
if (StringUtils.isNotEmpty(authType)) {
preAuthUrl.append("&auth_type=").append(authType);
} }
if(StringUtils.isNotEmpty(bizAppid)){ if (StringUtils.isNotEmpty(bizAppid)) {
preAuthUrl = preAuthUrl + "&biz_appid=" + bizAppid; preAuthUrl.append("&biz_appid=").append(bizAppid);
} }
return preAuthUrl;
return preAuthUrl.toString();
} }