Skip to content

Commit 95821da

Browse files
committed
实现获取自定义菜单配置的接口 for #70
1 parent fd4505d commit 95821da

File tree

6 files changed

+389
-24
lines changed

6 files changed

+389
-24
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/menu/WxMenu.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package me.chanjar.weixin.common.bean.menu;
22

3+
import me.chanjar.weixin.common.util.ToStringUtils;
4+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
5+
36
import java.io.InputStream;
47
import java.io.InputStreamReader;
58
import java.io.Serializable;
69
import java.nio.charset.StandardCharsets;
710
import java.util.ArrayList;
811
import java.util.List;
912

10-
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
11-
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
12-
1313
/**
14-
* 企业号菜单
14+
* 菜单(公众号和企业号共用的)
1515
*
1616
* @author Daniel Qian
1717
*/
@@ -61,9 +61,7 @@ public String toJson() {
6161

6262
@Override
6363
public String toString() {
64-
return "WxMenu{" +
65-
"buttons=" + this.buttons +
66-
'}';
64+
return ToStringUtils.toSimpleString(this);
6765
}
6866

6967
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMenuService.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import me.chanjar.weixin.common.bean.menu.WxMenu;
44
import me.chanjar.weixin.common.exception.WxErrorException;
5+
import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult;
56

67
/**
78
* 菜单相关操作接口
@@ -56,4 +57,20 @@ public interface WxMpMenuService {
5657
*/
5758
WxMenu menuTryMatch(String userid) throws WxErrorException;
5859

60+
/**
61+
* <pre>
62+
* 获取自定义菜单配置接口
63+
* 本接口将会提供公众号当前使用的自定义菜单的配置,如果公众号是通过API调用设置的菜单,则返回菜单的开发配置,而如果公众号是在公众平台官网通过网站功能发布菜单,则本接口返回运营者设置的菜单配置。
64+
请注意:
65+
1、第三方平台开发者可以通过本接口,在旗下公众号将业务授权给你后,立即通过本接口检测公众号的自定义菜单配置,并通过接口再次给公众号设置好自动回复规则,以提升公众号运营者的业务体验。
66+
2、本接口与自定义菜单查询接口的不同之处在于,本接口无论公众号的接口是如何设置的,都能查询到接口,而自定义菜单查询接口则仅能查询到使用API设置的菜单配置。
67+
3、认证/未认证的服务号/订阅号,以及接口测试号,均拥有该接口权限。
68+
4、从第三方平台的公众号登录授权机制上来说,该接口从属于消息与菜单权限集。
69+
5、本接口中返回的图片/语音/视频为临时素材(临时素材每次获取都不同,3天内有效,通过素材管理-获取临时素材接口来获取这些素材),本接口返回的图文消息为永久素材素材(通过素材管理-获取永久素材接口来获取这些素材)。
70+
* 接口调用请求说明:
71+
http请求方式: GET(请使用https协议)
72+
https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=ACCESS_TOKEN
73+
*</pre>
74+
*/
75+
WxMpGetSelfMenuInfoResult getSelfMenuInfo() throws WxErrorException;
5976
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMenuServiceImpl.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3-
import org.slf4j.Logger;
4-
import org.slf4j.LoggerFactory;
5-
63
import me.chanjar.weixin.common.bean.menu.WxMenu;
74
import me.chanjar.weixin.common.exception.WxErrorException;
85
import me.chanjar.weixin.mp.api.WxMpMenuService;
96
import me.chanjar.weixin.mp.api.WxMpService;
7+
import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult;
8+
import me.chanjar.weixin.mp.bean.menu.WxMpSelfMenuInfo;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
1011

1112
/**
1213
* Created by Binary Wang on 2016/7/21.
1314
*/
1415
public class WxMpMenuServiceImpl implements WxMpMenuService {
1516
private static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/menu";
1617
private static Logger log = LoggerFactory
17-
.getLogger(WxMpMenuServiceImpl.class);
18+
.getLogger(WxMpMenuServiceImpl.class);
1819

1920
private WxMpService wxMpService;
2021

@@ -74,10 +75,17 @@ public WxMenu menuTryMatch(String userid) throws WxErrorException {
7475
} catch (WxErrorException e) {
7576
// 46003 不存在的菜单数据 46002 不存在的菜单版本
7677
if (e.getError().getErrorCode() == 46003
77-
|| e.getError().getErrorCode() == 46002) {
78+
|| e.getError().getErrorCode() == 46002) {
7879
return null;
7980
}
8081
throw e;
8182
}
8283
}
84+
85+
@Override
86+
public WxMpGetSelfMenuInfoResult getSelfMenuInfo() throws WxErrorException {
87+
String url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info";
88+
String resultContent = this.wxMpService.get(url, null);
89+
return WxMpGetSelfMenuInfoResult.fromJson(resultContent);
90+
}
8391
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package me.chanjar.weixin.mp.bean.menu;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import me.chanjar.weixin.common.util.ToStringUtils;
5+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
6+
7+
/**
8+
* <pre>
9+
* Created by Binary Wang on 2016-11-25.
10+
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
11+
* </pre>
12+
*/
13+
public class WxMpGetSelfMenuInfoResult {
14+
@SerializedName("selfmenu_info")
15+
private WxMpSelfMenuInfo selfMenuInfo;
16+
17+
@SerializedName("is_menu_open")
18+
private Integer isMenuOpen;
19+
20+
public static WxMpGetSelfMenuInfoResult fromJson(String json) {
21+
return WxGsonBuilder.create().fromJson(json, WxMpGetSelfMenuInfoResult.class);
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return ToStringUtils.toSimpleString(this);
27+
}
28+
29+
public WxMpSelfMenuInfo getSelfMenuInfo() {
30+
return selfMenuInfo;
31+
}
32+
33+
public void setSelfMenuInfo(WxMpSelfMenuInfo selfMenuInfo) {
34+
this.selfMenuInfo = selfMenuInfo;
35+
}
36+
37+
public Integer getIsMenuOpen() {
38+
return isMenuOpen;
39+
}
40+
41+
public void setIsMenuOpen(Integer isMenuOpen) {
42+
this.isMenuOpen = isMenuOpen;
43+
}
44+
}

0 commit comments

Comments
 (0)