|
@@ -36,9 +36,15 @@ import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.BufferedReader;
|
|
|
import java.io.File;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
import java.util.Arrays;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import static com.ylx.massage.utils.OtherUtil.verification;
|
|
@@ -137,6 +143,104 @@ public class WeChatController extends BaseController {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 处理微信公众号请求信息
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping("/verifyToken")
|
|
|
+ @ResponseBody
|
|
|
+ @Log(title = "处理微信公众号请求信息", businessType = BusinessType.OTHER)
|
|
|
+ public String handlePublicMsg(HttpServletRequest request) throws Exception {
|
|
|
+ log.info("处理微信公众号请求信息:{}", request.toString());
|
|
|
+ // 获得微信端返回的xml数据
|
|
|
+ InputStream is = null;
|
|
|
+ InputStreamReader isr = null;
|
|
|
+ BufferedReader br = null;
|
|
|
+ try {
|
|
|
+ is = request.getInputStream();
|
|
|
+ isr = new InputStreamReader(is, "utf-8");
|
|
|
+ br = new BufferedReader(isr);
|
|
|
+ String str = null;
|
|
|
+ StringBuffer returnXml= new StringBuffer();
|
|
|
+ while ((str = br.readLine()) != null) {
|
|
|
+ //返回的是xml数据
|
|
|
+ returnXml.append(str);
|
|
|
+ }
|
|
|
+ log.info("微信端返回的xml数据:{}", returnXml.toString());
|
|
|
+ Map<String, String> encryptMap = WeChatUtil.xmlToMap(returnXml.toString());
|
|
|
+ // 得到公众号传来的加密信息并解密,得到的是明文xml数据
|
|
|
+// String decryptXml = WXPublicUtils.decrypt(encryptMap.get("Encrypt"));
|
|
|
+ // 将xml数据转换为map
|
|
|
+// Map<String, String> decryptMap = WeChatUtil.xmlToMap(decryptXml);
|
|
|
+
|
|
|
+ // 区分消息类型
|
|
|
+ String msgType = encryptMap.get("MsgType");
|
|
|
+ // 普通消息
|
|
|
+ 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 ("shortvideo".equals(msgType)) { // 小视频消息
|
|
|
+ // todo 处理小视频消息
|
|
|
+ } else if ("location".equals(msgType)) { // 地理位置消息
|
|
|
+ // todo 处理地理位置消息
|
|
|
+ } else if ("link".equals(msgType)) { // 链接消息
|
|
|
+ // todo 处理链接消息
|
|
|
+ }
|
|
|
+ // 事件推送
|
|
|
+ else if ("event".equals(msgType)) { // 事件消息
|
|
|
+ // 区分事件推送
|
|
|
+ String event = encryptMap.get("Event");
|
|
|
+ if ("subscribe".equals(event)) { // 订阅事件 或 未关注扫描二维码事件
|
|
|
+ // 返回消息时ToUserName的值与FromUserName的互换
|
|
|
+ Map<String, String> returnMap = new HashMap<>();
|
|
|
+ returnMap.put("ToUserName", encryptMap.get("FromUserName"));
|
|
|
+ returnMap.put("FromUserName", encryptMap.get("ToUserName"));
|
|
|
+ returnMap.put("CreateTime", new Date().getTime()+"");
|
|
|
+ returnMap.put("MsgType", "text");
|
|
|
+ returnMap.put("Content", "https://www.baidu.com");
|
|
|
+ String encryptMsg = weChatUtil.mapToXml(returnMap).toString();
|
|
|
+ return encryptMsg;
|
|
|
+ } else if ("unsubscribe".equals(event)) { // 取消订阅事件
|
|
|
+ // todo 处理取消订阅事件
|
|
|
+ } else if ("SCAN".equals(event)) { // 已关注扫描二维码事件
|
|
|
+ // 返回消息时ToUserName的值与FromUserName的互换
|
|
|
+ Map<String, String> returnMap = new HashMap<>();
|
|
|
+ returnMap.put("ToUserName", encryptMap.get("FromUserName"));
|
|
|
+ returnMap.put("FromUserName", encryptMap.get("ToUserName"));
|
|
|
+ returnMap.put("CreateTime", new Date().getTime()+"");
|
|
|
+ returnMap.put("MsgType", "text");
|
|
|
+ returnMap.put("Content", "https://www.baidu.com");
|
|
|
+ String encryptMsg = WeChatUtil.mapToXml(returnMap).toString();
|
|
|
+ return encryptMsg;
|
|
|
+ } else if ("LOCATION".equals(event)) { // 上报地理位置事件
|
|
|
+ // todo 处理上报地理位置事件
|
|
|
+ } else if ("CLICK".equals(event)) { // 点击菜单拉取消息时的事件推送事件
|
|
|
+ // todo 处理点击菜单拉取消息时的事件推送事件
|
|
|
+ } else if ("VIEW".equals(event)) { // 点击菜单跳转链接时的事件推送
|
|
|
+ // todo 处理点击菜单跳转链接时的事件推送
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("处理微信公众号请求信息,失败", e);
|
|
|
+ } finally {
|
|
|
+ if (null != is) {
|
|
|
+ is.close();
|
|
|
+ }
|
|
|
+ if (null != isr) {
|
|
|
+ isr.close();
|
|
|
+ }
|
|
|
+ if (null != br) {
|
|
|
+ br.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
/**
|
|
|
* 获取微信code
|
|
|
*
|