|
@@ -0,0 +1,136 @@
|
|
1
|
+package com.huiju.estateagents.controller;
|
|
2
|
+
|
|
3
|
+import com.huiju.estateagents.base.BaseController;
|
|
4
|
+import com.huiju.estateagents.common.CommConstant;
|
|
5
|
+import com.huiju.estateagents.common.WxUtils;
|
|
6
|
+import com.huiju.estateagents.entity.TaMiniapp;
|
|
7
|
+import com.huiju.estateagents.redpack.entity.TaMpPerson;
|
|
8
|
+import com.huiju.estateagents.redpack.service.ITaMpPersonService;
|
|
9
|
+import com.huiju.estateagents.service.IMiniAppService;
|
|
10
|
+import com.huiju.estateagents.third.entity.TaMpInfo;
|
|
11
|
+import com.huiju.estateagents.third.service.ITaMpInfoService;
|
|
12
|
+import lombok.extern.slf4j.Slf4j;
|
|
13
|
+import me.chanjar.weixin.common.api.WxConsts;
|
|
14
|
+import me.chanjar.weixin.common.error.WxErrorException;
|
|
15
|
+import me.chanjar.weixin.mp.api.WxMpKefuService;
|
|
16
|
+import me.chanjar.weixin.mp.api.WxMpService;
|
|
17
|
+import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
|
|
18
|
+import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
|
|
19
|
+import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
|
|
20
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
21
|
+import org.springframework.beans.factory.annotation.Value;
|
|
22
|
+import org.springframework.web.bind.annotation.*;
|
|
23
|
+
|
|
24
|
+import java.time.LocalDateTime;
|
|
25
|
+
|
|
26
|
+@Slf4j
|
|
27
|
+@RestController
|
|
28
|
+@RequestMapping("/api/mp/portal/{appid}")
|
|
29
|
+public class WxPortalController extends BaseController {
|
|
30
|
+
|
|
31
|
+ @Autowired
|
|
32
|
+ ITaMpInfoService iTaMpInfoService;
|
|
33
|
+
|
|
34
|
+ @Autowired
|
|
35
|
+ ITaMpPersonService iTaMpPersonService;
|
|
36
|
+
|
|
37
|
+ @Autowired
|
|
38
|
+ private IMiniAppService iMiniAppService;
|
|
39
|
+
|
|
40
|
+ @Autowired
|
|
41
|
+ WxUtils wxUtils;
|
|
42
|
+
|
|
43
|
+ @Value("${wx.miniapp.index}")
|
|
44
|
+ String miniAppIndex;
|
|
45
|
+
|
|
46
|
+ /**
|
|
47
|
+ * 微信公众号接入
|
|
48
|
+ * @param appid
|
|
49
|
+ * @param signature
|
|
50
|
+ * @param timestamp
|
|
51
|
+ * @param nonce
|
|
52
|
+ * @param echostr
|
|
53
|
+ * @return
|
|
54
|
+ */
|
|
55
|
+ @GetMapping(produces = "text/plain;charset=utf-8")
|
|
56
|
+ public String authGet(@PathVariable String appid,
|
|
57
|
+ @RequestParam(name = "signature", required = false) String signature,
|
|
58
|
+ @RequestParam(name = "timestamp", required = false) String timestamp,
|
|
59
|
+ @RequestParam(name = "nonce", required = false) String nonce,
|
|
60
|
+ @RequestParam(name = "echostr", required = false) String echostr) {
|
|
61
|
+ return echostr;
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ @PostMapping(produces = "application/xml; charset=UTF-8")
|
|
65
|
+ public String post(@PathVariable String appid,
|
|
66
|
+ @RequestBody String requestBody,
|
|
67
|
+ @RequestParam("signature") String signature,
|
|
68
|
+ @RequestParam("timestamp") String timestamp,
|
|
69
|
+ @RequestParam("nonce") String nonce,
|
|
70
|
+ @RequestParam("openid") String openid,
|
|
71
|
+ @RequestParam(name = "encrypt_type", required = false) String encType,
|
|
72
|
+ @RequestParam(name = "msg_signature", required = false) String msgSignature) {
|
|
73
|
+
|
|
74
|
+ log.info("\n接收微信请求:[openid=[{}], [signature=[{}], encType=[{}], msgSignature=[{}],"
|
|
75
|
+ + " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
|
|
76
|
+ openid, signature, encType, msgSignature, timestamp, nonce, requestBody);
|
|
77
|
+
|
|
78
|
+ WxMpService mpService = wxUtils.getMpService().switchoverTo(appid);
|
|
79
|
+ if (null == mpService) {
|
|
80
|
+ throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid));
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ if (!mpService.checkSignature(timestamp, nonce, signature)) {
|
|
84
|
+ throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
|
|
85
|
+ }
|
|
86
|
+
|
|
87
|
+ // 对应表数据
|
|
88
|
+ TaMpInfo mpInfo = iTaMpInfoService.getById(appid);
|
|
89
|
+
|
|
90
|
+ WxMpXmlMessage inMessage = null;
|
|
91
|
+ WxMpXmlOutMessage outMessage = null;
|
|
92
|
+ if (encType == null) {
|
|
93
|
+ // 明文传输的消息
|
|
94
|
+ inMessage = WxMpXmlMessage.fromXml(requestBody);
|
|
95
|
+ } else if ("aes".equalsIgnoreCase(encType)) {
|
|
96
|
+ // aes加密的消息
|
|
97
|
+ inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody, mpService.getWxMpConfigStorage(),
|
|
98
|
+ timestamp, nonce, msgSignature);
|
|
99
|
+ log.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ // 关注公众号
|
|
103
|
+ if (inMessage.getMsgType().equals(WxConsts.XmlMsgType.EVENT) && inMessage.getEvent().equals(WxConsts.EventType.SUBSCRIBE)) {
|
|
104
|
+ // 新增人员
|
|
105
|
+ TaMpPerson taMpPerson = new TaMpPerson();
|
|
106
|
+ taMpPerson.setAppid(appid);
|
|
107
|
+ taMpPerson.setMpOpenid(openid);
|
|
108
|
+ taMpPerson.setOrgId(mpInfo.getOrgId());
|
|
109
|
+ taMpPerson.setCreateDate(LocalDateTime.now());
|
|
110
|
+ taMpPerson.setStatus(CommConstant.STATUS_NORMAL);
|
|
111
|
+ TaMpPerson person = iTaMpPersonService.newPerson(taMpPerson);
|
|
112
|
+
|
|
113
|
+ // 发送客服消息
|
|
114
|
+ sendMiniAppKefuMessage(mpService, encType, mpInfo, inMessage, person);
|
|
115
|
+ }
|
|
116
|
+
|
|
117
|
+ return "";
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ private void sendMiniAppKefuMessage(WxMpService mpService, String encType, TaMpInfo mpInfo, WxMpXmlMessage message, TaMpPerson person) {
|
|
121
|
+
|
|
122
|
+ TaMiniapp taMiniapp = iMiniAppService.getAppByOrg(mpInfo.getOrgId());
|
|
123
|
+
|
|
124
|
+ WxMpKefuMessage kefuMessage = WxMpKefuMessage.MINIPROGRAMPAGE().appId(taMiniapp.getMiniappId())
|
|
125
|
+ .title(taMiniapp.getName() + "欢迎您!")
|
|
126
|
+ .pagePath(miniAppIndex + "?mpOpenId=" + message.getOpenId())
|
|
127
|
+ .thumbMediaId(mpInfo.getMiniappThumb())
|
|
128
|
+ .build();
|
|
129
|
+
|
|
130
|
+ try {
|
|
131
|
+ mpService.getKefuService().sendKefuMessage(kefuMessage);
|
|
132
|
+ } catch (WxErrorException e) {
|
|
133
|
+ e.printStackTrace();
|
|
134
|
+ }
|
|
135
|
+ }
|
|
136
|
+}
|