EqbHylServiceImpl.java 29 KB

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