WeixinApiController.java
5.8 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
package com.rnt.controller;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.CallbackIpApi;
import com.jfinal.weixin.sdk.api.CustomServiceApi;
import com.jfinal.weixin.sdk.api.MenuApi;
import com.jfinal.weixin.sdk.api.QrcodeApi;
import com.jfinal.weixin.sdk.api.ShorturlApi;
import com.jfinal.weixin.sdk.api.TemplateMsgApi;
import com.jfinal.weixin.sdk.api.UserApi;
import com.jfinal.weixin.sdk.jfinal.ApiController;
public class WeixinApiController extends ApiController {
/**
* 为WeixinConfig onLineTokenUrl处提供AccessToken
*
* 此处是为了开发测试和生产环境同时使用一套appId时为开发测试环境提供AccessToken
*
* 设计初衷:https://www.oschina.net/question/2702126_2237352
*/
public void getToken() {
String key = getPara("key");
String json = ApiConfigKit.getAccessTokenCache().get(key);
renderText(json);
}
/**
* 获取公众号菜单
*/
public void getMenu() {
ApiResult apiResult = MenuApi.getMenu();
if (apiResult.isSucceed())
renderText(apiResult.getJson());
else
renderText(apiResult.getErrorMsg());
}
/**
* 创建菜单
*/
public void createMenu()
{
String str = "{\n" +
" \"button\": [\n" +
" {\n" +
" \"name\": \"进入理财\",\n" +
" \"url\": \"http://m.bajie8.com/bajie/enter\",\n" +
" \"type\": \"view\"\n" +
" },\n" +
" {\n" +
" \"name\": \"安全保障\",\n" +
" \"key\": \"112\",\n" +
"\t \"type\": \"click\"\n" +
" },\n" +
" {\n" +
"\t \"name\": \"使用帮助\",\n" +
"\t \"url\": \"http://m.bajie8.com/footer/cjwt\",\n" +
"\t \"type\": \"view\"\n" +
" }\n" +
" ]\n" +
"}";
ApiResult apiResult = MenuApi.createMenu(str);
if (apiResult.isSucceed())
renderText(apiResult.getJson());
else
renderText(apiResult.getErrorMsg());
}
/**
* 获取公众号关注用户
*/
public void getFollowers()
{
ApiResult apiResult = UserApi.getFollows();
renderText(apiResult.getJson());
}
/**
* 获取用户信息
*/
public void getUserInfo()
{
ApiResult apiResult = UserApi.getUserInfo("ohbweuNYB_heu_buiBWZtwgi4xzU");
renderText(apiResult.getJson());
}
/**
* 发送模板消息
*/
public void sendMsg()
{
String str = " {\n" +
" \"touser\":\"ohbweuNYB_heu_buiBWZtwgi4xzU\",\n" +
" \"template_id\":\"9SIa8ph1403NEM3qk3z9-go-p4kBMeh-HGepQZVdA7w\",\n" +
" \"url\":\"http://www.sina.com\",\n" +
" \"topcolor\":\"#FF0000\",\n" +
" \"data\":{\n" +
" \"first\": {\n" +
" \"value\":\"恭喜你购买成功!\",\n" +
" \"color\":\"#173177\"\n" +
" },\n" +
" \"keyword1\":{\n" +
" \"value\":\"去哪儿网发的酒店红包(1个)\",\n" +
" \"color\":\"#173177\"\n" +
" },\n" +
" \"keyword2\":{\n" +
" \"value\":\"1元\",\n" +
" \"color\":\"#173177\"\n" +
" },\n" +
" \"remark\":{\n" +
" \"value\":\"欢迎再次购买!\",\n" +
" \"color\":\"#173177\"\n" +
" }\n" +
" }\n" +
" }";
ApiResult apiResult = TemplateMsgApi.send(str);
renderText(apiResult.getJson());
}
/**
* 获取参数二维码
*/
public void getQrcode()
{
String str = "{\"expire_seconds\": 604800, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": 123}}}";
ApiResult apiResult = QrcodeApi.create(str);
renderText(apiResult.getJson());
// String str = "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"123\"}}}";
// ApiResult apiResult = QrcodeApi.create(str);
// renderText(apiResult.getJson());
}
/**
* 长链接转成短链接
*/
public void getShorturl()
{
String str = "{\"action\":\"long2short\"," +
"\"long_url\":\"http://wap.koudaitong.com/v2/showcase/goods?alias=128wi9shh&spm=h56083&redirect_count=1\"}";
ApiResult apiResult = ShorturlApi.getShorturl(str);
renderText(apiResult.getJson());
}
/**
* 获取客服聊天记录
*/
public void getRecord()
{
String str = "{\n" +
" \"endtime\" : 987654321,\n" +
" \"pageindex\" : 1,\n" +
" \"pagesize\" : 10,\n" +
" \"starttime\" : 123456789\n" +
" }";
ApiResult apiResult = CustomServiceApi.getRecord(str);
renderText(apiResult.getJson());
}
/**
* 获取微信服务器IP地址
*/
public void getCallbackIp()
{
ApiResult apiResult = CallbackIpApi.getCallbackIp();
renderText(apiResult.getJson());
}
}