Skip to content

Commit dadcc25

Browse files
committed
简化代码
1 parent ebf30b7 commit dadcc25

File tree

3 files changed

+18
-29
lines changed

3 files changed

+18
-29
lines changed

src/main/java/com/github/binarywang/demo/wx/mp/controller/WxMenuController.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.github.binarywang.demo.wx.mp.controller;
22

3+
import lombok.AllArgsConstructor;
34
import me.chanjar.weixin.common.api.WxConsts;
45
import me.chanjar.weixin.common.bean.menu.WxMenu;
56
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
67
import me.chanjar.weixin.common.error.WxErrorException;
78
import me.chanjar.weixin.mp.api.WxMpService;
89
import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult;
910
import me.chanjar.weixin.mp.bean.menu.WxMpMenu;
10-
import org.springframework.beans.factory.annotation.Autowired;
1111
import org.springframework.web.bind.annotation.*;
1212
import org.springframework.web.context.request.RequestContextHolder;
1313
import org.springframework.web.context.request.ServletRequestAttributes;
@@ -21,15 +21,11 @@
2121
/**
2222
* @author Binary Wang(https://github.com/binarywang)
2323
*/
24+
@AllArgsConstructor
2425
@RestController
2526
@RequestMapping("/wx/menu/{appid}")
2627
public class WxMenuController {
27-
private WxMpService wxService;
28-
29-
@Autowired
30-
public WxMenuController(WxMpService wxService) {
31-
this.wxService = wxService;
32-
}
28+
private final WxMpService wxService;
3329

3430
/**
3531
* <pre>

src/main/java/com/github/binarywang/demo/wx/mp/controller/WxPortalController.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.github.binarywang.demo.wx.mp.controller;
22

3+
import lombok.AllArgsConstructor;
4+
import lombok.extern.log4j.Log4j;
5+
import lombok.extern.slf4j.Slf4j;
36
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
47
import org.apache.commons.lang3.StringUtils;
58
import org.slf4j.Logger;
@@ -20,20 +23,13 @@
2023
/**
2124
* @author Binary Wang(https://github.com/binarywang)
2225
*/
26+
@Slf4j
27+
@AllArgsConstructor
2328
@RestController
2429
@RequestMapping("/wx/portal/{appid}")
2530
public class WxPortalController {
26-
private final Logger logger = LoggerFactory.getLogger(this.getClass());
27-
28-
private WxMpService wxService;
29-
30-
private WxMpMessageRouter messageRouter;
31-
32-
@Autowired
33-
public WxPortalController(WxMpService wxService, WxMpMessageRouter messageRouter) {
34-
this.wxService = wxService;
35-
this.messageRouter = messageRouter;
36-
}
31+
private final WxMpService wxService;
32+
private final WxMpMessageRouter messageRouter;
3733

3834
@GetMapping(produces = "text/plain;charset=utf-8")
3935
public String authGet(@PathVariable String appid,
@@ -42,7 +38,7 @@ public String authGet(@PathVariable String appid,
4238
@RequestParam(name = "nonce", required = false) String nonce,
4339
@RequestParam(name = "echostr", required = false) String echostr) {
4440

45-
this.logger.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature,
41+
log.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature,
4642
timestamp, nonce, echostr);
4743
if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
4844
throw new IllegalArgumentException("请求参数非法,请核实!");
@@ -68,7 +64,7 @@ public String post(@PathVariable String appid,
6864
@RequestParam("openid") String openid,
6965
@RequestParam(name = "encrypt_type", required = false) String encType,
7066
@RequestParam(name = "msg_signature", required = false) String msgSignature) {
71-
this.logger.info("\n接收微信请求:[openid=[{}], [signature=[{}], encType=[{}], msgSignature=[{}],"
67+
log.info("\n接收微信请求:[openid=[{}], [signature=[{}], encType=[{}], msgSignature=[{}],"
7268
+ " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
7369
openid, signature, encType, msgSignature, timestamp, nonce, requestBody);
7470

@@ -94,7 +90,7 @@ public String post(@PathVariable String appid,
9490
// aes加密的消息
9591
WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody, wxService.getWxMpConfigStorage(),
9692
timestamp, nonce, msgSignature);
97-
this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
93+
log.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
9894
WxMpXmlOutMessage outMessage = this.route(inMessage);
9995
if (outMessage == null) {
10096
return "";
@@ -103,15 +99,15 @@ public String post(@PathVariable String appid,
10399
out = outMessage.toEncryptedXml(wxService.getWxMpConfigStorage());
104100
}
105101

106-
this.logger.debug("\n组装回复信息:{}", out);
102+
log.debug("\n组装回复信息:{}", out);
107103
return out;
108104
}
109105

110106
private WxMpXmlOutMessage route(WxMpXmlMessage message) {
111107
try {
112108
return this.messageRouter.route(message);
113109
} catch (Exception e) {
114-
this.logger.error("路由消息时出现异常!", e);
110+
log.error("路由消息时出现异常!", e);
115111
}
116112

117113
return null;

src/main/java/com/github/binarywang/demo/wx/mp/controller/WxRedirectController.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.binarywang.demo.wx.mp.controller;
22

33
import com.github.binarywang.demo.wx.mp.config.WxMpConfiguration;
4+
import lombok.AllArgsConstructor;
45
import me.chanjar.weixin.common.error.WxErrorException;
56
import me.chanjar.weixin.mp.api.WxMpService;
67
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
@@ -17,15 +18,11 @@
1718
/**
1819
* @author Edward
1920
*/
21+
@AllArgsConstructor
2022
@Controller
2123
@RequestMapping("/wx/redirect/{appid}")
2224
public class WxRedirectController {
23-
private WxMpService wxService;
24-
25-
@Autowired
26-
public WxRedirectController(WxMpService wxService) {
27-
this.wxService = wxService;
28-
}
25+
private final WxMpService wxService;
2926

3027
@RequestMapping("/greet")
3128
public String greetUser(@PathVariable String appid, @RequestParam String code, ModelMap map) {

0 commit comments

Comments
 (0)