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); }