home il y a 2 ans
Parent
commit
31993897ea

+ 44 - 29
hx-service/weixin/src/main/java/com/fjhx/controller/TestController.java

@@ -1,8 +1,9 @@
 package com.fjhx.controller;
 
+import cn.hutool.core.io.IoUtil;
 import com.fjhx.constants.WeChatConstants;
+import com.fjhx.utils.aes.AesException;
 import com.fjhx.utils.aes.WXBizMsgCrypt;
-import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
@@ -12,13 +13,16 @@ import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
 import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
 
 import javax.servlet.ServletInputStream;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
 import java.io.BufferedReader;
+import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.StringReader;
 
@@ -52,7 +56,7 @@ public class TestController {
         // 随机字符串
         String echoStr = request.getParameter("echostr");
 
-        log.info("data:   msg_signature:{}; timestamp:{}; nonce:{}; echostr:{}", msgSignature, timestamp, nonce, echoStr);
+        log.error("data:   msg_signature:{}; timestamp:{}; nonce:{}; echostr:{}", msgSignature, timestamp, nonce, echoStr);
 
         try {
             WXBizMsgCrypt wxBizMsgCrypt = new WXBizMsgCrypt(WeChatConstants.TOKEN, WeChatConstants.ENCODING_AES_KEY, WeChatConstants.CORP_ID);
@@ -72,9 +76,8 @@ public class TestController {
      *
      * @link { https://developer.work.weixin.qq.com/tutorial/detail/38 }
      */
-    @SneakyThrows
     @PostMapping(value = "/command")
-    public String command(HttpServletRequest request, HttpServletResponse response) {
+    public String command(HttpServletRequest request) {
         // 微信加密签名
         String sReqMsgSig = request.getParameter("msg_signature");
         // 时间戳
@@ -84,32 +87,44 @@ public class TestController {
 
         // 密文,对应POST请求的数据
         StringBuilder sReqData = new StringBuilder();
-        ServletInputStream in = request.getInputStream();
-        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
-        String tempStr;
-        while (null != (tempStr = reader.readLine())) {
-            sReqData.append(tempStr);
-        }
 
-        WXBizMsgCrypt wxBizMsgCrypt = new WXBizMsgCrypt(WeChatConstants.TOKEN, WeChatConstants.ENCODING_AES_KEY, WeChatConstants.CORP_ID);
-
-        // 刷新ticket,AuthCode
-        String sMsg = wxBizMsgCrypt.DecryptMsg(sReqMsgSig, sReqTimeStamp, sReqNonce, sReqData.toString());
-        log.error("command:" + sMsg);
-        // TODO: 解析出明文xml标签的内容进行处理
-        // For example:
-        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
-        DocumentBuilder db = dbf.newDocumentBuilder();
-        StringReader sr = new StringReader(sMsg);
-        InputSource is = new InputSource(sr);
-        Document document = db.parse(is);
-
-        Element root = document.getDocumentElement();
-        NodeList nodelist = root.getElementsByTagName("Content");
-
-        for (int i = 0; i < nodelist.getLength(); i++) {
-            String Content = nodelist.item(i).getTextContent();
-            System.out.println("Content" + i + ": " + Content);
+        BufferedReader reader = null;
+        try {
+            ServletInputStream in = request.getInputStream();
+            reader = new BufferedReader(new InputStreamReader(in));
+
+            String tempStr;
+            while (null != (tempStr = reader.readLine())) {
+                sReqData.append(tempStr);
+            }
+
+            WXBizMsgCrypt wxBizMsgCrypt = new WXBizMsgCrypt(WeChatConstants.TOKEN, WeChatConstants.ENCODING_AES_KEY, WeChatConstants.CORP_ID);
+
+            // 刷新ticket,AuthCode
+            String sMsg = wxBizMsgCrypt.DecryptMsg(sReqMsgSig, sReqTimeStamp, sReqNonce, sReqData.toString());
+
+            log.error("command:" + sMsg);
+
+            // TODO: 解析出明文xml标签的内容进行处理
+            // For example:
+            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+            DocumentBuilder db = dbf.newDocumentBuilder();
+            StringReader sr = new StringReader(sMsg);
+            InputSource is = new InputSource(sr);
+            Document document = db.parse(is);
+
+            Element root = document.getDocumentElement();
+            NodeList nodelist = root.getElementsByTagName("Content");
+
+            for (int i = 0; i < nodelist.getLength(); i++) {
+                String Content = nodelist.item(i).getTextContent();
+                System.out.println("Content" + i + ": " + Content);
+            }
+        } catch (IOException | SAXException | ParserConfigurationException | AesException e) {
+            e.printStackTrace();
+            return "error";
+        } finally {
+            IoUtil.close(reader);
         }
 
         return "success";

+ 9 - 9
hx-service/weixin/src/main/java/com/fjhx/utils/Sample.java

@@ -1,14 +1,14 @@
 package com.fjhx.utils;
 
-import com.fjhx.utils.aes.WXBizMsgCrypt;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.NodeList;
-import org.xml.sax.InputSource;
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import java.io.StringReader;
+//import com.fjhx.utils.aes.WXBizMsgCrypt;
+//import org.w3c.dom.Document;
+//import org.w3c.dom.Element;
+//import org.w3c.dom.NodeList;
+//import org.xml.sax.InputSource;
+//
+//import javax.xml.parsers.DocumentBuilder;
+//import javax.xml.parsers.DocumentBuilderFactory;
+//import java.io.StringReader;
 
 public class Sample {
 

+ 40 - 8
hx-service/weixin/src/main/java/com/fjhx/utils/WeixinUtil.java

@@ -12,34 +12,66 @@ public class WeixinUtil {
     private static final RestTemplate restTemplate = new RestTemplate();
 
 //    public static void main(String[] args) {
-//        String suiteToken = getSuiteToken("dsaasdas");
-//        System.out.println(suiteToken);
+//        String suiteToken = code2Session("dasasd");
 //    }
 
+
+    /**
+     * 由企业微信后台定时推送给"指令回调URL"解析出的suiteTicket
+     *
+     * @return suiteTicket
+     */
+    public static String getSuiteTicket() {
+        return "test";
+    }
+
     /**
      * 获取第三方应用凭证
+     * TODO 缓存token
      *
-     * @param suiteTicket
-     * @return
+     * @return suite_access_token
      * @link { https://developer.work.weixin.qq.com/document/path/90600 }
      */
-    public static String getSuiteToken(String suiteTicket) {
+    public static String getSuiteToken() {
         Map<String, String> body = new HashMap<>();
         body.put("suite_id", WeChatConstants.SUITE_ID);
         body.put("suite_secret", WeChatConstants.SUITE_SECRET);
-        body.put("suite_ticket", suiteTicket);
+        body.put("suite_ticket", getSuiteTicket());
 
         String resultStr = restTemplate.postForObject(
                 "https://qyapi.weixin.qq.com/cgi-bin/service/get_suite_token", body, String.class);
 
-
         JSONObject jsonObject = JSONObject.parseObject(resultStr);
 
         System.out.println(jsonObject);
 
-
         return resultStr;
     }
 
+    /**
+     * 获取 session_key、用户userid以及用户所在企业的corpid等信息
+     * TODO 缓存token
+     *
+     * @param jsCode 前端传入临时授权码
+     * @return {
+     * <p> corpid: 用户所属企业的corpid
+     * <p> userid: 用户在企业内的UserID,对应管理端的账号,企业内唯一。注意:如果用户所在企业并没有安装此小程序应用,则返回加密的userid
+     * <p> session_key: 会话密钥
+     * <p> open_userid: 全局唯一。对于同一个服务商,不同应用获取到企业内同一个成员的open_userid是相同的,最多64个字节;同一用户,对于不同服务商open_userid是不同的
+     * <p> errcode: 返回码
+     * <p> errmsg: 对返回码的文本描述内容
+     * }
+     * @link { https://developer.work.weixin.qq.com/document/path/92423 }
+     */
+    public static String code2Session(String jsCode) {
+        String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/service/miniprogram/jscode2session" +
+                "?suite_access_token=%s&js_code=%s&grant_type=authorization_code", getSuiteToken(), jsCode);
+
+        String result = restTemplate.getForObject(url, String.class);
+
+        System.out.println(result);
+
+        return result;
+    }
 
 }