Skip to content

Commit 45db364

Browse files
committed
update logger related code , using @slf4j instead
1 parent 517dc7e commit 45db364

12 files changed

+189
-186
lines changed

commit.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
git commit -a -m "update wxjava sdk to 4.6.0"
1+
git commit -a -m "update logger related code , using @slf4j instead"
2+
#git commit -a -m "update wxjava sdk to 4.6.0"
23
git pull --rebase
34

45
git push origin master

src/main/java/com/github/binarywang/demo/wx/cp/controller/mutil/WxJsController.java

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
@RequiredArgsConstructor
2323
@RequestMapping("/wx/cp/js/{corpId}/{agentId}")
2424
public class WxJsController {
25-
private final Logger logger = LoggerFactory.getLogger(this.getClass());
2625
private final StringRedisTemplate stringRedisTemplate;
2726

2827
@PostMapping("/getJsConf")

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

+70-71
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
import com.github.binarywang.demo.wx.cp.config.mutil.WxCpConfiguration;
44
import com.github.binarywang.demo.wx.cp.utils.JsonUtils;
55
import lombok.RequiredArgsConstructor;
6+
import lombok.extern.slf4j.Slf4j;
67
import lombok.val;
78
import me.chanjar.weixin.common.error.WxErrorException;
89
import me.chanjar.weixin.cp.api.WxCpService;
910
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
1011
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
1112
import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil;
1213
import org.apache.commons.lang3.StringUtils;
13-
import org.slf4j.Logger;
14-
import org.slf4j.LoggerFactory;
1514
import org.springframework.data.redis.core.StringRedisTemplate;
1615
import org.springframework.web.bind.annotation.*;
1716

@@ -20,89 +19,89 @@
2019
/**
2120
* @author <a href="https://github.com/0katekate0">Wang_Wong</a>
2221
*/
22+
@Slf4j
2323
@RestController
2424
@RequiredArgsConstructor
2525
@RequestMapping("/wx/cp/portal/{corpId}/{agentId}")
2626
public class WxPortalController {
27-
private final Logger logger = LoggerFactory.getLogger(this.getClass());
28-
private final StringRedisTemplate stringRedisTemplate;
29-
30-
@PostMapping("/test")
31-
public String test(@PathVariable String corpId,
32-
@RequestParam(name = "userId", required = true) String userId) throws WxErrorException {
33-
34-
String s = stringRedisTemplate.opsForValue().get("test");
35-
logger.info("data is {}", s);
36-
37-
final val wxCpService = WxCpConfiguration.getCpService(corpId,10001);
38-
List<String> list = wxCpService.getExternalContactService().listExternalContacts(userId);
39-
40-
return list.toString();
41-
}
42-
43-
@GetMapping(produces = "text/plain;charset=utf-8")
44-
public String authGet(@PathVariable String corpId,
45-
@PathVariable Integer agentId,
46-
@RequestParam(name = "msg_signature", required = false) String signature,
47-
@RequestParam(name = "timestamp", required = false) String timestamp,
48-
@RequestParam(name = "nonce", required = false) String nonce,
49-
@RequestParam(name = "echostr", required = false) String echostr) {
50-
log.info("\n接收到来自微信服务器的认证消息:signature = [{}], timestamp = [{}], nonce = [{}], echostr = [{}]",
51-
signature, timestamp, nonce, echostr);
52-
53-
if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
54-
throw new IllegalArgumentException("请求参数非法,请核实!");
55-
}
27+
private final StringRedisTemplate stringRedisTemplate;
5628

57-
final WxCpService wxCpService = WxCpConfiguration.getCpService(corpId, agentId);
58-
if (wxCpService == null) {
59-
throw new IllegalArgumentException(String.format("未找到对应agentId=[%d]的配置,请核实!", agentId));
60-
}
29+
@PostMapping("/test")
30+
public String test(@PathVariable String corpId,
31+
@RequestParam(name = "userId", required = true) String userId) throws WxErrorException {
32+
33+
String s = stringRedisTemplate.opsForValue().get("test");
34+
log.info("data is {}", s);
6135

62-
if (wxCpService.checkSignature(signature, timestamp, nonce, echostr)) {
63-
return new WxCpCryptUtil(wxCpService.getWxCpConfigStorage()).decrypt(echostr);
36+
final val wxCpService = WxCpConfiguration.getCpService(corpId, 10001);
37+
List<String> list = wxCpService.getExternalContactService().listExternalContacts(userId);
38+
39+
return list.toString();
6440
}
6541

66-
return "非法请求";
67-
}
68-
69-
@PostMapping(produces = "application/xml; charset=UTF-8")
70-
public String post(@PathVariable String corpId,
71-
@PathVariable Integer agentId,
72-
@RequestBody String requestBody,
73-
@RequestParam("msg_signature") String signature,
74-
@RequestParam("timestamp") String timestamp,
75-
@RequestParam("nonce") String nonce) {
76-
log.info("\n接收微信请求:[signature=[{}], timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
77-
signature, timestamp, nonce, requestBody);
78-
79-
final WxCpService wxCpService = WxCpConfiguration.getCpService(corpId, agentId);
80-
if (wxCpService == null) {
81-
throw new IllegalArgumentException(String.format("未找到对应agentId=[%d]的配置,请核实!", agentId));
42+
@GetMapping(produces = "text/plain;charset=utf-8")
43+
public String authGet(@PathVariable String corpId,
44+
@PathVariable Integer agentId,
45+
@RequestParam(name = "msg_signature", required = false) String signature,
46+
@RequestParam(name = "timestamp", required = false) String timestamp,
47+
@RequestParam(name = "nonce", required = false) String nonce,
48+
@RequestParam(name = "echostr", required = false) String echostr) {
49+
log.info("\n接收到来自微信服务器的认证消息:signature = [{}], timestamp = [{}], nonce = [{}], echostr = [{}]",
50+
signature, timestamp, nonce, echostr);
51+
52+
if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
53+
throw new IllegalArgumentException("请求参数非法,请核实!");
54+
}
55+
56+
final WxCpService wxCpService = WxCpConfiguration.getCpService(corpId, agentId);
57+
if (wxCpService == null) {
58+
throw new IllegalArgumentException(String.format("未找到对应agentId=[%d]的配置,请核实!", agentId));
59+
}
60+
61+
if (wxCpService.checkSignature(signature, timestamp, nonce, echostr)) {
62+
return new WxCpCryptUtil(wxCpService.getWxCpConfigStorage()).decrypt(echostr);
63+
}
64+
65+
return "非法请求";
8266
}
8367

84-
WxCpXmlMessage inMessage = WxCpXmlMessage.fromEncryptedXml(requestBody, wxCpService.getWxCpConfigStorage(),
85-
timestamp, nonce, signature);
86-
log.debug("\n消息解密后内容为:\n{} ", JsonUtils.toJson(inMessage));
87-
WxCpXmlOutMessage outMessage = this.route(corpId, agentId, inMessage);
88-
if (outMessage == null) {
89-
return "";
68+
@PostMapping(produces = "application/xml; charset=UTF-8")
69+
public String post(@PathVariable String corpId,
70+
@PathVariable Integer agentId,
71+
@RequestBody String requestBody,
72+
@RequestParam("msg_signature") String signature,
73+
@RequestParam("timestamp") String timestamp,
74+
@RequestParam("nonce") String nonce) {
75+
log.info("\n接收微信请求:[signature=[{}], timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
76+
signature, timestamp, nonce, requestBody);
77+
78+
final WxCpService wxCpService = WxCpConfiguration.getCpService(corpId, agentId);
79+
if (wxCpService == null) {
80+
throw new IllegalArgumentException(String.format("未找到对应agentId=[%d]的配置,请核实!", agentId));
81+
}
82+
83+
WxCpXmlMessage inMessage = WxCpXmlMessage.fromEncryptedXml(requestBody, wxCpService.getWxCpConfigStorage(),
84+
timestamp, nonce, signature);
85+
log.debug("\n消息解密后内容为:\n{} ", JsonUtils.toJson(inMessage));
86+
WxCpXmlOutMessage outMessage = this.route(corpId, agentId, inMessage);
87+
if (outMessage == null) {
88+
return "";
89+
}
90+
91+
String out = outMessage.toEncryptedXml(wxCpService.getWxCpConfigStorage());
92+
log.debug("\n组装回复信息:{}", out);
93+
return out;
9094
}
9195

92-
String out = outMessage.toEncryptedXml(wxCpService.getWxCpConfigStorage());
93-
log.debug("\n组装回复信息:{}", out);
94-
return out;
95-
}
96+
private WxCpXmlOutMessage route(String corpId, Integer agentId, WxCpXmlMessage message) {
97+
try {
98+
return WxCpConfiguration.getRouters().get(corpId + agentId).route(message);
99+
} catch (Exception e) {
100+
log.error(e.getMessage(), e);
101+
}
96102

97-
private WxCpXmlOutMessage route(String corpId, Integer agentId, WxCpXmlMessage message) {
98-
try {
99-
return WxCpConfiguration.getRouters().get(corpId + agentId).route(message);
100-
} catch (Exception e) {
101-
log.error(e.getMessage(), e);
103+
return null;
102104
}
103105

104-
return null;
105-
}
106-
107106

108107
}

src/main/java/com/github/binarywang/demo/wx/cp/controller/single/WxJsController.java

-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
//@RequestMapping("/wx/cp/js/{corpId}/{agentId}/getJsConf")
2222

2323
public class WxJsController {
24-
private final Logger logger = LoggerFactory.getLogger(this.getClass());
25-
2624
@PostMapping("/getJsConf")
2725
public Map getJsConf(
2826
@PathVariable String corpId,

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.github.binarywang.demo.wx.cp.config.single.WxCpConfiguration;
44
import com.github.binarywang.demo.wx.cp.utils.JsonUtils;
5+
import lombok.extern.slf4j.Slf4j;
56
import me.chanjar.weixin.cp.api.WxCpService;
67
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
78
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
@@ -16,9 +17,8 @@
1617
*/
1718
//@RestController
1819
//@RequestMapping("/wx/cp/portal/{agentId}")
20+
@Slf4j
1921
public class WxPortalController {
20-
private final Logger logger = LoggerFactory.getLogger(this.getClass());
21-
2222
@GetMapping(produces = "text/plain;charset=utf-8")
2323
public String authGet(@PathVariable Integer agentId,
2424
@RequestParam(name = "msg_signature", required = false) String signature,

src/main/java/com/github/binarywang/demo/wx/cp/handler/AbstractHandler.java

-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@
99
* @author <a href="https://github.com/binarywang">Binary Wang</a>
1010
*/
1111
public abstract class AbstractHandler implements WxCpMessageHandler {
12-
protected Logger logger = LoggerFactory.getLogger(getClass());
1312
}
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
package com.github.binarywang.demo.wx.cp.handler;
22

3-
import java.util.Map;
4-
5-
import org.springframework.stereotype.Component;
6-
73
import com.github.binarywang.demo.wx.cp.builder.TextBuilder;
84
import com.github.binarywang.demo.wx.cp.utils.JsonUtils;
5+
import lombok.extern.slf4j.Slf4j;
96
import me.chanjar.weixin.common.session.WxSessionManager;
107
import me.chanjar.weixin.cp.api.WxCpService;
118
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
129
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
10+
import org.springframework.stereotype.Component;
11+
12+
import java.util.Map;
1313

1414
/**
1515
* 通讯录变更事件处理器.
1616
*
1717
* @author <a href="https://github.com/binarywang">Binary Wang</a>
1818
*/
19+
@Slf4j
1920
@Component
2021
public class ContactChangeHandler extends AbstractHandler {
2122

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

28-
return new TextBuilder().build(content, wxMessage, cpService);
29-
}
29+
return new TextBuilder().build(content, wxMessage, cpService);
30+
}
3031

3132
}
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
11
package com.github.binarywang.demo.wx.cp.handler;
22

3-
import java.util.Map;
4-
5-
import org.springframework.stereotype.Component;
6-
73
import com.github.binarywang.demo.wx.cp.builder.TextBuilder;
4+
import lombok.extern.slf4j.Slf4j;
85
import me.chanjar.weixin.common.api.WxConsts;
96
import me.chanjar.weixin.common.session.WxSessionManager;
107
import me.chanjar.weixin.cp.api.WxCpService;
118
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
129
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
10+
import org.springframework.stereotype.Component;
11+
12+
import java.util.Map;
1313

1414
/**
1515
* @author <a href="https://github.com/binarywang">Binary Wang</a>
1616
*/
17+
@Slf4j
1718
@Component
1819
public class LocationHandler extends AbstractHandler {
1920

20-
@Override
21-
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService cpService,
22-
WxSessionManager sessionManager) {
23-
if (wxMessage.getMsgType().equals(WxConsts.XmlMsgType.LOCATION)) {
24-
//TODO 接收处理用户发送的地理位置消息
25-
try {
26-
String content = "感谢反馈,您的的地理位置已收到!";
27-
return new TextBuilder().build(content, wxMessage, null);
28-
} catch (Exception e) {
29-
log.error("位置消息接收处理失败", e);
21+
@Override
22+
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService cpService,
23+
WxSessionManager sessionManager) {
24+
if (wxMessage.getMsgType().equals(WxConsts.XmlMsgType.LOCATION)) {
25+
//TODO 接收处理用户发送的地理位置消息
26+
try {
27+
String content = "感谢反馈,您的的地理位置已收到!";
28+
return new TextBuilder().build(content, wxMessage, null);
29+
} catch (Exception e) {
30+
log.error("位置消息接收处理失败", e);
31+
return null;
32+
}
33+
}
34+
35+
//上报地理位置事件
36+
log.info("\n上报地理位置,纬度 : {}\n经度 : {}\n精度 : {}",
37+
wxMessage.getLatitude(), wxMessage.getLongitude(), String.valueOf(wxMessage.getPrecision()));
38+
39+
//TODO 可以将用户地理位置信息保存到本地数据库,以便以后使用
40+
3041
return null;
31-
}
3242
}
3343

34-
//上报地理位置事件
35-
log.info("\n上报地理位置,纬度 : {}\n经度 : {}\n精度 : {}",
36-
wxMessage.getLatitude(), wxMessage.getLongitude(), String.valueOf(wxMessage.getPrecision()));
37-
38-
//TODO 可以将用户地理位置信息保存到本地数据库,以便以后使用
39-
40-
return null;
41-
}
42-
4344
}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
package com.github.binarywang.demo.wx.cp.handler;
22

3-
import java.util.Map;
4-
5-
import org.springframework.stereotype.Component;
6-
73
import com.github.binarywang.demo.wx.cp.utils.JsonUtils;
4+
import lombok.extern.slf4j.Slf4j;
85
import me.chanjar.weixin.common.session.WxSessionManager;
96
import me.chanjar.weixin.cp.api.WxCpService;
107
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
118
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
9+
import org.springframework.stereotype.Component;
10+
11+
import java.util.Map;
1212

1313
/**
14-
* @author <a href="https://github.com/binarywang">Binary Wang</a>
14+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
1515
*/
16+
@Slf4j
1617
@Component
1718
public class LogHandler extends AbstractHandler {
18-
@Override
19-
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService cpService,
20-
WxSessionManager sessionManager) {
21-
log.info("\n接收到请求消息,内容:{}", JsonUtils.toJson(wxMessage));
22-
return null;
23-
}
19+
@Override
20+
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService cpService,
21+
WxSessionManager sessionManager) {
22+
log.info("\n接收到请求消息,内容:{}", JsonUtils.toJson(wxMessage));
23+
return null;
24+
}
2425

2526
}

0 commit comments

Comments
 (0)