dev-discount
袁野 2 years ago
parent 6d4259111b
commit ace46b4beb
  1. 53
      hai-service/src/main/java/com/hai/model/TextMessage.java
  2. 65
      v1/src/main/java/com/v1/config/WeChatQrcodeUtils.java
  3. 6
      v1/src/main/java/com/v1/config/WxConfig.java
  4. 78
      v1/src/main/java/com/v1/controller/WxMsgController.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;
}
}

@ -1,6 +1,7 @@
package com.v1.config; package com.v1.config;
import com.hai.model.TextMessage;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
@ -10,6 +11,7 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.File; import java.io.File;
import java.util.Map;
/** /**
* @serviceName WeChatQrcodeUtils.java * @serviceName WeChatQrcodeUtils.java
@ -118,4 +120,67 @@ public class WeChatQrcodeUtils {
} }
/**
* 处理 subscribe 类型的event
*
* @param map
* @return
*/
public String handleEventSubscribe(Map<String, String> 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<String, String> 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 += "<ToUserName><![CDATA[";
xml += textMessage.getToUserName();
xml += "]]></ToUserName>";
xml += "<FromUserName><![CDATA[";
xml += textMessage.getFromUserName();
xml += "]]></FromUserName>";
xml += "<CreateTime>";
xml += textMessage.getCreateTime();
xml += "</CreateTime>";
xml += "<MsgType><![CDATA[";
xml += textMessage.getMsgType();
xml += "]]></MsgType>";
xml += "<Content><![CDATA[";
xml += textMessage.getContent();
xml += "]]></Content>";
xml += "</xml>";
}
return xml;
}
} }

@ -1,6 +1,7 @@
package com.v1.config; package com.v1.config;
import com.hai.config.CommonSysConst; import com.hai.config.CommonSysConst;
import com.hai.model.TextMessage;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import java.util.Map;
/** /**
* @serviceName .java * @serviceName .java
* @author Sum1Dream * @author Sum1Dream
@ -48,4 +51,7 @@ public class WxConfig {
wxMpService.setWxMpConfigStorage(wxMpConfigStorage()); wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService; return wxMpService;
} }
} }

@ -9,8 +9,10 @@ import com.hai.common.pay.util.XmlUtil;
import com.hai.common.utils.HttpsUtils; import com.hai.common.utils.HttpsUtils;
import com.hai.common.utils.ResponseMsgUtil; import com.hai.common.utils.ResponseMsgUtil;
import com.hai.common.utils.WxUtils; import com.hai.common.utils.WxUtils;
import com.hai.model.HighMerchantStoreModel;
import com.hai.model.ResponseData; 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.SysConst;
import com.v1.config.WeChatQrcodeUtils; import com.v1.config.WeChatQrcodeUtils;
import io.swagger.annotations.Api; 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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; 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.annotation.Resource;
import javax.servlet.http.HttpServletRequest; 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.HashMap;
import java.util.Map; import java.util.Map;
import java.util.SortedMap; import java.util.SortedMap;
@ -48,12 +58,14 @@ public class WxMsgController {
@Autowired @Autowired
private WeChatQrcodeUtils weChatQrcodeUtils; private WeChatQrcodeUtils weChatQrcodeUtils;
@Resource
private HighMerchantStoreService merchantStoreService;
@RequestMapping(value = "/verifyWxToken", method = RequestMethod.GET) @RequestMapping(value = "/verifyWxToken", method = RequestMethod.POST)
@ResponseBody @ResponseBody
@ApiOperation(value = "验证servlet") @ApiOperation(value = "验证servlet")
public String verifyWxToken(HttpServletRequest request) { public void verifyWxToken(HttpServletRequest request , HttpServletResponse response) {
try { try {
log.info("进入公众号!!!!!!!"); log.info("进入公众号!!!!!!!");
@ -63,19 +75,69 @@ public class WxMsgController {
String notifyXml; String notifyXml;
notifyXml = IOUtil.inputStreamToString(request.getInputStream(), "UTF-8"); notifyXml = IOUtil.inputStreamToString(request.getInputStream(), "UTF-8");
log.info("微信支付系统发送的数据:" + notifyXml); log.info("微信系统发送的数据:" + notifyXml);
SortedMap<String, String> map = XmlUtil.parseXmlToTreeMap(notifyXml, "UTF-8"); SortedMap<String, String> 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"); 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() + "<a href='https://hsgcs.dctpay.com/hsgH5?accountId=000000&gasKey=" + map.get("EventKey") + "&staffCode='>点击这里一键加油</a>";
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() + "<a href='https://hsgcs.dctpay.com/hsgH5?accountId=000000&gasKey=" + map.get("EventKey") + "&staffCode='>点击这里一键加油</a>";
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) { } catch (Exception e) {
return null; // return null;
} }
} }
@RequestMapping(value = "/getWxToken", method = RequestMethod.POST) @RequestMapping(value = "/getWxToken", method = RequestMethod.POST)
@ResponseBody @ResponseBody
@ApiOperation(value = "获取微信token") @ApiOperation(value = "获取微信token")
@ -110,7 +172,7 @@ public class WxMsgController {
@RequestMapping(value = "/createQrcode", method = RequestMethod.GET) @RequestMapping(value = "/createQrcode", method = RequestMethod.GET)
@ResponseBody @ResponseBody
@ApiOperation(value = "创建生成二维码") @ApiOperation(value = "创建生成二维码")
public ResponseData createQrcode(@RequestParam(name = "sceneId", required = false) Integer sceneId) { public ResponseData createQrcode(@RequestParam(name = "sceneId", required = false) String sceneId) {
try { try {
return ResponseMsgUtil.success(weChatQrcodeUtils.qrCodeCreateLastTicket(sceneId)); return ResponseMsgUtil.success(weChatQrcodeUtils.qrCodeCreateLastTicket(sceneId));
} catch (WxErrorException e) { } catch (WxErrorException e) {

Loading…
Cancel
Save