View Javadoc
1   package com.foxinmy.weixin4j.model;
2   
3   import java.io.Serializable;
4   import java.util.ArrayList;
5   import java.util.Arrays;
6   import java.util.List;
7   
8   import com.alibaba.fastjson.annotation.JSONField;
9   import com.foxinmy.weixin4j.type.ButtonType;
10  
11  /**
12   * 菜单按钮
13   * <p>
14   * 目前自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单,一级菜单最多4个汉字,二级菜单最多7个汉字,多出来的部分将会以"..."代替
15   * 请注意,创建自定义菜单后,由于微信客户端缓存,需要24小时微信客户端才会展现出来,建议测试时可以尝试取消关注公众账号后再次关注,则可以看到创建后的效果
16   * </p>
17   *
18   * @className Button
19   * @author jinyu(foxinmy@gmail.com)
20   * @date 2014年4月5日
21   * @since JDK 1.6
22   */
23  public class Button implements Serializable {
24  
25      private static final long serialVersionUID = -6422234732203854866L;
26  
27      /**
28       * 菜单标题,不超过16个字节,子菜单不超过40个字节
29       */
30      private String name;
31      /**
32       * 菜单类型 </br>
33       * <font color="red"> 公众平台官网上能够设置的菜单类型有view、text、img、photo、video、voice
34       * </font>
35       *
36       * @see com.foxinmy.weixin4j.type.ButtonType
37       */
38      private String type;
39      /**
40       * 菜单KEY值,根据type的类型而定
41       * </p>
42       * 通过公众平台设置的自定义菜单:</br>
43       * <li>text:保存文字;
44       * <li>img、voice:保存媒体ID;
45       * <li>video:保存视频URL;
46       * <li>news:保存图文消息媒体ID
47       * <li>view:保存链接URL;
48       * <p>
49       * 使用API设置的自定义菜单:
50       * </p>
51       * <li>click、scancode_push、scancode_waitmsg、pic_sysphoto、pic_photo_or_album、
52       * pic_weixin、location_select:保存key;
53       * <li>view:保存链接URL;
54       * <li>media_id、view_limited:保存媒体ID
55       */
56      private String content;
57      /**
58       * 扩展属性,比如在公众平台设置菜单时的图文列表
59       */
60      @JSONField(serialize = false, deserialize = false)
61      private Object extra;
62      /**
63       * miniprogram类型必须 小程序的appid(仅认证公众号可配置)
64       */
65      private String appid;
66      /**
67       * miniprogram类型必须 小程序的页面路径
68       */
69      private String pagepath;
70  
71      /**
72       * 二级菜单数组,个数应为1~5个
73       */
74      @JSONField(name = "sub_button")
75      private List<Button> subs;
76  
77      protected Button() {
78          this.subs = new ArrayList<Button>();
79      }
80  
81      /**
82       * 创建一个具有子菜单的菜单
83       *
84       * @param name
85       *            菜单名
86       * @param subButtons
87       *            二级菜单列表
88       */
89      public Button(String name, Button... subButtons) {
90          this.name = name;
91          this.subs = new ArrayList<Button>(Arrays.asList(subButtons));
92      }
93  
94      /**
95       * 创建一个普通菜单
96       *
97       * @param name
98       *            菜单名
99       * @param content
100      *            菜单内容
101      * @param type
102      *            菜单类型
103      */
104     public Button(String name, String content, ButtonType type) {
105         this.name = name;
106         this.content = content;
107         this.type = type.name();
108         this.subs = new ArrayList<Button>();
109     }
110 
111     /**
112      * 小程序菜单
113      *
114      * @param name
115      *            菜单名
116      * @param url
117      *            小程序的url页面
118      * @param appid
119      *            小程序的appid
120      * @param pagepath
121      *            小程序员的页面路径
122      */
123     public Button(String name, String url, String appid, String pagepath) {
124         this.name = name;
125         this.content = url;
126         this.appid = appid;
127         this.pagepath = pagepath;
128         this.type = ButtonType.miniprogram.name();
129         this.subs = new ArrayList<Button>();
130     }
131 
132     public String getName() {
133         return name;
134     }
135 
136     public void setName(String name) {
137         this.name = name;
138     }
139 
140     public String getType() {
141         return type;
142     }
143 
144     public void setType(String type) {
145         this.type = type;
146     }
147 
148     public void setType(ButtonType type) {
149         this.type = type.name();
150     }
151 
152     public String getContent() {
153         return content;
154     }
155 
156     public void setContent(String content) {
157         this.content = content;
158     }
159 
160     public Object getExtra() {
161         return extra;
162     }
163 
164     /**
165      * 扩展只读属性,设置无效
166      *
167      * @param extra
168      */
169     public void setExtra(Object extra) {
170         this.extra = extra;
171     }
172 
173     public String getAppid() {
174         return appid;
175     }
176 
177     public void setAppid(String appid) {
178         this.appid = appid;
179     }
180 
181     public String getPagepath() {
182         return pagepath;
183     }
184 
185     public void setPagepath(String pagepath) {
186         this.pagepath = pagepath;
187     }
188 
189     public List<Button> getSubs() {
190         return subs;
191     }
192 
193     public void setSubs(List<Button> subs) {
194         this.subs = subs;
195     }
196 
197     public Button pushSub(Button btn) {
198         this.subs.add(btn);
199         return this;
200     }
201 
202     @Override
203     public String toString() {
204         return "Button [name=" + name + ", type=" + type + ", content=" + content + ", extra=" + extra + ", appid="
205                 + appid + ", pagepath=" + pagepath + ", subs=" + subs + "]";
206     }
207 }