Skip to content

Commit

Permalink
简化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed May 18, 2019
1 parent ebf30b7 commit dadcc25
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.github.binarywang.demo.wx.mp.controller;

import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult;
import me.chanjar.weixin.mp.bean.menu.WxMpMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
Expand All @@ -21,15 +21,11 @@
/**
* @author Binary Wang(https://github.com/binarywang)
*/
@AllArgsConstructor
@RestController
@RequestMapping("/wx/menu/{appid}")
public class WxMenuController {
private WxMpService wxService;

@Autowired
public WxMenuController(WxMpService wxService) {
this.wxService = wxService;
}
private final WxMpService wxService;

/**
* <pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.github.binarywang.demo.wx.mp.controller;

import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand All @@ -20,20 +23,13 @@
/**
* @author Binary Wang(https://github.com/binarywang)
*/
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/wx/portal/{appid}")
public class WxPortalController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());

private WxMpService wxService;

private WxMpMessageRouter messageRouter;

@Autowired
public WxPortalController(WxMpService wxService, WxMpMessageRouter messageRouter) {
this.wxService = wxService;
this.messageRouter = messageRouter;
}
private final WxMpService wxService;
private final WxMpMessageRouter messageRouter;

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

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

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

this.logger.debug("\n组装回复信息:{}", out);
log.debug("\n组装回复信息:{}", out);
return out;
}

private WxMpXmlOutMessage route(WxMpXmlMessage message) {
try {
return this.messageRouter.route(message);
} catch (Exception e) {
this.logger.error("路由消息时出现异常!", e);
log.error("路由消息时出现异常!", e);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.binarywang.demo.wx.mp.controller;

import com.github.binarywang.demo.wx.mp.config.WxMpConfiguration;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
Expand All @@ -17,15 +18,11 @@
/**
* @author Edward
*/
@AllArgsConstructor
@Controller
@RequestMapping("/wx/redirect/{appid}")
public class WxRedirectController {
private WxMpService wxService;

@Autowired
public WxRedirectController(WxMpService wxService) {
this.wxService = wxService;
}
private final WxMpService wxService;

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

0 comments on commit dadcc25

Please sign in to comment.