yaoyu 2 gadi atpakaļ
vecāks
revīzija
717c4e800d

+ 1 - 1
pu/META-INF/thopenapi.upm

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding='gb2312'?>
-<module name="tbb">
+<module name="pu">
     <public>
  	    <component remote="true" singleton="true" tx="NONE">
             <interface>nc.itf.pu.service.IInvoiceOCRMaintain</interface>

+ 114 - 0
pu/pu/src/private/nc/impl/pu/service/InvoiceOCRMaintainImpl.java

@@ -0,0 +1,114 @@
+package nc.impl.pu.service;
+
+import java.io.File;
+
+
+import org.jeecg.common.util.HttpHelper;
+
+import com.alibaba.fastjson.JSONObject;
+
+import nc.bs.framework.common.NCLocator;
+import nc.itf.pu.service.IInvoiceOCRMaintain;
+import nc.itf.uap.IUAPQueryBS;
+import nc.jdbc.framework.processor.ColumnProcessor;
+import nc.log.NcLog;
+import nc.vo.pubapp.pattern.exception.ExceptionUtils;
+
+public class InvoiceOCRMaintainImpl implements IInvoiceOCRMaintain{
+	
+	
+	private String url = "https://api.mypiaojia.com/api/invoiceApi/";
+	private String url2 = "https://up.mypiaojia.com/api/invoiceApi/";
+
+	
+	@Override
+	public JSONObject startInvoiceOCR(File file) {
+		try {
+			NcLog.info("流程开始");
+			//获取发票云token
+			JSONObject tokenjb = getOCRToken();
+			NcLog.info("获取发票云token:"+tokenjb.toString());
+			String token = tokenjb.getString("data");
+			//上传发票到发票云系统
+			JSONObject uploadjb = uploadInvoice(token, file);
+			NcLog.info("上传发票到发票云:"+uploadjb.toString());
+			//进行orc识别
+			JSONObject scanjb = scanInvoice(token,uploadjb);
+			NcLog.info("发票云OCR识别:"+scanjb.toString());
+			
+			return scanjb;
+			
+		} catch (Exception e) {
+			ExceptionUtils.wrappBusinessException(e.getMessage());
+			NcLog.info("流程失败:"+e.getMessage());
+		}
+		return null;
+	}
+	
+	
+	/**
+	 * 获取发票云token
+	 * @return token
+	 * @throws Exception
+	 */
+	private JSONObject getOCRToken() throws Exception{
+		String sql = "select defaultvalue from pub_sysinittemp where  initcode ='fptoken'";
+		IUAPQueryBS iUAPQueryBS= (IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class.getName());
+		JSONObject jsonobject = JSONObject.parseObject(iUAPQueryBS.executeQuery(sql, new ColumnProcessor()).toString());
+		JSONObject responseMsg = HttpHelper.httpTokenPost(url+"token/buildToken", jsonobject.getString("appId"), jsonobject.getString("cid"), jsonobject.getString("secret"));
+		if(responseMsg.get("data") != null) {
+			return responseMsg;		
+		}else {
+			throw new Exception("获取发票云token失败:"+responseMsg.getJSONObject("res_base").getString("message"));
+		}
+
+	}
+	
+	/**
+	 * 上传发票到发票云系统
+	 * @param token
+	 * @param file
+	 * @return
+	 * @throws Exception
+	 */
+	private JSONObject uploadInvoice(String token, File file) throws Exception {
+		NcLog.info("上传发票到发票云系统开始");
+		JSONObject responseMsg = HttpHelper.httpFilePost(url2+"file/uploadFileSpilit",token, file);
+		if(responseMsg.get("data") != null) {
+			return responseMsg;
+		}else {
+			throw new Exception("上传发票到发票云系统失败:"+responseMsg.getJSONObject("res_base").getString("message"));
+		}
+	}
+	
+	
+	/**
+	 * 进行orc识别
+	 * @param token
+	 * @param uploadjb   上传发票到发票云系统时返回的数据
+	 * @return
+	 * @throws Exception
+	 */
+	private JSONObject scanInvoice(String token,JSONObject uploadjb) throws Exception {
+		 JSONObject jsonobject = new JSONObject();
+         jsonobject.put("image_index", uploadjb.getJSONObject("data").getString("id"));
+         jsonobject.put("fileTypeStr", uploadjb.getJSONObject("data").getString("fileType"));
+         jsonobject.put("flag", "13");
+         jsonobject.put("is_save", "0");
+         jsonobject.put("is_sync", "1");
+         jsonobject.put("ocrMap","");
+         jsonobject.put("operate_type",0);
+         JSONObject responseMsg = HttpHelper.httpPost(url+"invoice/ocr", token, jsonobject);
+         if(responseMsg.get("data") != null) {
+ 			return responseMsg;
+ 		}else {
+ 			throw new Exception("发票orc识别失败:"+responseMsg.getJSONObject("actionMsg").getString("message"));
+ 		}
+	}
+	
+	
+	//删除发票云系统发票
+
+	
+	//修改发票云发票状态
+}

+ 129 - 0
pu/pu/src/private/org/jeecg/common/util/HttpHelper.java

