EqbHylServiceImpl.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. package com.malk.hengyilong.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.malk.hengyilong.service.EqbHylService;
  5. import com.malk.hengyilong.utils.HTTPHelper;
  6. import com.malk.server.aliwork.YDConf;
  7. import com.malk.server.aliwork.YDParam;
  8. import com.malk.server.aliwork.YDSearch;
  9. import com.malk.server.common.McR;
  10. import com.malk.service.aliwork.YDClient;
  11. import com.malk.utils.UtilMap;
  12. import org.apache.commons.codec.binary.Base64;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.stereotype.Service;
  16. import javax.crypto.Mac;
  17. import javax.crypto.spec.SecretKeySpec;
  18. import java.io.UnsupportedEncodingException;
  19. import java.security.InvalidKeyException;
  20. import java.security.MessageDigest;
  21. import java.security.NoSuchAlgorithmException;
  22. import java.text.MessageFormat;
  23. import java.time.Instant;
  24. import java.time.ZoneId;
  25. import java.time.format.DateTimeFormatter;
  26. import java.util.*;
  27. /**
  28. * 功能:
  29. * 作者:hanxue
  30. * 日期:2026/4/24 18:04
  31. */
  32. @Service
  33. public class EqbHylServiceImpl implements EqbHylService {
  34. @Autowired
  35. private YDClient ydClient;
  36. @Value("${eqb.appId}")
  37. private String appId;
  38. @Value("${eqb.appKey}")
  39. private String appKey;
  40. @Value("${eqb.host}")
  41. private String host;
  42. @Value("${eqb.orgIdSh}")
  43. private String orgIdSh;
  44. @Value("${eqb.orgIdHyl}")
  45. private String orgIdHyl;
  46. @Value("${eqb.psnId}")
  47. private String psnId;
  48. @Value("${server.host}")
  49. private String serverHost;
  50. private static String bjdHYL_docTemplateId ="1d2f50e82f514144b72313b1dddcb552";
  51. private static String bjdSH_docTemplateId ="e7440c3f38724b239d7e9dcf7329804a";
  52. private static String ht_docTemplateId ="d481b2222879447f84d992bac20784d0";
  53. // 测试报价单模板ID:ee7bdf1404154c0686ed38a7cf65a7ff
  54. // 正式报价单模板ID:1d2f50e82f514144b72313b1dddcb552
  55. // 正式合同模板ID:0064564bea704fc587a249680064b98c
  56. // 正式合同新模板ID:d481b2222879447f84d992bac20784d0
  57. @Override
  58. public McR quotationToEqb(Map map) {
  59. // 1、获取宜搭报价单数据
  60. String formInstance = UtilMap.getString(map, "formInstance");
  61. Map formData = ydClient.queryData(YDParam.builder()
  62. .formInstanceId(formInstance)
  63. .build(), YDConf.FORM_QUERY.retrieve_id).getFormData();
  64. System.out.println(formData);
  65. try {
  66. // 2、根据模板填充报价单数据
  67. String reportType = UtilMap.getString(map, "type");
  68. JSONObject reqBodyObj = new JSONObject();
  69. List<Map> components = new ArrayList<>();
  70. boolean shouldExecutePost = false;
  71. String fileName="";
  72. switch (reportType){
  73. case "quotation":
  74. fileName = UtilMap.getString(formData, "textField_mn5mis1m");
  75. getQuotation(formData, components, reqBodyObj, fileName);
  76. shouldExecutePost = true;
  77. break;
  78. case "contract":
  79. String signStatus = UtilMap.getString(formData, "radioField_mkcbpvh8");
  80. fileName = UtilMap.getString(formData, "textField_mjmlcp4s");
  81. if ("我方模版".equals(signStatus)) {
  82. getContract(formData, components, reqBodyObj, fileName);
  83. // 只有满足“我方模版”条件,才打开开关
  84. shouldExecutePost = true;
  85. }
  86. break;
  87. default:break;
  88. }
  89. // 直接退出当前方法,不再执行下面的 eqbPost
  90. if (!shouldExecutePost) {
  91. return McR.success("购销模板需要客户模板,无需发起签署");
  92. }
  93. Map fileResult = eqbPost("/v3/files/create-by-doc-template", reqBodyObj);
  94. String fileId = UtilMap.getString(fileResult,"fileId");
  95. // 文档转换pdf需时间--否则文档未成功转换成pdf
  96. try {
  97. Thread.sleep(3000);
  98. } catch (InterruptedException e) {
  99. throw new RuntimeException(e);
  100. }
  101. // 3、基于生成文件发起签署
  102. JSONObject reqBodyObjSign = new JSONObject();
  103. //3.1设置待签署文件信息
  104. Map docs = new HashMap();
  105. docs.put("fileId", fileId);
  106. reqBodyObjSign.put("docs", Arrays.asList(docs));
  107. // 3.2设置签署流程配置项
  108. Map signFlowConfig = new HashMap();
  109. signFlowConfig.put("signFlowTitle",fileName);
  110. signFlowConfig.put("autoFinish",true);
  111. signFlowConfig.put("notifyUrl",serverHost+"/hengyilong/callback");
  112. reqBodyObjSign.put("signFlowConfig",signFlowConfig);
  113. // 3.3设置签署参与方
  114. // 3.3.1设置我方签署信息
  115. Map signer = new HashMap();
  116. signer.put("signerType",1);//签署方类型,0 - 个人,1 - 企业/机构,2 - 法定代表人,3 - 经办人
  117. signer.put("signConfig",UtilMap.map("signOrder",1));
  118. Map orgSignerInfo = new HashMap();
  119. //企业/机构账号ID
  120. if ("恒益隆贸易(上海)有限公司".equals(UtilMap.getString(formData, "selectField_mjmlcp4y"))) {
  121. orgSignerInfo.put("orgId",orgIdHyl);
  122. }else if ("上海绍贺贸易有限公司".equals(UtilMap.getString(formData, "selectField_mjmlcp4y"))){
  123. orgSignerInfo.put("orgId",orgIdSh);
  124. }
  125. //经办人信息
  126. Map transactorInfo = new HashMap();
  127. transactorInfo.put("psnId",psnId);//经办人id
  128. orgSignerInfo.put("transactorInfo",transactorInfo);
  129. signer.put("orgSignerInfo",orgSignerInfo);
  130. Map signField = new HashMap();
  131. signField.put("fileId",fileId);
  132. signField.put("customBizNum",formInstance);
  133. // 签章区配置项
  134. Map normalSignFieldConfig = new HashMap();
  135. normalSignFieldConfig.put("freeMode",true);
  136. signField.put("normalSignFieldConfig",normalSignFieldConfig);
  137. signer.put("signFields",Arrays.asList(signField));
  138. List<Map> signersList = new ArrayList<>();
  139. signersList.add(signer);
  140. // 3.3.2设置对方签署信息
  141. if("需要".equals(UtilMap.getString(formData, "radioField_mqejx60u"))){
  142. Map otherSigner = new HashMap();
  143. otherSigner.put("signerType",1);//签署方类型,0 - 个人,1 - 企业/机构,2 - 法定代表人,3 - 经办人
  144. otherSigner.put("signConfig",UtilMap.map("signOrder",2));
  145. Map otherOrgSignerInfo = new HashMap();
  146. //企业/机构账号名称
  147. otherOrgSignerInfo.put("orgName",UtilMap.getString(formData, "textField_mqejx60w"));
  148. otherOrgSignerInfo.put("orgInfo",UtilMap.map("orgIDCardNum, orgIDCardType",UtilMap.getString(formData, "textField_mqejx60x"),"CRED_ORG_USCC"));
  149. //经办人信息
  150. Map otherTransactorInfo = new HashMap();
  151. otherTransactorInfo.put("psnAccount",UtilMap.getString(formData, "textField_mqejx60y"));//经办人账号标识,手机号或邮箱,必须配合psnName(经办人姓名)传入
  152. otherTransactorInfo.put("psnInfo",UtilMap.map("psnName",UtilMap.getString(formData, "textField_mqj4ypzj")));//经办人姓名
  153. otherOrgSignerInfo.put("transactorInfo",otherTransactorInfo);
  154. otherSigner.put("orgSignerInfo",otherOrgSignerInfo);
  155. Map otherSignField = new HashMap();
  156. otherSignField.put("fileId",fileId);
  157. otherSignField.put("customBizNum",formInstance);
  158. // 签章区配置项
  159. Map otherSignFieldConfig = new HashMap();
  160. otherSignFieldConfig.put("freeMode",true);
  161. otherSignField.put("normalSignFieldConfig",otherSignFieldConfig);
  162. otherSigner.put("signFields",Arrays.asList(otherSignField));
  163. signersList.add(otherSigner);
  164. }
  165. reqBodyObjSign.put("signers",signersList);
  166. Map signResult = eqbPost("/v3/sign-flow/create-by-file", reqBodyObjSign);
  167. String signFlowId = UtilMap.getString(signResult,"signFlowId");
  168. //4、回写签署地址到宜搭
  169. JSONObject reqBodyObj3 = new JSONObject();
  170. Map operator = new HashMap();
  171. operator.put("psnId", psnId);
  172. reqBodyObj3.put("operator", operator);
  173. Map signLink = eqbPost("/v3/sign-flow/" + signFlowId + "/sign-url", reqBodyObj3);
  174. String shortUrl = UtilMap.getString(signLink, "shortUrl");
  175. ydClient.operateData(YDParam.builder()
  176. .formInstanceId(formInstance)
  177. .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("textField_moh0gw9l, textareaField_mqj4ypzk",shortUrl,"成功推送e签宝")))
  178. .build(), YDConf.FORM_OPERATION.update);
  179. return McR.success(shortUrl);
  180. // return McR.success(fileId);
  181. } catch (Exception e) {
  182. ydClient.operateData(YDParam.builder()
  183. .formInstanceId(formInstance)
  184. .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("textareaField_mqj4ypzk",e.getMessage())))
  185. .build(), YDConf.FORM_OPERATION.update);
  186. return McR.errorParam(e.getMessage());
  187. }
  188. }
  189. // 生成报价单模板文件数据
  190. private static void getQuotation(Map formData,List<Map> components,JSONObject reqBodyObj,String fileName){
  191. try{
  192. //客户名称
  193. Map clientName = new HashMap();
  194. clientName.put("componentKey", "clientName");
  195. clientName.put("componentValue",formData.containsKey("textField_mkdmgdot")?UtilMap.getString(formData, "textField_mkdmgdot"):"");
  196. //需方联系人
  197. Map contactPerson = new HashMap();
  198. contactPerson.put("componentKey", "contactPerson");
  199. contactPerson.put("componentValue",formData.containsKey("textField_ml0iaa3i")?UtilMap.getString(formData, "textField_ml0iaa3i"):"");
  200. //需方地址
  201. Map address = new HashMap();
  202. address.put("componentKey", "address");
  203. address.put("componentValue",formData.containsKey("textField_mogtxb0g")?UtilMap.getString(formData, "textField_mogtxb0g"):"");
  204. //客户联系方式
  205. Map clientContactInfo = new HashMap();
  206. clientContactInfo.put("componentKey", "clientContactInfo");
  207. clientContactInfo.put("componentValue",formData.containsKey("textField_ml0iaa3k")?UtilMap.getString(formData, "textField_ml0iaa3k"):"");
  208. //需方电子邮箱
  209. Map clientEmail = new HashMap();
  210. clientEmail.put("componentKey", "clientEmail");
  211. clientEmail.put("componentValue",formData.containsKey("textField_mogtxb0h")?UtilMap.getString(formData, "textField_mogtxb0h"):"");
  212. //我方公司
  213. Map company = new HashMap();
  214. company.put("componentKey", "company");
  215. company.put("componentValue", formData.containsKey("selectField_mjmlcp4y")?UtilMap.getString(formData, "selectField_mjmlcp4y"):"");
  216. // 我方公司印章
  217. Map companySeal = new HashMap();
  218. companySeal.put("componentKey", "companySeal");
  219. companySeal.put("componentValue", formData.containsKey("selectField_mjmlcp4y")?UtilMap.getString(formData, "selectField_mjmlcp4y"):"");
  220. Map companyEng = new HashMap();
  221. if ("恒益隆贸易(上海)有限公司".equals(UtilMap.getString(formData, "selectField_mjmlcp4y"))) {
  222. companyEng.put("componentKey", "companyEng");
  223. companyEng.put("componentValue", "Wells International Trading (Shanghai) Co.,Ltd");
  224. }else if ("上海绍贺贸易有限公司".equals(UtilMap.getString(formData, "selectField_mjmlcp4y"))){
  225. companyEng.put("componentKey", "companyEng");
  226. companyEng.put("componentValue", "Shanghai Shaw He Enterprise Co.,Ltd");
  227. }
  228. //销售人员
  229. Map salesPerson = new HashMap();
  230. salesPerson.put("componentKey", "salesPerson");
  231. salesPerson.put("componentValue", formData.containsKey("employeefield_3VMLhM35")?UtilMap.getList(formData, "employeefield_3VMLhM35").get(0):"");
  232. //供方联系方式
  233. Map contactInfo = new HashMap();
  234. contactInfo.put("componentKey", "contactInfo");
  235. contactInfo.put("componentValue",formData.containsKey("textField_mogubh6s")?UtilMap.getString(formData, "textField_mogubh6s"):"");
  236. //供方电子邮箱
  237. Map email = new HashMap();
  238. email.put("componentKey", "email");
  239. email.put("componentValue",formData.containsKey("textField_mogubh6t")?UtilMap.getString(formData, "textField_mogubh6t"):"");
  240. //报价日期
  241. Map quotationDate = new HashMap();
  242. if(formData.containsKey("dateField_mjmkwpsf")){
  243. long quotationTimestamp = UtilMap.getLong(formData, "dateField_mjmkwpsf"); // 例如对应 2026-04-27
  244. String quotationDateStr = Instant.ofEpochMilli(quotationTimestamp)
  245. .atZone(ZoneId.systemDefault())
  246. .format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
  247. quotationDate.put("componentKey", "quotationDate");
  248. quotationDate.put("componentValue", quotationDateStr);
  249. }else{
  250. quotationDate.put("componentKey", "quotationDate");
  251. quotationDate.put("componentValue", "");
  252. }
  253. //有效期
  254. Map validityPeriod = new HashMap();
  255. if(formData.containsKey("datefield_SBfn4X3p")){
  256. long validityTimestamp = UtilMap.getLong(formData, "datefield_SBfn4X3p"); // 例如对应 2026-04-27
  257. String validityPeriodStr = Instant.ofEpochMilli(validityTimestamp)
  258. .atZone(ZoneId.systemDefault())
  259. .format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
  260. validityPeriod.put("componentKey", "validityPeriod");
  261. validityPeriod.put("componentValue", validityPeriodStr);
  262. }else{
  263. validityPeriod.put("componentKey", "validityPeriod");
  264. validityPeriod.put("componentValue", "");
  265. }
  266. // 产品明细
  267. List<Map> productTable = UtilMap.getList(formData, "tableField_mkdl67qc");
  268. List<Map> productDetail = new ArrayList<>();
  269. productDetail.add(getRowMap(false));
  270. for (int i = 0; i < productTable.size(); i++) {
  271. String productType = productTable.get(i).containsKey("selectField_mq6g5d7h")?UtilMap.getString(productTable.get(i), "selectField_mq6g5d7h"):"";//产品类型
  272. String productName = productTable.get(i).containsKey("textField_mq505rze")?UtilMap.getString(productTable.get(i), "textField_mq505rze"):"";//产品名称
  273. // String productModel = productTable.get(i).containsKey("textField_mogxdk7x")?UtilMap.getString(productTable.get(i), "textField_mogxdk7x"):"";//产品型号--舍弃
  274. String productSize = productTable.get(i).containsKey("textField_mogxdk7y")?UtilMap.getString(productTable.get(i), "textField_mogxdk7y"):"";//产品规格
  275. String productNum = productTable.get(i).containsKey("numberField_mkdl67qd")?UtilMap.getString(productTable.get(i), "numberField_mkdl67qd"):"";//数量
  276. String productUnit = productTable.get(i).containsKey("textField_mq6g5d7g")?UtilMap.getString(productTable.get(i), "textField_mq6g5d7g"):"";//单位
  277. double productPrice = productTable.get(i).containsKey("numberField_mkdl67qe")?UtilMap.getDouble(productTable.get(i), "numberField_mkdl67qe"):0;//单价
  278. String note = productTable.get(i).containsKey("textField_mkdl67qg")?UtilMap.getString(productTable.get(i), "textField_mkdl67qg"):"";//备注
  279. if ("恒益隆贸易(上海)有限公司".equals(UtilMap.getString(formData, "selectField_mjmlcp4y"))) {
  280. if (i == 0){
  281. productDetail.add(getRowMap(false,productType,productName,productSize,productNum,productUnit,productPrice,note));
  282. }else {
  283. productDetail.add(getRowMap(true,productType,productName,productSize,productNum,productUnit,productPrice,note));
  284. }
  285. }else if ("上海绍贺贸易有限公司".equals(UtilMap.getString(formData, "selectField_mjmlcp4y"))){
  286. if (i == 0){
  287. productDetail.add(getRowMap(false,productName,productSize,productNum,productUnit,productPrice,note));
  288. }else {
  289. productDetail.add(getRowMap(true,productName,productSize,productNum,productUnit,productPrice,note));
  290. }
  291. }
  292. }
  293. Map productDetailMap = new HashMap();
  294. productDetailMap.put("componentKey", "productDetail");
  295. productDetailMap.put("componentValue", JSONObject.toJSONString(productDetail));
  296. //交货方式
  297. Map submitType = new HashMap();
  298. submitType.put("componentKey", "submitType");
  299. submitType.put("componentValue", formData.containsKey("textField_mogtxb0m")?UtilMap.getString(formData, "textField_mogtxb0m"):"");
  300. //付款方式
  301. Map payType = new HashMap();
  302. payType.put("componentKey", "payType");
  303. payType.put("componentValue", formData.containsKey("textField_mogtxb0o")?UtilMap.getString(formData, "textField_mogtxb0o"):"");
  304. //送货日期
  305. Map deliverDate = new HashMap();
  306. deliverDate.put("componentKey", "deliverDate");
  307. deliverDate.put("componentValue", formData.containsKey("textField_mogtxb0q")?UtilMap.getString(formData, "textField_mogtxb0q"):"");
  308. components.add(clientName);
  309. components.add(salesPerson);
  310. components.add(contactPerson);
  311. components.add(contactInfo);
  312. components.add(address);
  313. components.add(email);
  314. components.add(clientContactInfo);
  315. components.add(quotationDate);
  316. components.add(clientEmail);
  317. components.add(validityPeriod);
  318. components.add(productDetailMap);
  319. components.add(company);
  320. components.add(companyEng);
  321. components.add(companySeal);
  322. components.add(submitType);
  323. components.add(payType);
  324. components.add(deliverDate);
  325. if ("恒益隆贸易(上海)有限公司".equals(UtilMap.getString(formData, "selectField_mjmlcp4y"))) {
  326. reqBodyObj.put("docTemplateId", bjdHYL_docTemplateId);
  327. }else if ("上海绍贺贸易有限公司".equals(UtilMap.getString(formData, "selectField_mjmlcp4y"))){
  328. reqBodyObj.put("docTemplateId", bjdSH_docTemplateId);
  329. }
  330. reqBodyObj.put("fileName", fileName+".pdf");
  331. reqBodyObj.put("components", components);
  332. }catch (Exception e){
  333. e.printStackTrace();
  334. }
  335. }
  336. // 生成合同订单模板文件数据
  337. private static void getContract(Map formData,List<Map> components,JSONObject reqBodyObj,String fileName){
  338. try{
  339. //我方公司
  340. Map companyTitle = new HashMap();
  341. companyTitle.put("componentKey", "companyTitle");
  342. companyTitle.put("componentValue", formData.containsKey("selectField_mjmlcp4y")?UtilMap.getString(formData, "selectField_mjmlcp4y"):"");
  343. //我方公司英文
  344. Map companyEng = new HashMap();
  345. if ("恒益隆贸易(上海)有限公司".equals(UtilMap.getString(formData, "selectField_mjmlcp4y"))) {
  346. companyEng.put("componentKey", "companyEng");
  347. companyEng.put("componentValue", "Wells International Trading (Shanghai) Co.,Ltd");
  348. }else if ("上海绍贺贸易有限公司".equals(UtilMap.getString(formData, "selectField_mjmlcp4y"))){
  349. companyEng.put("componentKey", "companyEng");
  350. companyEng.put("componentValue", "Shanghai Shaw He Enterprise Co.,Ltd");
  351. }
  352. //供方
  353. Map supplier = new HashMap();
  354. supplier.put("componentKey", "supplier");
  355. supplier.put("componentValue", formData.containsKey("selectField_mjmlcp4y")?UtilMap.getString(formData, "selectField_mjmlcp4y"):"");
  356. //送货地址
  357. Map productAddress = new HashMap();
  358. productAddress.put("componentKey", "productAddress");
  359. productAddress.put("componentValue", formData.containsKey("textareaField_ml3g7xro")?UtilMap.getString(formData, "textareaField_ml3g7xro"):"");
  360. //需方
  361. Map buyer = new HashMap();
  362. buyer.put("componentKey", "buyer");
  363. buyer.put("componentValue", formData.containsKey("textField_mkkr6eru")?UtilMap.getString(formData, "textField_mkkr6eru"):"");
  364. //合同编号
  365. Map contractNo = new HashMap();
  366. contractNo.put("componentKey", "contractNo");
  367. contractNo.put("componentValue", formData.containsKey("textfield_AevzfRIt")?UtilMap.getString(formData, "textfield_AevzfRIt"):"");
  368. //签订日期
  369. Map signDate = new HashMap();
  370. if(formData.containsKey("datefield_oEfE9lwc")){
  371. long quotationTimestamp = UtilMap.getLong(formData, "datefield_oEfE9lwc"); // 例如对应 2026-04-27
  372. String signDateStr = Instant.ofEpochMilli(quotationTimestamp)
  373. .atZone(ZoneId.systemDefault())
  374. .format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
  375. signDate.put("componentKey", "signDate");
  376. signDate.put("componentValue", signDateStr);
  377. }else{
  378. signDate.put("componentKey", "signDate");
  379. signDate.put("componentValue", "");
  380. }
  381. //签订地址
  382. Map signAddress = new HashMap();
  383. signAddress.put("componentKey", "signAddress");
  384. signAddress.put("componentValue", formData.containsKey("textField_mp5c1fkv")?UtilMap.getString(formData, "textField_mp5c1fkv"):"");
  385. //我方公司名称
  386. Map companyName = new HashMap();
  387. companyName.put("componentKey", "companyName");
  388. companyName.put("componentValue", formData.containsKey("selectField_mjmlcp4y")?UtilMap.getString(formData, "selectField_mjmlcp4y"):"");
  389. //供方签约代表人
  390. Map supplySignatory = new HashMap();
  391. supplySignatory.put("componentKey", "supplySignatory");
  392. supplySignatory.put("componentValue", formData.containsKey("employeefield_3VMLhM35")?UtilMap.getList(formData, "employeefield_3VMLhM35").get(0):"");
  393. //需方公司名称
  394. Map buyerCompany = new HashMap();
  395. buyerCompany.put("componentKey", "buyerCompany");
  396. buyerCompany.put("componentValue", formData.containsKey("textField_mkkr6eru")?UtilMap.getString(formData, "textField_mkkr6eru"):"");
  397. //需方公司地址
  398. Map buyerAddress = new HashMap();
  399. buyerAddress.put("componentKey", "buyerAddress");
  400. buyerAddress.put("componentValue",formData.containsKey("textareaField_ml3g7xro")?UtilMap.getString(formData, "textareaField_ml3g7xro"):"");
  401. //需方法定代表人
  402. Map buyerBoss = new HashMap();
  403. buyerBoss.put("componentKey", "buyerBoss");
  404. buyerBoss.put("componentValue",formData.containsKey("textField_mp5c1fl4")?UtilMap.getString(formData, "textField_mp5c1fl4"):"");
  405. //需方签约代表人
  406. Map buyerSignatory = new HashMap();
  407. buyerSignatory.put("componentKey", "buyerSignatory");
  408. buyerSignatory.put("componentValue",formData.containsKey("textField_ml0iaa3i")?UtilMap.getString(formData, "textField_ml0iaa3i"):"");
  409. //电话传真
  410. Map buyerContact = new HashMap();
  411. buyerContact.put("componentKey", "buyerContact");
  412. buyerContact.put("componentValue",formData.containsKey("textField_ml0iaa3k")?UtilMap.getString(formData, "textField_ml0iaa3k"):"");
  413. //需方开户行
  414. Map buyerBank = new HashMap();
  415. buyerBank.put("componentKey", "buyerBank");
  416. buyerBank.put("componentValue",formData.containsKey("textfield_QTa8eERa")?UtilMap.getString(formData, "textfield_QTa8eERa"):"");
  417. //需方银行账号
  418. Map buyerAccount = new HashMap();
  419. buyerAccount.put("componentKey", "buyerAccount");
  420. buyerAccount.put("componentValue",formData.containsKey("textField_mjl28mb4")?UtilMap.getString(formData, "textField_mjl28mb4"):"");
  421. //税号
  422. Map buyerTax = new HashMap();
  423. buyerTax.put("componentKey", "buyerTax");
  424. buyerTax.put("componentValue",formData.containsKey("textfield_N5RrGSke")?UtilMap.getString(formData, "textfield_N5RrGSke"):"");
  425. // 产品明细
  426. List<Map> productTable = UtilMap.getList(formData, "tableField_mkdlflkr");
  427. List<Map> productDetail = new ArrayList<>();
  428. productDetail.add(getRowMap(false));
  429. for (int i = 0; i < productTable.size(); i++) {
  430. String productName = productTable.get(i).containsKey("textField_mq505rze")?UtilMap.getString(productTable.get(i), "textField_mq505rze"):"";//产品名称
  431. String productModel = productTable.get(i).containsKey("textField_mp5c1fkt")?UtilMap.getString(productTable.get(i), "textField_mp5c1fkt"):"";//规格型号
  432. String productUnit = productTable.get(i).containsKey("textField_mp5c1fku")?UtilMap.getString(productTable.get(i), "textField_mp5c1fku"):"";//产品单位
  433. String productNum = productTable.get(i).containsKey("numberField_mkdlflkt")?UtilMap.getString(productTable.get(i), "numberField_mkdlflkt"):"";//数量
  434. double productPrice = productTable.get(i).containsKey("numberField_mkdlflku")?UtilMap.getDouble(productTable.get(i), "numberField_mkdlflku"):0;//单价
  435. double productSum = productTable.get(i).containsKey("numberField_mkdlflkv")?UtilMap.getDouble(productTable.get(i), "numberField_mkdlflkv"):0;//金额小计
  436. String note = productTable.get(i).containsKey("textField_mkdlflkw")?UtilMap.getString(productTable.get(i), "textField_mkdlflkw"):"";//备注
  437. if (i == 0){
  438. productDetail.add(getRowMap(false,productName,productModel,productUnit,productNum,productPrice,productSum,note));
  439. }else{
  440. productDetail.add(getRowMap(true,productName,productModel,productUnit,productNum,productPrice,productSum,note));
  441. }
  442. }
  443. if (!productTable.isEmpty()) {
  444. productDetail.add(getRowMap(true, "金额合计", "大写金额(人民币):",UtilMap.getString(formData, "textField_mpandaox"), "", "", "小写(RMB):", UtilMap.getDouble(formData, "numberfield_WAUnrcAf")));
  445. }
  446. Map productDetailMap = new HashMap();
  447. productDetailMap.put("componentKey", "productDetail");
  448. productDetailMap.put("componentValue", JSONObject.toJSONString(productDetail));
  449. components.add(companyTitle);
  450. components.add(companyEng);
  451. components.add(supplier);
  452. components.add(productAddress);
  453. components.add(buyer);
  454. components.add(contractNo);
  455. components.add(signDate);
  456. components.add(signAddress);
  457. components.add(companyName);
  458. components.add(supplySignatory);
  459. components.add(buyerCompany);
  460. components.add(buyerAddress);
  461. components.add(buyerSignatory);
  462. components.add(buyerBoss);
  463. components.add(buyerContact);
  464. components.add(buyerBank);
  465. components.add(buyerAccount);
  466. components.add(buyerTax);
  467. components.add(productDetailMap);
  468. reqBodyObj.put("docTemplateId", ht_docTemplateId);
  469. reqBodyObj.put("fileName", fileName+".pdf");
  470. reqBodyObj.put("components", components);
  471. }catch (Exception e){
  472. e.printStackTrace();
  473. }
  474. }
  475. private static Map getRowMap(boolean insertRow,Object... columnValues) {
  476. Map row = new HashMap();
  477. for (int i = 0; i < columnValues.length; i++) {
  478. row.put("column" + (i + 1), columnValues[i]);
  479. }
  480. Map result = new HashMap();
  481. result.put("row",row);
  482. result.put("insertRow",insertRow);
  483. return result;
  484. }
  485. /***
  486. * e签宝post请求
  487. */
  488. public Map eqbPost(String postUrl,JSONObject reqBodyObj) {
  489. // 完整的请求地址
  490. String postAllUrl = host+postUrl;
  491. try {
  492. // 请求Body体数据
  493. String reqBodyData = reqBodyObj.toString();
  494. // 对请求Body体内的数据计算ContentMD5
  495. String contentMD5 = doContentMD5(reqBodyData);
  496. System.out.println("请求body数据:"+reqBodyData);
  497. System.out.println("body的md5值:"+ contentMD5);
  498. // 构建待签名字符串
  499. String method = "POST";
  500. String accept = "*/*";
  501. String contentType = "application/json";
  502. String url = postUrl;
  503. String date = "";
  504. String headers = "";
  505. StringBuffer sb = new StringBuffer();
  506. sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
  507. .append(contentType).append("\n").append(date).append("\n");
  508. if ("".equals(headers)) {
  509. sb.append(headers).append(url);
  510. } else {
  511. sb.append(headers).append("\n").append(url);
  512. }
  513. // 构建参与请求签名计算的明文
  514. String plaintext = sb.toString();
  515. // 计算请求签名值
  516. String reqSignature = doSignatureBase64(plaintext, appKey);
  517. System.out.println("计算请求签名值:"+reqSignature);
  518. // 获取时间戳(精确到毫秒)
  519. long timeStamp = timeStamp();
  520. // 构建请求头
  521. LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
  522. header.put("X-Tsign-Open-App-Id", appId);
  523. header.put("X-Tsign-Open-Auth-Mode", "Signature");
  524. header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
  525. header.put("Accept", accept);
  526. header.put("Content-Type", contentType);
  527. header.put("X-Tsign-Open-Ca-Signature", reqSignature);
  528. header.put("Content-MD5", contentMD5);
  529. // 发送POST请求
  530. String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
  531. System.out.println("请求返回信息: " + result);
  532. Map resultObj =(Map) JSONObject.parse(result);
  533. Map data = UtilMap.getMap(resultObj, "data");
  534. return data;
  535. } catch (Exception e) {
  536. e.printStackTrace();
  537. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  538. System.out.println(msg);
  539. throw new RuntimeException(e);
  540. }
  541. }
  542. public static String doContentMD5(String str) throws Exception {
  543. byte[] md5Bytes = null;
  544. MessageDigest md5 = null;
  545. String contentMD5 = null;
  546. try {
  547. md5 = MessageDigest.getInstance("MD5");
  548. // 计算md5函数
  549. md5.update(str.getBytes("UTF-8"));
  550. // 获取文件MD5的二进制数组(128位)
  551. md5Bytes = md5.digest();
  552. // 对Body体MD5的二进制数组(128位)进行Base64编码(而不是对32位的16进制字符串进行编码)
  553. contentMD5 = new String(org.apache.commons.codec.binary.Base64.encodeBase64(md5Bytes), "UTF-8");
  554. } catch (NoSuchAlgorithmException e) {
  555. String msg = MessageFormat.format("不支持此算法: {0}", e.getMessage());
  556. Exception ex = new Exception(msg);
  557. ex.initCause(e);
  558. throw ex;
  559. } catch (UnsupportedEncodingException e) {
  560. String msg = MessageFormat.format("不支持的字符编码: {0}", e.getMessage());
  561. Exception ex = new Exception(msg);
  562. ex.initCause(e);
  563. throw ex;
  564. }
  565. return contentMD5;
  566. }
  567. public static long timeStamp() {
  568. long timeStamp = System.currentTimeMillis();
  569. return timeStamp;
  570. }
  571. /***
  572. * 计算请求签名值
  573. *
  574. * @param message 待计算的消息
  575. * @param secret 密钥
  576. * @return HmacSHA256计算后摘要值的Base64编码
  577. * @throws Exception 加密过程中的异常信息
  578. */
  579. public static String doSignatureBase64(String message, String secret) throws Exception {
  580. String algorithm = "HmacSHA256";
  581. Mac hmacSha256;
  582. String digestBase64 = null;
  583. try {
  584. hmacSha256 = Mac.getInstance(algorithm);
  585. byte[] keyBytes = secret.getBytes("UTF-8");
  586. byte[] messageBytes = message.getBytes("UTF-8");
  587. hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, algorithm));
  588. // 使用HmacSHA256对二进制数据消息Bytes计算摘要
  589. byte[] digestBytes = hmacSha256.doFinal(messageBytes);
  590. // 把摘要后的结果digestBytes使用Base64进行编码
  591. digestBase64 = new String(Base64.encodeBase64(digestBytes), "UTF-8");
  592. } catch (NoSuchAlgorithmException e) {
  593. String msg = MessageFormat.format("不支持此算法: {0}", e.getMessage());
  594. Exception ex = new Exception(msg);
  595. ex.initCause(e);
  596. throw ex;
  597. } catch (UnsupportedEncodingException e) {
  598. String msg = MessageFormat.format("不支持的字符编码: {0}", e.getMessage());
  599. Exception ex = new Exception(msg);
  600. ex.initCause(e);
  601. throw ex;
  602. } catch (InvalidKeyException e) {
  603. String msg = MessageFormat.format("无效的密钥规范: {0}", e.getMessage());
  604. Exception ex = new Exception(msg);
  605. ex.initCause(e);
  606. throw ex;
  607. }
  608. return digestBase64;
  609. }
  610. }