MenuApi.java
5.27 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
/**
* Copyright (c) 2011-2014, James Zhan 詹波 (jfinal@126.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
*/
package com.jfinal.weixin.sdk.api;
import com.jfinal.kit.PropKit;
import com.jfinal.weixin.sdk.utils.HttpUtils;
import com.jfinal.weixin.sdk.utils.JsonUtils;
import java.util.HashMap;
import java.util.Map;
/**
* menu api
*/
public class MenuApi {
private static String getMenu = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=";
private static String createMenu = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
/**
* 查询自定义菜单
* @return {ApiResult}
*/
public static ApiResult getMenu() {
String jsonResult = HttpUtils.get(getMenu + AccessTokenApi.getAccessTokenStr());
return new ApiResult(jsonResult);
}
/**
* 创建自定义菜单
* @param jsonStr json字符串
* @return {ApiResult}
*/
public static ApiResult createMenu(String jsonStr) {
String jsonResult = HttpUtils.post(createMenu + AccessTokenApi.getAccessTokenStr(), jsonStr);
return new ApiResult(jsonResult);
}
private static String deleteMenuUrl = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=";
/**
* 自定义菜单删除接口
* @return ApiResult
*/
public static ApiResult deleteMenu() {
String jsonResult = HttpUtils.get(deleteMenuUrl + AccessTokenApi.getAccessTokenStr());
return new ApiResult(jsonResult);
}
private static String addConditionalUrl = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=";
/**
* 创建个性化菜单
* @param jsonStr json字符串
* @return {ApiResult}
*/
public static ApiResult addConditional(String jsonStr) {
String jsonResult = HttpUtils.post(addConditionalUrl + AccessTokenApi.getAccessTokenStr(), jsonStr);
return new ApiResult(jsonResult);
}
private static String delConditionalUrl = "https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token=";
/**
* 删除个性化菜单
* @param menuid menuid为菜单id,可以通过自定义菜单查询接口获取。
* @return ApiResult
*/
public static ApiResult delConditional(String menuid) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("menuid", menuid);
String url = delConditionalUrl + AccessTokenApi.getAccessTokenStr();
String jsonResult = HttpUtils.post(url, JsonUtils.toJson(params));
return new ApiResult(jsonResult);
}
private static String tryMatchUrl = "https://api.weixin.qq.com/cgi-bin/menu/trymatch?access_token=";
/**
* 测试个性化菜单匹配结果
* @param userId user_id可以是粉丝的OpenID,也可以是粉丝的微信号。
* @return ApiResult
*/
public static ApiResult tryMatch(String userId) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("user_id", userId);
String url = tryMatchUrl + AccessTokenApi.getAccessTokenStr();
String jsonResult = HttpUtils.post(url, JsonUtils.toJson(params));
return new ApiResult(jsonResult);
}
private static String getCurrentSelfMenuInfoUrl = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=";
/**
* 获取自定义菜单配置接口
* @return {ApiResult}
*/
public static ApiResult getCurrentSelfMenuInfo() {
String jsonResult = HttpUtils.get(getCurrentSelfMenuInfoUrl + AccessTokenApi.getAccessTokenStr());
return new ApiResult(jsonResult);
}
public static void main(String[] args) {
PropKit.use("a_little_config.txt");
ApiConfig apiConfig = new ApiConfig();
apiConfig.setAppId(PropKit.get("appId"));
apiConfig.setAppSecret(PropKit.get("appSecret"));
ApiConfigKit.putApiConfig(apiConfig);
//String accessToken = AccessTokenApi.getAccessTokenStr();
//System.out.println(accessToken);
String json ="{"+
"\"button\":["+
"{"+
"\"type\":\"view\","+
"\"name\":\"支付\","+
"\"url\":\"http://atao.s1.natapp.cc/pay/selectCarView\""+
"},"+
"{"+
"\"type\":\"view\","+
"\"name\":\"月卡\","+
"\"url\":\"http://atao.s1.natapp.cc/park/parkListView\""+
"},"+
"{"+
"\"name\":\"关于我们\","+
"\"sub_button\":["+
"{"+
"\"type\":\"click\","+
"\"name\":\"最新咨询\","+
"\"key\":\"V1001_GOOD\""+
"},"+
"{"+
"\"type\":\"view\","+
"\"name\":\"欢迎下载\","+
"\"url\":\"http://fir.im/rntaz\""+
"},"+
"{"+
"\"type\":\"click\","+
"\"name\":\"热线电话\","+
"\"key\":\"V1002_GOOD\""+
"},{"+
"\"type\":\"click\","+
"\"name\":\"公司简介\","+
"\"key\":\"V1003_GOOD\""+
"}]"+
"}]"+
"}";
ApiResult result = MenuApi.deleteMenu();
System.out.println(result);
ApiResult createResult = MenuApi.createMenu(json);
System.out.println(createResult);
}
}