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
23
24
25
26
27
28
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
42
43
44
45
46
47
48
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
88
89
90
91
92
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
118
119
120
121
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 }