Explorar o código

1、部分修改测试

Bingo %!s(int64=3) %!d(string=hai) anos
pai
achega
8dea209168

+ 6 - 1
pom.xml

@@ -70,10 +70,15 @@
         </dependency>
         <!--        spring Boot JDBC-->
         <!--        MySQL版本不同需要的连接jar包的版本也不同-->
+<!--        <dependency>-->
+<!--            <groupId>mysql</groupId>-->
+<!--            <artifactId>mysql-connector-java</artifactId>-->
+<!--            <version>5.1.6</version>-->
+<!--        </dependency>-->
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
-            <version>5.1.6</version>
+            <scope>runtime</scope>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>

+ 40 - 40
src/main/java/com/chuanghai/repair/auth/CacheListener.java

@@ -1,40 +1,40 @@
-package com.chuanghai.repair.auth;
-
-import com.chuanghai.repair.service.ServiceImpl.CacheManagerImpl;
-import com.chuanghai.repair.utils.CreateTokenUtil;
-
-import java.text.SimpleDateFormat;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.logging.Logger;
-
-/**
- * @Author: bingo
- * @Date: 2022/3/4 星期五 13:09
- * @Description: com.chuanghai.repair.auth
- * @version: 1.0
- */
-public class CacheListener {
-    Logger logger = Logger.getLogger("cacheLog");
-    private CacheManagerImpl cacheManagerImpl;
-
-    public CacheListener(CacheManagerImpl cacheManagerImpl) {
-        this.cacheManagerImpl = cacheManagerImpl;
-    }
-
-    public void startListen() {
-        new Thread() {
-            public void run() {
-                    while (true) {
-                        for (String key : cacheManagerImpl.getAllKeys()) {
-                            if (cacheManagerImpl.isTimeOut(key)) {
-                                cacheManagerImpl.clearByKey(key);
-                                logger.info(key + "缓存被清除");
-                            }
-                        }
-                    }
-            }
-        }.start();
-
-    }
-}
+//package com.chuanghai.repair.auth;
+//
+//import com.chuanghai.repair.service.ServiceImpl.CacheManagerImpl;
+//import com.chuanghai.repair.utils.CreateTokenUtil;
+//
+//import java.text.SimpleDateFormat;
+//import java.util.Calendar;
+//import java.util.Date;
+//import java.util.logging.Logger;
+//
+///**
+// * @Author: bingo
+// * @Date: 2022/3/4 星期五 13:09
+// * @Description: com.chuanghai.repair.auth
+// * @version: 1.0
+// */
+//public class CacheListener {
+//    Logger logger = Logger.getLogger("cacheLog");
+//    private CacheManagerImpl cacheManagerImpl;
+//
+//    public CacheListener(CacheManagerImpl cacheManagerImpl) {
+//        this.cacheManagerImpl = cacheManagerImpl;
+//    }
+//
+//    public void startListen() {
+//        new Thread() {
+//            public void run() {
+//                    while (true) {
+//                        for (String key : cacheManagerImpl.getAllKeys()) {
+//                            if (cacheManagerImpl.isTimeOut(key)) {
+//                                cacheManagerImpl.clearByKey(key);
+//                                logger.info(key + "缓存被清除");
+//                            }
+//                        }
+//                    }
+//            }
+//        }.start();
+//
+//    }
+//}

+ 19 - 18
src/main/java/com/chuanghai/repair/controller/AutoSendOrderCentroller.java

@@ -1,7 +1,7 @@
 package com.chuanghai.repair.controller;
 
 
-import com.chuanghai.repair.auth.CacheListener;
+//import com.chuanghai.repair.auth.CacheListener;
 import com.chuanghai.repair.config.WeixiaoConfig;
 import com.chuanghai.repair.entity.RepairsOrder;
 import com.chuanghai.repair.service.RepairsOrderService;
@@ -37,7 +37,8 @@ import java.util.stream.Collectors;
 
 @RestController
 @RequestMapping("/auto")
