Create_by_fileOSFService.java 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. package com.kingdee.eas.custom.esign.osf;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.google.common.collect.Lists;
  6. import com.google.common.collect.Maps;
  7. import com.google.common.collect.Sets;
  8. import com.kingdee.bos.BOSException;
  9. import com.kingdee.bos.Context;
  10. import com.kingdee.bos.bsf.service.app.IHRMsfService;
  11. import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
  12. import com.kingdee.bos.metadata.entity.SelectorItemCollection;
  13. import com.kingdee.bos.metadata.entity.SelectorItemInfo;
  14. import com.kingdee.eas.common.EASBizException;
  15. import com.kingdee.eas.custom.esign.*;
  16. import com.kingdee.eas.custom.esign.bizEnum.ComponentTypeEnum;
  17. import com.kingdee.eas.custom.esign.bizEnum.EsignConfigEnum;
  18. import com.kingdee.eas.custom.esign.bizEnum.EsignStatusEnum;
  19. import com.kingdee.eas.custom.esign.tsign.hz.comm.EsignHttpResponse;
  20. import com.kingdee.eas.custom.esign.tsign.hz.exception.EsignException;
  21. import com.kingdee.eas.custom.esign.util.EsignConfig;
  22. import com.kingdee.eas.custom.esign.util.EsignHttpUtil;
  23. import com.kingdee.eas.util.app.DbUtil;
  24. import com.kingdee.jdbc.rowset.IRowSet;
  25. import org.apache.commons.lang3.StringUtils;
  26. import java.net.URISyntaxException;
  27. import java.sql.SQLException;
  28. import java.util.*;
  29. import java.util.concurrent.TimeUnit;
  30. /**
  31. * description: Create_by_fileOSFService <br>
  32. * date: 28/11/2025 上午 9:46 <br>
  33. * author: lhbj <br>
  34. * version: 1.0 <br>
  35. */
  36. public class Create_by_fileOSFService implements IHRMsfService {
  37. private static final String SUCCESS_CODE = "0";
  38. private static final String PREVIEW_OPERATION = "预览";
  39. private static final int MAX_RETRY_COUNT = 20;
  40. private static final int RETRY_INTERVAL_SECONDS = 3;
  41. @Override
  42. public Object process(Context context, Map map) throws EASBizException, BOSException {
  43. String mack = (String) map.get("mack");
  44. String data = (String) map.get("data");
  45. Map<String, Object> resul = Maps.newHashMap();
  46. try {
  47. JSONObject jsonObject = JSON.parseObject(data);
  48. if ("preview".equals(mack)) {
  49. for (Map.Entry<String, Object> fileEntry : jsonObject.entrySet()) {
  50. JSONObject fieldObject = (JSONObject) fileEntry.getValue();
  51. EsignHttpResponse response = this.previewFile(context, fieldObject, "预览");
  52. if (response.getStatus() >= 200 && response.getStatus() < 300) {
  53. resul.putAll(this.processFileResponse(response, context));
  54. } else {
  55. resul.put("code", response.getStatus());
  56. resul.put("message", "网络异常");
  57. resul.put("data", null);
  58. }
  59. }
  60. } else {
  61. Map<String, Object> signMap = Maps.newHashMap();
  62. String sourceId = jsonObject.getString("sourceId");
  63. String signFlowTitle = jsonObject.getString("signFlowTitle");
  64. String personId = jsonObject.getString("personId");
  65. String operatorId = jsonObject.getString("operatorId");
  66. //文件
  67. JSONObject templateInfo = jsonObject.getJSONObject("templateInfo");
  68. //签署方信息
  69. JSONObject signInfo = jsonObject.getJSONObject("signInfo");
  70. JSONArray signs = signInfo.getJSONArray("signers");
  71. Boolean isRadio = false;
  72. for (int i = 0; i < signs.size(); i++) {
  73. Map<String, Object> signMapInfo = Maps.newHashMap();
  74. JSONObject sign = signs.getJSONObject(i);
  75. Map<String, Object> orgPsnSignMap = Maps.newHashMap();
  76. JSONObject orgSignerInfo = sign.getJSONObject("orgSignerInfo");
  77. JSONObject psnSignerInfo = sign.getJSONObject("psnSignerInfo");
  78. //企业/机构签署方信息
  79. if (null != orgSignerInfo) {
  80. //isRadio = orgSignerInfo.getBoolean("isRadio");
  81. }
  82. }
  83. if (isRadio) {
  84. List<Map<String, Object>> docs = this.addDocs(context, jsonObject, templateInfo);
  85. List<Map<String, Object>> signDocs = Lists.newArrayList();
  86. for (Map<String, Object> doc : docs) {
  87. Map<String, Object> docNew = new HashMap<>(doc);
  88. docNew.remove("signListGroup");
  89. signDocs.add(docNew);
  90. }
  91. signMap.put("docs", signDocs);
  92. if (null != docs && docs.size() > 0) {
  93. //签署流程配置项
  94. Map<String, Object> signFlowConfig = this.addSignFlowConfig(context, signFlowTitle, jsonObject);
  95. signMap.put("signFlowConfig", signFlowConfig);
  96. //签署方信息
  97. List<Map<String, Object>> signers = this.addSigners(context, signs, docs, sourceId);
  98. signMap.put("signers", signers);
  99. //抄送方信息
  100. JSONArray copis = signInfo.getJSONArray("copiers");
  101. List<Map<String, Object>> copiers = this.addCopiers(context, copis);
  102. if (copiers.size() > 0) {
  103. signMap.put("copiers", copiers);
  104. }
  105. System.out.println("--------------------------------------------------------------------------------------");
  106. System.out.println("signMap:" + JSON.toJSONString(signMap));
  107. System.out.println("--------------------------------------------------------------------------------------");
  108. EsignHttpResponse response = EsignHttpUtil.create_by_file(context, personId, signFlowTitle, operatorId, sourceId, JSON.toJSONString(signMap));
  109. if (response.getStatus() >= 200 && response.getStatus() < 300) {
  110. JSONObject body = JSON.parseObject(response.getBody());
  111. resul.putAll(body);
  112. } else {
  113. resul.put("code", 403);
  114. resul.put("message", "参数异常,没有模版id");
  115. resul.put("data", null);
  116. }
  117. }
  118. } else {
  119. //处理文件字段
  120. Map<String, List<Map<String, Object>>> pDocs = this.addPreviewDocs(context, jsonObject, templateInfo);
  121. List<Map<String, Object>> components = pDocs.get("components");
  122. //设置待签署文件信息
  123. List<Map<String, Object>> docs = pDocs.get("docs");
  124. List<Map<String, Object>> signDocs = Lists.newArrayList();
  125. for (Map<String, Object> doc : docs) {
  126. Map<String, Object> docNew = new HashMap<>(doc);
  127. docNew.remove("signListGroup");
  128. signDocs.add(docNew);
  129. }
  130. signMap.put("docs", signDocs);
  131. String eSignTemplateNum = jsonObject.getString("eSignTemplateNum");
  132. if (null != docs) {
  133. //签署流程配置项
  134. Map<String, Object> signFlowConfig = this.addSignFlowConfig(context, signFlowTitle, jsonObject);
  135. signMap.put("signFlowConfig", signFlowConfig);
  136. // //签署方信息
  137. // JSONObject signInfo = jsonObject.getJSONObject("signInfo");
  138. // JSONArray signs = signInfo.getJSONArray("signers");
  139. // List<Map<String, Object>> signers = this.addSigners(context, signs, docs, sourceId);
  140. // signMap.put("signers", signers);
  141. // //抄送方信息
  142. // JSONArray copis = signInfo.getJSONArray("copiers");
  143. // List<Map<String, Object>> copiers = this.addCopiers(context, copis);
  144. // if (copiers.size() > 0) {
  145. // signMap.put("copiers", copiers);
  146. // }
  147. //
  148. //必须要存在签署文件才能签署
  149. if (docs.size() > 0) {
  150. if (StringUtils.isNotBlank(eSignTemplateNum)) {
  151. String sealId = "";
  152. Map<String, List<Map<String, Object>>> pMap = this.addParticipants(context, signInfo, eSignTemplateNum, sealId, sourceId);
  153. signMap.put("signTemplateId", eSignTemplateNum);
  154. signMap.put("components", components);
  155. signMap.putAll(pMap);
  156. System.out.println("--------------------------------------------------------------------------------------");
  157. System.out.println("signMap:" + JSON.toJSONString(signMap));
  158. System.out.println("--------------------------------------------------------------------------------------");
  159. EsignHttpResponse response = EsignHttpUtil.createBySignTemplate(context, personId, signFlowTitle, operatorId, sourceId, JSON.toJSONString(signMap));
  160. if (response.getStatus() >= 200 && response.getStatus() < 300) {
  161. JSONObject body = JSON.parseObject(response.getBody());
  162. resul.putAll(body);
  163. } else {
  164. resul.put("code", response.getStatus());
  165. resul.put("message", "网络异常");
  166. resul.put("data", null);
  167. }
  168. } else {
  169. // System.out.println("--------------------------------------------------------------------------------------");
  170. // System.out.println("signMap:" + JSON.toJSONString(signMap));
  171. // System.out.println("--------------------------------------------------------------------------------------");
  172. // EsignHttpResponse response = EsignHttpUtil.create_by_file(context, personId, signFlowTitle, operatorId, sourceId, JSON.toJSONString(signMap));
  173. // if (response.getStatus() >= 200 && response.getStatus() < 300) {
  174. // JSONObject body = JSON.parseObject(response.getBody());
  175. // resul.putAll(body);
  176. // } else {
  177. resul.put("code", 403);
  178. resul.put("message", "参数异常,没有模版id");
  179. resul.put("data", null);
  180. // }
  181. }
  182. }
  183. }
  184. }
  185. }
  186. } catch (EsignException | URISyntaxException | SQLException e) {
  187. e.printStackTrace();
  188. resul.put("code", 400);
  189. resul.put("message", e.getMessage());
  190. resul.put("data", null);
  191. }
  192. return resul;
  193. }
  194. /**
  195. * 根据”填写模板生成文件“返回的文件id查询文件处理状态
  196. * 需要阻塞等待这个状态变为2或者5才能调用发起签署接口
  197. *
  198. * @param response
  199. * @param context
  200. * @return
  201. */
  202. public Map<String, Object> processFileResponse(EsignHttpResponse response, Context context) {
  203. Map<String, Object> result = Maps.newHashMap();
  204. JSONObject body = JSON.parseObject(response.getBody());
  205. if (!SUCCESS_CODE.equals(String.valueOf(body.get("code")))) {
  206. result.putAll(body);
  207. return result;
  208. }
  209. JSONObject jsonData = body.getJSONObject("data");
  210. String fileId = jsonData.getString("fileId");
  211. JSONObject pollData = pollFileDownloadUrl(context, fileId);
  212. if (null != pollData && StringUtils.isNotBlank(pollData.getString("fileDownloadUrl"))) {
  213. jsonData.putAll(pollData);
  214. }
  215. result.putAll(body);
  216. System.out.println("processFileResponse:" + result);
  217. return result;
  218. }
  219. /**
  220. * 阻塞循环调用获取文件信息接口
  221. *
  222. * @param context
  223. * @param fileId
  224. * @return
  225. */
  226. private JSONObject pollFileDownloadUrl(Context context, String fileId) {
  227. for (int i = MAX_RETRY_COUNT; i > 0; i--) {
  228. try {
  229. JSONObject data = tryGetFileDownloadUrl(context, fileId);
  230. if (null != data && StringUtils.isNotBlank(data.getString("fileDownloadUrl"))) {
  231. return data;
  232. }
  233. TimeUnit.SECONDS.sleep(RETRY_INTERVAL_SECONDS);
  234. } catch (InterruptedException e) {
  235. Thread.currentThread().interrupt();
  236. break;
  237. } catch (Exception e) {
  238. // 记录日志,继续重试
  239. e.printStackTrace();
  240. }
  241. }
  242. return null;
  243. }
  244. /**
  245. * 等待这个状态变为2或者5才返回
  246. *
  247. * @param context
  248. * @param fileId
  249. * @return
  250. */
  251. private JSONObject tryGetFileDownloadUrl(Context context, String fileId) {
  252. try {
  253. EsignHttpResponse response = EsignHttpUtil.getFileStatus(context, fileId, PREVIEW_OPERATION);
  254. if (response.getStatus() < 200 || response.getStatus() >= 300) {
  255. return null;
  256. }
  257. JSONObject responseBody = JSON.parseObject(response.getBody());
  258. if (!SUCCESS_CODE.equals(String.valueOf(responseBody.get("code")))) {
  259. return null;
  260. }
  261. JSONObject data = responseBody.getJSONObject("data");
  262. String fileStatus = data.getString("fileStatus");
  263. Set<String> COMPLETED_STATUS = Sets.newHashSet();
  264. COMPLETED_STATUS.add("2");
  265. COMPLETED_STATUS.add("5");
  266. if (COMPLETED_STATUS.contains(fileStatus)) {
  267. return data;
  268. }
  269. } catch (EsignException e) {
  270. e.printStackTrace();
  271. }
  272. return null;
  273. }
  274. /**
  275. * 设置待签署文件位置信息
  276. *
  277. * @param context
  278. * @param fileId
  279. * @param keyword
  280. * @param sourceId
  281. * @return
  282. * @throws EsignException
  283. */
  284. public JSONObject addPositions(Context context, String fileId, String keyword, String sourceId) throws EsignException {
  285. List<String> keywords = Lists.newArrayList();
  286. keywords.add(keyword);
  287. JSONObject coordinate = null;
  288. System.out.println("respPosition:fileId:" + fileId);
  289. System.out.println("respPosition:keywords:" + keywords);
  290. EsignHttpResponse respPosition = EsignHttpUtil.keyword_positions(context, fileId, keywords, sourceId);
  291. System.out.println("respPosition:" + respPosition.getBody());
  292. if (respPosition.getStatus() >= 200 && respPosition.getStatus() < 300) {
  293. JSONObject resp = JSON.parseObject(respPosition.getBody());
  294. if ("0".equals(String.valueOf(resp.get("code")))) {
  295. JSONObject keywordData = (JSONObject) resp.get("data");
  296. if (null != keywordData) {
  297. JSONArray keywordPositions = keywordData.getJSONArray("keywordPositions");
  298. if (null != keywordPositions && keywordPositions.size() > 0) {
  299. JSONObject keywordPs = keywordPositions.getJSONObject(keywordPositions.size() - 1);
  300. if (keywordPs.getBoolean("searchResult")) {
  301. if (null != keywordPs) {
  302. JSONArray positions = keywordPs.getJSONArray("positions");
  303. if (null != positions && positions.size() > 0) {
  304. JSONObject position = positions.getJSONObject(positions.size() - 1);
  305. if (null != position) {
  306. Object pageNum = position.get("pageNum");
  307. JSONArray coordinates = position.getJSONArray("coordinates");
  308. if (null != coordinates && coordinates.size() > 0) {
  309. coordinate = coordinates.getJSONObject(coordinates.size() - 1);
  310. if (null != coordinate) {
  311. coordinate.put("positionPage", pageNum);
  312. }
  313. }
  314. }
  315. }
  316. }
  317. }
  318. }
  319. }
  320. }
  321. System.out.println("coordinate:" + coordinate);
  322. }
  323. return coordinate;
  324. }
  325. /**
  326. * 获取签署人的签署区
  327. *
  328. * @param context
  329. * @param fields
  330. * @param fileId
  331. * @param sourceId
  332. * @return
  333. * @throws EsignException
  334. */
  335. public Map<String, Map<String, Map<String, Object>>> addsignAreaList(Context context, Map<String, Object> fields, String fileId, String sourceId, String docTemplateId) throws EsignException, BOSException, EASBizException, SQLException {
  336. //Map<String, List<Map<String, Object>>> signListGroup = Maps.newHashMap();
  337. Map<String, Map<String, Map<String, Object>>> signMapGroup = Maps.newHashMap();
  338. //获取文件模版字段
  339. IESignTemplateFileEntry ieSignTemplateFileEntry = ESignTemplateFileEntryFactory.getLocalInstance(context);
  340. SelectorItemCollection selColl = new SelectorItemCollection();
  341. selColl.add(new SelectorItemInfo("*"));
  342. selColl.add(new SelectorItemInfo("fields.*"));
  343. selColl.add(new SelectorItemInfo("fields.relatedControl.*"));
  344. String sel = "select fid from CT_ESI_ESignTemplateFileEntry where cfeSignTemplateId = ?";
  345. IRowSet rs = DbUtil.executeQuery(context, sel.toString(), new Object[]{docTemplateId});
  346. String Entryid = "";
  347. if (rs.next()) {
  348. Entryid = rs.getString("fid");
  349. }
  350. ESignTemplateFileEntryInfo eSignTemplateFileEntry = ieSignTemplateFileEntry.getESignTemplateFileEntryInfo(new ObjectUuidPK(Entryid), selColl);
  351. Map<String, ESignTemplateFileEntryFieldInfo> fieldsMap = Maps.newHashMap();
  352. ESignTemplateFileEntryFieldCollection entryFieldCollection = eSignTemplateFileEntry.getFields();
  353. for (int i = 0; i < entryFieldCollection.size(); i++) {
  354. ESignTemplateFileEntryFieldInfo fieldInfo = entryFieldCollection.get(i);
  355. //失效的控件跳过
  356. if (fieldInfo.isInvalid()) {
  357. continue;
  358. }
  359. String fieldId = fieldInfo.getTemplateFieldId().toString();
  360. JSONObject field = (JSONObject) fields.get(fieldId);
  361. if (ComponentTypeEnum.SIGN_AREA.equals(fieldInfo.getComponentType())) {
  362. Map<String, Object> signMap = Maps.newHashMap();
  363. if (StringUtils.isBlank(fieldInfo.getSigningParty())) {
  364. Map<String, Object> errMap = Maps.newHashMap();
  365. errMap.put("模版", eSignTemplateFileEntry.getName());
  366. errMap.put("控件", fieldInfo.getTemplateFieldName());
  367. errMap.put("内容", "签署方为空请维护!");
  368. throw new EsignException(JSON.toJSONString(errMap));
  369. }
  370. signMap.put("key", fieldInfo.getSigningParty());
  371. signMap.put("fileId", fileId);
  372. signMap.put("fieldId", fieldInfo.getTemplateFieldId());
  373. signMap.put("signFieldType", 0);
  374. String keyword = fieldInfo.getTemplateFieldName();
  375. if (keyword.indexOf("骑缝") >= 0) {
  376. signMap.put("signFieldStyle", 2);
  377. signMap.put("positionX", fieldInfo.getPositionX());
  378. signMap.put("positionY", fieldInfo.getPositionY());
  379. signMap.put("positionPage", fieldInfo.getPageNum());
  380. } else {
  381. signMap.put("signFieldStyle", 1);
  382. if (null == fieldInfo.getPositionX() && null == fieldInfo.getPositionY()) {
  383. Map<String, Object> errMap = Maps.newHashMap();
  384. errMap.put("模版", eSignTemplateFileEntry.getName());
  385. errMap.put("控件", fieldInfo.getTemplateFieldName());
  386. errMap.put("内容", "印章坐标为空请维护!");
  387. throw new EsignException(JSON.toJSONString(errMap));
  388. }
  389. signMap.put("positionX", fieldInfo.getPositionX());
  390. signMap.put("positionY", fieldInfo.getPositionY());
  391. signMap.put("positionPage", fieldInfo.getPageNum());
  392. }
  393. Map<String, Map<String, Object>> skMap = signMapGroup.get((String) signMap.get("key"));
  394. if (null == skMap) {
  395. skMap = Maps.newHashMap();
  396. signMapGroup.put((String) signMap.get("key"), skMap);
  397. }
  398. skMap.put(fieldInfo.getTemplateFieldId(), signMap);
  399. }
  400. //签署日期
  401. if (ComponentTypeEnum.DATESIGNATURE.equals(fieldInfo.getComponentType())) {
  402. fieldsMap.put(fieldId, fieldInfo);
  403. }
  404. }
  405. //签署日期
  406. for (Map.Entry<String, ESignTemplateFileEntryFieldInfo> fieldLinkEntry : fieldsMap.entrySet()) {
  407. //签署日期
  408. ESignTemplateFileEntryFieldInfo fieldLink = fieldLinkEntry.getValue();
  409. //签署方
  410. ESignTemplateFileEntryFieldInfo fieldInfo = fieldLink.getRelatedControl();
  411. if (null == fieldInfo) {
  412. Map<String, Object> errMap = Maps.newHashMap();
  413. errMap.put("模版", eSignTemplateFileEntry.getName());
  414. errMap.put("控件", fieldLink.getTemplateFieldName());
  415. errMap.put("内容", "签署方关联关系为空请维护!");
  416. throw new EsignException(JSON.toJSONString(errMap));
  417. }
  418. if (!ComponentTypeEnum.SIGN_AREA.equals(fieldInfo.getComponentType())) {
  419. Map<String, Object> errMap = Maps.newHashMap();
  420. errMap.put("模版", eSignTemplateFileEntry.getName());
  421. errMap.put("控件", fieldLink.getTemplateFieldName());
  422. errMap.put("内容", "关联关系不为签署区,请检查!");
  423. throw new EsignException(JSON.toJSONString(errMap));
  424. }
  425. Map<String, Object> signMap = Maps.newHashMap();
  426. //签署日期坐标
  427. Map<String, Object> signDateConfig = Maps.newHashMap();
  428. signDateConfig.put("showSignDate", 1);
  429. if (null != fieldLink.getPositionX() && null != fieldLink.getPositionY()) {
  430. signDateConfig.put("signDatePositionX", fieldLink.getPositionX());
  431. signDateConfig.put("signDatePositionY", fieldLink.getPositionY());
  432. }
  433. if (null != fieldLink.get("fontSize")) {
  434. signDateConfig.put("fontSize", fieldLink.get("fontSize"));
  435. }
  436. if (null != fieldLink.get("dateFormat")) {
  437. signDateConfig.put("dateFormat", fieldLink.get("dateFormat"));
  438. }
  439. signMap.put("signDateConfig", signDateConfig);
  440. signMap.put("key", fieldInfo.getSigningParty());
  441. signMap.put("fileId", fileId);
  442. signMap.put("signFieldType", 0);
  443. String keyword = fieldInfo.getTemplateFieldName();
  444. if (keyword.indexOf("骑缝") >= 0) {
  445. signMap.put("signFieldStyle", 2);
  446. signMap.put("positionX", fieldInfo.getPositionX());
  447. signMap.put("positionY", fieldInfo.getPositionY());
  448. signMap.put("positionPage", fieldInfo.getPageNum());
  449. } else {
  450. signMap.put("signFieldStyle", 1);
  451. if (null == fieldInfo.getPositionX() && null == fieldInfo.getPositionY()) {
  452. Map<String, Object> errMap = Maps.newHashMap();
  453. errMap.put("模版", eSignTemplateFileEntry.getName());
  454. errMap.put("控件", fieldInfo.getTemplateFieldName());
  455. errMap.put("内容", "印章坐标为空请维护!");
  456. throw new EsignException(JSON.toJSONString(errMap));
  457. }
  458. signMap.put("positionX", fieldInfo.getPositionX());
  459. signMap.put("positionY", fieldInfo.getPositionY());
  460. signMap.put("positionPage", fieldInfo.getPageNum());
  461. }
  462. Map<String, Map<String, Object>> skMap = signMapGroup.get((String) signMap.get("key"));
  463. if (null == skMap) {
  464. skMap = Maps.newHashMap();
  465. signMapGroup.put((String) signMap.get("key"), skMap);
  466. }
  467. skMap.remove(fieldInfo.getTemplateFieldId());
  468. skMap.put(fieldLink.getTemplateFieldId(), signMap);
  469. }
  470. System.out.println("signMapGroup:" + signMapGroup);
  471. return signMapGroup;
  472. }
  473. /**
  474. * 设置待签署文件信息
  475. *
  476. * @param context
  477. * @param jsonObject
  478. * @param templateInfo
  479. * @return
  480. * @throws EsignException
  481. */
  482. public List<Map<String, Object>> addDocs(Context context, JSONObject jsonObject, JSONObject templateInfo) throws EsignException, EASBizException, BOSException, SQLException {
  483. //设置待签署文件信息
  484. Map<String, Object> resul = Maps.newHashMap();
  485. List<Map<String, Object>> docs = Lists.newArrayList();
  486. for (Map.Entry<String, Object> fileEntry : templateInfo.entrySet()) {
  487. JSONObject fieldObject = (JSONObject) fileEntry.getValue();
  488. String docTemplateId = fieldObject.getString("id");
  489. String fileName = fieldObject.getString("name");
  490. String eSignTemplateNum = fieldObject.getString("eSignTemplateNum");
  491. if (StringUtils.isBlank(docTemplateId)) {
  492. docTemplateId = fieldObject.getString("tableId");
  493. }
  494. if (StringUtils.isBlank(fileName)) {
  495. fileName = fieldObject.getString("tableName");
  496. }
  497. if (StringUtils.isNotBlank(eSignTemplateNum)) {
  498. jsonObject.put("eSignTemplateNum", eSignTemplateNum);
  499. }
  500. Object order = fieldObject.get("order");
  501. String sourceId = jsonObject.getString("sourceId");
  502. this.initDocAndSignAreaList(context, resul, docs, fieldObject, docTemplateId, fileName, order, sourceId);
  503. }
  504. return docs;
  505. }
  506. /**
  507. * 设置待签署文件信息
  508. *
  509. * @param context
  510. * @param jsonObject
  511. * @param templateInfo
  512. * @return
  513. * @throws EsignException
  514. */
  515. public Map<String, List<Map<String, Object>>> addPreviewDocs(Context context, JSONObject jsonObject, JSONObject templateInfo) throws EsignException, EASBizException, BOSException, SQLException {
  516. //设置待签署文件信息
  517. Map<String, List<Map<String, Object>>> resul = Maps.newHashMap();
  518. List<Map<String, Object>> docs = Lists.newArrayList();
  519. List<Map<String, Object>> components = Lists.newArrayList();
  520. for (Map.Entry<String, Object> fileEntry : templateInfo.entrySet()) {
  521. JSONObject fieldObject = (JSONObject) fileEntry.getValue();
  522. String docTemplateId = fieldObject.getString("id");
  523. String fileName = fieldObject.getString("name");
  524. String eSignTemplateNum = fieldObject.getString("eSignTemplateNum");
  525. if (StringUtils.isBlank(docTemplateId)) {
  526. docTemplateId = fieldObject.getString("tableId");
  527. }
  528. if (StringUtils.isBlank(fileName)) {
  529. fileName = fieldObject.getString("tableName");
  530. }
  531. if (StringUtils.isNotBlank(eSignTemplateNum)) {
  532. jsonObject.put("eSignTemplateNum", eSignTemplateNum);
  533. }
  534. Object order = fieldObject.get("order");
  535. String sourceId = jsonObject.getString("sourceId");
  536. Map<String, Object> doc = Maps.newHashMap();
  537. doc.put("fileId", docTemplateId);
  538. doc.put("fileName", fileName);
  539. if (null != order) {
  540. doc.put("order", Integer.parseInt(String.valueOf(order)));
  541. }
  542. System.out.println("addDocs:" + fieldObject);
  543. JSONObject fields = fieldObject.getJSONObject("fields");
  544. for (Map.Entry<String, Object> field : fields.entrySet()) {
  545. Map<String, Object> component = Maps.newHashMap();
  546. component.put("componentId", field.getKey());
  547. component.put("fileId", docTemplateId);
  548. JSONObject fieldInfo = (JSONObject) field.getValue();
  549. String dataType = (String) fieldInfo.get("dataType");
  550. if (ComponentTypeEnum.SIGN_AREA.getAlias().equals(dataType)) {
  551. continue;
  552. }
  553. if (ComponentTypeEnum.DATESIGNATURE.getAlias().equals(dataType)) {
  554. continue;
  555. }
  556. if (null == fieldInfo.get("value")) {
  557. continue;
  558. }
  559. component.put("componentValue", fieldInfo.get("value"));
  560. components.add(component);
  561. }
  562. //取得这个文件的签署人的签署区
  563. Map<String, Map<String, Map<String, Object>>> signAreaList = this.addsignAreaList(context, fields, (String) docTemplateId, sourceId, docTemplateId);
  564. doc.put("signListGroup", signAreaList);
  565. docs.add(doc);
  566. }
  567. resul.put("docs", docs);
  568. resul.put("components", components);
  569. return resul;
  570. }
  571. private void initDocAndSignAreaList(Context context, Map<String, Object> resul, List<Map<String, Object>> docs, JSONObject fieldObject, String docTemplateId, String fileName, Object order, String sourceId) throws EsignException, BOSException, EASBizException, SQLException {
  572. EsignHttpResponse response = this.previewFile(context, fieldObject, sourceId);
  573. resul.put("id", docTemplateId);
  574. resul.put("name", fileName);
  575. if (response.getStatus() >= 200 && response.getStatus() < 300) {
  576. Map<String, Object> body = this.processFileResponse(response, context);
  577. if ("0".equals(String.valueOf(body.get("code")))) {
  578. JSONObject file = (JSONObject) body.get("data");
  579. Map<String, Object> doc = Maps.newHashMap();
  580. doc.put("fileId", file.get("fileId"));
  581. doc.put("fileName", fileName);
  582. if (null != order) {
  583. doc.put("order", Integer.parseInt(String.valueOf(order)));
  584. }
  585. System.out.println("addDocs:" + fieldObject);
  586. JSONObject fields = fieldObject.getJSONObject("fields");
  587. //取得这个文件的签署人的签署区
  588. Map<String, Map<String, Map<String, Object>>> signAreaList = this.addsignAreaList(context, fields, (String) file.get("fileId"), sourceId, docTemplateId);
  589. doc.put("signListGroup", signAreaList);
  590. docs.add(doc);
  591. } else {
  592. //文件模板生成失败
  593. resul.putAll(body);
  594. throw new EsignException(JSON.toJSONString(resul));
  595. }
  596. } else {
  597. throw new EsignException(JSON.toJSONString(response));
  598. }
  599. }
  600. /**
  601. * 抄送方信息
  602. *
  603. * @param copis
  604. * @return
  605. */
  606. public List<Map<String, Object>> addCopiers(Context context, JSONArray copis) {
  607. List<Map<String, Object>> copiers = Lists.newArrayList();
  608. for (int i = 0; i < copis.size(); i++) {
  609. JSONObject cop = copis.getJSONObject(i);
  610. Map<String, Object> copierPsn = Maps.newHashMap();
  611. Map<String, Object> copierPsnInfo = Maps.newHashMap();
  612. copierPsnInfo.put("psnAccount", cop.getString("psnAccount"));
  613. copierPsn.put("copierPsnInfo", copierPsnInfo);
  614. copiers.add(copierPsn);
  615. }
  616. return copiers;
  617. }
  618. /**
  619. * 添加签署组织
  620. *
  621. * @param context
  622. * @param orgSignerInfo
  623. * @param docs
  624. * @param sourceId
  625. * @return
  626. * @throws EsignException
  627. * @throws URISyntaxException
  628. */
  629. public Map<String, Object> addOrgSignerInfo(Context context, JSONObject orgSignerInfo, List<Map<String, Object>> docs, String sourceId) throws EsignException, URISyntaxException {
  630. List<Map<String, Object>> resulList = Lists.newArrayList();
  631. List<Map<String, Object>> resulErrList = Lists.newArrayList();
  632. Map<String, Object> resul = Maps.newHashMap();
  633. Map<String, Object> signMapInfo = Maps.newHashMap();
  634. Map<String, Object> orgPsnSignMap = Maps.newHashMap();
  635. String orgName = orgSignerInfo.getString("orgName");
  636. EsignHttpResponse response = EsignHttpUtil.getOrgIdentity_infoByOrgName(context, orgName);
  637. if (response.getStatus() >= 200 && response.getStatus() < 300) {
  638. JSONObject body = JSON.parseObject(response.getBody());
  639. if ("0".equals(String.valueOf(body.get("code")))) {
  640. JSONObject orgPsn = body.getJSONObject("data");
  641. String orgId = orgPsn.getString("orgId");
  642. orgName = orgPsn.getString("orgName");
  643. //自动落章只能传orgId
  644. if (true == orgSignerInfo.getBoolean("isRadio")) {
  645. orgPsnSignMap.put("orgId", orgId);
  646. } else {
  647. orgPsnSignMap.put("orgName", orgName);
  648. Map<String, Object> transactorInfo = Maps.newHashMap();
  649. transactorInfo.put("psnAccount", orgSignerInfo.getString("psnAccount"));
  650. Map<String, Object> psnInfo = Maps.newHashMap();
  651. psnInfo.put("psnName", orgSignerInfo.getString("psnName"));
  652. transactorInfo.put("psnInfo", psnInfo);
  653. orgPsnSignMap.put("transactorInfo", transactorInfo);
  654. orgPsnSignMap.put("orgInfo", orgPsn.getJSONObject("orgInfo"));
  655. }
  656. } else {
  657. //失败
  658. resul.putAll(body);
  659. throw new EsignException(JSON.toJSONString(resul));
  660. }
  661. } else {
  662. throw new EsignException("网络异常:" + JSON.toJSONString(response));
  663. }
  664. Integer signOrder = orgSignerInfo.getInteger("signOrder");
  665. //设置签署方的签署顺序
  666. if (null != signOrder) {
  667. Map<String, Object> signConfig = Maps.newHashMap();
  668. signConfig.put("signOrder", signOrder);
  669. signMapInfo.put("signConfig", signConfig);
  670. }
  671. signMapInfo.put("orgSignerInfo", orgPsnSignMap);
  672. //设置签署方的通知方式
  673. String noticeTypes = orgSignerInfo.getString("noticeTypes");
  674. Map<String, String> noticeConfig = Maps.newHashMap();
  675. if (StringUtils.isNotBlank(noticeTypes)) {
  676. noticeConfig.put("noticeTypes", noticeTypes);
  677. } else {
  678. noticeConfig.put("noticeTypes", "1,2");
  679. }
  680. signMapInfo.put("noticeConfig", noticeConfig);
  681. //签署区信息
  682. String signName = orgSignerInfo.getString("signName");
  683. if (StringUtils.isBlank(signName)) {
  684. signName = "签署方" + signOrder;
  685. }
  686. List<Map<String, Object>> signFields = Lists.newArrayList();
  687. for (Map<String, Object> doc : docs) {
  688. Map<String, Map<String, Map<String, Object>>> signListGroup = (Map<String, Map<String, Map<String, Object>>>) doc.get("signListGroup");
  689. //判断是否存在签署区
  690. if (null != signListGroup && signListGroup.size() > 0) {
  691. Map<String, Map<String, Object>> fieldList = signListGroup.get(signName);
  692. if (null != fieldList) {
  693. for (Map<String, Object> fieldMap : fieldList.values()) {
  694. Map<String, Object> field = Maps.newHashMap();
  695. field.put("fileId", fieldMap.get("fileId"));
  696. field.put("customBizNum", doc.get("fileName"));
  697. if (0 == ((int) fieldMap.get("signFieldType"))) {
  698. field.put("signFieldType", fieldMap.get("signFieldType"));
  699. //签章区配置项(指定signFieldType为 0 - 签章区时,该参数为必传项)
  700. Map<String, Object> normalSignFieldConfig = Maps.newHashMap();
  701. if (true == orgSignerInfo.getBoolean("isRadio")) {
  702. //企业/机构自动落章(autoSign为true),请不要传该参数。
  703. orgPsnSignMap.remove("transactorInfo");
  704. //自动落章
  705. normalSignFieldConfig.put("freeMode", false);
  706. normalSignFieldConfig.put("autoSign", true);
  707. //印章id
  708. normalSignFieldConfig.put("assignedSealId", orgSignerInfo.getString("sealId"));
  709. //印章类型:1印章,2骑缝章
  710. normalSignFieldConfig.put("signFieldStyle", fieldMap.get("signFieldStyle"));
  711. Map<String, Object> signFieldPosition = Maps.newHashMap();
  712. if (1 == ((int) fieldMap.get("signFieldStyle"))) {
  713. //正常章
  714. if (null != fieldMap.get("positionPage") && null != fieldMap.get("positionX") && null != fieldMap.get("positionY")) {
  715. signFieldPosition.put("positionPage", fieldMap.get("positionPage"));
  716. signFieldPosition.put("positionX", fieldMap.get("positionX"));
  717. signFieldPosition.put("positionY", fieldMap.get("positionY"));
  718. Map<String, Object> signDateConfig = (Map<String, Object>) fieldMap.get("signDateConfig");
  719. if (null == signDateConfig) {
  720. signDateConfig = Maps.newHashMap();
  721. //signDateConfig.put("showSignDate", 1);
  722. }
  723. field.put("signDateConfig", signDateConfig);
  724. } else {
  725. Map<String, Object> res = Maps.newHashMap();
  726. //失败
  727. res.put("文件模板:" + doc.get("fileName"), signName + ",未匹配到签署区,请检查模板的签署区命名规范。签署区命名规则:如(“甲方:(盖章)_签署方1_X50_Y50“)根据“_”分割字符串,第一个是要盖章的内容位置标识;第二个是签署方用来判断这个签署区是谁的;X50表示:印章位置向右移动50;Y50表示:印章位置向上移动50");
  728. resulErrList.add(res);
  729. }
  730. } else {
  731. //骑缝章
  732. signFieldPosition.put("positionY", fieldMap.get("positionY"));
  733. }
  734. normalSignFieldConfig.put("signFieldPosition", signFieldPosition);
  735. } else {
  736. //手动签章
  737. normalSignFieldConfig.put("freeMode", false);
  738. normalSignFieldConfig.put("autoSign", false);
  739. //印章id
  740. normalSignFieldConfig.put("assignedSealId", orgSignerInfo.getString("sealId"));
  741. //印章类型:1印章,2骑缝章
  742. normalSignFieldConfig.put("signFieldStyle", fieldMap.get("signFieldStyle"));
  743. Map<String, Object> signFieldPosition = Maps.newHashMap();
  744. if (1 == ((int) fieldMap.get("signFieldStyle"))) {
  745. //正常章
  746. if (null != fieldMap.get("positionPage") && null != fieldMap.get("positionX") && null != fieldMap.get("positionY")) {
  747. signFieldPosition.put("positionPage", fieldMap.get("positionPage"));
  748. signFieldPosition.put("positionX", fieldMap.get("positionX"));
  749. signFieldPosition.put("positionY", fieldMap.get("positionY"));
  750. Map<String, Object> signDateConfig = (Map<String, Object>) fieldMap.get("signDateConfig");
  751. if (null == signDateConfig) {
  752. signDateConfig = Maps.newHashMap();
  753. //signDateConfig.put("showSignDate", 1);
  754. }
  755. field.put("signDateConfig", signDateConfig);
  756. } else {
  757. Map<String, Object> res = Maps.newHashMap();
  758. //失败
  759. res.put("文件模板:" + doc.get("fileName"), signName + ",未匹配到签署区,请检查模板的签署区命名规范。签署区命名规则:如(“甲方:(盖章)_签署方1_X50_Y50“)根据“_”分割字符串,第一个是要盖章的内容位置标识;第二个是签署方用来判断这个签署区是谁的;X50表示:印章位置向右移动50;Y50表示:印章位置向上移动50");
  760. resulErrList.add(res);
  761. }
  762. } else {
  763. //骑缝章
  764. signFieldPosition.put("positionY", fieldMap.get("positionY"));
  765. }
  766. normalSignFieldConfig.put("signFieldPosition", signFieldPosition);
  767. }
  768. field.put("normalSignFieldConfig", normalSignFieldConfig);
  769. } else {
  770. field.put("signFieldType", fieldMap.get("signFieldType"));
  771. //签章区配置项(指定signFieldType为 0 - 签章区时,该参数为必传项)
  772. Map<String, Object> remarkSignFieldConfig = Maps.newHashMap();
  773. remarkSignFieldConfig.put("freeMode", true);
  774. remarkSignFieldConfig.put("inputType", 2);
  775. field.put("remarkSignFieldConfig", remarkSignFieldConfig);
  776. }
  777. signFields.add(field);
  778. }
  779. } else {
  780. Map<String, Object> res = Maps.newHashMap();
  781. //失败
  782. res.put("文件模板:" + doc.get("fileName"), signName + ",未匹配到签署区,请检查模板的签署区命名规范。签署区命名规则:如(“甲方:(盖章)_签署方1_X50_Y50“)根据“_”分割字符串,第一个是要盖章的内容位置标识;第二个是签署方用来判断这个签署区是谁的;X50表示:印章位置向右移动50;Y50表示:印章位置向上移动50");
  783. resulList.add(res);
  784. }
  785. }
  786. }
  787. if (signFields.size() <= 0) {
  788. throw new EsignException(JSON.toJSONString(resulList));
  789. }
  790. if (resulErrList.size() > 0) {
  791. throw new EsignException(JSON.toJSONString(resulErrList));
  792. }
  793. signMapInfo.put("signFields", signFields);
  794. signMapInfo.put("signerType", 1);
  795. return signMapInfo;
  796. }
  797. /**
  798. * 添加签署人
  799. *
  800. * @param context
  801. * @param psnSignerInfo
  802. * @param docs
  803. * @param sourceId
  804. * @return
  805. * @throws EsignException
  806. * @throws URISyntaxException
  807. */
  808. public Map<String, Object> addPsnSignerInfo(Context context, JSONObject psnSignerInfo, List<Map<String, Object>> docs, String sourceId) throws EsignException, URISyntaxException {
  809. List<Map<String, Object>> resulList = Lists.newArrayList();
  810. Map<String, Object> resul = Maps.newHashMap();
  811. Map<String, Object> signMapInfo = Maps.newHashMap();
  812. Map<String, Object> orgPsnSignMap = Maps.newHashMap();
  813. //个人签署方信息
  814. String psnAccount = psnSignerInfo.getString("psnAccount");
  815. String psnName = psnSignerInfo.getString("psnName");
  816. Integer signOrder = psnSignerInfo.getInteger("signOrder");
  817. //设置签署方的签署顺序
  818. if (null != signOrder) {
  819. Map<String, Object> signConfig = Maps.newHashMap();
  820. signConfig.put("signOrder", signOrder);
  821. signMapInfo.put("signConfig", signConfig);
  822. }
  823. orgPsnSignMap.put("psnAccount", psnAccount);
  824. Map<String, String> psnInfo = Maps.newHashMap();
  825. psnInfo.put("psnName", psnName);
  826. orgPsnSignMap.put("psnInfo", psnInfo);
  827. //个人签署方信息
  828. signMapInfo.put("psnSignerInfo", orgPsnSignMap);
  829. //设置签署方的通知方式
  830. String noticeTypes = psnSignerInfo.getString("noticeTypes");
  831. Map<String, String> noticeConfig = Maps.newHashMap();
  832. if (StringUtils.isNotBlank(noticeTypes)) {
  833. noticeConfig.put("noticeTypes", noticeTypes);
  834. } else {
  835. noticeConfig.put("noticeTypes", "1,2");
  836. }
  837. signMapInfo.put("noticeConfig", noticeConfig);
  838. //签署区信息
  839. List<Map<String, Object>> signFields = Lists.newArrayList();
  840. // for (Map<String, Object> doc : docs) {
  841. // Map<String, Object> field = Maps.newHashMap();
  842. // field.put("fileId", doc.get("fileId"));
  843. // field.put("customBizNum", sourceId);
  844. // field.put("signFieldType", 0);
  845. // //签章区配置项(指定signFieldType为 0 - 签章区时,该参数为必传项)
  846. // Map<String, Object> normalSignFieldConfig = Maps.newHashMap();
  847. // normalSignFieldConfig.put("freeMode", true);
  848. // normalSignFieldConfig.put("movableSignField", false);
  849. //
  850. // field.put("normalSignFieldConfig", normalSignFieldConfig);
  851. // signFields.add(field);
  852. // }
  853. //签署区信息
  854. String signName = psnSignerInfo.getString("signName");
  855. if (StringUtils.isBlank(signName)) {
  856. signName = "签署方" + signOrder;
  857. }
  858. for (Map<String, Object> doc : docs) {
  859. Map<String, Map<String, Map<String, Object>>> signListGroup = (Map<String, Map<String, Map<String, Object>>>) doc.get("signListGroup");
  860. Map<String, Map<String, Object>> fieldList = signListGroup.get(signName);
  861. if (null != fieldList) {
  862. for (Map<String, Object> fieldMap : fieldList.values()) {
  863. Map<String, Object> field = Maps.newHashMap();
  864. field.put("fileId", fieldMap.get("fileId"));
  865. field.put("customBizNum", doc.get("fileName"));
  866. if (0 == ((int) fieldMap.get("signFieldType"))) {
  867. field.put("signFieldType", fieldMap.get("signFieldType"));
  868. //签章区配置项(指定signFieldType为 0 - 签章区时,该参数为必传项)
  869. Map<String, Object> normalSignFieldConfig = Maps.newHashMap();
  870. //手动签章
  871. normalSignFieldConfig.put("freeMode", false);
  872. normalSignFieldConfig.put("autoSign", false);
  873. //印章id
  874. //normalSignFieldConfig.put("assignedSealId", orgSignerInfo.getString("sealId"));
  875. //印章类型:1印章,2骑缝章
  876. normalSignFieldConfig.put("signFieldStyle", fieldMap.get("signFieldStyle"));
  877. Map<String, Object> signFieldPosition = Maps.newHashMap();
  878. if (1 == ((int) fieldMap.get("signFieldStyle"))) {
  879. //正常章
  880. if (null != fieldMap.get("positionPage") && null != fieldMap.get("positionX") && null != fieldMap.get("positionY")) {
  881. signFieldPosition.put("positionPage", fieldMap.get("positionPage"));
  882. signFieldPosition.put("positionX", fieldMap.get("positionX"));
  883. signFieldPosition.put("positionY", fieldMap.get("positionY"));
  884. Map<String, Object> signDateConfig = (Map<String, Object>) fieldMap.get("signDateConfig");
  885. if (null == signDateConfig) {
  886. signDateConfig = Maps.newHashMap();
  887. }
  888. field.put("signDateConfig", signDateConfig);
  889. }
  890. } else {
  891. //骑缝章
  892. signFieldPosition.put("positionY", fieldMap.get("positionY"));
  893. }
  894. normalSignFieldConfig.put("signFieldPosition", signFieldPosition);
  895. field.put("normalSignFieldConfig", normalSignFieldConfig);
  896. } else {
  897. field.put("signFieldType", fieldMap.get("signFieldType"));
  898. //签章区配置项(指定signFieldType为 0 - 签章区时,该参数为必传项)
  899. Map<String, Object> remarkSignFieldConfig = Maps.newHashMap();
  900. remarkSignFieldConfig.put("freeMode", true);
  901. remarkSignFieldConfig.put("inputType", 2);
  902. field.put("remarkSignFieldConfig", remarkSignFieldConfig);
  903. }
  904. signFields.add(field);
  905. }
  906. } else {
  907. Map<String, Object> res = Maps.newHashMap();
  908. //失败
  909. res.put("文件模板:" + doc.get("fileName"), signName + ",未匹配到签署区,请检查模板的签署区命名规范。签署区命名规则:如(“甲方:(盖章)_签署方1_X50_Y50“)根据“_”分割字符串,第一个是要盖章的内容位置标识;第二个是签署方用来判断这个签署区是谁的;X50表示:印章位置向右移动50;Y50表示:印章位置向上移动50");
  910. resulList.add(res);
  911. }
  912. }
  913. if (signFields.size() <= 0) {
  914. throw new EsignException(JSON.toJSONString(resulList));
  915. }
  916. signMapInfo.put("signFields", signFields);
  917. signMapInfo.put("signerType", 0);
  918. return signMapInfo;
  919. }
  920. /**
  921. * 处理签署方信息
  922. *
  923. * @param context
  924. * @param signs
  925. * @param docs
  926. * @param sourceId
  927. * @return
  928. * @throws EsignException
  929. * @throws URISyntaxException
  930. */
  931. public List<Map<String, Object>> addSigners(Context context, JSONArray signs, List<Map<String, Object>> docs, String sourceId) throws EsignException, URISyntaxException {
  932. List<Map<String, Object>> signers = Lists.newArrayList();
  933. for (int i = 0; i < signs.size(); i++) {
  934. Map<String, Object> signMapInfo = Maps.newHashMap();
  935. JSONObject sign = signs.getJSONObject(i);
  936. Map<String, Object> orgPsnSignMap = Maps.newHashMap();
  937. JSONObject orgSignerInfo = sign.getJSONObject("orgSignerInfo");
  938. JSONObject psnSignerInfo = sign.getJSONObject("psnSignerInfo");
  939. //企业/机构签署方信息
  940. if (null != orgSignerInfo) {
  941. signMapInfo = addOrgSignerInfo(context, orgSignerInfo, docs, sourceId);
  942. } else {
  943. //个人签署方信息
  944. signMapInfo = addPsnSignerInfo(context, psnSignerInfo, docs, sourceId);
  945. }
  946. signers.add(signMapInfo);
  947. }
  948. return signers;
  949. }
  950. /**
  951. * todo查询流程模板详情 https://open.esign.cn/doc/opendoc/file-and-template3/pfzut7ho9obc7c5r
  952. * 处理签署方信息
  953. *
  954. * @param context
  955. * @param signTemplateId
  956. * @param sealOwnerId
  957. * @param sourceId
  958. * @return
  959. * @throws EsignException
  960. * @throws URISyntaxException
  961. */
  962. public Map<String, List<Map<String, Object>>> addParticipants(Context context, JSONObject signInfo, String signTemplateId, String sealOwnerId, String sourceId) throws EsignException, URISyntaxException {
  963. Map<String, List<Map<String, Object>>> participants_addCopiers = Maps.newHashMap();
  964. List<Map<String, Object>> participants = Lists.newArrayList();
  965. List<Map<String, Object>> addCopiers = Lists.newArrayList();
  966. String orgName = EsignConfig.getInstance().get("orgName");
  967. EsignHttpResponse response = EsignHttpUtil.getOrgIdentity_infoByOrgName(context, orgName);
  968. if (response.getStatus() >= 200 && response.getStatus() < 300) {
  969. JSONObject body = JSON.parseObject(response.getBody());
  970. if ("0".equals(String.valueOf(body.get("code")))) {
  971. JSONObject orgPsn = body.getJSONObject("data");
  972. String orgId = orgPsn.getString("orgId");
  973. orgName = orgPsn.getString("orgName");
  974. //自动落章只能传orgId
  975. EsignHttpResponse std = EsignHttpUtil.getSign_templates_detail(context, signTemplateId, orgId, false, sourceId);
  976. if (std.getStatus() >= 200 && std.getStatus() < 300) {
  977. JSONObject bodyStd = JSON.parseObject(std.getBody());
  978. if ("0".equals(String.valueOf(bodyStd.get("code")))) {
  979. JSONObject orgPsnStd = bodyStd.getJSONObject("data");
  980. JSONArray parts = orgPsnStd.getJSONArray("participants");
  981. if (null != parts) {
  982. for (int i = 0; i < parts.size(); i++) {
  983. JSONObject p = parts.getJSONObject(i);
  984. JSONObject orgParticipant = p.getJSONObject("orgParticipant");
  985. JSONObject psnParticipant = p.getJSONObject("psnParticipant");
  986. String participantFlag = p.getString("participantFlag");
  987. JSONArray signs = signInfo.getJSONArray("signers");
  988. for (int x = 0; x < signs.size(); x++) {
  989. JSONObject sign = signs.getJSONObject(x);
  990. JSONObject orgSignerInfo = sign.getJSONObject("orgSignerInfo");
  991. if (null != orgSignerInfo) {
  992. String signName = orgSignerInfo.getString("signName");
  993. if (participantFlag.equals(signName)) {
  994. orgParticipant = new JSONObject();
  995. p.put("orgParticipant", orgParticipant);
  996. //orgParticipant.put("sealOwnerId",sealOwnerId);
  997. //orgParticipant.put("orgId",orgId);
  998. orgParticipant.put("orgName", orgSignerInfo.getString("orgName"));
  999. JSONObject transactor = new JSONObject();
  1000. transactor.put("transactorName", orgSignerInfo.getString("psnName"));
  1001. transactor.put("transactorPsnAccount", orgSignerInfo.getString("psnAccount"));
  1002. orgParticipant.put("transactor", transactor);
  1003. //orgParticipant.put("sealOwnerId",sealOwnerId);
  1004. //orgParticipant.put("sealOwnerId",sealOwnerId);
  1005. }
  1006. } else {
  1007. JSONObject psnSignerInfo = sign.getJSONObject("psnSignerInfo");
  1008. if (null != psnSignerInfo) {
  1009. String signName = psnSignerInfo.getString("signName");
  1010. if (participantFlag.equals(signName)) {
  1011. if (null == psnParticipant) {
  1012. psnParticipant = new JSONObject();
  1013. }
  1014. p.put("psnParticipant", psnParticipant);
  1015. //orgParticipant.put("sealOwnerId",sealOwnerId);
  1016. psnParticipant.put("psnAccount", psnSignerInfo.get("psnAccount"));
  1017. psnParticipant.put("psnName", psnSignerInfo.get("psnName"));
  1018. //orgParticipant.put("orgName",orgName);
  1019. //orgParticipant.put("sealOwnerId",sealOwnerId);
  1020. //orgParticipant.put("sealOwnerId",sealOwnerId);
  1021. }
  1022. }
  1023. }
  1024. }
  1025. participants.add(p);
  1026. }
  1027. }
  1028. JSONArray copiers = orgPsnStd.getJSONArray("copiers");
  1029. if (null != copiers) {
  1030. for (int i = 0; i < copiers.size(); i++) {
  1031. addCopiers.add((Map<String, Object>) copiers.get(i));
  1032. }
  1033. }
  1034. }
  1035. }
  1036. }
  1037. }
  1038. participants_addCopiers.put("participants", participants);
  1039. participants_addCopiers.put("addCopiers", addCopiers);
  1040. return participants_addCopiers;
  1041. }
  1042. /**
  1043. * 签署流程配置项
  1044. *
  1045. * @param context
  1046. * @param signFlowTitle
  1047. * @param jsonObject
  1048. * @return
  1049. */
  1050. public Map<String, Object> addSignFlowConfig(Context context, String signFlowTitle, JSONObject jsonObject) {
  1051. Map<String, Object> signFlowConfig = Maps.newHashMap();
  1052. signFlowConfig.put("signFlowTitle", signFlowTitle);
  1053. Boolean autoStart = jsonObject.getBoolean("autoStart");
  1054. if (null != autoStart) {
  1055. signFlowConfig.put("autoStart", autoStart);
  1056. } else {
  1057. signFlowConfig.put("autoStart", true);
  1058. }
  1059. Boolean autoFinish = jsonObject.getBoolean("autoFinish");
  1060. if (null != autoFinish) {
  1061. signFlowConfig.put("autoFinish", autoFinish);
  1062. } else {
  1063. signFlowConfig.put("autoFinish", true);
  1064. }
  1065. String notifyUrl = jsonObject.getString("notifyUrl");
  1066. if (null != notifyUrl) {
  1067. signFlowConfig.put("notifyUrl", notifyUrl);
  1068. } else {
  1069. notifyUrl = EsignConfig.getInstance().get("notifyUrl");
  1070. signFlowConfig.put("notifyUrl", notifyUrl);
  1071. }
  1072. Map<String, Object> signConfig = Maps.newHashMap();
  1073. signConfig.put("availableSignClientTypes", "1,2");
  1074. signConfig.put("autoFillAndSubmit", "false");
  1075. signConfig.put("editComponentValue", "true");
  1076. Map<String, Object> noticeConfig = Maps.newHashMap();
  1077. noticeConfig.put("noticeTypes", "1,2");
  1078. noticeConfig.put("examineNotice", "true");
  1079. signFlowConfig.put("signConfig", signConfig);
  1080. signFlowConfig.put("noticeConfig", noticeConfig);
  1081. return signFlowConfig;
  1082. }
  1083. /**
  1084. * 根据传入数据调用 ”填写模板生成文件“ 接口
  1085. *
  1086. * @param jsonObject
  1087. * @return
  1088. */
  1089. public EsignHttpResponse previewFile(Context context, JSONObject jsonObject, String sourceId) throws EsignException {
  1090. String docTemplateId = jsonObject.getString("id");
  1091. String fileName = jsonObject.getString("name");
  1092. if (StringUtils.isBlank(docTemplateId)) {
  1093. docTemplateId = jsonObject.getString("tableId");
  1094. }
  1095. if (StringUtils.isBlank(fileName)) {
  1096. fileName = jsonObject.getString("tableName");
  1097. }
  1098. Map<String, Object> map = Maps.newHashMap();
  1099. map.put("docTemplateId", docTemplateId);
  1100. map.put("fileName", fileName);
  1101. List<Map<String, Object>> components = Lists.newArrayList();
  1102. JSONObject fields = jsonObject.getJSONObject("fields");
  1103. for (Map.Entry<String, Object> field : fields.entrySet()) {
  1104. Map<String, Object> component = Maps.newHashMap();
  1105. component.put("componentId", field.getKey());
  1106. JSONObject fieldInfo = (JSONObject) field.getValue();
  1107. String dataType = (String) fieldInfo.get("dataType");
  1108. if (ComponentTypeEnum.SIGN_AREA.getAlias().equals(dataType)) {
  1109. continue;
  1110. }
  1111. if (ComponentTypeEnum.DATESIGNATURE.getAlias().equals(dataType)) {
  1112. continue;
  1113. }
  1114. if (null == fieldInfo.get("value")) {
  1115. continue;
  1116. }
  1117. component.put("componentValue", fieldInfo.get("value"));
  1118. components.add(component);
  1119. }
  1120. map.put("components", components);
  1121. EsignHttpResponse response = EsignHttpUtil.createByDocTemplate(context, JSON.toJSONString(map), sourceId);
  1122. return response;
  1123. }
  1124. }