Create_by_fileOSFService.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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.kingdee.bos.BOSException;
  8. import com.kingdee.bos.Context;
  9. import com.kingdee.bos.bsf.service.app.IHRMsfService;
  10. import com.kingdee.eas.common.EASBizException;
  11. import com.kingdee.eas.custom.esign.tsign.hz.comm.EsignHttpResponse;
  12. import com.kingdee.eas.custom.esign.tsign.hz.exception.EsignException;
  13. import com.kingdee.eas.custom.esign.util.EsignConfig;
  14. import com.kingdee.eas.custom.esign.util.EsignHttpUtil;
  15. import org.apache.commons.lang3.StringUtils;
  16. import java.net.URISyntaxException;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.Set;
  20. /**
  21. * description: Create_by_fileOSFService <br>
  22. * date: 28/11/2025 上午 9:46 <br>
  23. * author: lhbj <br>
  24. * version: 1.0 <br>
  25. */
  26. public class Create_by_fileOSFService implements IHRMsfService {
  27. public static void main(String[] args) {
  28. JSONObject jsonObject = JSON.parseObject("{\"tableName\":\"3-信息安全承诺书.pdf\",\"tableId\":\"4b19400154d1444e969bf69a6ce0c83c\",\"fields\":{\"49e2cabb3fff4e0090d580f7d4d6e0fc\":{\"name\":\"单行文本1\",\"dataType\":\"单行文本\",\"dataFormat\":\"undefined\",\"value\":\"大王大大\"},\"700825a785484185a6e3fd02e358792c\":{\"name\":\"工号\",\"dataType\":\"单行文本\",\"dataFormat\":\"undefined\",\"value\":\"1234534\"},\"a95f4a5d491d473cbba755a9bb84a04a\":{\"name\":\"日期1\",\"dataType\":\"Date\",\"dataFormat\":\"undefined\",\"value\":\"2024-12-03 00:00:00\"},\"35e09954d6aa41538bd27ffc42da59aa\":{\"name\":\"部门\",\"dataType\":\"String\",\"dataFormat\":\"undefined\",\"value\":\"HR产品处\"},\"df8743514be54648a1bc755cd51e315c\":{\"name\":\"多行文本1\",\"dataType\":\"String\",\"dataFormat\":\"undefined\",\"value\":\"4-1-3\"}}}");
  29. for (Map.Entry<String, Object> fileEntry : jsonObject.entrySet()) {
  30. JSONObject fieldObject = (JSONObject) fileEntry.getValue();
  31. }
  32. }
  33. @Override
  34. public Object process(Context context, Map map) throws EASBizException, BOSException {
  35. String mack = (String) map.get("mack");
  36. String data = (String) map.get("data");
  37. Map<String, Object> resul = Maps.newHashMap();
  38. try {
  39. JSONObject jsonObject = JSON.parseObject(data);
  40. if ("preview".equals(mack)) {
  41. EsignHttpResponse response = this.previewFile(context, jsonObject, "预览");
  42. if (response.getStatus() >= 200 && response.getStatus() < 300) {
  43. JSONObject body = JSON.parseObject(response.getBody());
  44. if("0".equals(String.valueOf(body.get("code")))) {
  45. JSONObject jsonData = body.getJSONObject("data");
  46. String fileId = jsonData.getString("fileId");
  47. for (int i = 10; i > 0; i--) {
  48. EsignHttpResponse response1 = EsignHttpUtil.getFileStatus(context,fileId,"预览");
  49. if (response1.getStatus() >= 200 && response1.getStatus() < 300) {
  50. }
  51. }
  52. }
  53. resul.putAll(body);
  54. } else {
  55. resul.put("code", response.getStatus());
  56. resul.put("message", "网络异常");
  57. resul.put("data", null);
  58. }
  59. } else {
  60. Map<String, Object> signMap = Maps.newHashMap();
  61. String sourceId = jsonObject.getString("sourceId");
  62. String signFlowTitle = jsonObject.getString("signFlowTitle");
  63. //文件
  64. JSONObject templateInfo = jsonObject.getJSONObject("templateInfo");
  65. //设置待签署文件信息
  66. List<Map<String, Object>> docs = this.addDocs(context, jsonObject, templateInfo);
  67. signMap.put("docs", docs);
  68. //签署流程配置项
  69. Map<String, Object> signFlowConfig = this.addSignFlowConfig(context, signFlowTitle, jsonObject);
  70. signMap.put("signFlowConfig", signFlowConfig);
  71. //签署方信息
  72. JSONObject signInfo = jsonObject.getJSONObject("signInfo");
  73. JSONArray signs = signInfo.getJSONArray("signers");
  74. List<Map<String, Object>> signers = this.addSigners(context, signs, docs, sourceId);
  75. signMap.put("signers", signers);
  76. //抄送方信息
  77. JSONArray copis = signInfo.getJSONArray("copiers");
  78. List<Map<String, Object>> copiers = this.addCopiers(context, copis);
  79. if (copiers.size() > 0) {
  80. signMap.put("copiers", copiers);
  81. }
  82. }
  83. } catch (EsignException | URISyntaxException e) {
  84. e.printStackTrace();
  85. }
  86. return resul;
  87. }
  88. /**
  89. * 设置待签署文件信息
  90. *
  91. * @param context
  92. * @param jsonObject
  93. * @param templateInfo
  94. * @return
  95. * @throws EsignException
  96. */
  97. public List<Map<String, Object>> addDocs(Context context, JSONObject jsonObject, JSONObject templateInfo) throws EsignException {
  98. //设置待签署文件信息
  99. Map<String, Object> resul = Maps.newHashMap();
  100. List<Map<String, Object>> docs = Lists.newArrayList();
  101. for (Map.Entry<String, Object> fileEntry : templateInfo.entrySet()) {
  102. JSONObject fieldObject = (JSONObject) fileEntry.getValue();
  103. String docTemplateId = jsonObject.getString("id");
  104. String fileName = jsonObject.getString("name");
  105. Object order = jsonObject.get("order");
  106. String sourceId = jsonObject.getString("sourceId");
  107. EsignHttpResponse response = this.previewFile(context, fieldObject, sourceId);
  108. if (response.getStatus() >= 200 && response.getStatus() < 300) {
  109. JSONObject body = JSON.parseObject(response.getBody());
  110. if ("0".equals(String.valueOf(body.get("code")))) {
  111. JSONObject file = body.getJSONObject("data");
  112. Map<String, Object> doc = Maps.newHashMap();
  113. doc.put("fileId", file.get("fileId"));
  114. doc.put("fileName", fileName);
  115. if (null != order) {
  116. doc.put("order", Integer.parseInt(String.valueOf(order)));
  117. }
  118. docs.add(doc);
  119. } else {
  120. //文件模板生成失败
  121. resul.putAll(body);
  122. throw new EsignException(JSON.toJSONString(resul));
  123. }
  124. } else {
  125. throw new EsignException("网络异常:" + JSON.toJSONString(response));
  126. }
  127. }
  128. return docs;
  129. }
  130. /**
  131. * 抄送方信息
  132. *
  133. * @param copis
  134. * @return
  135. */
  136. public List<Map<String, Object>> addCopiers(Context context, JSONArray copis) {
  137. List<Map<String, Object>> copiers = Lists.newArrayList();
  138. for (int i = 0; i < copis.size(); i++) {
  139. JSONObject cop = copis.getJSONObject(i);
  140. Map<String, Object> copierPsn = Maps.newHashMap();
  141. Map<String, Object> copierPsnInfo = Maps.newHashMap();
  142. copierPsnInfo.put("psnAccount", cop.getString("psnAccount"));
  143. copierPsn.put("copierPsnInfo", copierPsnInfo);
  144. copiers.add(copierPsn);
  145. }
  146. return copiers;
  147. }
  148. /**
  149. * @param context
  150. * @param orgSignerInfo
  151. * @param docs
  152. * @param sourceId
  153. * @return
  154. * @throws EsignException
  155. * @throws URISyntaxException
  156. */
  157. public Map<String, Object> addOrgSignerInfo(Context context, JSONObject orgSignerInfo, List<Map<String, Object>> docs, String sourceId) throws EsignException, URISyntaxException {
  158. Map<String, Object> resul = Maps.newHashMap();
  159. Map<String, Object> signMapInfo = Maps.newHashMap();
  160. Map<String, Object> orgPsnSignMap = Maps.newHashMap();
  161. String orgName = orgSignerInfo.getString("orgName");
  162. EsignHttpResponse response = EsignHttpUtil.getOrgIdentity_infoByOrgName(context, orgName);
  163. if (response.getStatus() >= 200 && response.getStatus() < 300) {
  164. JSONObject body = JSON.parseObject(response.getBody());
  165. if ("0".equals(String.valueOf(body.get("code")))) {
  166. JSONObject orgPsn = body.getJSONObject("data");
  167. String orgId = orgPsn.getString("orgId");
  168. orgName = orgPsn.getString("orgName");
  169. orgPsnSignMap.put("orgId", orgId);
  170. orgPsnSignMap.put("orgName", orgName);
  171. orgPsnSignMap.put("orgInfo", orgPsn.getJSONObject("orgInfo"));
  172. } else {
  173. //失败
  174. resul.putAll(body);
  175. throw new EsignException(JSON.toJSONString(resul));
  176. }
  177. } else {
  178. throw new EsignException("网络异常:" + JSON.toJSONString(response));
  179. }
  180. Integer signOrder = orgSignerInfo.getInteger("signOrder");
  181. //设置签署方的签署顺序
  182. if (null != signOrder) {
  183. Map<String, Object> signConfig = Maps.newHashMap();
  184. signConfig.put("signOrder", signOrder);
  185. signMapInfo.put("signConfig", signConfig);
  186. }
  187. signMapInfo.put("orgSignerInfo", orgPsnSignMap);
  188. //设置签署方的通知方式
  189. String noticeTypes = orgSignerInfo.getString("noticeTypes");
  190. Map<String, String> noticeConfig = Maps.newHashMap();
  191. if (StringUtils.isNotBlank(noticeTypes)) {
  192. noticeConfig.put("noticeTypes", noticeTypes);
  193. } else {
  194. noticeConfig.put("noticeTypes", "1,2");
  195. }
  196. signMapInfo.put("noticeConfig", noticeConfig);
  197. //签署区信息
  198. List<Map<String, Object>> signFields = Lists.newArrayList();
  199. for (Map<String, Object> doc : docs) {
  200. Map<String, Object> field = Maps.newHashMap();
  201. field.put("fileId", doc.get("fileId"));
  202. field.put("customBizNum", sourceId);
  203. field.put("signFieldType", 0);
  204. //签章区配置项(指定signFieldType为 0 - 签章区时,该参数为必传项)
  205. Map<String, Object> normalSignFieldConfig = Maps.newHashMap();
  206. normalSignFieldConfig.put("freeMode", true);
  207. field.put("normalSignFieldConfig", normalSignFieldConfig);
  208. signFields.add(field);
  209. }
  210. signMapInfo.put("signFields", signFields);
  211. return signMapInfo;
  212. }
  213. /**
  214. * @param context
  215. * @param psnSignerInfo
  216. * @param docs
  217. * @param sourceId
  218. * @return
  219. * @throws EsignException
  220. * @throws URISyntaxException
  221. */
  222. public Map<String, Object> addPsnSignerInfo(Context context, JSONObject psnSignerInfo, List<Map<String, Object>> docs, String sourceId) throws EsignException, URISyntaxException {
  223. Map<String, Object> resul = Maps.newHashMap();
  224. Map<String, Object> signMapInfo = Maps.newHashMap();
  225. Map<String, Object> orgPsnSignMap = Maps.newHashMap();
  226. //个人签署方信息
  227. String psnAccount = psnSignerInfo.getString("psnAccount");
  228. String psnName = psnSignerInfo.getString("psnName");
  229. Integer signOrder = psnSignerInfo.getInteger("signOrder");
  230. //设置签署方的签署顺序
  231. if (null != signOrder) {
  232. Map<String, Object> signConfig = Maps.newHashMap();
  233. signConfig.put("signOrder", signOrder);
  234. signMapInfo.put("signConfig", signConfig);
  235. }
  236. orgPsnSignMap.put("psnAccount", psnAccount);
  237. Map<String, String> psnInfo = Maps.newHashMap();
  238. psnInfo.put("psnName", psnName);
  239. orgPsnSignMap.put("psnInfo", psnInfo);
  240. //个人签署方信息
  241. signMapInfo.put("psnSignerInfo", orgPsnSignMap);
  242. //设置签署方的通知方式
  243. String noticeTypes = psnSignerInfo.getString("noticeTypes");
  244. Map<String, String> noticeConfig = Maps.newHashMap();
  245. if (StringUtils.isNotBlank(noticeTypes)) {
  246. noticeConfig.put("noticeTypes", noticeTypes);
  247. } else {
  248. noticeConfig.put("noticeTypes", "1,2");
  249. }
  250. signMapInfo.put("noticeConfig", noticeConfig);
  251. //签署区信息
  252. List<Map<String, Object>> signFields = Lists.newArrayList();
  253. for (Map<String, Object> doc : docs) {
  254. Map<String, Object> field = Maps.newHashMap();
  255. field.put("fileId", doc.get("fileId"));
  256. field.put("customBizNum", sourceId);
  257. field.put("signFieldType", 0);
  258. //签章区配置项(指定signFieldType为 0 - 签章区时,该参数为必传项)
  259. Map<String, Object> normalSignFieldConfig = Maps.newHashMap();
  260. normalSignFieldConfig.put("freeMode", true);
  261. field.put("normalSignFieldConfig", normalSignFieldConfig);
  262. signFields.add(field);
  263. }
  264. signMapInfo.put("signFields", signFields);
  265. return signMapInfo;
  266. }
  267. /**
  268. * 签署方信息
  269. *
  270. * @param context
  271. * @param signs
  272. * @param docs
  273. * @param sourceId
  274. * @return
  275. * @throws EsignException
  276. * @throws URISyntaxException
  277. */
  278. public List<Map<String, Object>> addSigners(Context context, JSONArray signs, List<Map<String, Object>> docs, String sourceId) throws EsignException, URISyntaxException {
  279. List<Map<String, Object>> signers = Lists.newArrayList();
  280. for (int i = 0; i < signs.size(); i++) {
  281. Map<String, Object> signMapInfo = Maps.newHashMap();
  282. JSONObject sign = signs.getJSONObject(i);
  283. Map<String, Object> orgPsnSignMap = Maps.newHashMap();
  284. JSONObject orgSignerInfo = sign.getJSONObject("orgSignerInfo");
  285. JSONObject psnSignerInfo = sign.getJSONObject("psnSignerInfo");
  286. //企业/机构签署方信息
  287. if (null != orgSignerInfo) {
  288. signMapInfo = addOrgSignerInfo(context, orgSignerInfo, docs, sourceId);
  289. } else {
  290. //个人签署方信息
  291. signMapInfo = addPsnSignerInfo(context, psnSignerInfo, docs, sourceId);
  292. }
  293. signers.add(signMapInfo);
  294. }
  295. return signers;
  296. }
  297. /**
  298. * 签署流程配置项
  299. *
  300. * @param context
  301. * @param signFlowTitle
  302. * @param jsonObject
  303. * @return
  304. */
  305. public Map<String, Object> addSignFlowConfig(Context context, String signFlowTitle, JSONObject jsonObject) {
  306. Map<String, Object> signFlowConfig = Maps.newHashMap();
  307. signFlowConfig.put("signFlowTitle", signFlowTitle);
  308. Boolean autoStart = jsonObject.getBoolean("autoStart");
  309. if (null != autoStart) {
  310. signFlowConfig.put("autoStart", autoStart);
  311. } else {
  312. signFlowConfig.put("autoStart", true);
  313. }
  314. Boolean autoFinish = jsonObject.getBoolean("autoFinish");
  315. if (null != autoFinish) {
  316. signFlowConfig.put("autoFinish", autoFinish);
  317. } else {
  318. signFlowConfig.put("autoFinish", true);
  319. }
  320. String notifyUrl = jsonObject.getString("notifyUrl");
  321. if (null != notifyUrl) {
  322. signFlowConfig.put("notifyUrl", notifyUrl);
  323. } else {
  324. notifyUrl = EsignConfig.getInstance().get("notifyUrl");
  325. signFlowConfig.put("notifyUrl", notifyUrl);
  326. }
  327. return signFlowConfig;
  328. }
  329. /**
  330. * {
  331. * "name": "信息安全承诺书.html",
  332. * "id": "c964cb6a67dd40f4a92d4f8e67462e17",
  333. * "fields": {
  334. * "5b5d7563808d457ea125b000ff2a8d1d": {
  335. * "name": "部门",
  336. * "dataType": "单行文本",
  337. * "value": null
  338. * },
  339. * "5fcd7b97621c4cd1a5f514e7f06cb0da": {
  340. * "name": "工号",
  341. * "dataType": "单行文本",
  342. * "value": null
  343. * }
  344. * }
  345. * {
  346. * "docTemplateId":"8726f6b***03a56d",
  347. * "fileName":"某公司的交易协议签署文件",
  348. * "components":[
  349. * {
  350. * "componentId":"59af7766***36ef41b",
  351. * "componentKey":"",
  352. * "componentValue":"这里是填充的文本"
  353. * },
  354. * {
  355. * "componentId":"7315e9af**72d2dac40",
  356. * "componentKey":"",
  357. * "componentValue":"2022/01/01"
  358. * }
  359. * ]
  360. * }
  361. *
  362. * @param jsonObject
  363. * @return
  364. */
  365. public EsignHttpResponse previewFile(Context context, JSONObject jsonObject, String sourceId) throws EsignException {
  366. String docTemplateId = jsonObject.getString("id");
  367. String fileName = jsonObject.getString("name");
  368. if (StringUtils.isBlank(docTemplateId)) {
  369. docTemplateId = jsonObject.getString("tableId");
  370. }
  371. if (StringUtils.isBlank(fileName)) {
  372. fileName = jsonObject.getString("tableName");
  373. }
  374. Map<String, Object> map = Maps.newHashMap();
  375. map.put("docTemplateId", docTemplateId);
  376. map.put("fileName", fileName);
  377. List<Map<String, Object>> components = Lists.newArrayList();
  378. JSONObject fields = jsonObject.getJSONObject("fields");
  379. for (Map.Entry<String, Object> field : fields.entrySet()) {
  380. Map<String, Object> component = Maps.newHashMap();
  381. component.put("componentId", field.getKey());
  382. JSONObject fieldInfo = (JSONObject) field.getValue();
  383. component.put("componentValue", fieldInfo.get("value"));
  384. components.add(component);
  385. }
  386. map.put("components", components);
  387. EsignHttpResponse response = EsignHttpUtil.createByDocTemplate(context, JSON.toJSONString(map), sourceId);
  388. //html 文件需要等待转换完成
  389. return response;
  390. }
  391. }