EqbServiceImpl.java 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. package com.malk.huagao.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.malk.huagao.service.EqbService;
  4. import com.malk.huagao.utils.HTTPHelper;
  5. import com.malk.server.aliwork.YDConf;
  6. import com.malk.server.aliwork.YDParam;
  7. import com.malk.server.common.McR;
  8. import com.malk.service.aliwork.YDClient;
  9. import com.malk.service.dingtalk.DDClient;
  10. import com.malk.utils.UtilHttp;
  11. import com.malk.utils.UtilMap;
  12. import org.apache.commons.codec.binary.Base64;
  13. import org.slf4j.MDC;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.beans.factory.annotation.Value;
  16. import org.springframework.scheduling.annotation.Async;
  17. import org.springframework.stereotype.Service;
  18. import javax.crypto.Mac;
  19. import javax.crypto.spec.SecretKeySpec;
  20. import java.io.FileOutputStream;
  21. import java.io.InputStream;
  22. import java.io.UnsupportedEncodingException;
  23. import java.net.HttpURLConnection;
  24. import java.net.URL;
  25. import java.security.InvalidKeyException;
  26. import java.security.MessageDigest;
  27. import java.security.NoSuchAlgorithmException;
  28. import java.text.MessageFormat;
  29. import java.text.SimpleDateFormat;
  30. import java.util.*;
  31. @Service
  32. public class EqbServiceImpl implements EqbService {
  33. @Autowired
  34. private YDConf ydConf;
  35. @Autowired
  36. private YDClient ydClient;
  37. @Autowired
  38. private DDClient ddClient;
  39. // 应用ID
  40. final static String appId = "5112033166";
  41. // 应用密钥(AppSecret)
  42. final static String appKey = "c0641a1f648a68a7014be5a490f0159a";
  43. // e签宝接口调用域名(正式环境)
  44. final static String host = "https://openapi.esign.cn";
  45. //正式环境模板id 经销商模板
  46. final static String jxs_templateId = "6a23f76742d74b858cdc2259a5d5c32f";
  47. //正式环境模板id 其他客户模板
  48. final static String qtkh_templateId = "8b7fef2257bc4093ada330dbf7c874c5";
  49. //正式环境模板id 采购订单模板
  50. final static String cgdd_templateId = "a0827f8946994ebfbed7f44e6b8a6ed8";
  51. @Value(value = "${eqb.downloadFilePath}")
  52. private String downloadFilePath;
  53. @Value(value = "${server.host}")
  54. private String serverHost;
  55. @Value(value = "${eqb.signatoryPsnId}")
  56. private String signatoryPsnId;
  57. @Value(value = "${dingtalk.atUserId}")
  58. private String atUserId;
  59. @Async
  60. @Override
  61. public void autoAgree(String signFlowId,String processInstanceId) {
  62. MDC.put("MDC_KEY_PID","1003");
  63. //1、签署后文件回写宜搭
  64. Map fileInfo = downloadSignedFile(signFlowId);
  65. String fileId = UtilMap.getString(fileInfo, "fileId");
  66. String fileName = UtilMap.getString(fileInfo, "fileName");
  67. String downloadUrl = serverHost + "/huagao/eqb/files/" + fileId + ".pdf?option=download";
  68. String previewUrl = serverHost + "/huagao/eqb/files/" + fileId + ".pdf?option=preview";
  69. //更新宜搭实例签署后文件
  70. Map formData = new HashMap();
  71. Map file = new HashMap();
  72. file.put("downloadUrl",downloadUrl);
  73. file.put("name",fileName);
  74. file.put("previewUrl",previewUrl);
  75. file.put("url",downloadUrl);
  76. file.put("ext","pdf");
  77. formData.put("attachmentField_mj8dt3g2",Arrays.asList(file));
  78. ydClient.operateData(YDParam.builder()
  79. .updateFormDataJson(JSONObject.toJSONString(formData))
  80. .formInstanceId(processInstanceId)
  81. .build(), YDConf.FORM_OPERATION.update);
  82. //2、自动同意宜搭审批节点
  83. String activityId = "node_ockpz6phx73";//审批节点id【e签宝签署(系统节点,禁止删除!)】
  84. long taskId = 0;
  85. String userId = "";//审批人id
  86. //获取审批记录
  87. List<Map> result = (List<Map>) ydClient.queryData(YDParam.builder()
  88. .processInstanceId(processInstanceId)
  89. .build(), YDConf.FORM_QUERY.retrieve_approval_record).getResult();
  90. for (Map approveInfo : result) {
  91. if (activityId.equals(UtilMap.getString(approveInfo, "activityId"))){
  92. //获取taskId
  93. taskId = UtilMap.getLong(approveInfo, "taskId");
  94. userId = UtilMap.getString(approveInfo, "operatorUserId");
  95. break;
  96. }
  97. }
  98. //审批
  99. Map body = new HashMap();
  100. body.put("outResult","AGREE");//AGREE:同意 DISAGREE:拒绝。
  101. body.put("appType",ydConf.getAppType());
  102. body.put("systemToken",ydConf.getSystemToken());
  103. body.put("remark","e签宝签署完成,自动通过");//审批意见
  104. body.put("processInstanceId",processInstanceId);//审批实例id
  105. body.put("userId",userId);//用户userid
  106. body.put("taskId",taskId);//任务id
  107. UtilHttp.doPost("https://api.dingtalk.com/v1.0/yida/tasks/execute", ddClient.initTokenHeader(), null,body);
  108. }
  109. @Async
  110. @Override
  111. public McR sign(Map map) {
  112. MDC.put("MDC_KEY_PID","1003");
  113. //1、查询宜搭实例详情
  114. String formInstId = UtilMap.getString(map, "formInstId");
  115. Map formData = ydClient.queryData(YDParam.builder()
  116. .formInstanceId(formInstId)
  117. .build(), YDConf.FORM_QUERY.retrieve_id).getFormData();
  118. String type = UtilMap.getString(formData, "selectField_mj89qndi");//用印文件类型
  119. String fileName = UtilMap.getString(formData, "textField_meqr38kq");//用印文件名称
  120. String signType = UtilMap.getString(formData, "radioField_mk0s5sf9");//签章类型
  121. JSONObject reqBodyObj = new JSONObject();
  122. List<Map> components = new ArrayList<>();
  123. switch (type){
  124. case "销售合同-经销商":
  125. getJxsObj(formData, components, reqBodyObj, fileName);
  126. break;
  127. case "销售合同-其他客户":
  128. getQtkhObj(formData,components,reqBodyObj,fileName);
  129. break;
  130. case "采购订单":
  131. getCgddObj(formData,components,reqBodyObj,fileName);
  132. break;
  133. default:break;
  134. }
  135. //2、填充模板生成文件
  136. Map result = eqbPost("/v3/files/create-by-doc-template", reqBodyObj);
  137. String fileId = UtilMap.getString(result,"fileId");
  138. //文档转换pdf需时间
  139. try {
  140. Thread.sleep(3000);
  141. } catch (InterruptedException e) {
  142. throw new RuntimeException(e);
  143. }
  144. //3、基于文件发起签署
  145. JSONObject reqBodyObj2 = new JSONObject();
  146. //设置待签署文件信息
  147. Map docs = new HashMap();
  148. docs.put("fileId", fileId);
  149. reqBodyObj2.put("docs", Arrays.asList(docs));
  150. //签署流程配置项
  151. Map signFlowConfig = new HashMap();
  152. signFlowConfig.put("signFlowTitle",fileName);
  153. signFlowConfig.put("autoFinish",true);
  154. signFlowConfig.put("notifyUrl",serverHost + "/huagao/eqb/callback");//回调地址
  155. reqBodyObj2.put("signFlowConfig",signFlowConfig);
  156. //签署方信息
  157. //我方
  158. Map signer = new HashMap();
  159. signer.put("signerType",1);//签署方类型,0 - 个人,1 - 企业/机构,2 - 法定代表人,3 - 经办人
  160. Map orgSignerInfo = new HashMap();
  161. orgSignerInfo.put("orgId","a5ec8fb7d8cc4276bd486824df0ec640");//华高机构id
  162. //企业/机构经办人信息
  163. Map transactorInfo = new HashMap();
  164. transactorInfo.put("psnId",signatoryPsnId);//陈伟东
  165. orgSignerInfo.put("transactorInfo",transactorInfo);
  166. signer.put("orgSignerInfo",orgSignerInfo);
  167. /*Map psnSignerInfo = new HashMap();
  168. psnSignerInfo.put("psnId",signatoryPsnId);//陈伟东
  169. signer.put("psnSignerInfo",psnSignerInfo);*/
  170. Map signField = new HashMap();
  171. signField.put("fileId",fileId);
  172. signField.put("customBizNum",formInstId);
  173. Map normalSignFieldConfig = new HashMap();
  174. normalSignFieldConfig.put("freeMode",true);
  175. /*normalSignFieldConfig.put("autoSign",true);
  176. normalSignFieldConfig.put("signFieldStyle",1);
  177. Map signFieldPosition = new HashMap();
  178. signFieldPosition.put("positionPage",1);
  179. normalSignFieldConfig.put("signFieldPosition",signFieldPosition);*/
  180. signField.put("normalSignFieldConfig",normalSignFieldConfig);
  181. signer.put("signFields",Arrays.asList(signField));
  182. reqBodyObj2.put("signers",Arrays.asList(signer));
  183. //他方
  184. String jbr = "";
  185. String jbrdh = "";
  186. String orgName = "";
  187. if ("双方签章".equals(signType)){
  188. switch (type){
  189. case "销售合同-经销商":
  190. jbr = UtilMap.getString(formData, "textField_mk0s5sfa");//需方经办人
  191. jbrdh = UtilMap.getString(formData, "textField_mf50cbqw");//需方经办人电话
  192. orgName = UtilMap.getString(formData, "textField_mjaukh2p");//需方名称
  193. break;
  194. case "销售合同-其他客户":
  195. jbr = UtilMap.getString(formData, "textField_mk0s5sfh");//需方经办人
  196. jbrdh = UtilMap.getString(formData, "textField_mk0s5sfg");//需方经办人电话
  197. orgName = UtilMap.getString(formData, "textField_mjaukh2p");//需方名称
  198. break;
  199. case "采购订单":
  200. jbr = UtilMap.getString(formData, "textField_mj8dt3g8");//供方经办人
  201. jbrdh = UtilMap.getString(formData, "textField_mj8dt3g9");//供方经办人电话
  202. orgName = UtilMap.getString(formData, "textField_mj8dt3g7");//供方名称
  203. break;
  204. default:break;
  205. }
  206. Map signer2 = new HashMap();
  207. signer2.put("signerType",1);//签署方类型,0 - 个人,1 - 企业/机构,2 - 法定代表人,3 - 经办人
  208. Map orgSignerInfo2 = new HashMap();
  209. orgSignerInfo2.put("orgName",orgName);//对方机构名称
  210. //企业/机构经办人信息
  211. Map transactorInfo2 = new HashMap();
  212. transactorInfo2.put("psnAccount",jbrdh);//经办人账号标识,手机号或邮箱,必须配合psnName(经办人姓名)传入
  213. Map psnInfo = new HashMap();
  214. psnInfo.put("psnName",jbr);//经办人姓名
  215. transactorInfo2.put("psnInfo",psnInfo);
  216. orgSignerInfo2.put("transactorInfo",transactorInfo2);
  217. signer2.put("orgSignerInfo",orgSignerInfo2);
  218. Map signField2 = new HashMap();
  219. signField2.put("fileId",fileId);
  220. signField2.put("customBizNum",formInstId);
  221. Map normalSignFieldConfig2 = new HashMap();
  222. normalSignFieldConfig2.put("freeMode",true);
  223. signField2.put("normalSignFieldConfig",normalSignFieldConfig2);
  224. signer2.put("signFields",Arrays.asList(signField2));
  225. reqBodyObj2.put("signers",Arrays.asList(signer,signer2));
  226. }
  227. Map result2 = eqbPost("/v3/sign-flow/create-by-file", reqBodyObj2);
  228. String signFlowId = UtilMap.getString(result2, "signFlowId");
  229. //4、回写签署地址
  230. JSONObject reqBodyObj3 = new JSONObject();
  231. Map operator = new HashMap();
  232. operator.put("psnId", signatoryPsnId);
  233. reqBodyObj3.put("operator", operator);
  234. Map result3 = eqbPost("/v3/sign-flow/" + signFlowId + "/sign-url", reqBodyObj3);
  235. String shortUrl = UtilMap.getString(result3, "shortUrl");
  236. ydClient.operateData(YDParam.builder()
  237. .formInstanceId(formInstId)
  238. .content("签署地址:"+shortUrl)
  239. .userId("yida_pub_account")
  240. .atUserId(atUserId)//陈伟东
  241. .build(), YDConf.FORM_OPERATION.remarks);
  242. if ("双方签章".equals(signType)){
  243. System.out.println("获取对方签署地址");
  244. JSONObject reqBodyObj4 = new JSONObject();
  245. Map operator2 = new HashMap();
  246. operator2.put("psnAccount", jbrdh);
  247. reqBodyObj4.put("operator", operator2);
  248. reqBodyObj4.put("needLogin", true);
  249. Map result4 = eqbPost("/v3/sign-flow/" + signFlowId + "/sign-url", reqBodyObj4);
  250. String shortUrl2 = UtilMap.getString(result4, "shortUrl");
  251. ydClient.operateData(YDParam.builder()
  252. .formInstanceId(formInstId)
  253. .content("对方签署地址:"+shortUrl2)
  254. .userId("yida_pub_account")
  255. .build(), YDConf.FORM_OPERATION.remarks);
  256. }
  257. return McR.success();
  258. }
  259. private void getJxsObj(Map formData,List<Map> components,JSONObject reqBodyObj,String fileName){
  260. String dgdbh = UtilMap.getString(formData, "textField_mjas1rkm");//订购单编号
  261. String xf = UtilMap.getString(formData, "textField_mjaukh2p");//需方
  262. String xfdz = UtilMap.getString(formData, "textareaField_mf50cbqt");//需方地址
  263. String xfdh = UtilMap.getString(formData, "textField_mf50cbqw");//需方电话
  264. String xfjbr = UtilMap.getString(formData, "textField_mj8dt3fj");//需方经办人
  265. String gfdh = UtilMap.getString(formData, "textField_mjau7ffr");//供方电话
  266. String gfjbr = UtilMap.getString(formData, "textField_mjauqjr9");//供方经办人
  267. long ddrq = UtilMap.getLong(formData, "dateField_mj8dt3fa");//订单日期
  268. String ghkjxybh = UtilMap.getString(formData, "textField_mjas1rkn");//供货框架协议编号
  269. Double jehj = UtilMap.getDouble(formData, "numberField_me1335n7");//金额合计
  270. String jehjdx = UtilMap.getString(formData, "textField_mf50cbvj");//金额合计大写
  271. String fktj = UtilMap.getString(formData, "radioField_mf50cbu7");//付款条件
  272. String xfnsrsbh = UtilMap.getString(formData, "textField_mf50cbuj");//需方纳税人识别号
  273. String xfkhyh = UtilMap.getString(formData, "textField_mf50cbup");//需方开户银行
  274. String xfzh = UtilMap.getString(formData, "textField_mf50cbuk");//需方账号
  275. List<Map> xshtmxList = UtilMap.getList(formData, "tableField_mf50cbre");//销售合同明细
  276. List<Map> jhdzList = UtilMap.getList(formData, "tableField_mj8dt3fk");//交货地址
  277. //订购单编号
  278. Map jxsComponent = new HashMap();
  279. jxsComponent.put("componentKey", "dgdbh");
  280. jxsComponent.put("componentValue", dgdbh);
  281. //需方
  282. Map jxsComponent2 = new HashMap();
  283. jxsComponent2.put("componentKey", "xf");
  284. jxsComponent2.put("componentValue", xf);
  285. //需方地址
  286. Map jxsComponent3 = new HashMap();
  287. jxsComponent3.put("componentKey", "xfdz");
  288. jxsComponent3.put("componentValue", xfdz);
  289. //需方电话
  290. Map jxsComponent4 = new HashMap();
  291. jxsComponent4.put("componentKey", "xfdh");
  292. jxsComponent4.put("componentValue", xfdh);
  293. //需方经办人
  294. Map jxsComponent5 = new HashMap();
  295. jxsComponent5.put("componentKey", "xfjbr");
  296. jxsComponent5.put("componentValue", xfjbr);
  297. //供方电话
  298. Map jxsComponent6 = new HashMap();
  299. jxsComponent6.put("componentKey", "gfdh");
  300. jxsComponent6.put("componentValue", gfdh);
  301. //供方经办人
  302. Map jxsComponent7 = new HashMap();
  303. jxsComponent7.put("componentKey", "gfjbr");
  304. jxsComponent7.put("componentValue", gfjbr);
  305. //订单日期
  306. Map jxsComponent8 = new HashMap();
  307. jxsComponent8.put("componentKey", "ddrq");
  308. jxsComponent8.put("componentValue", ddrq);
  309. //供货框架协议编号
  310. Map jxsComponent9 = new HashMap();
  311. jxsComponent9.put("componentKey", "ghkjxybh");
  312. jxsComponent9.put("componentValue", ghkjxybh);
  313. //金额合计
  314. Map jxsComponent10 = new HashMap();
  315. jxsComponent10.put("componentKey", "jehj");
  316. jxsComponent10.put("componentValue", jehj);
  317. //金额合计大写
  318. Map jxsComponent11 = new HashMap();
  319. jxsComponent11.put("componentKey", "jehjdx");
  320. jxsComponent11.put("componentValue", jehjdx);
  321. //付款条件
  322. Map jxsComponent12 = new HashMap();
  323. if ("款到发货".equals(fktj)){
  324. jxsComponent12.put("componentKey", "xkhh");
  325. jxsComponent12.put("componentValue", true);
  326. }else {
  327. jxsComponent12.put("componentKey", "qt");
  328. jxsComponent12.put("componentValue", true);
  329. }
  330. //需方纳税人识别号
  331. Map jxsComponent13 = new HashMap();
  332. jxsComponent13.put("componentKey", "xfnsrsbh");
  333. jxsComponent13.put("componentValue", xfnsrsbh);
  334. //需方开户银行
  335. Map jxsComponent14 = new HashMap();
  336. jxsComponent14.put("componentKey", "xfkhyh");
  337. jxsComponent14.put("componentValue", xfkhyh);
  338. //需方账号
  339. Map jxsComponent15 = new HashMap();
  340. jxsComponent15.put("componentKey", "xfzh");
  341. jxsComponent15.put("componentValue", xfzh);
  342. //销售合同明细
  343. List<Map> xshtmx = new ArrayList<>();
  344. xshtmx.add(getRowMap(false));
  345. for (int i = 0; i < xshtmxList.size(); i++) {
  346. String cpmc = UtilMap.getString(xshtmxList.get(i), "textField_mf50cbrf");//产品名称
  347. String cpxh = UtilMap.getString(xshtmxList.get(i), "textField_mf50cbrg");//产品型号
  348. String dw = UtilMap.getString(xshtmxList.get(i), "textField_mf50cbrh");//单位
  349. String xssl = UtilMap.getString(xshtmxList.get(i), "numberField_mfbx1pr4");//销售数量
  350. double lsdj = UtilMap.getDouble(xshtmxList.get(i), "numberField_mf50cbri");//零售单价
  351. double pfyhdj = UtilMap.getDouble(xshtmxList.get(i), "numberField_mf50cbrj");//批发优惠单价
  352. String zbq = UtilMap.getString(xshtmxList.get(i), "textField_mjas1rkx");//质保期
  353. if (i == 0){
  354. xshtmx.add(getRowMap(false,i+1,cpmc,cpxh,dw,lsdj,pfyhdj,zbq));
  355. }else {
  356. xshtmx.add(getRowMap(true,i+1,cpmc,cpxh,dw,lsdj,pfyhdj,zbq));
  357. }
  358. }
  359. Map jxsComponent16 = new HashMap();
  360. jxsComponent16.put("componentKey", "xshtmx");
  361. jxsComponent16.put("componentValue", JSONObject.toJSONString(xshtmx));
  362. //交货地址
  363. List<Map> jhdz = new ArrayList<>();
  364. jhdz.add(getRowMap(false));
  365. for (int i = 0; i < jhdzList.size(); i++) {
  366. String shdz = UtilMap.getString(jhdzList.get(i), "textField_mj8dt3fl");//收货地址
  367. String shr = UtilMap.getString(jhdzList.get(i), "textField_mj8dt3fm");//收货人
  368. String shrlxdh = UtilMap.getString(jhdzList.get(i), "textField_mj8dt3fn");//收货人联系电话
  369. if (i == 0){
  370. jhdz.add(getRowMap(false,i+1,shdz,shr,shrlxdh));
  371. }else {
  372. jhdz.add(getRowMap(true,i+1,shdz,shr,shrlxdh));
  373. }
  374. }
  375. Map jxsComponent17 = new HashMap();
  376. jxsComponent17.put("componentKey", "jhdz");
  377. jxsComponent17.put("componentValue", JSONObject.toJSONString(jhdz));
  378. Map jxsComponent19 = new HashMap();
  379. jxsComponent19.put("componentKey", "xf2");
  380. jxsComponent19.put("componentValue", xf);
  381. Map jxsComponent20 = new HashMap();
  382. jxsComponent20.put("componentKey", "xfdz2");
  383. jxsComponent20.put("componentValue", xfdz);
  384. components.add(jxsComponent);
  385. components.add(jxsComponent2);
  386. components.add(jxsComponent3);
  387. components.add(jxsComponent4);
  388. components.add(jxsComponent5);
  389. components.add(jxsComponent6);
  390. components.add(jxsComponent7);
  391. components.add(jxsComponent8);
  392. components.add(jxsComponent9);
  393. components.add(jxsComponent10);
  394. components.add(jxsComponent11);
  395. components.add(jxsComponent12);
  396. components.add(jxsComponent13);
  397. components.add(jxsComponent14);
  398. components.add(jxsComponent15);
  399. components.add(jxsComponent16);
  400. components.add(jxsComponent17);
  401. components.add(jxsComponent19);
  402. components.add(jxsComponent20);
  403. if (!"款到发货".equals(fktj)){
  404. Map jxsComponent18 = new HashMap();
  405. jxsComponent18.put("componentKey", "qtnr");
  406. jxsComponent18.put("componentValue", fktj);
  407. components.add(jxsComponent18);
  408. }
  409. // 构建请求Body体
  410. reqBodyObj.put("docTemplateId", jxs_templateId);
  411. reqBodyObj.put("fileName", fileName+".pdf");
  412. reqBodyObj.put("components", components);
  413. }
  414. private void getQtkhObj(Map formData, List<Map> components, JSONObject reqBodyObj, String fileName){
  415. String htbh = UtilMap.getString(formData, "textField_mjb71q95");//合同编号
  416. String jf = UtilMap.getString(formData, "textField_mjaukh2p");//甲方
  417. double xsze = UtilMap.getDouble(formData, "numberField_mjb71qac");//销售总额
  418. String fkfs = UtilMap.getString(formData, "textField_mjb886fw");//付款方式
  419. String jfnsrsbh = UtilMap.getString(formData, "textField_mjb71q9w");//甲方纳税人识别号
  420. String spyx = UtilMap.getString(formData, "textField_mjb71q9y");//收票邮箱
  421. String jfdb = UtilMap.getString(formData, "textField_mjb886fx");//甲方代表
  422. String yfdb = UtilMap.getString(formData, "textField_mj8dt3fj");//乙方代表
  423. List<Map> xshtmxList = UtilMap.getList(formData, "tableField_mjb71qab");//销售合同明细
  424. //订购单编号
  425. Map jxsComponent = new HashMap();
  426. jxsComponent.put("componentKey", "htbh");
  427. jxsComponent.put("componentValue", htbh);
  428. //甲方
  429. Map jxsComponent2 = new HashMap();
  430. jxsComponent2.put("componentKey", "jf");
  431. jxsComponent2.put("componentValue", jf);
  432. //销售总额
  433. Map jxsComponent3 = new HashMap();
  434. jxsComponent3.put("componentKey", "xsze");
  435. jxsComponent3.put("componentValue", xsze);
  436. //付款方式
  437. Map jxsComponent4 = new HashMap();
  438. jxsComponent4.put("componentKey", "fkfs");
  439. jxsComponent4.put("componentValue", fkfs);
  440. //甲方纳税人识别号
  441. Map jxsComponent5 = new HashMap();
  442. jxsComponent5.put("componentKey", "jfnsrsbh");
  443. jxsComponent5.put("componentValue", jfnsrsbh);
  444. //收票邮箱
  445. Map jxsComponent6 = new HashMap();
  446. jxsComponent6.put("componentKey", "spyx");
  447. jxsComponent6.put("componentValue", spyx);
  448. //甲方代表
  449. Map jxsComponent7 = new HashMap();
  450. jxsComponent7.put("componentKey", "jfdb");
  451. jxsComponent7.put("componentValue", jfdb);
  452. //乙方代表
  453. Map jxsComponent8 = new HashMap();
  454. jxsComponent8.put("componentKey", "yfdb");
  455. jxsComponent8.put("componentValue", yfdb);
  456. //销售合同明细
  457. List<Map> xshtmx = new ArrayList<>();
  458. xshtmx.add(getRowMap(false));
  459. for (int i = 0; i < xshtmxList.size(); i++) {
  460. String cpmc = UtilMap.getString(xshtmxList.get(i), "textField_mjb71qa4");//产品名称
  461. String cpxh = UtilMap.getString(xshtmxList.get(i), "textField_mjb71qa5");//产品型号
  462. String dw = UtilMap.getString(xshtmxList.get(i), "textField_mjb71qa6");//单位
  463. String xssl = UtilMap.getString(xshtmxList.get(i), "numberField_mjb71qa7");//销售数量
  464. double hsdj = UtilMap.getDouble(xshtmxList.get(i), "numberField_mjb71qa8");//含税单价
  465. int zbq = UtilMap.getInt(xshtmxList.get(i), "numberField_mjb71qaw");//质保期
  466. String smzs = UtilMap.getString(xshtmxList.get(i), "textField_mjb71qam");//扫描张数
  467. double jshj = UtilMap.getDouble(xshtmxList.get(i), "numberField_mjb71qan");//价税合计
  468. double zke = UtilMap.getDouble(xshtmxList.get(i), "numberField_mjb71qao");//折扣额
  469. String bz = UtilMap.getString(xshtmxList.get(i), "textField_mjb71qaq");//备注
  470. if (i == 0){
  471. xshtmx.add(getRowMap(false,cpmc,cpxh,dw,xssl,hsdj,zbq+"年",smzs,jshj,zke,bz));
  472. }else {
  473. xshtmx.add(getRowMap(true,cpmc,cpxh,dw,xssl,hsdj,zbq+"年",smzs,jshj,zke,bz));
  474. }
  475. }
  476. Map jxsComponent9 = new HashMap();
  477. jxsComponent9.put("componentKey", "xshtmx");
  478. jxsComponent9.put("componentValue", JSONObject.toJSONString(xshtmx));
  479. //甲方
  480. Map jxsComponent10 = new HashMap();
  481. jxsComponent10.put("componentKey", "jf2");
  482. jxsComponent10.put("componentValue", jf);
  483. //甲方
  484. Map jxsComponent11 = new HashMap();
  485. jxsComponent11.put("componentKey", "jf3");
  486. jxsComponent11.put("componentValue", jf);
  487. components.add(jxsComponent);
  488. components.add(jxsComponent2);
  489. components.add(jxsComponent3);
  490. components.add(jxsComponent4);
  491. components.add(jxsComponent5);
  492. components.add(jxsComponent6);
  493. components.add(jxsComponent7);
  494. components.add(jxsComponent8);
  495. components.add(jxsComponent9);
  496. components.add(jxsComponent10);
  497. components.add(jxsComponent11);
  498. // 构建请求Body体
  499. reqBodyObj.put("docTemplateId", qtkh_templateId);
  500. reqBodyObj.put("fileName", fileName+".pdf");
  501. reqBodyObj.put("components", components);
  502. }
  503. private void getCgddObj(Map formData,List<Map> components,JSONObject reqBodyObj,String fileName){
  504. String cgddbh = UtilMap.getString(formData, "textField_mj9msq4v");//采购订单编号
  505. long date = UtilMap.getLong(formData, "dateField_mj8dt3g6");//签订日期
  506. String yf = UtilMap.getString(formData, "textField_mj8dt3g7");//乙方
  507. String yfdz = UtilMap.getString(formData, "textField_mj8dt3gt");//乙方地址
  508. String yflxr = UtilMap.getString(formData, "textField_mj8dt3g8");//乙方联系人
  509. String yfdh = UtilMap.getString(formData, "textField_mj8dt3g9");//乙方电话
  510. String nsrsbh = UtilMap.getString(formData, "textField_mj8dt3gs");//乙方纳税人识别号
  511. String khyh = UtilMap.getString(formData, "textField_mj8dt3gq");//乙方开户银行
  512. String zh = UtilMap.getString(formData, "textField_mj8dt3gr");//乙方账号
  513. String zlyq = UtilMap.getString(formData, "textField_mj8dt3gn");//质量要求
  514. String jsfs = UtilMap.getString(formData, "textField_mj8dt3go");//结算方式及付款期限
  515. String bz = UtilMap.getString(formData, "textareaField_mj9msq4w");//备注
  516. List<Map> cgmx = UtilMap.getList(formData, "tableField_mizdd3qf");//采购明细
  517. double zjyf = UtilMap.getDouble(formData, "numberField_mj9msq4x");//总计应付
  518. String jflxr = UtilMap.getString(formData, "textField_mjb2kuhf");//甲方联系人
  519. String jflxdh = UtilMap.getString(formData, "textField_mjb2kuhh");//甲方联系电话
  520. //订单编号
  521. Map component = new HashMap();
  522. component.put("componentKey", "ddbh");
  523. component.put("componentValue", cgddbh);
  524. //签订日期
  525. Map component2 = new HashMap();
  526. component2.put("componentKey", "qdrq");
  527. component2.put("componentValue", date);
  528. //乙方
  529. Map component3 = new HashMap();
  530. component3.put("componentKey", "yf");
  531. component3.put("componentValue", yf);
  532. //乙方联系人
  533. Map component4 = new HashMap();
  534. component4.put("componentKey", "yflxr");
  535. component4.put("componentValue", yflxr);
  536. //乙方联系电话
  537. Map component5 = new HashMap();
  538. component5.put("componentKey", "yflxdh");
  539. component5.put("componentValue", yfdh);
  540. //备注
  541. Map component6 = new HashMap();
  542. component6.put("componentKey", "bz");
  543. component6.put("componentValue", bz);
  544. //销售合同明细
  545. List<Map> cghtmx = new ArrayList<>();
  546. cghtmx.add(getRowMap(false));
  547. for (int i = 0; i < cgmx.size(); i++) {
  548. String wlbm = UtilMap.getString(cgmx.get(i), "textField_mizdd3qg");//物料编码
  549. String wlmc = UtilMap.getString(cgmx.get(i), "textField_mizdd3qh");//物料名称
  550. String ggxh = UtilMap.getString(cgmx.get(i), "textField_mizdd3qi");//规格型号
  551. String cgdw = UtilMap.getString(cgmx.get(i), "textField_mizdd3qj");//采购单位
  552. double cgsl = UtilMap.getDouble(cgmx.get(i), "numberField_mizdd3ql");//采购数量
  553. double hsdj = UtilMap.getDouble(cgmx.get(i), "numberField_mizdd3qs");//含税单价
  554. double jshj = UtilMap.getDouble(cgmx.get(i), "numberField_mizdd3qx");//价税合计
  555. long jhrq = UtilMap.getLong(cgmx.get(i), "dateField_mizdd3qp");//交货日期
  556. //时间戳转化为年月日
  557. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  558. String jhrqStr = sdf.format(new Date(jhrq));
  559. String mxbz = UtilMap.getString(cgmx.get(i), "textField_mizdd3r0");//备注
  560. if (i == 0){
  561. cghtmx.add(getRowMap(false,i+1,wlbm,wlmc,null,ggxh,cgsl,cgdw,hsdj,jshj,jhrqStr,mxbz));
  562. }else {
  563. cghtmx.add(getRowMap(true,i+1,wlbm,wlmc,null,ggxh,cgsl,cgdw,hsdj,jshj,jhrqStr,mxbz));
  564. }
  565. }
  566. Map component7 = new HashMap();
  567. component7.put("componentKey", "cgmx");
  568. component7.put("componentValue", JSONObject.toJSONString(cghtmx));
  569. //总计应付
  570. Map component8 = new HashMap();
  571. component8.put("componentKey", "zjyf");
  572. component8.put("componentValue", zjyf);
  573. //质量要求
  574. Map component9 = new HashMap();
  575. component9.put("componentKey", "zlyq");
  576. component9.put("componentValue", zlyq);
  577. //结算方式及付款期限
  578. Map component10 = new HashMap();
  579. component10.put("componentKey", "jsfs");
  580. component10.put("componentValue", jsfs);
  581. //乙方
  582. Map component11 = new HashMap();
  583. component11.put("componentKey", "yf2");
  584. component11.put("componentValue", yf);
  585. //乙方开户银行
  586. Map component12 = new HashMap();
  587. component12.put("componentKey", "yfkhyh");
  588. component12.put("componentValue", khyh);
  589. //乙方账号
  590. Map component13 = new HashMap();
  591. component13.put("componentKey", "yfzh");
  592. component13.put("componentValue", zh);
  593. //乙方纳税人识别号
  594. Map component14 = new HashMap();
  595. component14.put("componentKey", "yfnsrsbh");
  596. component14.put("componentValue", nsrsbh);
  597. //乙方地址
  598. Map component15 = new HashMap();
  599. component15.put("componentKey", "yfdz");
  600. component15.put("componentValue", yfdz);
  601. //甲方联系人
  602. Map component16 = new HashMap();
  603. component16.put("componentKey", "jflxr");
  604. component16.put("componentValue", jflxr);
  605. //甲方联系电话
  606. Map component17 = new HashMap();
  607. component17.put("componentKey", "jflxdh");
  608. component17.put("componentValue", jflxdh);
  609. components.add(component);
  610. components.add(component2);
  611. components.add(component3);
  612. components.add(component4);
  613. components.add(component5);
  614. components.add(component6);
  615. components.add(component7);
  616. components.add(component8);
  617. components.add(component9);
  618. components.add(component10);
  619. components.add(component11);
  620. components.add(component12);
  621. components.add(component13);
  622. components.add(component14);
  623. components.add(component15);
  624. components.add(component16);
  625. components.add(component17);
  626. // 构建请求Body体
  627. reqBodyObj.put("docTemplateId", cgdd_templateId);
  628. reqBodyObj.put("fileName", fileName+".pdf");
  629. reqBodyObj.put("components", components);
  630. }
  631. /***
  632. * e签宝post请求
  633. */
  634. public static Map eqbPost(String postUrl,JSONObject reqBodyObj) {
  635. // 完整的请求地址
  636. String postAllUrl = host + postUrl;
  637. try {
  638. // 请求Body体数据
  639. String reqBodyData = reqBodyObj.toString();
  640. // 对请求Body体内的数据计算ContentMD5
  641. String contentMD5 = doContentMD5(reqBodyData);
  642. System.out.println("请求body数据:"+reqBodyData);
  643. System.out.println("body的md5值:"+ contentMD5);
  644. // 构建待签名字符串
  645. String method = "POST";
  646. String accept = "*/*";
  647. String contentType = "application/json";
  648. String url = postUrl;
  649. String date = "";
  650. String headers = "";
  651. StringBuffer sb = new StringBuffer();
  652. sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
  653. .append(contentType).append("\n").append(date).append("\n");
  654. if ("".equals(headers)) {
  655. sb.append(headers).append(url);
  656. } else {
  657. sb.append(headers).append("\n").append(url);
  658. }
  659. // 构建参与请求签名计算的明文
  660. String plaintext = sb.toString();
  661. // 计算请求签名值
  662. String reqSignature = doSignatureBase64(plaintext, appKey);
  663. System.out.println("计算请求签名值:"+reqSignature);
  664. // 获取时间戳(精确到毫秒)
  665. long timeStamp = timeStamp();
  666. // 构建请求头
  667. LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
  668. header.put("X-Tsign-Open-App-Id", appId);
  669. header.put("X-Tsign-Open-Auth-Mode", "Signature");
  670. header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
  671. header.put("Accept", accept);
  672. header.put("Content-Type", contentType);
  673. header.put("X-Tsign-Open-Ca-Signature", reqSignature);
  674. header.put("Content-MD5", contentMD5);
  675. // 发送POST请求
  676. String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
  677. System.out.println("请求返回信息: " + result);
  678. Map resultObj =(Map) JSONObject.parse(result);
  679. Map data = UtilMap.getMap(resultObj, "data");
  680. return data;
  681. } catch (Exception e) {
  682. e.printStackTrace();
  683. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  684. System.out.println(msg);
  685. throw new RuntimeException(e);
  686. }
  687. }
  688. private Map downloadSignedFile(String signFlowId){
  689. // 计算签名拼接的url
  690. String postUrl = "/v3/sign-flow/" + signFlowId + "/file-download-url";
  691. Map result = eqbPost(postUrl, new JSONObject());
  692. List<Map> files =(List<Map>) UtilMap.getList(result, "files");
  693. String downloadUrl = UtilMap.getString(files.get(0), "downloadUrl");
  694. String fileName = UtilMap.getString(files.get(0), "fileName");
  695. UUID fileId = UUID.randomUUID();
  696. String downloadPath = downloadFilePath + fileId + ".pdf";
  697. downloadFile(downloadUrl,downloadPath);
  698. Map result2 = new HashMap();
  699. result2.put("downloadPath",downloadPath);
  700. result2.put("fileName",fileName);
  701. result2.put("fileId",fileId);
  702. return result2;
  703. }
  704. //文件下载到本地
  705. private void downloadFile(String downloadUri,String downloadPath){
  706. try {
  707. URL url = new URL(downloadUri);
  708. HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
  709. int responseCode = httpConn.getResponseCode();
  710. // 检查HTTP响应代码是否为200
  711. if (responseCode == HttpURLConnection.HTTP_OK) {
  712. InputStream inputStream = httpConn.getInputStream();
  713. FileOutputStream outputStream = new FileOutputStream(downloadPath);
  714. byte[] buffer = new byte[4096];
  715. int bytesRead = -1;
  716. while ((bytesRead = inputStream.read(buffer)) != -1) {
  717. outputStream.write(buffer, 0, bytesRead);
  718. }
  719. outputStream.close();
  720. inputStream.close();
  721. } else {
  722. System.out.println("无法下载文件。HTTP响应代码: " + responseCode);
  723. }
  724. httpConn.disconnect();
  725. }catch (Exception e){
  726. throw new RuntimeException(e);
  727. }
  728. }
  729. /***
  730. *
  731. * @param str 待计算的消息
  732. * @return MD5计算后摘要值的Base64编码(ContentMD5)
  733. * @throws Exception 加密过程中的异常信息
  734. */
  735. public static String doContentMD5(String str) throws Exception {
  736. byte[] md5Bytes = null;
  737. MessageDigest md5 = null;
  738. String contentMD5 = null;
  739. try {
  740. md5 = MessageDigest.getInstance("MD5");
  741. // 计算md5函数
  742. md5.update(str.getBytes("UTF-8"));
  743. // 获取文件MD5的二进制数组(128位)
  744. md5Bytes = md5.digest();
  745. // 把MD5摘要后的二进制数组md5Bytes使用Base64进行编码(而不是对32位的16进制字符串进行编码)
  746. contentMD5 = new String(Base64.encodeBase64(md5Bytes), "UTF-8");
  747. } catch (NoSuchAlgorithmException e) {
  748. String msg = MessageFormat.format("不支持此算法: {0}", e.getMessage());
  749. Exception ex = new Exception(msg);
  750. ex.initCause(e);
  751. throw ex;
  752. } catch (UnsupportedEncodingException e) {
  753. String msg = MessageFormat.format("不支持的字符编码: {0}", e.getMessage());
  754. Exception ex = new Exception(msg);
  755. ex.initCause(e);
  756. throw ex;
  757. }
  758. return contentMD5;
  759. }
  760. /***
  761. * 计算请求签名值
  762. *
  763. * @param message 待计算的消息
  764. * @param secret 密钥
  765. * @return HmacSHA256计算后摘要值的Base64编码
  766. * @throws Exception 加密过程中的异常信息
  767. */
  768. public static String doSignatureBase64(String message, String secret) throws Exception {
  769. String algorithm = "HmacSHA256";
  770. Mac hmacSha256;
  771. String digestBase64 = null;
  772. try {
  773. hmacSha256 = Mac.getInstance(algorithm);
  774. byte[] keyBytes = secret.getBytes("UTF-8");
  775. byte[] messageBytes = message.getBytes("UTF-8");
  776. hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, algorithm));
  777. // 使用HmacSHA256对二进制数据消息Bytes计算摘要
  778. byte[] digestBytes = hmacSha256.doFinal(messageBytes);
  779. // 把摘要后的结果digestBytes使用Base64进行编码
  780. digestBase64 = new String(Base64.encodeBase64(digestBytes), "UTF-8");
  781. } catch (NoSuchAlgorithmException e) {
  782. String msg = MessageFormat.format("不支持此算法: {0}", e.getMessage());
  783. Exception ex = new Exception(msg);
  784. ex.initCause(e);
  785. throw ex;
  786. } catch (UnsupportedEncodingException e) {
  787. String msg = MessageFormat.format("不支持的字符编码: {0}", e.getMessage());
  788. Exception ex = new Exception(msg);
  789. ex.initCause(e);
  790. throw ex;
  791. } catch (InvalidKeyException e) {
  792. String msg = MessageFormat.format("无效的密钥规范: {0}", e.getMessage());
  793. Exception ex = new Exception(msg);
  794. ex.initCause(e);
  795. throw ex;
  796. }
  797. return digestBase64;
  798. }
  799. /***
  800. * 获取时间戳(毫秒级)
  801. *
  802. * @return 毫秒级时间戳,如 1578446909000
  803. */
  804. public static long timeStamp() {
  805. long timeStamp = System.currentTimeMillis();
  806. return timeStamp;
  807. }
  808. private static Map getRowMap(boolean insertRow,Object... columnValues) {
  809. Map row = new HashMap();
  810. for (int i = 0; i < columnValues.length; i++) {
  811. row.put("column" + (i + 1), columnValues[i]);
  812. }
  813. Map result = new HashMap();
  814. result.put("row",row);
  815. result.put("insertRow",insertRow);
  816. return result;
  817. }
  818. }