Browse Source

随身邦 E签宝 文档上传 及日志配置bug修改

qxp 4 years ago
parent
commit
cfc5633a92

+ 1 - 1
suishenbang-admin/src/main/resources/application-dev.yml

@@ -10,7 +10,6 @@ ruoyi:
   demoEnabled: true
   # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath)
   profile: D:/home
-  logPath: D:/home/log/
   # 获取ip地址开关
   addressEnabled: true
   cloudPath: http://10.32.2.212:8000/
@@ -42,6 +41,7 @@ logging:
   level:
     com.dgtly: debug
     org.springframework: warn
+  path: D:/home/logs/
 
 
 

+ 1 - 1
suishenbang-admin/src/main/resources/application-prod.yml

@@ -10,7 +10,6 @@ ruoyi:
   demoEnabled: true
   # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath)
   profile: /data/project/file
-  logPath: /data/project/logs/
   # 获取ip地址开关
   addressEnabled: false
   cloudPath: http://127.0.0.1:8000
@@ -42,6 +41,7 @@ logging:
   level:
     com.dgtly: debug
     org.springframework: warn
+  path: /data/project/logs/
 
 # 用户配置
 user:

+ 1 - 1
suishenbang-admin/src/main/resources/application-test.yml

@@ -10,7 +10,6 @@ ruoyi:
   demoEnabled: true
   # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath)
   profile: /home/suishenbang/file
-  logPath: /home/suishenbang/logs/
   # 获取ip地址开关
   addressEnabled: true
   cloudPath: http://10.32.2.212:8000/
@@ -42,6 +41,7 @@ logging:
   level:
     com.dgtly: debug
     org.springframework: warn
+  path: /home/suishenbang/logs/
 
 # 用户配置
 user:

+ 1 - 1
suishenbang-admin/src/main/resources/application-uat.yml

@@ -10,7 +10,6 @@ ruoyi:
   demoEnabled: true
   # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath)
   profile: /home/admin/project/file
-  logPath: /home/admin/project/logs/
   # 获取ip地址开关
   addressEnabled: false
   cloudPath: http://127.0.0.1:8000
@@ -42,6 +41,7 @@ logging:
   level:
     com.dgtly: debug
     org.springframework: warn
+  path: /home/admin/project/logs/
 
 # 用户配置
 user:

+ 1 - 2
suishenbang-admin/src/main/resources/logback.xml

@@ -1,8 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <configuration>
-    <springProperty scope="context" name="logPath" source="ruoyi.logPath" defaultValue="/home/admin/project"/>
     <!-- 日志存放路径 -->
-    <property name="log.path"  value="${logPath}"/>
+    <property name="log.path"  value="${LOG_PATH:-.}"/>
     <!-- 日志输出格式 -->
 	<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
 

+ 1 - 1
suishenbang-api/src/main/resources/application-dev.yml

@@ -8,7 +8,6 @@ ruoyi:
   copyrightYear: 2020
   # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath)
   profile: D:/home
-  logPath: D:/home/logs/
   # 获取ip地址开关
   addressEnabled: true
   #是否开启swagger
@@ -45,6 +44,7 @@ logging:
   level:
     com.dgtly: debug
     org.springframework: warn
+  path: D:/home/logs/
 # Spring配置
 spring:
   jackson:

+ 1 - 1
suishenbang-api/src/main/resources/application-prod.yml

@@ -8,7 +8,6 @@ ruoyi:
   copyrightYear: 2020
   # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath)
   profile: /data/project/file
-  logPath: /data/project/logs/
   # 获取ip地址开关
   addressEnabled: false
   #是否开启swagger
@@ -44,6 +43,7 @@ logging:
   level:
     com.dgtly: debug
     org.springframework: warn
+  path: /data/project/logs/
 # Spring配置
 spring:
   jackson:

+ 1 - 1
suishenbang-api/src/main/resources/application-test.yml

@@ -8,7 +8,6 @@ ruoyi:
   copyrightYear: 2020
   # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath)
   profile: /home/suishenbang/file