@@ -0,0 +1,129 @@
+package org.jeecg.common.util;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+import org.apache.commons.codec.digest.DigestUtils;
+
+import com.alibaba.fastjson.JSONObject;
+
+public class  HttpHelper {
+	 public static JSONObject httpFilePost(String uploadUrl,String token, File file) throws Exception {
+		 String LINEND = "\r\n";
+		 String boundary = UUID.randomUUID().toString();
+		 FileInputStream fileInputStream = new FileInputStream(file);
+	     URL url = new URL(uploadUrl);
+	     HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
+	     httpURLConnection.setDoInput(true);
+	     httpURLConnection.setUseCaches(false);
+	     httpURLConnection.setDoOutput(true);
+	     httpURLConnection.setRequestMethod("POST");
+	     httpURLConnection.setRequestProperty("connection", "keep-alive");
+	     httpURLConnection.setRequestProperty("Charsert", "UTF-8");
+	     httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
+	     httpURLConnection.setRequestProperty("token", token);
+	     httpURLConnection.setRequestProperty("Accept","*/*");
+	     DataOutputStream outStream = new DataOutputStream(httpURLConnection.getOutputStream());
+         StringBuilder stringBuilder = new StringBuilder();
+         stringBuilder.append("--").append(boundary).append(LINEND);
+         stringBuilder.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + LINEND);
+         stringBuilder.append("Content-Type: " + HttpURLConnection.guessContentTypeFromName(file.getName()) + LINEND);
+         stringBuilder.append("Content-Transfer-Encoding: binary"+ LINEND);
+         stringBuilder.append(LINEND);
+         outStream.write(stringBuilder.toString().getBytes());
+    	 InputStream is = new FileInputStream(file);
+    	 byte[] buffer = new byte[1024];
+    	 int len = 0;
+    	 while ((len = is.read(buffer)) != -1){
+    		 outStream.write(buffer, 0, len);
+    	 }
+    	 is.close();
+    	 outStream.write(LINEND.getBytes());
+    	 byte[] end_data = ("--" + boundary + "--" + LINEND).getBytes();
+    	 outStream.write(end_data);
+    	 outStream.flush();
+         fileInputStream.close();
+         JSONObject responseMsg = readStream(httpURLConnection.getInputStream());
+         outStream.close();
+         httpURLConnection.disconnect();    
+         return responseMsg;
+	 }
+	 
+	 
+	 public static JSONObject httpPost(String uploadUrl,String token,JSONObject jsob) throws Exception {
+		 URL url = new URL(uploadUrl);
+	     HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
+	     httpURLConnection.setDoInput(true);
+	     httpURLConnection.setUseCaches(false);
+	     httpURLConnection.setDoOutput(true);
+	     httpURLConnection.setRequestMethod("POST");
+	     httpURLConnection.setRequestProperty("Content-Type","application/json");
+	     httpURLConnection.setRequestProperty("charset","UTF-8");
+	     httpURLConnection.setRequestProperty("token", token);
+         OutputStream os = httpURLConnection.getOutputStream();
+         os.write(jsob.toString().getBytes());
+         os.flush();
+         os.close();
+         JSONObject responseMsg = readStream(httpURLConnection.getInputStream());
+      	 httpURLConnection.disconnect();
+      	 return responseMsg;
+		 
+	 }
+	 
+	 
+	 public static JSONObject httpTokenPost(String uploadUrl,String appkey,String cid,String secret) throws Exception {
+		 URL url = new URL(uploadUrl);
+	     HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
+	     httpURLConnection.setDoInput(true);
+	     httpURLConnection.setUseCaches(false);
+	     httpURLConnection.setDoOutput(true);
+	     httpURLConnection.setRequestMethod("POST");
+	     httpURLConnection.setRequestProperty("Content-Type","application/json");
+	     httpURLConnection.setRequestProperty("charset","UTF-8");
+	     httpURLConnection.setRequestProperty("appkey", appkey);
+	     long l = System.currentTimeMillis();
+	     Map<String, Object> map = new HashMap<String, Object>();
+	     map.put("role", 1);
+		 map.put("name", "dmy06");
+	     map.put("userId", "2108");//OA用户体系的用户id
+	     map.put("cid", cid);
+	     String s1 = JSONObject.toJSONString(map);
+	     String s2 = s1 + "&time=" + l + "&secret="+secret;
+	     String newMd5 = DigestUtils.md5Hex(s2);
+	     String s4 = s1 + "&time=" + l + "&md5=" + newMd5;
+	     // 写入请求体
+	     byte[] requestBodyBytes = s4.getBytes(StandardCharsets.UTF_8);
+	     OutputStream outputStream = httpURLConnection.getOutputStream();
+	     outputStream.write(requestBodyBytes);
+	     outputStream.flush();
+	     outputStream.close();
+	     JSONObject responseMsg = readStream(httpURLConnection.getInputStream());
+	     httpURLConnection.disconnect();
+	     return responseMsg;
+		 
+	 }
+	 
+	 
+	 private static JSONObject readStream(InputStream inputStream) throws Exception {
+		 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+	     byte[] buffer = new byte[1024];
+	     int bytesRead;
+	     while ((bytesRead = inputStream.read(buffer)) != -1) {
+	    	 outputStream.write(buffer, 0, bytesRead);
+	     }
+	     byte[] bytes = outputStream.toByteArray();
+	     String result = new String(bytes, "UTF-8");
+	     outputStream.close();
+	     return JSONObject.parseObject(result);
+	}
+}