Jelajahi Sumber

项目迭代

刘一凡 2 tahun lalu
induk
melakukan
7ce1dd428a

+ 49 - 88
src/main/java/com/chuanghai/h3c_reporting/controller/InformationReportingController.java

@@ -77,7 +77,6 @@ public class InformationReportingController {
     @Value("${wx.template_id}")
     private String template_id;
 
-    private WxController access_token_inf = new WxController();
 
     /**
      * 信息上传
@@ -187,11 +186,11 @@ public class InformationReportingController {
         //向方法传入openid、ps、title
         String ps = reporting.getPs();
         if (ps.equals("")) {
-            String a = send(openid, "无备注", "项目有效");
-            System.out.println("备注为:" + ps);
+            String a = wxController.send(openid, "无备注", "项目有效");
+            System.out.println("有效备注为(无备注):" + ps);
         }else {
-            String a = send(openid, ps, "项目有效");
-            System.out.println("备注为:" + ps);
+            String a = wxController.send(openid, ps, "项目有效");
+            System.out.println("有效备注为(有备注):" + ps);
         }
 
 
@@ -245,11 +244,11 @@ public class InformationReportingController {
         String ps = reporting.getPs();
 
         if (ps.equals("")) {
-            String a = send(openid, "无备注", "项目无效");
-            System.out.println("备注为:" + ps);
+            String a = wxController.send(openid, "无备注", "项目无效");
+            System.out.println("失效备注为(无备注):" + ps);
         }else {
-            String a = send(openid, ps, "项目无效");
-            System.out.println("备注为:" + ps);
+            String a = wxController.send(openid, ps, "项目无效");
+            System.out.println("失效备注为(有备注):" + ps);
         }
 
         //System.out.println(ps);
@@ -272,87 +271,49 @@ public class InformationReportingController {
     }
 
 
-    /**
-     * 小程序订阅消息
-     */
-    public String send(String openid, String ps, String title) {
-
-        // access_token是存在有效期的,过期就刷新
-        if (access_token_inf.access_token.equals("")) {
-            wxController.getAccessToken();
-//            getAccessToken();
-        }
-
-        System.out.println("订阅消息接口里的access_token_inf: " + wxController.access_token);
-
-        JSONObject message = new JSONObject();
-        message.put("touser", openid);
-        message.put("template_id", template_id);
-        //message.put("page", "");
-        message.put("miniprogram_state", "formal");
-        message.put("lang", "zh_CN");
-
-        JSONObject data = new JSONObject();
-
-        // 审核结果
-        JSONObject thing1 = new JSONObject();
-        thing1.put("value", title);//变为title
-
-        // 备注
-        JSONObject thing5 = new JSONObject();
-        thing5.put("value", ps);
 
-        // 审核结果
-        data.put("thing1", thing1);
-        // 备注
-        data.put("thing5", thing5);
 
-        message.put("data", data);
-        System.out.println(message);
-        return HttpsClient.sendJson("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + access_token_inf.access_token, message);
-    }
-
-    public String getOpenId(String code) throws IOException {
-
-        // access_token是存在有效期的,过期就刷新
-        if (access_token_inf.access_token.equals("")) {
-            wxController.getAccessToken();
-        }
-        System.out.println("access_token_inf: " + wxController.access_token);
-        // 构造请求URL
-        //String requestUrl = OPENID_URL + "?appid=" + appID + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
-        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appID + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
-
-        // 发送请求并获取响应
-        URL url = new URL(requestUrl);
-        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
-        connection.setRequestMethod("GET");
-        connection.setDoOutput(true);
-        connection.setDoInput(true);
-        connection.connect();
-        InputStream inputStream = connection.getInputStream();
-        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
-        StringBuilder stringBuilder = new StringBuilder();
-        String line;
-        while ((line = reader.readLine()) != null) {
-            stringBuilder.append(line);
-        }
-        reader.close();
-        connection.disconnect();
-
-        // 解析响应结果,提取openid
-        String response = stringBuilder.toString();
-        String openid = null;
-        if (response.contains("openid")) {
-            int beginIndex = response.indexOf("openid") + 9;
-            int endIndex = response.indexOf("\"", beginIndex);
-            openid = response.substring(beginIndex, endIndex);
-        }
-
-        //String openid="1234512345123451234512345123";
-
-        return openid;
-    }
+//    public String getOpenId(String code) throws IOException {
+//
+//        // access_token是存在有效期的,过期就刷新
+//        if (access_token_inf.access_token.equals("")) {
+//            wxController.getAccessToken();
+//        }
+//        System.out.println("access_token_inf: " + wxController.access_token);
+//        // 构造请求URL
+//        //String requestUrl = OPENID_URL + "?appid=" + appID + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
+//        String requestUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appID + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
+//
+//        // 发送请求并获取响应
+//        URL url = new URL(requestUrl);
+//        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+//        connection.setRequestMethod("GET");
+//        connection.setDoOutput(true);
+//        connection.setDoInput(true);
+//        connection.connect();
+//        InputStream inputStream = connection.getInputStream();
+//        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
+//        StringBuilder stringBuilder = new StringBuilder();
+//        String line;
+//        while ((line = reader.readLine()) != null) {
+//            stringBuilder.append(line);
+//        }
+//        reader.close();
+//        connection.disconnect();
+//
+//        // 解析响应结果,提取openid
+//        String response = stringBuilder.toString();
+//        String openid = null;
+//        if (response.contains("openid")) {
+//            int beginIndex = response.indexOf("openid") + 9;
+//            int endIndex = response.indexOf("\"", beginIndex);
+//            openid = response.substring(beginIndex, endIndex);
+//        }
+//
+//        //String openid="1234512345123451234512345123";
+//
+//        return openid;
+//    }
 
     public String getOpenidByCode(String code) {
         //页面获取openId接口

+ 12 - 42
src/main/java/com/chuanghai/h3c_reporting/controller/WxController.java

@@ -203,27 +203,24 @@ public class WxController {
         }*/
 
     }
-    /*
-    * 小程序订阅消息
-    *
-    * 前端传 openid,审核结果result,备注ps
-    * */
-   /* @GetMapping("/send")
-    public String send(String openid,String ps) throws Exception {
+
+    /**
+     * 小程序订阅消息
+     */
+    public String send(String openid, String ps, String title) {
 
         // access_token是存在有效期的,过期就刷新
         if (access_token.equals("")) {
             getAccessToken();
+//            getAccessToken();
         }
 
-        //拿到审核结果
-        InformationReporting reporting1 = new InformationReporting();
-        //String title = reporting1.getTitle();
+        System.out.println("订阅消息接口里的access_token: " + access_token);
 
         JSONObject message = new JSONObject();
         message.put("touser", openid);
         message.put("template_id", template_id);
-        message.put("page", "index");
+        //message.put("page", "");
         message.put("miniprogram_state", "formal");
         message.put("lang", "zh_CN");
 
@@ -238,39 +235,12 @@ public class WxController {
         thing5.put("value", ps);
 
         // 审核结果
-        data.put("thing1",thing1);
+        data.put("thing1", thing1);
         // 备注
-        data.put("thing5",thing5);
+        data.put("thing5", thing5);
 
         message.put("data", data);
         System.out.println(message);
-        return HttpsClient.sendJson("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+access_token, message);
-    }*/
-
-    //发送订阅消息
-    /*private String push(String openid) throws Exception{
-        RestTemplate restTemplate = new RestTemplate();
-
-        // access_token是存在有效期的,过期就刷新
-        if (access_token.equals("")) {
-            getAccessToken();
-        }
-        //getAccessToken
-        // 没有加redis,每次获取最新的sendURL
-        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + access_token;
-        //拼接推送的模版
-        Message message = new Message();
-        message.setTouser(openid);//用户的openid
-        message.setTemplate_id(template_id);//订阅消息模板id
-        //message.setPage("pages/index/index");
-
-        Map<String, TemplateData> m = new HashMap<>();
-        m.put("thing1", new TemplateData("审核结果"));
-        m.put("thing5", new TemplateData("备注"));
-
-        message.setData(m);
-        ResponseEntity<String> responseEntity =
-                restTemplate.postForEntity(url, message, String.class);
-        return responseEntity.getBody();
-    }*/
+        return HttpsClient.sendJson("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + access_token, message);
+    }
 }