-  logPath: /home/suishenbang/logs/
   # 获取ip地址开关
   addressEnabled: true
   #是否开启swagger
@@ -44,6 +43,7 @@ logging:
   level:
     com.dgtly: debug
     org.springframework: warn
+  path: /home/suishenbang/logs/
 # Spring配置
 spring:
   jackson:

+ 1 - 1
suishenbang-api/src/main/resources/application-uat.yml

@@ -8,7 +8,6 @@ ruoyi:
   copyrightYear: 2020
   # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath)
   profile: /home/admin/project/file
-  logPath: /home/admin/project/logs/
   # 获取ip地址开关
   addressEnabled: false
   #是否开启swagger
@@ -44,6 +43,7 @@ logging:
   level:
     com.dgtly: debug
     org.springframework: warn
+  path: /home/admin/project/logs/
 # Spring配置
 spring:
   jackson:

+ 1 - 2
suishenbang-api/src/main/resources/logback.xml

@@ -1,8 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <configuration>
-    <springProperty scope="context" name="logPath" source="ruoyi.logPath" defaultValue="/home/admin/project"/>
     <!-- 日志存放路径 -->
-    <property name="log.path"  value="${logPath}"/>
+    <property name="log.path"  value="${LOG_PATH:-.}"/>
     <!-- 日志输出格式 -->
 	<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
 

+ 7 - 19
suishenbang-api/src/test/java/test/MyTest.java

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.dgtly.ApiApplication;
 import com.dgtly.wxportal.service.ESignService;
 import com.dgtly.wxportal.utils.ESign.ESignHttpUtil;
+import com.dgtly.wxportal.utils.ESign.ESignUploadUtil;
 import com.dgtly.wxportal.utils.ESign.ESignUrl;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -12,6 +13,9 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
