From 04fb35d1ba6751c1682eb980398fc580888b6708 Mon Sep 17 00:00:00 2001 From: whhya <2458317501@qq.com> Date: Tue, 21 Jul 2020 10:20:12 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20#1604=20=E4=BF=AE=E5=A4=8D=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E5=BC=80=E6=94=BE=E5=B9=B3=E5=8F=B0=E5=B8=90=E5=8F=B7?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3,?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=8C=87=E5=AE=9AappId=E7=9A=84access=5Ftoke?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix:修改微信开放平台帐号管理相关接口,使用指定appId的access_token,非开放平台自身的component_access_token --- .../chanjar/weixin/common/api/WxConsts.java | 14 +++++- .../open/api/WxOpenComponentService.java | 16 ++++--- .../api/impl/WxOpenComponentServiceImpl.java | 45 ++++++++++++++----- 3 files changed, 58 insertions(+), 17 deletions(-) diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxConsts.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxConsts.java index e63b4a8c6..7f1dcdf02 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxConsts.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/api/WxConsts.java @@ -394,5 +394,17 @@ public class WxConsts { public static final String OPERATORDEFAULT = "DEFAULT"; } - + /** + * appId 类型 + */ + public static class AppIdType { + /** + * 公众号appId类型 + */ + public static final String MP_TYPE = "mp"; + /** + * 小程序appId类型 + */ + public static final String MINI_TYPE = "mini"; + } } 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 4e0a2bccf..68dfb3d60 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 @@ -415,43 +415,47 @@ public interface WxOpenComponentService { * 创建 开放平台帐号并绑定公众号/小程序. * https://api.weixin.qq.com/cgi-bin/open/create * - * @param appId 公众号/小程序的appId + * @param appId 公众号/小程序的appId + * @param appIdType appId类型 me.chanjar.weixin.common.api.WxConsts.AppIdType mp-公众号 mini-小程序 * @return . wx open create result * @throws WxErrorException . */ - WxOpenCreateResult createOpenAccount(String appId) throws WxErrorException; + WxOpenCreateResult createOpenAccount(String appId, String appIdType) throws WxErrorException; /** * https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/api/account/bind.html * 将公众号/小程序绑定到开放平台帐号下 * * @param appId 公众号/小程序的appId + * @param appIdType appId类型 me.chanjar.weixin.common.api.WxConsts.AppIdType mp-公众号 mini-小程序 * @param openAppid 开放平台帐号 appid,由创建开发平台帐号接口返回 * @return the boolean * @throws WxErrorException the wx error exception */ - Boolean bindOpenAccount(String appId, String openAppid) throws WxErrorException; + Boolean bindOpenAccount(String appId, String appIdType, String openAppid) throws WxErrorException; /** * https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/api/account/unbind.html * 将公众号/小程序从开放平台帐号下解绑 * * @param appId 公众号/小程序的appId + * @param appIdType appId类型 me.chanjar.weixin.common.api.WxConsts.AppIdType mp-公众号 mini-小程序 * @param openAppid 开放平台帐号 appid,由创建开发平台帐号接口返回 * @return the boolean * @throws WxErrorException the wx error exception */ - Boolean unbindOpenAccount(String appId, String openAppid) throws WxErrorException; + Boolean unbindOpenAccount(String appId, String appIdType, String openAppid) throws WxErrorException; /** * https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/api/account/get.html * 获取公众号/小程序所绑定的开放平台帐号 * - * @param appId 公众号/小程序的appId + * @param appId 公众号/小程序的appId + * @param appIdType appId类型 me.chanjar.weixin.common.api.WxConsts.AppIdType mp-公众号 mini-小程序 * @return 开放平台帐号 appid,由创建开发平台帐号接口返回 * @throws WxErrorException the wx error exception */ - WxOpenGetResult getOpenAccount(String appId) throws WxErrorException; + WxOpenGetResult getOpenAccount(String appId, String appIdType) throws WxErrorException; /** * https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=21538208049W8uwq&token=&lang=zh_CN 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 5e54929a0..fe2a782e4 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 @@ -5,6 +5,7 @@ import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import me.chanjar.weixin.common.api.WxConsts; import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.util.crypto.SHA1; @@ -474,47 +475,71 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { post(DELETE_TEMPLATE_URL, param.toString(), "access_token"); } + /** + * 微信开放平台帐号管理统一请求入口 + * + * @param appId 操作appId 小程序/公众号 + * @param appIdType 操作类型 小程序/公众号 + * @param requestUrl 请求地址 + * @param param 请求参数 + * @return 请求结果 + * @throws WxErrorException + */ + private String openAccountServicePost(String appId, String appIdType, String requestUrl, JsonObject param) throws WxErrorException { + String result = ""; + switch (appIdType) { + case WxConsts.AppIdType.MP_TYPE: + WxMpService wxMpService = this.getWxMpServiceByAppid(appId); + result = wxMpService.post(requestUrl, param.toString()); + return result; + case WxConsts.AppIdType.MINI_TYPE: + WxOpenMaService maService = this.getWxMaServiceByAppid(appId); + result = maService.post(requestUrl, param.toString()); + return result; + default: + throw new WxErrorException(WxError.builder().errorCode(-1).errorMsg("appIdType类型异常").build()); + } + } + @Override - public WxOpenCreateResult createOpenAccount(String appId) throws WxErrorException { + public WxOpenCreateResult createOpenAccount(String appId, String appIdType) throws WxErrorException { JsonObject param = new JsonObject(); param.addProperty("appid", appId); - String json = post(CREATE_OPEN_URL, param.toString(), "access_token"); + String json = openAccountServicePost(appId, appIdType, CREATE_OPEN_URL, param); return WxOpenCreateResult.fromJson(json); } @Override - public Boolean bindOpenAccount(String appId, String openAppid) throws WxErrorException { + public Boolean bindOpenAccount(String appId, String appIdType, String openAppid) throws WxErrorException { JsonObject param = new JsonObject(); param.addProperty("appid", appId); param.addProperty("open_appid", openAppid); - String json = post(BIND_OPEN_URL, param.toString(), "access_token"); - + String json = openAccountServicePost(appId, appIdType, BIND_OPEN_URL, param); return WxOpenResult.fromJson(json).isSuccess(); } @Override - public Boolean unbindOpenAccount(String appId, String openAppid) throws WxErrorException { + public Boolean unbindOpenAccount(String appId, String appIdType, String openAppid) throws WxErrorException { JsonObject param = new JsonObject(); param.addProperty("appid", appId); param.addProperty("open_appid", openAppid); - String json = post(UNBIND_OPEN_URL, param.toString(), "access_token"); - + String json = openAccountServicePost(appId, appIdType, UNBIND_OPEN_URL, param); return WxOpenResult.fromJson(json).isSuccess(); } @Override - public WxOpenGetResult getOpenAccount(String appId) throws WxErrorException { + public WxOpenGetResult getOpenAccount(String appId, String appIdType) throws WxErrorException { JsonObject param = new JsonObject(); param.addProperty("appid", appId); - String json = post(GET_OPEN_URL, param.toString(), "access_token"); + String json = openAccountServicePost(appId, appIdType, GET_OPEN_URL, param); return WxOpenGetResult.fromJson(json); }