|
3 | 3 | import com.github.binarywang.demo.wx.cp.config.mutil.WxCpConfiguration;
|
4 | 4 | import com.github.binarywang.demo.wx.cp.utils.JsonUtils;
|
5 | 5 | import lombok.RequiredArgsConstructor;
|
| 6 | +import lombok.extern.slf4j.Slf4j; |
6 | 7 | import lombok.val;
|
7 | 8 | import me.chanjar.weixin.common.error.WxErrorException;
|
8 | 9 | import me.chanjar.weixin.cp.api.WxCpService;
|
9 | 10 | import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
|
10 | 11 | import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
|
11 | 12 | import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil;
|
12 | 13 | import org.apache.commons.lang3.StringUtils;
|
13 |
| -import org.slf4j.Logger; |
14 |
| -import org.slf4j.LoggerFactory; |
15 | 14 | import org.springframework.data.redis.core.StringRedisTemplate;
|
16 | 15 | import org.springframework.web.bind.annotation.*;
|
17 | 16 |
|
|
20 | 19 | /**
|
21 | 20 | * @author <a href="https://github.com/0katekate0">Wang_Wong</a>
|
22 | 21 | */
|
| 22 | +@Slf4j |
23 | 23 | @RestController
|
24 | 24 | @RequiredArgsConstructor
|
25 | 25 | @RequestMapping("/wx/cp/portal/{corpId}/{agentId}")
|
26 | 26 | 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; |
56 | 28 |
|
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); |
61 | 35 |
|
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(); |
64 | 40 | }
|
65 | 41 |
|
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 "非法请求"; |
82 | 66 | }
|
83 | 67 |
|
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; |
90 | 94 | }
|
91 | 95 |
|
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 | + } |
96 | 102 |
|
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; |
102 | 104 | }
|
103 | 105 |
|
104 |
| - return null; |
105 |
| - } |
106 |
| - |
107 | 106 |
|
108 | 107 | }
|
0 commit comments