Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:修改微信开放平台帐号管理相关接口,使用指定appId的access_token,非开放平台自身的component_access_… #1676

Merged
merged 2 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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