Skip to content

Commit

Permalink
update logger related code , using @slf4j instead
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Mar 2, 2024
1 parent 517dc7e commit 45db364
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 186 deletions.
3 changes: 2 additions & 1 deletion commit.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
git commit -a -m "update wxjava sdk to 4.6.0"
git commit -a -m "update logger related code , using @slf4j instead"
#git commit -a -m "update wxjava sdk to 4.6.0"
git pull --rebase

git push origin master
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
@RequiredArgsConstructor
@RequestMapping("/wx/cp/js/{corpId}/{agentId}")
public class WxJsController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final StringRedisTemplate stringRedisTemplate;

@PostMapping("/getJsConf")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import com.github.binarywang.demo.wx.cp.config.mutil.WxCpConfiguration;
import com.github.binarywang.demo.wx.cp.utils.JsonUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

Expand All @@ -20,89 +19,89 @@
/**
* @author <a href="https://github.com/0katekate0">Wang_Wong</a>
*/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/wx/cp/portal/{corpId}/{agentId}")
public class WxPortalController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final StringRedisTemplate stringRedisTemplate;

@PostMapping("/test")
public String test(@PathVariable String corpId,
@RequestParam(name = "userId", required = true) String userId) throws WxErrorException {

String s = stringRedisTemplate.opsForValue().get("test");
logger.info("data is {}", s);

final val wxCpService = WxCpConfiguration.getCpService(corpId,10001);
List<String> list = wxCpService.getExternalContactService().listExternalContacts(userId);

return list.toString();
}

