View Javadoc
1   package com.foxinmy.weixin4j.qy.api;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import com.alibaba.fastjson.JSON;
7   import com.alibaba.fastjson.JSONArray;
8   import com.alibaba.fastjson.JSONObject;
9   import com.alibaba.fastjson.parser.deserializer.ExtraProcessor;
10  import com.alibaba.fastjson.parser.deserializer.ParseProcess;
11  import com.alibaba.fastjson.serializer.NameFilter;
12  import com.foxinmy.weixin4j.exception.WeixinException;
13  import com.foxinmy.weixin4j.http.weixin.ApiResult;
14  import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
15  import com.foxinmy.weixin4j.model.Button;
16  import com.foxinmy.weixin4j.model.Token;
17  import com.foxinmy.weixin4j.token.TokenManager;
18  import com.foxinmy.weixin4j.type.ButtonType;
19  import com.foxinmy.weixin4j.util.StringUtil;
20  
21  /**
22   * 菜单相关API
23   *
24   * @className MenuApi
25   * @author jinyu(foxinmy@gmail.com)
26   * @date 2014年9月25日
27   * @since JDK 1.6
28   * @see com.foxinmy.weixin4j.model.Button
29   */
30  public class MenuApi extends QyApi {
31  
32  	private final TokenManager tokenManager;
33  
34  	public MenuApi(TokenManager tokenManager) {
35  		this.tokenManager = tokenManager;
36  	}
37  
38  	/**
39  	 * 自定义菜单(管理员须拥有应用的管理权限 并且应用必须设置在回调模式)
40  	 *
41  	 * @param agentid
42  	 *            应用ID
43  	 *
44  	 * @param buttons
45  	 *            菜单列表
46  	 * @throws WeixinException
47  	 * @see <a href= "https://work.weixin.qq.com/api/doc#10786"> 创建自定义菜单</a>
48  	 * @see com.foxinmy.weixin4j.model.Button
49  	 */
50  	public ApiResult createMenu(int agentid, List<Button> buttons)
51  			throws WeixinException {
52  		String menu_create_uri = getRequestUri("menu_create_uri");
53  		Token token = tokenManager.getCache();
54  		JSONObject obj = new JSONObject();
55  		obj.put("button", buttons);
56  		WeixinResponse response = weixinExecutor
57  				.post(String.format(menu_create_uri, token.getAccessToken(),
58  						agentid), JSON.toJSONString(obj, new NameFilter() {
59  					@Override
60  					public String process(Object object, String name,
61  							Object value) {
62  						if (object instanceof Button
63  								&& name.equals("content")
64  								&& StringUtil.isNotBlank(((Button) object)
65  										.getType())) {
66  							ButtonType buttonType = ButtonType
67  									.valueOf(((Button) object).getType());
68  							if (ButtonType.view == buttonType) {
69  								return "url";
70  							} else if (ButtonType.media_id == buttonType
71  									|| ButtonType.view_limited == buttonType) {
72  								return "media_id";
73  							} else {
74  								return "key";
75  							}
76  						}
77  						return name;
78  					}
79  				}));
80  
81  		return response.getAsResult();
82  	}
83  
84  	/**
85  	 * 查询菜单(管理员须拥有应用的管理权限 并且应用必须设置在回调模式。)
86  	 *
87  	 * @param agentid
88  	 *            应用ID
89  	 * @return 菜单集合
90  	 * @throws WeixinException
91  	 * @see <a href= "https://work.weixin.qq.com/api/doc#10787"> 查询菜单</a>
92  	 * @see com.foxinmy.weixin4j.model.Button
93  	 */
94  	public List<Button> getMenu(int agentid) throws WeixinException {
95  		String menu_get_uri = getRequestUri("menu_get_uri");
96  		Token token = tokenManager.getCache();
97  		WeixinResponse response = weixinExecutor.get(String.format(
98  				menu_get_uri, token.getAccessToken(), agentid));
99  		JSONArray buttons = response.getAsJson().getJSONArray("button");
100 		List<Button> buttonList = new ArrayList<Button>(buttons.size());
101 		ParseProcess processor = new ExtraProcessor() {
102 			@Override
103 			public void processExtra(Object object, String key, Object value) {
104 				((Button) object).setContent(String.valueOf(value));
105 			}
106 		};
107 		for (int i = 0; i < buttons.size(); i++) {
108 			buttonList.add(JSON.parseObject(buttons.getString(i), Button.class,
109 					processor));
110 		}
111 		return buttonList;
112 	}
113 
114 	/**
115 	 * 删除菜单(管理员须拥有应用的管理权限 并且应用必须设置在回调模式)
116 	 *
117 	 * @param agentid
118 	 *            应用ID
119 	 * @throws WeixinException
120 	 * @see <a href= "https://work.weixin.qq.com/api/doc#10788"> 删除菜单</a>
121 	 * @return 处理结果
122 	 */
123 	public ApiResult deleteMenu(int agentid) throws WeixinException {
124 		String menu_delete_uri = getRequestUri("menu_delete_uri");
125 		Token token = tokenManager.getCache();
126 		WeixinResponse response = weixinExecutor.get(String.format(
127 				menu_delete_uri, token.getAccessToken(), agentid));
128 
129 		return response.getAsResult();
130 	}
131 }