1
1
package com .github .binarywang .demo .wx .mp .controller ;
2
2
3
- import java .net .MalformedURLException ;
4
- import java .net .URL ;
5
- import javax .servlet .http .HttpServletRequest ;
6
-
7
- import org .springframework .web .bind .annotation .GetMapping ;
8
- import org .springframework .web .bind .annotation .PathVariable ;
9
- import org .springframework .web .bind .annotation .PostMapping ;
10
- import org .springframework .web .bind .annotation .RequestBody ;
11
- import org .springframework .web .bind .annotation .RequestMapping ;
12
- import org .springframework .web .bind .annotation .RestController ;
13
- import org .springframework .web .context .request .RequestContextHolder ;
14
- import org .springframework .web .context .request .ServletRequestAttributes ;
15
-
16
- import com .github .binarywang .demo .wx .mp .config .WxMpConfiguration ;
17
3
import me .chanjar .weixin .common .api .WxConsts ;
18
4
import me .chanjar .weixin .common .bean .menu .WxMenu ;
19
5
import me .chanjar .weixin .common .bean .menu .WxMenuButton ;
20
6
import me .chanjar .weixin .common .error .WxErrorException ;
7
+ import me .chanjar .weixin .mp .api .WxMpService ;
21
8
import me .chanjar .weixin .mp .bean .menu .WxMpGetSelfMenuInfoResult ;
22
9
import me .chanjar .weixin .mp .bean .menu .WxMpMenu ;
10
+ import org .springframework .beans .factory .annotation .Autowired ;
11
+ import org .springframework .web .bind .annotation .*;
12
+ import org .springframework .web .context .request .RequestContextHolder ;
13
+ import org .springframework .web .context .request .ServletRequestAttributes ;
14
+
15
+ import javax .servlet .http .HttpServletRequest ;
16
+ import java .net .MalformedURLException ;
17
+ import java .net .URL ;
23
18
24
19
import static me .chanjar .weixin .common .api .WxConsts .MenuButtonType ;
25
20
29
24
@ RestController
30
25
@ RequestMapping ("/wx/menu/{appid}" )
31
26
public class WxMenuController {
27
+ private WxMpService wxService ;
28
+
29
+ @ Autowired
30
+ public WxMenuController (WxMpService wxService ) {
31
+ this .wxService = wxService ;
32
+ }
32
33
33
34
/**
34
35
* <pre>
@@ -42,7 +43,7 @@ public class WxMenuController {
42
43
*/
43
44
@ PostMapping ("/create" )
44
45
public String menuCreate (@ PathVariable String appid , @ RequestBody WxMenu menu ) throws WxErrorException {
45
- return WxMpConfiguration . getMpServices (). get (appid ).getMenuService ().menuCreate (menu );
46
+ return this . wxService . switchover1 (appid ).getMenuService ().menuCreate (menu );
46
47
}
47
48
48
49
@ GetMapping ("/create" )
@@ -91,10 +92,9 @@ public String menuCreateSample(@PathVariable String appid) throws WxErrorExcepti
91
92
if (servletRequestAttributes != null ) {
92
93
HttpServletRequest request = servletRequestAttributes .getRequest ();
93
94
URL requestURL = new URL (request .getRequestURL ().toString ());
94
- String url = WxMpConfiguration .getMpServices ().get (appid )
95
- .oauth2buildAuthorizationUrl (
96
- String .format ("%s://%s/wx/redirect/%s/greet" , requestURL .getProtocol (), requestURL .getHost (), appid ),
97
- WxConsts .OAuth2Scope .SNSAPI_USERINFO , null );
95
+ String url = this .wxService .switchover1 (appid ).oauth2buildAuthorizationUrl (
96
+ String .format ("%s://%s/wx/redirect/%s/greet" , requestURL .getProtocol (), requestURL .getHost (), appid ),
97
+ WxConsts .OAuth2Scope .SNSAPI_USERINFO , null );
98
98
button34 .setUrl (url );
99
99
}
100
100
@@ -103,7 +103,8 @@ public String menuCreateSample(@PathVariable String appid) throws WxErrorExcepti
103
103
button3 .getSubButtons ().add (button33 );
104
104
button3 .getSubButtons ().add (button34 );
105
105
106
- return WxMpConfiguration .getMpServices ().get (appid ).getMenuService ().menuCreate (menu );
106
+ this .wxService .switchover (appid );
107
+ return this .wxService .getMenuService ().menuCreate (menu );
107
108
}
108
109
109
110
/**
@@ -114,12 +115,11 @@ public String menuCreateSample(@PathVariable String appid) throws WxErrorExcepti
114
115
* 详情请见:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296&token=&lang=zh_CN
115
116
* </pre>
116
117
*
117
- * @param json
118
118
* @return 如果是个性化菜单,则返回menuid,否则返回null
119
119
*/
120
120
@ PostMapping ("/createByJson" )
121
121
public String menuCreate (@ PathVariable String appid , @ RequestBody String json ) throws WxErrorException {
122
- return WxMpConfiguration . getMpServices (). get (appid ).getMenuService ().menuCreate (json );
122
+ return this . wxService . switchover1 (appid ).getMenuService ().menuCreate (json );
123
123
}
124
124
125
125
/**
@@ -130,7 +130,7 @@ public String menuCreate(@PathVariable String appid, @RequestBody String json) t
130
130
*/
131
131
@ GetMapping ("/delete" )
132
132
public void menuDelete (@ PathVariable String appid ) throws WxErrorException {
133
- WxMpConfiguration . getMpServices (). get (appid ).getMenuService ().menuDelete ();
133
+ this . wxService . switchover1 (appid ).getMenuService ().menuDelete ();
134
134
}
135
135
136
136
/**
@@ -143,7 +143,7 @@ public void menuDelete(@PathVariable String appid) throws WxErrorException {
143
143
*/
144
144
@ GetMapping ("/delete/{menuId}" )
145
145
public void menuDelete (@ PathVariable String appid , @ PathVariable String menuId ) throws WxErrorException {
146
- WxMpConfiguration . getMpServices (). get (appid ).getMenuService ().menuDelete (menuId );
146
+ this . wxService . switchover1 (appid ).getMenuService ().menuDelete (menuId );
147
147
}
148
148
149
149
/**
@@ -154,7 +154,7 @@ public void menuDelete(@PathVariable String appid, @PathVariable String menuId)
154
154
*/
155
155
@ GetMapping ("/get" )
156
156
public WxMpMenu menuGet (@ PathVariable String appid ) throws WxErrorException {
157
- return WxMpConfiguration . getMpServices (). get (appid ).getMenuService ().menuGet ();
157
+ return this . wxService . switchover1 (appid ).getMenuService ().menuGet ();
158
158
}
159
159
160
160
/**
@@ -167,7 +167,7 @@ public WxMpMenu menuGet(@PathVariable String appid) throws WxErrorException {
167
167
*/
168
168
@ GetMapping ("/menuTryMatch/{userid}" )
169
169
public WxMenu menuTryMatch (@ PathVariable String appid , @ PathVariable String userid ) throws WxErrorException {
170
- return WxMpConfiguration . getMpServices (). get (appid ).getMenuService ().menuTryMatch (userid );
170
+ return this . wxService . switchover1 (appid ).getMenuService ().menuTryMatch (userid );
171
171
}
172
172
173
173
/**
@@ -187,6 +187,6 @@ public WxMenu menuTryMatch(@PathVariable String appid, @PathVariable String user
187
187
*/
188
188
@ GetMapping ("/getSelfMenuInfo" )
189
189
public WxMpGetSelfMenuInfoResult getSelfMenuInfo (@ PathVariable String appid ) throws WxErrorException {
190
- return WxMpConfiguration . getMpServices (). get (appid ).getMenuService ().getSelfMenuInfo ();
190
+ return this . wxService . switchover1 (appid ).getMenuService ().getSelfMenuInfo ();
191
191
}
192
192
}
0 commit comments