+import java.util.HashSet;
+import java.util.Set;
+
 @RunWith(SpringRunner.class)
 @SpringBootTest(classes=ApiApplication.class)
 public class MyTest {
@@ -23,28 +27,12 @@ public class MyTest {
     private ESignService eSignService;
     @Test
     public void test(){
+
+
         //创建文件
         String result = eSignService.upload("D:/SZSM/工作交接表.docx");
         System.out.println(result);
-
-        //注册用户
-        /*JSONObject json = new JSONObject();
-        json.put("thirdPartyUserId","18317541568");
-        json.put("name","张鼎");
-        json.put("idType","CRED_PSN_CH_IDCARD");
-        json.put("idNumber","410381200012281515");
-        json.put("mobile","18317541566");
-        json.put("email","18317541566@163.com");
-        String s=  eSignHttpUtil.doPost(ESignUrl.CreatePersonalAccount,json);
-        */
-
-        /*json.put("thirdPartyUserId","20190826110230222");
-        json.put("creator","40c83af8e2554732957ae14f357d37cb");
-        json.put("name","杭州天谷信息科技有限公司");
-        json.put("idType","CRED_PSN_CH_IDCARD");
-        json.put("idNumber","410381200012281514");
-        json.put("orgLegalIdNumber","410381200012281514");
-        json.put("orgLegalName","张鼎");*/
+        System.out.println("111111");
 
     }
 

+ 2 - 0
suishenbang-wxportal/suishenbang-wxportal-common/src/main/java/com/dgtly/wxportal/exception/ESignException.java

@@ -2,6 +2,7 @@ package com.dgtly.wxportal.exception;
 
 import com.dgtly.common.exception.BusinessException;
 import com.dgtly.wxportal.utils.ESign.ESignErrCode;
+import com.dgtly.wxportal.utils.ESign.ESignUrl;
 import com.dgtly.wxportal.utils.qywxutils.QyWxErrCode;
 
 public class ESignException extends BusinessException{
@@ -9,6 +10,7 @@ public class ESignException extends BusinessException{
     public ESignException(String code, String url,String message) {
         super("获取E签宝接口访问返回异常,请求地址为:"+url+"异常代码errcode:"+code+"---errmsg:"+message+"<=======>"+ESignErrCode.errMsg(code),new Throwable());
     }
+
     public ESignException(Integer code, String url,String message) {
         super("获取E签宝接口访问返回异常,请求地址为:"+url+"异常代码errcode:"+code+"---errmsg:"+message+"<=======>"+ESignErrCode.errMsg(code),new Throwable());
     }

+ 2 - 1
suishenbang-wxportal/suishenbang-wxportal-common/src/main/java/com/dgtly/wxportal/service/ESignService.java

@@ -19,7 +19,8 @@ public interface ESignService {
      * @auther: ZhangDing
      * @date: 2020-10-10 11:31
      */
-    public String upload(String url);
+    public String upload(String orderId);
+
 
 
     }

+ 44 - 9
suishenbang-wxportal/suishenbang-wxportal-common/src/main/java/com/dgtly/wxportal/service/impl/ESignServiceImpl.java

@@ -3,7 +3,7 @@ package com.dgtly.wxportal.service.impl;
 import com.alibaba.fastjson.JSONObject;
 import com.dgtly.wxportal.service.ESignService;
 import com.dgtly.wxportal.utils.ESign.ESignHttpUtil;
-import com.dgtly.wxportal.utils.ESign.ESignMD5;
+import com.dgtly.wxportal.utils.ESign.ESignUploadUtil;
 import com.dgtly.wxportal.utils.ESign.ESignUrl;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -26,17 +26,52 @@ public class ESignServiceImpl implements ESignService {
     private ESignHttpUtil eSignHttpUtil;
 
     @Override
-    public String upload(String url) {
-        File file = new File(url);
+    public String upload(String orderId) {
+        //根据订单id生成pdf文件
+        String filePath = creatPdf(orderId);
+        String md5 = ESignUploadUtil.getStringContentMD5(filePath);
+        String contentTtpe = "application/pdf";
+        int fileLength = ESignUploadUtil.getFileSize(filePath);
+        //根据文件获取上传路径
+        JSONObject urlData =  getUpUrl(filePath,md5,contentTtpe,fileLength).getJSONObject("data");
+        //文件id
+        String fileId = urlData.getString("fileId");
+        //文件上传路径
+        String uploadUrl = urlData.getString("uploadUrl");
+
+
+        //上传文件
+        String res = eSignHttpUtil.upLoadFile(uploadUrl,filePath,md5,contentTtpe,fileLength);
+
+        return res;
+    }
+
+    /**
+     * 创建收货单pdf文件
+     * @param orderId
+     * @return
+     */
+    private String creatPdf(String orderId){
+
+        return "D:/bc.pdf";
+    }
+
+    /**
+     * 获取文件上传路径
+     * @param filePath
+     * @return
+     */
+    private JSONObject getUpUrl(String filePath,String contentMd5,String contentType,int fileLength){
+        File file = new File(filePath);
         String fileName = file.getName();
         JSONObject json = new JSONObject();
-        json.put("contentMd5", ESignMD5.getStringContentMD5(url));
-        json.put("contentType","application/octet-stream");
-        json.put("convert2Pdf",true);
+        json.put("contentMd5", contentMd5);
+        json.put("contentType",contentType);
+        json.put("convert2Pdf",false);
         json.put("fileName",fileName);
-        json.put("fileSize", ESignMD5.getFileSize(url));
-        String s=  eSignHttpUtil.doPost(ESignUrl.uploadCreateFile,json);
-        return s;
+        json.put("fileSize",fileLength);
+        JSONObject result=  eSignHttpUtil.doPostGetJson(ESignUrl.uploadCreateFile,json);
+        return result;
     }
 
 }

+ 2 - 2
suishenbang-wxportal/suishenbang-wxportal-common/src/main/java/com/dgtly/wxportal/utils/ESign/ESignErrCode.java

@@ -23,13 +23,13 @@ public class ESignErrCode {
 		if(errCode != null && codeMap.containsKey(errCode+"")){
 			return codeMap.get(errCode+"");
 		}
-		return null;
+		return "不明错误";
 	}
     public static String errMsg(String errCode){
         if(errCode != null && codeMap.containsKey(errCode)){
             return codeMap.get(errCode+"");
         }
-        return null;
+        return "不明错误";
     }
 	
 }

+ 130 - 10
suishenbang-wxportal/suishenbang-wxportal-common/src/main/java/com/dgtly/wxportal/utils/ESign/ESignHttpUtil.java

@@ -2,18 +2,16 @@ package com.dgtly.wxportal.utils.ESign;
 
 import com.alibaba.fastjson.JSONObject;
 import com.dgtly.common.exception.BusinessException;
-import com.dgtly.common.utils.http.HttpUtils;
 import com.dgtly.wxportal.config.ESignConfig;
+import com.dgtly.wxportal.exception.ESignException;
+import org.apache.commons.io.FileUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import javax.net.ssl.*;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.PrintWriter;
+import java.io.*;
 import java.net.ConnectException;
 import java.net.SocketTimeoutException;
 import java.net.URL;
@@ -29,6 +27,24 @@ public class ESignHttpUtil {
     @Autowired
     private ESignConfig eSignConfig;
 
+    /**
+     * 根据E签宝的大部分返回值实体简单处理结果增加异常判断
+     * @param eurl
+     * @param param
+     * @param querys
+     * @return
+     */
+    public JSONObject doPostGetJson(ESignUrl eurl, JSONObject param,String ...querys){
+        String url = eurl.getUrl(querys);
+        String str = doPost(url,param.toJSONString());
+        JSONObject json =JSONObject.parseObject(str);
+        int code = json.getInteger("code");
+        if(code!=0){
+            throw new ESignException(code,url,json.getString("message"));
+        }
+        return json;
+    }
+
     public String doPost(ESignUrl eurl, JSONObject param,String ...querys){
         String url = eurl.getUrl(querys);
         return doPost(url,param.toJSONString());
@@ -36,7 +52,7 @@ public class ESignHttpUtil {
 
 
     /**
-     * 向指定 URL 发送POST方法的请求
+     * 向指定 URL 发送POST方法的请求并添加E签宝所需的请求头
      *
      * @param url 发送请求的 URL
      * @param json 请求参数,
@@ -84,22 +100,22 @@ public class ESignHttpUtil {
         }
         catch (ConnectException e)
         {
-            log.error("调用ESignHttpUtil.sendJsonPost ConnectException, url=" + url + ",param_body=" + json, e);
+            log.error("调用ESignHttpUtil.doPost ConnectException, url=" + url + ",param_body=" + json, e);
             throw new BusinessException("调用ESignHttpUtil.sendJsonPost ConnectException, url=" + url + ",param=" + json );
         }
         catch (SocketTimeoutException e)
         {
-            log.error("调用ESignHttpUtil.sendJsonPost SocketTimeoutException, url=" + url + ",param=" + json, e);
+            log.error("调用ESignHttpUtil.doPost SocketTimeoutException, url=" + url + ",param=" + json, e);
             throw new BusinessException("调用ESignHttpUtil.sendJsonPost SocketTimeoutException, url=" + url + ",param=" + json );
         }
         catch (IOException e)
         {
-            log.error("调用ESignHttpUtil.sendJsonPost IOException, url=" + url + ",param=" + json, e);
+            log.error("调用ESignHttpUtil.doPost IOException, url=" + url + ",param=" + json, e);
             throw new BusinessException("调用ESignHttpUtil.sendJsonPost IOException, url=" + url + ",param=" + json );
         }
         catch (Exception e)
         {
-            log.error("调用HttpsUtil.sendJsonPost Exception, url=" + url + ",param=" + json, e);
+            log.error("调用HttpsUtil.doPost Exception, url=" + url + ",param=" + json, e);
             throw new BusinessException("调用ESignHttpUtil.sendJsonPost Exception, url=" + url + ",param=" + json );
         }
         finally
@@ -208,4 +224,108 @@ public class ESignHttpUtil {
             return true;
         }
     }
+
+
+
+
+
+    /**
+     * 向E签宝指定 URL 上传文件
+     *
+     * @param url 发送请求的 URL
+     * @param filePath 文件路径
+     * @return 所代表远程资源的响应结果
+     */
+    public String upLoadFile(String url , String filePath ,String contentMD5,String contentType,int fileLength)
+    {
+
+
+        OutputStream out = null;
+        BufferedReader in = null;
+        StringBuilder result = new StringBuilder();
+        try
+        {
+
+            byte[] fileBytes = FileUtils.readFileToByteArray(new File(filePath));
+            log.info("sendPost - {}", url);
+            // 打开和URL之间的连接
+            SSLContext sc = SSLContext.getInstance("SSL");
+            sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new SecureRandom());
+
+            //创建连接
+            java.net.URL realUrl = new URL(url);
+            HttpsURLConnection conn = (HttpsURLConnection) realUrl.openConnection();
+            conn.setRequestMethod("PUT");
+            //设置ssl
+            // 从上述SSLContext对象中得到SSLSocketFactory对象
+            SSLSocketFactory ssf = sc.getSocketFactory();
+            conn.setSSLSocketFactory(ssf);
+
+
+
+            conn.setRequestProperty("accept", "*/*");
+            conn.setRequestProperty("connection", "Keep-Alive");
+            //设置请求头
+            conn.setRequestProperty("Content-Type",contentType);
+            conn.setRequestProperty("Content-MD5",contentMD5);
+            conn.setRequestProperty("Content-Length",fileLength+"");
+
+            conn.setUseCaches(false);//设置不要缓存
+            conn.setDoOutput(true);
+            conn.setDoInput(true);
+
+            //写入请求体
+            out = conn.getOutputStream();
+            out.write(fileBytes);
+            out.flush();
+            in = new BufferedReader( new InputStreamReader(conn.getInputStream()));
+            String line;
+            while ((line = in.readLine()) != null)
+            {
+                result.append(line);
+            }
+            log.info("recv - {}", result);
+        }
+        catch (ConnectException e)
+        {
+            log.error("调用ESignHttpUtil.upLoadFile ConnectException, url=" + url + ",filepath=" + filePath, e);
+            throw new BusinessException("调用ESignHttpUtil.upLoadFile ConnectException, url=" + url);
+        }
+        catch (SocketTimeoutException e)
+        {
+            log.error("调用ESignHttpUtil.upLoadFile ConnectException, url=" + url + ",filepath=" + filePath, e);
+            throw new BusinessException("调用ESignHttpUtil.upLoadFile SocketTimeoutException, url=" + url );
+        }
+        catch (IOException e)
+        {
+            log.error("调用ESignHttpUtil.upLoadFile ConnectException, url=" + url + ",filepath=" + filePath, e);
+            throw new BusinessException("调用ESignHttpUtil.upLoadFile IOException, url=" + url );
+        }
+        catch (Exception e)
+        {
+            log.error("调用ESignHttpUtil.upLoadFile ConnectException, url=" + url + ",filepath=" + filePath, e);
+            throw new BusinessException("调用ESignHttpUtil.upLoadFile Exception, url=" + url );
+        }
+        finally
+        {
+            try
+            {
+                if (out != null)
+                {
+                    out.close();
+                }
+                if (in != null)
+                {
+                    in.close();
+                }
+            }
+            catch (IOException ex)
+            {
+                log.error("调用in.close Exception, url=" + url, ex);
+            }
+        }
+        return result.toString();
+    }
+
+
 }

+ 3 - 1
suishenbang-wxportal/suishenbang-wxportal-common/src/main/java/com/dgtly/wxportal/utils/ESign/ESignMD5.java

@@ -18,7 +18,7 @@ import java.security.NoSuchAlgorithmException;
  * @date 2020-10-10 11:05
  * @since JDK1.8
  */
-public class ESignMD5 {
+public class ESignUploadUtil {
     /***
      * 计算字符串的Content-MD5
      * @param str 文件路径
@@ -78,4 +78,6 @@ public class ESignMD5 {
         }
         return md5Bytes;
     }
+
+
 }