-public class AutoSendOrderCentroller implements ApplicationListener<ContextRefreshedEvent> {
+//public class AutoSendOrderCentroller implements ApplicationListener<ContextRefreshedEvent> {
+public class AutoSendOrderCentroller {
 
     @Resource(name = "repairsOrderService")
     private RepairsOrderService repairsOrderService;
@@ -50,16 +51,16 @@ public class AutoSendOrderCentroller implements ApplicationListener<ContextRefre
     @Resource
     private WeixiaoConfig weixiaoConfig;
 
-    @Override
-    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
-        String accessTokenGlobal = new CreateTokenUtil().getAccessTokenGlobalA(weixiaoConfig.getAppKey(),weixiaoConfig.getAppSecret(),weixiaoConfig.getOcode(),weixiaoConfig.getServerUrl());
-        CacheManagerImpl cacheManagerImpl = new CacheManagerImpl();
-        CacheListener cacheListener = new CacheListener(cacheManagerImpl);
-        cacheManagerImpl.putCache("cache_token", accessTokenGlobal, 2*3610 * 1000L); //access_token 在2*3600*1000L 过期
-        cacheListener.startListen();
-        autoQueryOrder();
-
-    }
+//    @Override
+//    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
+//        String accessTokenGlobal = new CreateTokenUtil().getAccessTokenGlobalA(weixiaoConfig.getAppKey(),weixiaoConfig.getAppSecret(),weixiaoConfig.getOcode(),weixiaoConfig.getServerUrl());
+//        CacheManagerImpl cacheManagerImpl = new CacheManagerImpl();
+//        CacheListener cacheListener = new CacheListener(cacheManagerImpl);
+//        cacheManagerImpl.putCache("cache_token", accessTokenGlobal, 2*3610 * 1000L); //access_token 在2*3600*1000L 过期
+//        cacheListener.startListen();
+//        autoQueryOrder();
+//
+//    }
 
     /**
      * 30分钟查询一次已接订单的完成时间,30分钟发送通知一次
@@ -67,10 +68,10 @@ public class AutoSendOrderCentroller implements ApplicationListener<ContextRefre
     @Scheduled(cron = "0 0/30 * * * ?") //    0 0/30 * * * ?    30分钟调用一次
     public void autoQueryOrder(){
         String accessTokenGlobal = new CreateTokenUtil().getAccessTokenGlobalA(weixiaoConfig.getAppKey(),weixiaoConfig.getAppSecret(),weixiaoConfig.getOcode(),weixiaoConfig.getServerUrl());
-        CacheManagerImpl cacheManagerImpl = new CacheManagerImpl();
-        CacheListener cacheListener = new CacheListener(cacheManagerImpl);
-        cacheManagerImpl.putCache("cache_token", accessTokenGlobal, 2*3610 * 1000L); //access_token 在2*3600*1000L 过期
-        cacheListener.startListen();
+//        CacheManagerImpl cacheManagerImpl = new CacheManagerImpl();
+//        CacheListener cacheListener = new CacheListener(cacheManagerImpl);
+//        cacheManagerImpl.putCache("cache_token", accessTokenGlobal, 2*3610 * 1000L); //access_token 在2*3600*1000L 过期
+//        cacheListener.startListen();
         try {
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             Calendar calendar = Calendar.getInstance();
@@ -82,14 +83,14 @@ public class AutoSendOrderCentroller implements ApplicationListener<ContextRefre
 
             String idList = orderList.stream().filter(e -> e.getRepairsStudent() != null)
                     .map(e -> e.getRepairsStudent().getStudentId()).collect(Collectors.toSet()).toString();
-            String access_token = new CacheManagerImpl().getCacheByKey("cache_token").getDatas().toString();
+//            String access_token = new CacheManagerImpl().getCacheByKey("cache_token").getDatas().toString();
 
             Map<String, String> param = new HashMap<>();
             param.put("title", weixiaoConfig.getTitle());
             param.put("cards", idList);
             param.put("content", weixiaoConfig.getContent());
             param.put("sender", weixiaoConfig.getSender());
-            String url = "https://open.wecard.qq.com/cgi-bin/notice/send?access_token=" + access_token;
+            String url = "https://open.wecard.qq.com/cgi-bin/notice/send?access_token=" + accessTokenGlobal;
 
             RestTemplate restTemplate = new RestTemplate();
             HttpHeaders headers = new HttpHeaders();

+ 122 - 2
src/main/java/com/chuanghai/repair/controller/WxController.java

@@ -1,7 +1,9 @@
 package com.chuanghai.repair.controller;
 
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.chuanghai.repair.VO.IdCardNameVO;
 import com.chuanghai.repair.config.WeixiaoConfig;
 import com.chuanghai.repair.entity.RepairsStudent;
 import com.chuanghai.repair.service.RepairsAdminService;
@@ -9,6 +11,10 @@ import com.chuanghai.repair.service.RepairsOpenIDService;
 import com.chuanghai.repair.service.RepairsStudentService;
 import com.chuanghai.repair.service.RepairsWorkService;
 import com.chuanghai.repair.utils.CreateTokenUtil;
+import com.chuanghai.repair.utils.ReturnValueUtil;
+import com.chuanghai.repair.utils.UnicodeUtil;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpEntity;
@@ -66,8 +72,8 @@ public class WxController {
         String cardNumber = "";
         try {
             String grant_type = "authorization_code";
-           // String redirect_uri = "https://jtishfw.ncjti.edu.cn/baoxiu/repairApi/wx/getCardNumber";
-            String redirect_uri = "http://binguo.vaiwan.com/wx/getCardNumber";
+            String redirect_uri = "https://jtishfw.ncjti.edu.cn/baoxiu/repairApi/wx/getCardNumber";
+//            String redirect_uri = "http://binguo.vaiwan.com/wx/getCardNumber";
             String url = "https://open.wecard.qq.com/connect/oauth2/token"; //用于用户 获取基础的access_token
             Map<String, String> param = new HashMap<>();
             param.put("wxcode", wxcode);
@@ -139,6 +145,7 @@ public class WxController {
                 }
             }
             System.out.println("cardNumber-----------------"+cardNumber);
+            System.out.println("wxcode-----------------"+wxcode);
             System.out.println("accessTokenaccessTokenaccessToken--------    "+accessToken);
 
             RepairsStudent repairsStudent = new RepairsStudent(studentId, studentName, studentSex,
@@ -158,4 +165,117 @@ public class WxController {
 
     }
 
+
+    /**
+     * 微校授权回调地址
+     *
+     * @param wxcode 微校code
+     * @param state  透传数据
+     * @return
+     * @apiNote 用户跳转到微校授权链接上,微校服务器接收用户授权后会携带wxcode回调到该接口上,该接口使用wxcode获取到用户的信息。
+     * 如果用户信息获取成功,该接口会以 【fontendUrl + /?cardNumber=wx123456&name=测试用户】 的形式跳回到前端应用页面;
+     * 如果用户信息获取失败,该接口会以 【fontendUrl + /error/?errorMsg=获取用户信息失败】 的形式跳回到前端应用页面。
+     * <strong>注意:</strong>
+     * fontendUrl + 路由地址:前端项目地址+页面路由地址,用于前端页面接收用户的信息或错误信息,该地址需要前端人员与后台开发人员协调好。
+     * </p>
+     */
+    @GetMapping("weixiao/auth")
+    public String weixiaoAuth(String wxcode, String state) {
+        String idCard = "";
+        String name = "";
+
+        String tokenUrl = "https://open.wecard.qq.com/connect/oauth2/token";
+        Map<String, String> tokenParams = new HashMap<>();
+        tokenParams.put("wxcode", wxcode);
+        tokenParams.put("app_key", weixiaoConfig.getAppKey());
+        tokenParams.put("app_secret", weixiaoConfig.getAppSecret());
+        tokenParams.put("grant_type", "authorization_code");
+        tokenParams.put("redirect_uri", state); // 该地址需要与发起授权地址保持一直
+
+        try {
+            // wecode换取token
+            RestTemplate client = new RestTemplate();
+            HttpHeaders headers = new HttpHeaders();
+            headers.setContentType(MediaType.APPLICATION_JSON);
+            HttpEntity<Map<String, String>> request = new HttpEntity<>(tokenParams, headers);
+            //ResponseEntity<String> tokenResponse = client.postForEntity(tokenUrl, tokenParams, String.class);
+
+            ResponseEntity<String> tokenResponse = client.postForEntity(tokenUrl, request, String.class);
+
+            ObjectMapper mapper = new ObjectMapper();
+            Map<String, Object> jsonMap = mapper.readValue(tokenResponse.getBody(), new TypeReference<Map<String, Object>>() {
+            });
+            String accessToken = (String) jsonMap.get("access_token");
+
+            // token换取用户信息
+            String userInfoUrl = "https://open.wecard.qq.com/connect/oauth/get-user-info";
+            Map<String, String> userInfoParam = new HashMap<>();
+            userInfoParam.put("access_token", accessToken);
+            ResponseEntity<String> userInfoResponse = client.postForEntity(userInfoUrl, userInfoParam, String.class);
+
+            Map<String, Object> userInfoMap = mapper.readValue(userInfoResponse.getBody(), new TypeReference<Map<String, Object>>() {
+            });
+            String cardNumber = userInfoMap.get("card_number").toString();
+
+            String idCardName = getIdCard(cardNumber, accessToken);
+            String[] split = idCardName.split("-");
+            if (split.length > 0) {
+                idCard = split[0];
+                name = split[1];
+            }
+            String encodeName =URLEncoder.encode(name,"utf-8");
+//            return "redirect:" + "http://192.168.161.36:8080/jiaofei/qianyueH5/#/pages/index/index/?idCard=" + idCard + "&name=" + encodeName;
+//
+            return "redirect:" + "https://chtech.ncjti.edu.cn/jiaofei/qianyueH5/#/pages/index/index/?idCard=" + idCard + "&name=" + encodeName;
+        } catch (Exception e) {
+            e.printStackTrace();
+            return "redirect:" + "https://chtech.ncjti.edu.cn/jiaofei/qianyueH5/#/pages/index/index/#/error/";
+        }
+    }
+
+
+    /**
+     * 通过card_number查询身份证
+     *
+     * @param cardNumber
+     * @return
+     */
+    @GetMapping("getIdCard")
+    @ResponseBody
+    public String getIdCard(String cardNumber, String accessToken) {
+
+        RestTemplate client = new RestTemplate();
+        ObjectMapper mapper = new ObjectMapper();
+        String userInfoUrl = "https://open.wecard.qq.com/cgi-bin/user/search";
+        Map<String, String> userInfoParam = new HashMap<>();
+        userInfoParam.put("access_token", accessToken);
+        userInfoParam.put("keywords", cardNumber);
+        ResponseEntity<String> userInfoResponse = client.postForEntity(userInfoUrl, userInfoParam, String.class);
+        JSONObject jsonObj = JSON.parseObject(userInfoResponse.getBody());
+        JSONArray userlist = jsonObj.getJSONArray("userlist");
+        String id_card = "";
+        String name = "";
+        try {
+            Map<String, Object> userInfoMap = mapper.readValue(userlist.get(0).toString(), new TypeReference<Map<String, Object>>() {
+            });
+
+            if (userInfoMap.get("id_card") != null) {
+                id_card = userInfoMap.get("id_card").toString();
+            }
+            if (userInfoMap.get("name") != null) {
+                name = userInfoMap.get("name").toString();
+            }
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+
+        return id_card + "-" + name;
+    }
+
+
+
+
+
 }

+ 1 - 1
src/main/java/com/chuanghai/repair/utils/CreateTokenUtil.java

@@ -153,7 +153,7 @@ public class CreateTokenUtil {
     }
 
 
-    @Scheduled(cron = "0 59 0/1 * * ?") //    每隔 1hour59minutes 执行一次刷新access_token
+   // @Scheduled(cron = "0 59 0/1 * * ?") //    每隔 1hour59minutes 执行一次刷新access_token
     public static String getAccessTokenGlobal() {
         String access_token_global = "";
         try {

+ 5 - 4
src/main/resources/application.yml

@@ -10,12 +10,12 @@ spring:
     date-format: yyyy-MM-dd HH:mm:ss
     time-zone: GMT+8
   datasource:
-    url: jdbc:mysql://127.0.0.1:3306/repairs?characterEncoding=utf-8&useSSL=FALSE&useAffectedRows=TRUE&allowMultiQueries=true&serverTimezone=GMT%2B8
-    #url: jdbc:mysql://192.168.1.41:3306/repairs?characterEncoding=utf-8&useSSL=FALSE&useAffectedRows=TRUE&allowMultiQueries=true&serverTimezone=GMT%2B8
+#    url: jdbc:mysql://127.0.0.1:3306/repairs?characterEncoding=utf-8&useSSL=FALSE&useAffectedRows=TRUE&allowMultiQueries=true&serverTimezone=GMT%2B8
+    url: jdbc:mysql://192.168.1.41:3306/repairs?characterEncoding=utf-8&useSSL=FALSE&useAffectedRows=TRUE&allowMultiQueries=true&serverTimezone=GMT%2B8
 
-    driver-class-name: com.mysql.jdbc.Driver
+    #driver-class-name: com.mysql.jdbc.Driver
     #    mysql6以上需要使用下面的连接配置
-    #    driver-class-name: com.mysql.cj.jdbc.Driver
+    driver-class-name: com.mysql.cj.jdbc.Driver
     username: root
     password: root
 
@@ -60,6 +60,7 @@ img:
 weixiao:
   app_key: EE28EE2C93296F4E
   app_secret: 5071958561AA629530AAA31503088330
+
   ocode: 1015730314
   #serverUrl: http://10.244.137.3:8084
   requestUrl: https://jtishfw.ncjti.edu.cn/baoxiu/repair-h5