@GetMapping(produces = "text/plain;charset=utf-8")
public String authGet(@PathVariable String corpId,
@PathVariable Integer agentId,
@RequestParam(name = "msg_signature", required = false) String signature,
@RequestParam(name = "timestamp", required = false) String timestamp,
@RequestParam(name = "nonce", required = false) String nonce,
@RequestParam(name = "echostr", required = false) String echostr) {
log.info("\n接收到来自微信服务器的认证消息:signature = [{}], timestamp = [{}], nonce = [{}], echostr = [{}]",
signature, timestamp, nonce, echostr);

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

final WxCpService wxCpService = WxCpConfiguration.getCpService(corpId, agentId);
if (wxCpService == null) {
throw new IllegalArgumentException(String.format("未找到对应agentId=[%d]的配置,请核实!", agentId));
}
@PostMapping("/test")
public String test(@PathVariable String corpId,
@RequestParam(name = "userId", required = true) String userId) throws WxErrorException {

String s = stringRedisTemplate.opsForValue().get("test");
log.info("data is {}", s);

if (wxCpService.checkSignature(signature, timestamp, nonce, echostr)) {
return new WxCpCryptUtil(wxCpService.getWxCpConfigStorage()).decrypt(echostr);
final val wxCpService = WxCpConfiguration.getCpService(corpId, 10001);
List<String> list = wxCpService.getExternalContactService().listExternalContacts(userId);

return list.toString();
}

return "非法请求";
}

@PostMapping(produces = "application/xml; charset=UTF-8")
public String post(@PathVariable String corpId,
@PathVariable Integer agentId,
@RequestBody String requestBody,
@RequestParam("msg_signature") String signature,
@RequestParam("timestamp") String timestamp,
@RequestParam("nonce") String nonce) {
log.info("\n接收微信请求:[signature=[{}], timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
signature, timestamp, nonce, requestBody);

final WxCpService wxCpService = WxCpConfiguration.getCpService(corpId, agentId);
if (wxCpService == null) {
throw new IllegalArgumentException(String.format("未找到对应agentId=[%d]的配置,请核实!", agentId));
@GetMapping(produces = "text/plain;charset=utf-8")
public String authGet(@PathVariable String corpId,
@PathVariable Integer agentId,
@RequestParam(name = "msg_signature", required = false) String signature,
@RequestParam(name = "timestamp", required = false) String timestamp,
@RequestParam(name = "nonce", required = false) String nonce,
@RequestParam(name = "echostr", required = false) String echostr) {
log.info("\n接收到来自微信服务器的认证消息:signature = [{}], timestamp = [{}], nonce = [{}], echostr = [{}]",
signature, timestamp, nonce, echostr);

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

final WxCpService wxCpService = WxCpConfiguration.getCpService(corpId, agentId);
if (wxCpService == null) {
throw new IllegalArgumentException(String.format("未找到对应agentId=[%d]的配置,请核实!", agentId));
}

if (wxCpService.checkSignature(signature, timestamp, nonce, echostr)) {
return new WxCpCryptUtil(wxCpService.getWxCpConfigStorage()).decrypt(echostr);
}

return "非法请求";
}

WxCpXmlMessage inMessage = WxCpXmlMessage.fromEncryptedXml(requestBody, wxCpService.getWxCpConfigStorage(),
timestamp, nonce, signature);
log.debug("\n消息解密后内容为:\n{} ", JsonUtils.toJson(inMessage));
WxCpXmlOutMessage outMessage = this.route(corpId, agentId, inMessage);
if (outMessage == null) {
return "";
@PostMapping(produces = "application/xml; charset=UTF-8")
public String post(@PathVariable String corpId,
@PathVariable Integer agentId,
@RequestBody String requestBody,
@RequestParam("msg_signature") String signature,
@RequestParam("timestamp") String timestamp,
@RequestParam("nonce") String nonce) {
log.info("\n接收微信请求:[signature=[{}], timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
signature, timestamp, nonce, requestBody);

final WxCpService wxCpService = WxCpConfiguration.getCpService(corpId, agentId);
if (wxCpService == null) {
throw new IllegalArgumentException(String.format("未找到对应agentId=[%d]的配置,请核实!", agentId));
}

WxCpXmlMessage inMessage = WxCpXmlMessage.fromEncryptedXml(requestBody, wxCpService.getWxCpConfigStorage(),
timestamp, nonce, signature);
log.debug("\n消息解密后内容为:\n{} ", JsonUtils.toJson(inMessage));
WxCpXmlOutMessage outMessage = this.route(corpId, agentId, inMessage);
if (outMessage == null) {
return "";
}

String out = outMessage.toEncryptedXml(wxCpService.getWxCpConfigStorage());
log.debug("\n组装回复信息:{}", out);
return out;
}

String out = outMessage.toEncryptedXml(wxCpService.getWxCpConfigStorage());
log.debug("\n组装回复信息:{}", out);
return out;
}
private WxCpXmlOutMessage route(String corpId, Integer agentId, WxCpXmlMessage message) {
try {
return WxCpConfiguration.getRouters().get(corpId + agentId).route(message);
} catch (Exception e) {
log.error(e.getMessage(), e);
}

private WxCpXmlOutMessage route(String corpId, Integer agentId, WxCpXmlMessage message) {
try {
return WxCpConfiguration.getRouters().get(corpId + agentId).route(message);
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
}

return null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
//@RequestMapping("/wx/cp/js/{corpId}/{agentId}/getJsConf")

public class WxJsController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());

@PostMapping("/getJsConf")
public Map getJsConf(
@PathVariable String corpId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.binarywang.demo.wx.cp.config.single.WxCpConfiguration;
import com.github.binarywang.demo.wx.cp.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
Expand All @@ -16,9 +17,8 @@
*/
//@RestController
//@RequestMapping("/wx/cp/portal/{agentId}")
@Slf4j
public class WxPortalController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());

@GetMapping(produces = "text/plain;charset=utf-8")
public String authGet(@PathVariable Integer agentId,
@RequestParam(name = "msg_signature", required = false) String signature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public abstract class AbstractHandler implements WxCpMessageHandler {
protected Logger logger = LoggerFactory.getLogger(getClass());
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
package com.github.binarywang.demo.wx.cp.handler;

import java.util.Map;

import org.springframework.stereotype.Component;

import com.github.binarywang.demo.wx.cp.builder.TextBuilder;
import com.github.binarywang.demo.wx.cp.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
* 通讯录变更事件处理器.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@Slf4j
@Component
public class ContactChangeHandler extends AbstractHandler {

@Override
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService cpService,
WxSessionManager sessionManager) {
String content = "收到通讯录变更事件,内容:" + JsonUtils.toJson(wxMessage);
log.info(content);
@Override
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService cpService,
WxSessionManager sessionManager) {
String content = "收到通讯录变更事件,内容:" + JsonUtils.toJson(wxMessage);
log.info(content);

return new TextBuilder().build(content, wxMessage, cpService);
}
return new TextBuilder().build(content, wxMessage, cpService);
}

}
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
package com.github.binarywang.demo.wx.cp.handler;

import java.util.Map;

import org.springframework.stereotype.Component;

import com.github.binarywang.demo.wx.cp.builder.TextBuilder;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@Slf4j
@Component
public class LocationHandler extends AbstractHandler {

@Override
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService cpService,
WxSessionManager sessionManager) {
if (wxMessage.getMsgType().equals(WxConsts.XmlMsgType.LOCATION)) {
//TODO 接收处理用户发送的地理位置消息
try {
String content = "感谢反馈,您的的地理位置已收到!";
return new TextBuilder().build(content, wxMessage, null);
} catch (Exception e) {
log.error("位置消息接收处理失败", e);
@Override
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService cpService,
WxSessionManager sessionManager) {
if (wxMessage.getMsgType().equals(WxConsts.XmlMsgType.LOCATION)) {
//TODO 接收处理用户发送的地理位置消息
try {
String content = "感谢反馈,您的的地理位置已收到!";
return new TextBuilder().build(content, wxMessage, null);
} catch (Exception e) {
log.error("位置消息接收处理失败", e);
return null;
}
}

//上报地理位置事件
log.info("\n上报地理位置,纬度 : {}\n经度 : {}\n精度 : {}",
wxMessage.getLatitude(), wxMessage.getLongitude(), String.valueOf(wxMessage.getPrecision()));

//TODO 可以将用户地理位置信息保存到本地数据库,以便以后使用

return null;
}
}

//上报地理位置事件
log.info("\n上报地理位置,纬度 : {}\n经度 : {}\n精度 : {}",
wxMessage.getLatitude(), wxMessage.getLongitude(), String.valueOf(wxMessage.getPrecision()));

//TODO 可以将用户地理位置信息保存到本地数据库,以便以后使用

return null;
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package com.github.binarywang.demo.wx.cp.handler;

import java.util.Map;

import org.springframework.stereotype.Component;

import com.github.binarywang.demo.wx.cp.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@Slf4j
@Component
public class LogHandler extends AbstractHandler {
@Override
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService cpService,
WxSessionManager sessionManager) {
log.info("\n接收到请求消息,内容:{}", JsonUtils.toJson(wxMessage));
return null;
}
@Override
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService cpService,
WxSessionManager sessionManager) {
log.info("\n接收到请求消息,内容:{}", JsonUtils.toJson(wxMessage));
return null;
}

}
Loading

0 comments on commit 45db364

Please sign in to comment.