diff --git a/hai-service/src/main/java/com/hai/model/TextMessage.java b/hai-service/src/main/java/com/hai/model/TextMessage.java new file mode 100644 index 00000000..12cb829f --- /dev/null +++ b/hai-service/src/main/java/com/hai/model/TextMessage.java @@ -0,0 +1,53 @@ +package com.hai.model; + +/** + * @author sum1dream + */ +public class TextMessage { + + private String toUserName; + private String fromUserName; + private String msgType; + private String content; + private Long createTime; + + public String getToUserName() { + return toUserName; + } + + public void setToUserName(String toUserName) { + this.toUserName = toUserName; + } + + public String getFromUserName() { + return fromUserName; + } + + public void setFromUserName(String fromUserName) { + this.fromUserName = fromUserName; + } + + public String getMsgType() { + return msgType; + } + + public void setMsgType(String msgType) { + this.msgType = msgType; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public Long getCreateTime() { + return createTime; + } + + public void setCreateTime(Long createTime) { + this.createTime = createTime; + } +} diff --git a/v1/src/main/java/com/v1/config/WeChatQrcodeUtils.java b/v1/src/main/java/com/v1/config/WeChatQrcodeUtils.java index ced8374d..4851ede2 100644 --- a/v1/src/main/java/com/v1/config/WeChatQrcodeUtils.java +++ b/v1/src/main/java/com/v1/config/WeChatQrcodeUtils.java @@ -1,6 +1,7 @@ package com.v1.config; +import com.hai.model.TextMessage; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.mp.api.WxMpService; @@ -10,6 +11,7 @@ import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.io.File; +import java.util.Map; /** * @serviceName WeChatQrcodeUtils.java @@ -118,4 +120,67 @@ public class WeChatQrcodeUtils { } + /** + * 处理 subscribe 类型的event + * + * @param map + * @return + */ + public String handleEventSubscribe(Map map) { + String resXmlStr = getReturnMsgSubscribe(map); + return resXmlStr; + } + + + /** + * @Author Sum1Dream + * @Name getReturnMsgSubscribe + * @Description // 处理文本 + * @Date 16:34 2022/9/21 + * @Param [decryptMap] + * @Return java.lang.String + */ + public String getReturnMsgSubscribe(Map decryptMap) { + TextMessage textMessage = new TextMessage(); + textMessage.setToUserName(decryptMap.get("FromUserName")); + textMessage.setFromUserName(decryptMap.get("ToUserName")); + textMessage.setCreateTime(System.currentTimeMillis()); + textMessage.setMsgType("text"); + textMessage.setContent(decryptMap.get("content")); + return getXmlString(textMessage); + } + + /** + * @Author Sum1Dream + * @Name getXmlString + * @Description // 处理xml文件 + * @Date 16:34 2022/9/21 + * @Param [textMessage] + * @Return java.lang.String + */ + public String getXmlString(TextMessage textMessage) { + String xml = ""; + if (textMessage != null) { + xml = ""; + xml += ""; + xml += ""; + xml += ""; + xml += textMessage.getCreateTime(); + xml += ""; + xml += ""; + xml += ""; + xml += ""; + } + return xml; + } + + } diff --git a/v1/src/main/java/com/v1/config/WxConfig.java b/v1/src/main/java/com/v1/config/WxConfig.java index 286afac7..c137c4b0 100644 --- a/v1/src/main/java/com/v1/config/WxConfig.java +++ b/v1/src/main/java/com/v1/config/WxConfig.java @@ -1,6 +1,7 @@ package com.v1.config; import com.hai.config.CommonSysConst; +import com.hai.model.TextMessage; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; import me.chanjar.weixin.mp.config.WxMpConfigStorage; @@ -8,6 +9,8 @@ import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.util.Map; + /** * @serviceName .java * @author Sum1Dream @@ -48,4 +51,7 @@ public class WxConfig { wxMpService.setWxMpConfigStorage(wxMpConfigStorage()); return wxMpService; } + + + } diff --git a/v1/src/main/java/com/v1/controller/WxMsgController.java b/v1/src/main/java/com/v1/controller/WxMsgController.java index 12e0928d..18bd1f49 100644 --- a/v1/src/main/java/com/v1/controller/WxMsgController.java +++ b/v1/src/main/java/com/v1/controller/WxMsgController.java @@ -9,8 +9,10 @@ import com.hai.common.pay.util.XmlUtil; import com.hai.common.utils.HttpsUtils; import com.hai.common.utils.ResponseMsgUtil; import com.hai.common.utils.WxUtils; +import com.hai.model.HighMerchantStoreModel; import com.hai.model.ResponseData; -import com.hai.service.CommonService; +import com.hai.model.TextMessage; +import com.hai.service.HighMerchantStoreService; import com.v1.config.SysConst; import com.v1.config.WeChatQrcodeUtils; import io.swagger.annotations.Api; @@ -26,10 +28,18 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.xml.sax.InputSource; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.BufferedOutputStream; +import java.io.StringReader; import java.util.HashMap; import java.util.Map; import java.util.SortedMap; @@ -48,12 +58,14 @@ public class WxMsgController { @Autowired private WeChatQrcodeUtils weChatQrcodeUtils; + @Resource + private HighMerchantStoreService merchantStoreService; - @RequestMapping(value = "/verifyWxToken", method = RequestMethod.GET) + @RequestMapping(value = "/verifyWxToken", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "验证servlet") - public String verifyWxToken(HttpServletRequest request) { + public void verifyWxToken(HttpServletRequest request , HttpServletResponse response) { try { log.info("进入公众号!!!!!!!"); @@ -63,19 +75,69 @@ public class WxMsgController { String notifyXml; notifyXml = IOUtil.inputStreamToString(request.getInputStream(), "UTF-8"); - log.info("微信支付系统发送的数据:" + notifyXml); + log.info("微信系统发送的数据:" + notifyXml); SortedMap map = XmlUtil.parseXmlToTreeMap(notifyXml, "UTF-8"); + // openId + String userOpenId = map.get("FromUserName"); + // 微信账号 + String userName = map.get("ToUserName"); + // 事件 + String event = map.get("Event"); // 区分消息类型 String msgType = map.get("MsgType"); - log.info("微信支付系统发送的数据:" + msgType); - return null; + + // 普通消息 + if ("text".equals(msgType)) { + // todo 处理文本消息 + } else if ("image".equals(msgType)) { + // todo 处理图片消息 + } else if ("voice".equals(msgType)) { + // todo 处理语音消息 + } else if ("video".equals(msgType)) { + // todo 处理视频消息 + } + // 事件推送消息 + else if ("event".equals(msgType)) { + // 用户关注公众号 + if ("subscribe".equals(event)) { + log.info("进入扫码关注流程:" + event); + // 商户门店 + HighMerchantStoreModel store = merchantStoreService.getMerchantStoreByKey(map.get("EventKey")); + // todo 业务处理 + String content = "欢迎来到" + store.getStoreName() + "点击这里一键加油"; + + map.put("content" , content); + String mapToXml = weChatQrcodeUtils.handleEventSubscribe(map); + response.setCharacterEncoding("UTF-8"); + response.getWriter().print(mapToXml); + // todo 业务处理 + } + // 用户扫码进入公众号 + else if ("SCAN".equals(event)) { + // 商户门店 + HighMerchantStoreModel store = merchantStoreService.getMerchantStoreByKey(map.get("EventKey")); + // todo 业务处理 + String content = "欢迎来到" + store.getStoreName() + "点击这里一键加油"; + + map.put("content" , content); + String mapToXml = weChatQrcodeUtils.handleEventSubscribe(map); + response.setCharacterEncoding("UTF-8"); + response.getWriter().print(mapToXml); + + }else if ("unsubscribe".equals(event)) { + // todo 取消关注 业务处理 + } + + } } catch (Exception e) { - return null; +// return null; } } + + @RequestMapping(value = "/getWxToken", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "获取微信token") @@ -110,7 +172,7 @@ public class WxMsgController { @RequestMapping(value = "/createQrcode", method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "创建生成二维码") - public ResponseData createQrcode(@RequestParam(name = "sceneId", required = false) Integer sceneId) { + public ResponseData createQrcode(@RequestParam(name = "sceneId", required = false) String sceneId) { try { return ResponseMsgUtil.success(weChatQrcodeUtils.qrCodeCreateLastTicket(sceneId)); } catch (WxErrorException e) {