123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- package kd.imc.rim;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import javassist.Loader;
- import kd.bos.dataentity.entity.DynamicObject;
- import kd.bos.orm.query.QCP;
- import kd.bos.orm.query.QFilter;
- import kd.bos.script.annotations.KSObject;
- import kd.bos.servicehelper.BusinessDataServiceHelper;
- import org.apache.http.HttpEntity;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.ContentType;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClientBuilder;
- import org.apache.http.util.EntityUtils;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.text.SimpleDateFormat;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.Base64;
- import java.util.Date;
- import java.util.Iterator;
- @KSObject
- public class ApiHttpUtils {
- public static String Posthttp(String url, String Params){
- // 获得Http客户端
- CloseableHttpClient httpClient = HttpClientBuilder.create().build();
- // 创建Post请求
- //设置请求路径
- HttpPost httpPost = new HttpPost(url);
- httpPost.setHeader("Content-type", "application/json;charset=utf-8");
- JSONObject object = new JSONObject();
- JSONObject head = new JSONObject();
- head.put("mesgtype","bills_crop_base64");
- head.put("channelcode","JSX");
- Date date = new Date();
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
- String formatDate = dateFormat.format(date);
- head.put("channeldate",formatDate);
- LocalDateTime now = LocalDateTime.now();
- DateTimeFormatter hHmmss = DateTimeFormatter.ofPattern("HHmmss");
- DateTimeFormatter yyyyMMddHHmmssSSSS = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSSS");
- String channeltime = now.format(hHmmss);
- String hHmmssSSS1 = now.format(yyyyMMddHHmmssSSSS);
- head.put("channeltime",channeltime);
- head.put("channelserno",hHmmssSSS1);
- head.put("bron","");
- head.put("tellerno","");
- head.put("reserve","");
- head.put("dealcode","");
- head.put("dealmsg","");
- head.put("App_key","XYK_DEJ_Key");
- head.put("APP_secret","XYK_DEJ_SECRET");
- JSONObject body = new JSONObject();
- body.put("file_base64",Params);
- object.put("head",head);
- object.put("body",body);
- StringEntity entity = new StringEntity(object.toString(), ContentType.APPLICATION_JSON);
- httpPost.setEntity(entity);
- // 响应模型(发送post请求)
- CloseableHttpResponse response = null;
- try {
- response = httpClient.execute(httpPost);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- // 从响应模型中获取响应实体
- HttpEntity responseEntity = response.getEntity();
- JSONObject jsonObject = new JSONObject();
- if (responseEntity != null) {
- try {
- jsonObject = JSON.parseObject(EntityUtils.toString(response.getEntity()));
- JSONArray jsonArray = jsonObject.getJSONObject("boby").getJSONArray("object_list");
- return jsonArray.toJSONString();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- // 释放资源
- if (httpClient != null) {
- try {
- httpClient.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- if (response != null) {
- try {
- response.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return "";
- }
- public static String HttpPostExample () {
- try { // 图片路径
- String imagePath = "path_to_your_image_file.jpg";
- // 将图片转换为Base64编码
- byte[] imageBytes = java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(imagePath));
- String base64Image = Base64.getEncoder().encodeToString(imageBytes);
- // 创建URL对象
- URL url = new URL("http://10.3.2.70:8115");
- //打开HTTP连接
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("POST");
- connection.setRequestProperty("Content-Type", "application/json");
- connection.setRequestProperty("channelcode", "your_channel_code");
- connection.setRequestProperty("channeldate", "your_channel_date");
- connection.setDoOutput(true); // 将Base64编码的图片作为请求体发送
- try (OutputStream os = connection.getOutputStream())
- { os.write(base64Image.getBytes()); os.flush(); }
- //检查响应代码
- int responseCode = connection.getResponseCode();
- if (responseCode == HttpURLConnection.HTTP_OK) {
- System.out.println("Request was successful.");
- } else { System.out.println("Request failed. Response Code: " + responseCode); }
- // 关闭连接
- connection.disconnect(); }
- catch (Exception e) { e.printStackTrace(); }
- return null;
- }
- public static String toFiledata(String Params) throws Exception {
- //组装发票数据结构 !
- JSONObject json = new JSONObject();//返回最外层json
- json.put("errcode","0000");
- json.put("traceId","");
- json.put("description","操作成功");
- JSONObject data = new JSONObject();//数据层json
- json.put("batchNo","");
- JSONArray recoginitionData = new JSONArray();//数据层数组
- if("".equals(Params)){return null;};
- JSONArray objects = JSONArray.parseArray(Params);
- for(int a=0;a<objects.size();a++){
- Object object = objects.get(a);
- JSONObject respenjson = (JSONObject) JSONObject.toJSON(object);
- String type_description = respenjson.getString("type_description");//增值税电子普通发票 行程单 通用定额发票 火车票
- if("".equals(type_description)){return null;};
- JSONArray itemList = respenjson.getJSONArray("item_list");
- JSONObject item_list = new JSONObject();
- for (int i=0;i<itemList.size();i++){
- Object obj = itemList.get(i);
- JSONObject respenjsons = (JSONObject) JSONObject.toJSON(obj);
- String key = respenjsons.get("key").toString();// 获得key
- String value = respenjsons.get("value").toString();// 获得value
- item_list.put(key,value);
- }
- if("行程单".equals(type_description)){
- JSONArray flight_data_list = respenjson.getJSONArray("flight_data_list");
- for (int i=0;i<flight_data_list.size();i++){
- Object obj = flight_data_list.get(i);
- JSONArray xcobj = JSONArray.parseArray(obj.toString());
- for(int c=0;c<xcobj.size();c++){
- Object objc = flight_data_list.get(c);
- JSONObject respenjsons = (JSONObject) JSONObject.toJSON(objc);
- String key = respenjsons.get("key").toString();// 获得key
- String value = respenjsons.get("value").toString();// 获得value
- item_list.put(key,value);
- }
- }
- }
- QFilter nckd_File = new QFilter("nckd_filetype", QCP.equals, type_description);
- DynamicObject nckd_FileData = BusinessDataServiceHelper.loadSingle("nckd_filedataconvert", "id,nckd_filetype", new QFilter[]{nckd_File});
- JSONObject Filedata = FileData(nckd_FileData,item_list);
- recoginitionData.add(Filedata);
- }
- data.put("recoginitionData",recoginitionData);
- json.put("data",data);
- return json.toString();
- }
- //增值税电子普通发票
- public static JSONObject FileData(DynamicObject nckd_FileData,JSONObject item_list) throws Exception {
- JSONObject fileObj = new JSONObject();//实际数据json
- for (DynamicObject entryentity : nckd_FileData.getDynamicObjectCollection("nckd_treeentryentity")) {
- String nckd_keingde = entryentity.getString("nckd_keingde");
- String nckd_ocr = entryentity.getString("nckd_ocr");
- String nckd_acquiesce = entryentity.getString("nckd_acquiesce");
- if(!"".equals(nckd_ocr) && nckd_ocr!=null){
- fileObj.put(nckd_keingde,item_list.getString(nckd_ocr));
- }else if(!"".equals(nckd_acquiesce) && nckd_ocr!=null){
- fileObj.put(nckd_keingde,nckd_acquiesce);
- }else {
- fileObj.put(nckd_keingde,"");
- }
- }
- return fileObj;
- }
- //机票行程单
- public static String jpxcd(JSONObject item_list) throws Exception {
- return null;
- }
- //火车票
- public static String hcp(JSONObject item_list) throws Exception {
- //组装发票数据结构 !
- JSONObject json = new JSONObject();//返回最外层json
- json.put("errcode","0000");
- json.put("traceId","");
- json.put("description","操作成功");
- JSONObject data = new JSONObject();//数据层json
- json.put("batchNo","");
- JSONArray recoginitionData = new JSONArray();//数据层数组
- JSONObject fileObj = new JSONObject();//实际数据json
- fileObj.put("canBeDeduction","");//0
- fileObj.put("stationGetOn",item_list.getString("departure_station"));//出发站 ocr字段:departure_station
- fileObj.put("passengerName",item_list.getString("passenger_name"));//乘客 ocr字段:passenger_name
- fileObj.put("signStatus","");//0
- fileObj.put("downloadUrl","");//下载地址链接
- fileObj.put("fileHash","");//文件哈希值
- fileObj.put("seatNumber",item_list.getString("seat_number"));//座位 ocr字段:seat_number
- fileObj.put("localUrl","");//预览链接
- fileObj.put("trainNum",item_list.getString("train_number"));//车次编号 ocr字段:train_number
- fileObj.put("trainTime","");//
- fileObj.put("stationGetOff",item_list.getString("arrival_station"));//目的地 ocr字段:arrival_station
- fileObj.put("deductionStatus",1);
- fileObj.put("invoiceType","9");//发票类型
- fileObj.put("isRepeat",false);//是否重复
- fileObj.put("pixel","");//未知
- fileObj.put("oriImageSize","");//未知
- fileObj.put("pdfToImgSnapshotUrl","");//“”
- fileObj.put("orientation","0");//null
- fileObj.put("batchNo","");//
- fileObj.put("warningCode","1");//
- fileObj.put("originalState",0);//
- fileObj.put("originalUrl","");//""
- fileObj.put("invoiceDate",item_list.getString("departure_date"));//乘车日期 ocr字段:departure_date
- fileObj.put("serialNo","");//
- fileObj.put("seat",item_list.getString("class"));//几等座 ocr字段:class
- fileObj.put("totalAmount",item_list.getString("price"));//票价 ocr字段:price
- fileObj.put("taxRate","");//
- fileObj.put("customerIdentityNum",item_list.getString("passenger_id"));//身份证 ocr字段 passenger_id
- fileObj.put("oriOrientation","0");//
- fileObj.put("oriRegion","0");//
- fileObj.put("rotationAngle","");//未知字段
- fileObj.put("snapshotUrl","");//
- fileObj.put("imageSerialNo","");//未知字段
- fileObj.put("printingSequenceNo",item_list.getString("number"));//发票号码 ocr字段 number
- fileObj.put("recognitionSerialNo","");//
- fileObj.put("businessType",1);//未知字段
- fileObj.put("region","");//
- //值拼接完成后开始set数据结构e
- recoginitionData.add(fileObj);
- data.put("recoginitionData",recoginitionData);
- json.put("data",data);
- return json.toJSONString();
- }
- //通用定额发票
- public static String tydefp(JSONObject item_list) throws Exception {
- return null;
- }
- }
|