Skip to content

Commit

Permalink
🐛 #1604 修复微信开放平台帐号管理相关接口,使用指定appId的access_token
Browse files Browse the repository at this point in the history
* fix:修改微信开放平台帐号管理相关接口,使用指定appId的access_token,非开放平台自身的component_access_token
  • Loading branch information
whhya committed Jul 21, 2020
1 parent 4d14409 commit 04fb35d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -394,5 +394,17 @@ public static class NetCheckArgs {
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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -474,47 +475,71 @@ public void deleteTemplate(long templateId) throws WxErrorException {
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);
}

Expand Down

0 comments on commit 04fb35d

Please sign in to comment.