Skip to content

Commit 2d0cb1a

Browse files
silloybinarywang
authored andcommitted
#1212 增加开放平台账号管理接口
1 parent f86957f commit 2d0cb1a

File tree

4 files changed

+110
-8
lines changed

4 files changed

+110
-8
lines changed

weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenComponentService.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package me.chanjar.weixin.open.api;
22

33
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
4+
import com.google.gson.JsonObject;
45
import me.chanjar.weixin.common.error.WxErrorException;
56
import me.chanjar.weixin.mp.api.WxMpService;
67
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
78
import me.chanjar.weixin.open.bean.WxOpenCreateResult;
9+
import me.chanjar.weixin.open.bean.WxOpenGetResult;
810
import me.chanjar.weixin.open.bean.WxOpenMaCodeTemplate;
911
import me.chanjar.weixin.open.bean.message.WxOpenXmlMessage;
1012
import me.chanjar.weixin.open.bean.result.*;
13+
import me.chanjar.weixin.open.util.json.WxOpenGsonBuilder;
1114

1215
import java.util.List;
1316

@@ -47,6 +50,12 @@ public interface WxOpenComponentService {
4750

4851
String CREATE_OPEN_URL = "https://api.weixin.qq.com/cgi-bin/open/create";
4952

53+
String BIND_OPEN_URL = "https://api.weixin.qq.com/cgi-bin/open/bind";
54+
55+
String UNBIND_OPEN_URL = "https://api.weixin.qq.com/cgi-bin/open/unbind";
56+
57+
String GET_OPEN_URL = "https://api.weixin.qq.com/cgi-bin/open/get";
58+
5059
/**
5160
* 快速创建小程序接口.
5261
*/
@@ -212,6 +221,36 @@ public interface WxOpenComponentService {
212221
*/
213222
WxOpenCreateResult createOpenAccount(String appId) throws WxErrorException;
214223

224+
225+
/**
226+
* https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/api/account/bind.html
227+
* 将公众号/小程序绑定到开放平台帐号下
228+
*
229+
* @param appId 公众号/小程序的appId
230+
* @param openAppid 开放平台帐号 appid,由创建开发平台帐号接口返回
231+
*/
232+
Boolean bindOpenAccount(String appId, String openAppid) throws WxErrorException;
233+
234+
235+
/**
236+
* https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/api/account/unbind.html
237+
* 将公众号/小程序从开放平台帐号下解绑
238+
*
239+
* @param appId 公众号/小程序的appId
240+
* @param openAppid 开放平台帐号 appid,由创建开发平台帐号接口返回
241+
*/
242+
Boolean unbindOpenAccount(String appId, String openAppid) throws WxErrorException;
243+
244+
245+
/**
246+
* https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/api/account/get.html
247+
* 获取公众号/小程序所绑定的开放平台帐号
248+
*
249+
* @param appId 公众号/小程序的appId
250+
* @return 开放平台帐号 appid,由创建开发平台帐号接口返回
251+
*/
252+
WxOpenGetResult getOpenAccount(String appId) throws WxErrorException;
253+
215254
/**
216255
* https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=21538208049W8uwq&token=&lang=zh_CN
217256
* 第三方平台快速创建小程序.

weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenComponentServiceImpl.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
import me.chanjar.weixin.mp.api.WxMpService;
1313
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
1414
import me.chanjar.weixin.open.api.*;
15-
import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken;
16-
import me.chanjar.weixin.open.bean.WxOpenComponentAccessToken;
17-
import me.chanjar.weixin.open.bean.WxOpenCreateResult;
18-
import me.chanjar.weixin.open.bean.WxOpenMaCodeTemplate;
15+
import me.chanjar.weixin.open.bean.*;
1916
import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizationInfo;
2017
import me.chanjar.weixin.open.bean.message.WxOpenXmlMessage;
2118
import me.chanjar.weixin.open.bean.result.*;
@@ -450,6 +447,41 @@ public WxOpenCreateResult createOpenAccount(String appId) throws WxErrorExceptio
450447
return WxOpenCreateResult.fromJson(json);
451448
}
452449

450+
451+
@Override
452+
public Boolean bindOpenAccount(String appId, String openAppid) throws WxErrorException {
453+
JsonObject param = new JsonObject();
454+
param.addProperty("appid", appId);
455+
param.addProperty("open_appid", openAppid);
456+
457+
String json = post(BIND_OPEN_URL, param.toString(), "access_token");
458+
459+
return WxOpenResult.fromJson(json).isSuccess();
460+
}
461+
462+
463+
@Override
464+
public Boolean unbindOpenAccount(String appId, String openAppid) throws WxErrorException {
465+
JsonObject param = new JsonObject();
466+
param.addProperty("appid", appId);
467+
param.addProperty("open_appid", openAppid);
468+
469+
String json = post(UNBIND_OPEN_URL, param.toString(), "access_token");
470+
471+
return WxOpenResult.fromJson(json).isSuccess();
472+
}
473+
474+
475+
@Override
476+
public WxOpenGetResult getOpenAccount(String appId) throws WxErrorException {
477+
JsonObject param = new JsonObject();
478+
param.addProperty("appid", appId);
479+
480+
String json = post(GET_OPEN_URL, param.toString(), "access_token");
481+
return WxOpenGetResult.fromJson(json);
482+
}
483+
484+
453485
@Override
454486
public WxOpenResult fastRegisterWeapp(String name, String code, String codeType, String legalPersonaWechat, String legalPersonaName, String componentPhone) throws WxErrorException {
455487
JsonObject jsonObject = new JsonObject();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package me.chanjar.weixin.open.bean;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import lombok.EqualsAndHashCode;
6+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
7+
import me.chanjar.weixin.open.bean.result.WxOpenResult;
8+
9+
import java.io.Serializable;
10+
11+
/**
12+
* 文档地址:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/api/account/get.html
13+
*/
14+
@Data
15+
@EqualsAndHashCode(callSuper = false)
16+
public class WxOpenGetResult extends WxOpenResult implements Serializable {
17+
18+
private static final long serialVersionUID = -1196242565823312696L;
19+
20+
@SerializedName("open_appid")
21+
private String openAppid;
22+
23+
public static WxOpenGetResult fromJson(String json) {
24+
return WxGsonBuilder.create().fromJson(json, WxOpenGetResult.class);
25+
}
26+
27+
}

weixin-java-open/src/main/java/me/chanjar/weixin/open/bean/result/WxOpenResult.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package me.chanjar.weixin.open.bean.result;
22

3-
import java.io.Serializable;
4-
5-
import org.apache.commons.lang3.StringUtils;
6-
73
import lombok.Data;
4+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
85
import me.chanjar.weixin.open.util.json.WxOpenGsonBuilder;
6+
import org.apache.commons.lang3.StringUtils;
7+
8+
import java.io.Serializable;
99

1010
/**
1111
* 基础的微信开放平台请求结果.
@@ -26,6 +26,10 @@ public boolean isSuccess() {
2626
return StringUtils.equalsIgnoreCase(errcode, "0");
2727
}
2828

29+
public static WxOpenResult fromJson(String json) {
30+
return WxGsonBuilder.create().fromJson(json, WxOpenResult.class);
31+
}
32+
2933
@Override
3034
public String toString() {
3135
return WxOpenGsonBuilder.create().toJson(this);

0 commit comments

Comments
 (0)