Skip to content

Commit

Permalink
优化部分代码
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Sep 2, 2018
1 parent 26ba117 commit 53ce610
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,13 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package com.github.binarywang.demo.wx.mp.controller;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.binarywang.demo.wx.mp.config.WxMpConfiguration;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

/**
* @author Binary Wang(https://github.com/binarywang)
Expand All @@ -23,15 +30,18 @@ public String authGet(@PathVariable String appid,
@RequestParam(name = "timestamp", required = false) String timestamp,
@RequestParam(name = "nonce", required = false) String nonce,
@RequestParam(name = "echostr", required = false) String echostr) {
final WxMpService wxService = WxMpConfiguration.getMpServices().get(appid);

this.logger.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature,
timestamp, nonce, echostr);

if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
throw new IllegalArgumentException("请求参数非法,请核实!");
}

final WxMpService wxService = WxMpConfiguration.getMpServices().get(appid);
if (wxService == null) {
throw new IllegalArgumentException(String.format("未找到对应appid=[%d]的配置,请核实!", appid));
}

if (wxService.checkSignature(timestamp, nonce, signature)) {
return echostr;
}
Expand All @@ -45,12 +55,13 @@ public String post(@PathVariable String appid,
@RequestParam("signature") String signature,
@RequestParam("timestamp") String timestamp,
@RequestParam("nonce") String nonce,
@RequestParam("openid") String openid,
@RequestParam(name = "encrypt_type", required = false) String encType,
@RequestParam(name = "msg_signature", required = false) String msgSignature) {
final WxMpService wxService = WxMpConfiguration.getMpServices().get(appid);
this.logger.info("\n接收微信请求:[signature=[{}], encType=[{}], msgSignature=[{}],"
this.logger.info("\n接收微信请求:[openid=[{}], [signature=[{}], encType=[{}], msgSignature=[{}],"
+ " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
signature, encType, msgSignature, timestamp, nonce, requestBody);
openid, signature, encType, msgSignature, timestamp, nonce, requestBody);

if (!wxService.checkSignature(timestamp, nonce, signature)) {
throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.github.binarywang.demo.wx.mp.handler;

import java.util.Map;

import org.springframework.stereotype.Component;

import com.github.binarywang.demo.wx.mp.builder.TextBuilder;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
* @author Binary Wang(https://github.com/binarywang)
Expand All @@ -25,16 +26,22 @@ public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());

// 获取微信用户基本信息
WxMpUser userWxInfo = weixinService.getUserService()
.userInfo(wxMessage.getFromUser(), null);

if (userWxInfo != null) {
// TODO 可以添加关注用户到本地
try {
WxMpUser userWxInfo = weixinService.getUserService()
.userInfo(wxMessage.getFromUser(), null);
if (userWxInfo != null) {
// TODO 可以添加关注用户到本地数据库
}
} catch (WxErrorException e) {
if (e.getError().getErrorCode() == 48001) {
this.logger.info("该公众号没有获取用户信息权限!");
}
}


WxMpXmlOutMessage responseResult = null;
try {
responseResult = handleSpecial(wxMessage);
responseResult = this.handleSpecial(wxMessage);
} catch (Exception e) {
this.logger.error(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "wx.mp.configs",
"type": "java.util.List",
"description": "Description for wx.mp.configs. https://github.com/ettingshausen/weixin-java-mp-demo-springboot/blob/master/README.md."
"description": "Description for wx.mp.configs."
}
]
}

0 comments on commit 53ce610

Please sign in to comment.