Sfoglia il codice sorgente

fix 新增ocr接口

Administrator 3 anni fa
parent
commit
b4c5d4f2e3

+ 36 - 0
boman-modules/boman-file/src/main/java/com/boman/file/controller/SysFileController.java

@@ -1,7 +1,9 @@
 package com.boman.file.controller;
 
+import com.alibaba.fastjson.JSONObject;
 import com.boman.domain.dto.AjaxResult;
 import com.boman.domain.dto.Base64Dto;
+import com.boman.file.utils.HttpClientUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -15,6 +17,10 @@ import com.boman.common.core.utils.file.FileUtils;
 import com.boman.file.service.ISysFileService;
 import com.boman.domain.SysFile;
 
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * 文件请求处理
  * 
@@ -71,4 +77,34 @@ public class SysFileController
     public String getConfigPath(){
         return sysFileService.getConfigPath();
     }
+
+
+    /**
+     * ocr身份证识别,微信接口
+     * @return
+     */
+    @PostMapping("/ocrIdCard")
+    public String ocrIdCard(MultipartFile file){
+        String accessTokenResult = getAccessToken();
+        JSONObject jsonObject = JSONObject.parseObject(accessTokenResult);
+        String accessToken = jsonObject.getString("access_token");
+        //https://api.weixin.qq.com/cv/ocr/idcard?type=MODE&img_url=ENCODE_URL&access_token=ACCESS_TOCKEN
+        try {
+            String url = sysFileService.uploadFile(file);
+            String result = HttpClientUtils.doPost("https://api.weixin.qq.com/cv/ocr/idcard?type=photo&img_url="+url + "&access_token=" + accessToken,null);
+            System.out.println(result);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+    private static String getAccessToken(){
+        String accessToken = "";
+        try {
+            accessToken = HttpClientUtils.doGet1("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxb9b83f3c86545690&secret=95adc6921a24a3c6cff55f2a1290f6f6");
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return accessToken;
+    }
 }

+ 112 - 0
boman-modules/boman-file/src/main/java/com/boman/file/utils/HttpClientUtils.java

@@ -0,0 +1,112 @@
+package com.boman.file.utils;
+
+import org.apache.http.NameValuePair;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.util.EntityUtils;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+public class HttpClientUtils {
+
+    final static int TIMEOUT = 1000;
+    final static int TIMEOUT_MSEC = 5 * 1000;
+
+    public static String doPost(String url, Map<String, String> paramMap) throws IOException {
+        // 创建Httpclient对象
+        CloseableHttpClient httpClient = HttpClients.createDefault();
+        CloseableHttpResponse response = null;
+        String resultString = "";
+        try {
+            // 创建Http Post请求
+            HttpPost httpPost = new HttpPost(url);
+            // 创建参数列表
+            if (paramMap != null) {
+                List<NameValuePair> paramList = new ArrayList<>();
+                for (Map.Entry<String, String> param : paramMap.entrySet()) {
+                    paramList.add(new BasicNameValuePair(param.getKey(), param.getValue()));
+                }
+                // 模拟表单
+                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
+                httpPost.setEntity(entity);
+            }
+
+            httpPost.setConfig(builderRequestConfig());
+
+            // 执行http请求
+            response = httpClient.execute(httpPost);
+
+            resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
+        } catch (Exception e) {
+            throw e;
+        } finally {
+            try {
+                response.close();
+            } catch (IOException e) {
+                throw e;
+            }
+        }
+
+        return resultString;
+    }
+
+    public static String doGet(String url, Map<String, String> paramMap) throws IOException {
+        url += "?appid=" + paramMap.get("appid") + "&secret=" + paramMap.get("secret") + "&js_code=" + paramMap.get("js_code") + "&grant_type=" + paramMap.get("grant_type");
+        // 创建Httpclient对象
+        CloseableHttpClient httpClient = HttpClients.createDefault();
+        CloseableHttpResponse response = null;
+        String resultString = "";
+        try {
+            // 创建Http Post请求
+            HttpGet httpGet = new HttpGet(url);
+            // 执行http请求
+            response = httpClient.execute(httpGet);
+            resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
+        } catch (Exception e) {
+            throw e;
+        } finally {
+            try {
+                response.close();
+            } catch (IOException e) {
+                throw e;
+            }
+        }
+        return resultString;
+    }
+
+    public static String doGet1(String url) throws IOException {
+        // 创建Httpclient对象
+        CloseableHttpClient httpClient = HttpClients.createDefault();
+        CloseableHttpResponse response = null;
+        String resultString = "";
+        try {
+            // 创建Http Post请求
+            HttpGet httpGet = new HttpGet(url);
+            // 执行http请求
+            response = httpClient.execute(httpGet);
+            resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
+        } catch (Exception e) {
+            throw e;
+        } finally {
+            assert response != null;
+            response.close();
+        }
+        return resultString;
+    }
+
+    private static RequestConfig builderRequestConfig() {
+        return RequestConfig.custom()
+                .setConnectTimeout(TIMEOUT_MSEC)
+                .setConnectionRequestTimeout(TIMEOUT_MSEC)
+                .setSocketTimeout(TIMEOUT_MSEC).build();
+    }
+}

+ 1 - 32
boman-wechat/src/main/java/com/boman/wechat/controller/AppletLoginController.java

@@ -7,6 +7,7 @@ import com.boman.domain.dto.AppletSessionDTO;
 import com.boman.domain.dto.R;
 import com.boman.wechat.utils.HttpClientUtils;
 import com.boman.wechat.utils.WxCodeSessionUtil;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -38,36 +39,4 @@ public class AppletLoginController {
         AppletSessionDTO dto = codeUtil.jscode2Session(form);
         return tokenService.appletLogin(dto.getPhoneNumber());
     }
-
-    /**
-     * ocr身份证识别,微信接口
-     * @return
-     */
-    @PostMapping("/ocrIdCard")
-    public String ocrIdCard(MultipartFile file){
-        String accessTokenResult = getAccessToken();
-        JSONObject jsonObject = JSONObject.parseObject(accessTokenResult);
-        String accessToken = jsonObject.getString("access_token");
-        //https://api.weixin.qq.com/cv/ocr/idcard?type=MODE&img_url=ENCODE_URL&access_token=ACCESS_TOCKEN
-        Map<String,String> map = new HashMap<>();
-        try {
-            String encode = URLEncoder.encode("http://118.178.139.79:5002/statics/2021/09/16/1631776710012.jpg", "utf-8");
-            map.put("img_url","http://118.178.139.79:5002/statics/2021/09/16/1631776710012.jpg");
-            map.put("access_token",accessToken);
-            String s = HttpClientUtils.doPost("https://api.weixin.qq.com/cv/ocr/idcard?type=photo&img_url=http://118.178.139.79:5002/statics/2021/09/16/1631776710012.jpg" + "&access_token=" + accessToken,null);
-            System.out.println(s);
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-        return null;
-    }
-    private static String getAccessToken(){
-        String accessToken = "";
-        try {
-            accessToken = HttpClientUtils.doGet1("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxb9b83f3c86545690&secret=95adc6921a24a3c6cff55f2a1290f6f6");
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-        return accessToken;
-    }
 }