MenuUtil.java
6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.fh.util.weixin;
import net.sf.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import com.fh.entity.weixin.Button;
import com.fh.entity.weixin.CommonButton;
import com.fh.entity.weixin.ComplexButton;
import com.fh.entity.weixin.Menu;
import com.fh.entity.weixin.ViewButton;
import com.fh.util.PageData;
/**
*
* 类描述: 微信自定义菜单
* @author FH 31 35 96790
* 作者单位:
* 联系方式:
* 创建时间:206年11月1日
* @version 1.0
*/
public class MenuUtil {
/**
* 创建自定义菜单(每天限制1000次)
* */
public static int createMenu(Menu menu,String appid, String appsecret){
String jsonMenu=JSONObject.fromObject(menu).toString();
int status=0;
//System.out.println("菜单:"+jsonMenu);
Weixin wx = new Weixin();
String path="https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+wx.getAccess_token(appid, appsecret);
try {
URL url=new URL(path);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setDoOutput(true);
http.setDoInput(true);
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.connect();
OutputStream os = http.getOutputStream();
os.write(jsonMenu.getBytes("UTF-8"));
os.close();
InputStream is = http.getInputStream();
int size = is.available();
byte[] bt = new byte[size];
is.read(bt);
String message=new String(bt,"UTF-8");
JSONObject jsonMsg = JSONObject.fromObject(message);
status = Integer.parseInt(jsonMsg.getString("errcode"));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return status;
}
/**封装菜单数据
* @return
*/
public static Menu getMenu(List<PageData> varList){
PageData M1 = new PageData();
PageData M2 = new PageData();
PageData M3 = new PageData();
List<PageData> listm1 = new ArrayList<PageData>();
List<PageData> listm2 = new ArrayList<PageData>();
List<PageData> listm3 = new ArrayList<PageData>();
String[] arraym1 = {"M11","M12","M13","M14","M15"};
String[] arraym2 = {"M21","M22","M23","M24","M25"};
String[] arraym3 = {"M31","M32","M33","M34","M35"};
for(int i=0;i<varList.size();i++){
PageData pd = varList.get(i);
if("M1".equals(pd.getString("XID"))){
M1 = pd;
}else if("M2".equals(pd.getString("XID"))){
M2 = pd;
}else if("M3".equals(pd.getString("XID"))){
M3 = pd;
}
}
for(int n=0;n < arraym1.length;n++){
for(int i=0;i<varList.size();i++){
PageData pd = varList.get(i);
if(arraym1[n].equals(pd.getString("XID"))){
if(null == pd.getString("TITLE") || "".equals(pd.getString("TITLE").trim())){
break;
}
listm1.add(pd);
}
}
}
for(int n=0;n < arraym2.length;n++){
for(int i=0;i<varList.size();i++){
PageData pd = varList.get(i);
if(arraym2[n].equals(pd.getString("XID"))){
if(null == pd.getString("TITLE") || "".equals(pd.getString("TITLE").trim())){
break;
}
listm2.add(pd);
}
}
}
for(int n=0;n < arraym3.length;n++){
for(int i=0;i<varList.size();i++){
PageData pd = varList.get(i);
if(arraym3[n].equals(pd.getString("XID"))){
if(null == pd.getString("TITLE") || "".equals(pd.getString("TITLE").trim())){
break;
}
listm3.add(pd);
}
}
}
ComplexButton cx1 = new ComplexButton();
if(null != M1.getString("TITLE") && !"".equals(M1.getString("TITLE").trim())){
Button[] arrayB = new Button[listm1.size()];
for(int i=0;i<listm1.size();i++){
PageData pd = listm1.get(i);
if("click".equals(pd.getString("TYPE"))){
CommonButton cb = new CommonButton();
cb.setKey(null == pd.getString("CONTENT")?"":pd.getString("CONTENT").trim());
cb.setName(pd.getString("TITLE").trim());
cb.setType("click");
arrayB[i] = cb;
}else{
ViewButton vb = new ViewButton();
vb.setUrl(null == pd.getString("CONTENT")?"":pd.getString("CONTENT").trim());
vb.setName(pd.getString("TITLE").trim());
vb.setType("view");
arrayB[i] = vb;
}
}
cx1.setName(M1.getString("TITLE").trim());
cx1.setSub_button(arrayB);
}
ComplexButton cx2 = new ComplexButton();
if(null != M2.getString("TITLE") && !"".equals(M2.getString("TITLE").trim())){
Button[] arrayB = new Button[listm2.size()];
for(int i=0;i<listm2.size();i++){
PageData pd = listm2.get(i);
if("click".equals(pd.getString("TYPE"))){
CommonButton cb = new CommonButton();
cb.setKey(null == pd.getString("CONTENT")?"":pd.getString("CONTENT").trim());
cb.setName(pd.getString("TITLE").trim());
cb.setType("click");
arrayB[i] = cb;
}else{
ViewButton vb = new ViewButton();
vb.setUrl(null == pd.getString("CONTENT")?"":pd.getString("CONTENT").trim());
vb.setName(pd.getString("TITLE").trim());
vb.setType("view");
arrayB[i] = vb;
}
}
cx2.setName(M2.getString("TITLE").trim());
cx2.setSub_button(arrayB);
}
ComplexButton cx3 = new ComplexButton();
if(null != M3.getString("TITLE") && !"".equals(M3.getString("TITLE").trim())){
Button[] arrayB = new Button[listm3.size()];
for(int i=0;i<listm3.size();i++){
PageData pd = listm3.get(i);
if("click".equals(pd.getString("TYPE"))){
CommonButton cb = new CommonButton();
cb.setKey(null == pd.getString("CONTENT")?"":pd.getString("CONTENT").trim());
cb.setName(pd.getString("TITLE").trim());
cb.setType("click");
arrayB[i] = cb;
}else{
ViewButton vb = new ViewButton();
vb.setUrl(null == pd.getString("CONTENT")?"":pd.getString("CONTENT").trim());
vb.setName(pd.getString("TITLE").trim());
vb.setType("view");
arrayB[i] = vb;
}
}
cx3.setName(M3.getString("TITLE").trim());
cx3.setSub_button(arrayB);
}
Menu menu=new Menu();
menu.setButton(new ComplexButton[]{cx1,cx2,cx3});
return menu;
}
}