| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569 |
- package me.zhengjie.base.util;
- import cfca.sadk.algorithm.common.PKIException;
- import cfca.sadk.util.Base64;
- import cfca.trustsign.common.vo.cs.*;
- import cfca.trustsign.common.vo.request.tx3.*;
- import cfca.trustsign.common.vo.response.ErrorResVO;
- import cfca.trustsign.common.vo.response.tx3.*;
- import com.alibaba.fastjson.JSONObject;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import me.zhengjie.application.admin.service.DictDetailService;
- import me.zhengjie.sign.connector.HttpConnector;
- import me.zhengjie.sign.converter.JsonObjectMapper;
- import me.zhengjie.sign.util.PdfUtil;
- import me.zhengjie.sign.util.SecurityUtil;
- import me.zhengjie.sign.util.TimeUtil;
- import me.zhengjie.utils.SpringContextHolder;
- import org.springframework.stereotype.Component;
- import java.io.File;
- import java.io.InputStream;
- import java.util.List;
- import java.util.Map;
- @Component
- @Slf4j
- @RequiredArgsConstructor
- public class CFCACertUtil {
- private final HttpConnector httpConnector;
- private static final int PROXY_SIGN_ON = 1;
- private static final String PROJECT_CODE = "005";
- private static final String AUTH_MODE = "公安部";
- /**
- * 注册安心签企业用户
- *
- * @param enterprise 企业信息
- * @param enterpriseTransactor 企业经办人信息
- * @return 安心签用户ID
- * @throws Exception
- */
- public String registerCompany(EnterpriseVO enterprise, EnterpriseTransactorVO enterpriseTransactor)
- throws Exception {
- enterprise.setAuthenticationTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14));
- enterprise.setAuthenticationMode(AUTH_MODE);
- enterprise.setIdentTypeCode("N");
- enterpriseTransactor.setIdentTypeCode("0");
- Tx3002ReqVO tx3002ReqVO = new Tx3002ReqVO();
- tx3002ReqVO.setHead(HeadVO.builder().txTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14)).build());
- tx3002ReqVO.setEnterprise(enterprise);
- tx3002ReqVO.setEnterpriseTransactor(enterpriseTransactor);
- String response = requestAPI(tx3002ReqVO, "3002");
- if (!isSucess(response)) {
- ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class);
- System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage());
- throw new Exception("安心签服务请求失败");
- }
- Tx3002ResVO tx3002ResVO = new ObjectMapper().readValue(response, Tx3002ResVO.class);
- return tx3002ResVO.getEnterprise().getUserId();
- }
- /**
- * 注册安心签个人用户
- *
- * @throws Exception
- */
- public String registerPerson(PersonVO person) throws Exception {
- person.setAuthenticationTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14));
- person.setAuthenticationMode(AUTH_MODE);
- Tx3001ReqVO tx3001ReqVO = new Tx3001ReqVO();
- tx3001ReqVO.setHead(HeadVO.builder().txTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14)).build());
- tx3001ReqVO.setPerson(person);
- String response = requestAPI(tx3001ReqVO, "3001");
- if (!isSucess(response)) {
- ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class);
- System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage());
- throw new Exception("安心签服务请求失败");
- }
- Tx3001ResVO tx3001ResVO = new ObjectMapper().readValue(response, Tx3001ResVO.class);
- return tx3001ResVO.getPerson().getUserId();
- }
- /**TODO 暂时没有用到
- * 查询证书信息
- *
- * @param cert
- * @return
- * @throws Exception
- */
- public CertVO queryCertificate(CertVO cert) throws Exception {
- Tx3311ReqVO tx3311ReqVO = new Tx3311ReqVO();
- tx3311ReqVO.setHead(HeadVO.builder().txTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14)).build());
- tx3311ReqVO.setCert(cert);
- String response = requestAPI(tx3311ReqVO, "3311");
- if (!isSucess(response)) {
- ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class);
- System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage());
- throw new Exception("安心签服务请求失败");
- }
- Tx3311ResVO tx3311ResVO = new ObjectMapper().readValue(response, Tx3311ResVO.class);
- return tx3311ResVO.getCert();
- }
- // 9. 修改印章3012
- // 此接口用于客户平台修改某用户的印章图片。客户平台可以调用此接口修改某用户指定印章ID的印章图片。
- public void updateSealWithContent(String userId, String sealId, String content) throws Exception {
- HeadVO head = new HeadVO();
- head.setTxTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14));
- SealVO sealVO = new SealVO();
- sealVO.setSealId(sealId);
- sealVO.setImageData(content);
- SealUpdateVO sealUpdateVO = new SealUpdateVO();
- sealUpdateVO.setUserId(userId);
- sealUpdateVO.setSeal(sealVO);
- Tx3012ReqVO tx3012ReqVO = new Tx3012ReqVO();
- tx3012ReqVO.setHead(head);
- tx3012ReqVO.setSealUpdate(sealUpdateVO);
- String response = requestAPI(tx3012ReqVO, "3012");
- if (!isSucess(response)) {
- ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class);
- System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage());
- throw new Exception("安心签服务请求失败");
- }
- }
- /**
- * 修改签章
- *
- * @param userId
- * @param sealId
- * @param b
- * @throws Exception
- */
- public void updateSeal(String userId, String sealId, byte[] b) throws Exception {
- // 这里将图片的路径下载文件
- // byte[] b=DownloadUtils.downloadFile(filePath);
- String content = Base64.toBase64String(b);
- updateSealWithContent(userId, sealId, content);
- }
- /**
- * 为指定用户添加印章
- *
- * @param userId
- * @param content
- * @return 返回印章ID
- * @throws Exception
- */
- public String addSealWithContent(String userId, String content) throws Exception {
- HeadVO head = new HeadVO();
- head.setTxTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14));
- SealAddVO sealAddVO = new SealAddVO();
- sealAddVO.setUserId(userId);
- SealVO sealVO = new SealVO();
- sealVO.setImageData(content);
- sealAddVO.setSeal(sealVO);
- Tx3011ReqVO tx3011ReqVO = new Tx3011ReqVO();
- tx3011ReqVO.setHead(head);
- tx3011ReqVO.setSealAdd(sealAddVO);
- String response = requestAPI(tx3011ReqVO, "3011");
- if (!isSucess(response)) {
- ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class);
- System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage());
- throw new Exception("安心签服务请求失败");
- }
- Tx3011ResVO tx3011ResVO = new ObjectMapper().readValue(response, Tx3011ResVO.class);
- return tx3011ResVO.getSealAdd().getSeal().getSealId();
- }
- /**
- * 为指定用户添加印章
- *
- * @param userId
- * @param b
- * @return 返回印章ID
- * @throws Exception
- */
- public String addSeal(String userId, byte[] b) throws Exception {
- String content = Base64.toBase64String(b);
- return addSealWithContent(userId, content);
- }
- /**
- * 对指定用户发送授权验证码
- *
- * @param userId
- * @throws Exception
- */
- public boolean sendAuthMessage(String userId) throws Exception {
- Tx3101ReqVO tx3101ReqVO = new Tx3101ReqVO();
- HeadVO head = new HeadVO();
- head.setTxTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14));
- ProxySignVO proxySignVO = new ProxySignVO();
- proxySignVO.setUserId(userId);
- proxySignVO.setProjectCode(PROJECT_CODE);
- tx3101ReqVO.setHead(head);
- tx3101ReqVO.setProxySign(proxySignVO);
- String response = requestAPI(tx3101ReqVO, "3101");
- if (!isSucess(response)) {
- ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class);
- System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage());
- if (!"60030401".equals(errorResVO.getErrorCode())) {
- throw new Exception("安心签服务请求失败");
- } else {
- return true;
- }
- }
- return false;
- }
- /**
- * 确认授权验证码
- *
- * @param userId
- * @param checkCode
- * @throws Exception
- */
- public void confirmAuthMessage(String userId, String checkCode) throws Exception {
- Tx3102ReqVO tx3102ReqVO = new Tx3102ReqVO();
- HeadVO head = new HeadVO();
- head.setTxTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14));
- ProxySignVO proxySignVO = new ProxySignVO();
- proxySignVO.setUserId(userId);
- proxySignVO.setProjectCode(PROJECT_CODE);
- proxySignVO.setCheckCode(checkCode);
- tx3102ReqVO.setHead(head);
- tx3102ReqVO.setProxySign(proxySignVO);
- String response = requestAPI(tx3102ReqVO, "3102");
- if (!isSucess(response)) {
- ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class);
- System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage());
- throw new Exception("安心签服务请求失败");
- }
- }
- /**
- * 上传签署合同
- *
- * @param notaryUser 公证员签名信息
- * @param notaryOffice 公证处签名信息
- * @param file 签名文件
- * @return 返回合同编号
- * @throws Exception
- */
- public String uploadSignContract(UploadSignInfoVO notaryUser, UploadSignInfoVO notaryOffice,
- InputStream file, String fileName) throws Exception {
- DictDetailService dictDetailService = SpringContextHolder.getBean(DictDetailService.class);
- Map<String, String> map = dictDetailService.getValueByName("signed_position");
- // signed 公证员签名
- // 公证员签名
- String signed = map.get("signed");
- JSONObject json = JSONObject.parseObject(signed);
- String keyword = json.getString("keyword");
- String offsetX = json.getString("offsetX");
- String offsetY = json.getString("offsetY");
- String width = json.getString("width");
- String height = json.getString("height");
- SignKeywordVO keywordNotaryUser = getSignKeywork(keyword, offsetX, offsetY, width, height);
- setSignInfoDefault(notaryUser, keywordNotaryUser);
- // 盖章
- String stamp = map.get("stamp");
- json = JSONObject.parseObject(stamp);
- keyword = json.getString("keyword");
- offsetX = json.getString("offsetX");
- offsetY = json.getString("offsetY");
- width = json.getString("width");
- height = json.getString("height");
- SignKeywordVO keywordNotaryOffice = getSignKeywork(keyword, offsetX, offsetY, width, height);
- setSignInfoDefault(notaryOffice, keywordNotaryOffice);
- UploadSignInfoVO[] signInfos = { notaryUser, notaryOffice };
- UploadContractVO uploadContract = new UploadContractVO();
- uploadContract.setSignInfos(signInfos);
- uploadContract.setContractTypeCode("WT");
- uploadContract.setContractName("赋强公证");
- HeadVO head = new HeadVO();
- head.setTxTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14));
- Tx3203ReqVO tx3203ReqVO = new Tx3203ReqVO();
- tx3203ReqVO.setHead(head);
- tx3203ReqVO.setUploadContract(uploadContract);
- String response = requestAPI(tx3203ReqVO, "3203", file, fileName);
- if (!isSucess(response)) {
- ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class);
- System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage());
- throw new Exception("安心签服务请求失败");
- }
- Tx3203ResVO tx3203ResVO = new ObjectMapper().readValue(response, Tx3203ResVO.class);
- return tx3203ResVO.getContract().getContractNo();
- }
- // /**
- // * 上传签署合同
- // *
- // * @param user
- // * @param file
- // * @return
- // * @throws Exception
- // */
- // public static String uploadSignContract(UploadSignInfoVO user, InputStream file, String fileName) throws Exception {
- //
- // SignKeywordVO keywordUser = getSignKeywork(Constants.signed, "70", "0", "160", "60");
- // setSignInfoDefault(user, keywordUser);
- //
- // UploadSignInfoVO[] signInfos = { user };
- // UploadContractVO uploadContract = new UploadContractVO();
- // uploadContract.setSignInfos(signInfos);
- // uploadContract.setContractTypeCode("WT");
- // uploadContract.setContractName("赋强公证");
- //
- // HeadVO head = new HeadVO();
- // head.setTxTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14));
- //
- // Tx3203ReqVO tx3203ReqVO = new Tx3203ReqVO();
- // tx3203ReqVO.setHead(head);
- // tx3203ReqVO.setUploadContract(uploadContract);
- // // 读取文件的流,TODO
- // String response = requestAPI(tx3203ReqVO, "3203", file, fileName);
- // if (!isSucess(response)) {
- // ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class);
- // System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage());
- // throw new Exception("安心签服务请求失败");
- // }
- // Tx3203ResVO tx3203ResVO = new ObjectMapper().readValue(response, Tx3203ResVO.class);
- // return tx3203ResVO.getContract().getContractNo();
- // }
- /**
- * 下载签名公证书
- *
- * @param filePath
- * @param contractNo
- * @throws Exception
- */
- public String download(String filePath, String contractNo) throws Exception {
- byte[] fileBtye = httpConnector
- .getFile("platId/" + httpConnector.getPlatID() + "/contractNo/" + contractNo + "/downloading");
- String contentType;
- String suffix;
- if (PdfUtil.isPdf(fileBtye)) {
- contentType = "application/pdf";
- suffix = ".pdf";
- } else {
- contentType = "application/ofd";
- suffix = ".ofd";
- }
- String fullFilePath = filePath + contractNo + suffix;
- // 上传文件路径
- log.info("上传文件的目录:" + fullFilePath);
- FileUploadUtil.uploadFile(fullFilePath, contentType, fileBtye);
- return fullFilePath;
- }
- /**
- * 发送安心签API请求
- *
- * @param txReqVO
- * @param txCode
- * @return
- * @throws Exception
- */
- private String requestAPI(Object txReqVO, String txCode) throws Exception {
- return requestAPI(txReqVO, txCode, null);
- }
- /**
- * 发送安心签API请求
- *
- * @param txReqVO
- * @param txCode
- * @throws PKIException
- */
- private String requestAPI(Object txReqVO, String txCode, File file) throws Exception {
- String req = new JsonObjectMapper().writeValueAsString(txReqVO);
- System.out.println("req:" + req);
- String signature = SecurityUtil.p7SignMessageDetach(httpConnector.getKeyStorePath(), httpConnector.getKeyStorePassword(),
- httpConnector.getAlias(), req);
- String res = "";
- if ("3203".equals(txCode)) {
- res = httpConnector.post("platId/" + httpConnector.getPlatID() + "/txCode/" + txCode + "/transaction", req,
- signature, file);
- } else {
- res = httpConnector.post("platId/" + httpConnector.getPlatID() + "/txCode/" + txCode + "/transaction", req,
- signature);
- }
- System.out.println("res:" + res);
- return res;
- }
- /**
- * 发送安心签API请求
- *
- * @param txReqVO
- * @param txCode
- * @throws PKIException
- */
- private String requestAPI(Object txReqVO, String txCode, InputStream file, String fileName)
- throws Exception {
- String req = new JsonObjectMapper().writeValueAsString(txReqVO);
- System.out.println("req:" + req);
- String signature = SecurityUtil.p7SignMessageDetach(httpConnector.getKeyStorePath(), httpConnector.getKeyStorePassword(),
- httpConnector.getAlias(), req);
- String res = "";
- if ("3203".equals(txCode)) {
- res = httpConnector.post("platId/" + httpConnector.getPlatID() + "/txCode/" + txCode + "/transaction", req,
- signature, file, fileName);
- } else {
- res = httpConnector.post("platId/" + httpConnector.getPlatID() + "/txCode/" + txCode + "/transaction", req,
- signature);
- }
- System.out.println("res:" + res);
- return res;
- }
- /**
- * 多个人签证,最多三个人
- **
- * @param notaryUserInfos 签名多个,包含了可能是借款人,抵押人,担保人
- * @param notaryUser 公证员签名信息
- * @param seal 盖章的位置
- * @param file 文件的
- * @param fileName 文件名称
- * @return 返回合同编号
- * @throws Exception
- */
- public String uploadMultiSignContract(List<UploadSignInfoVO> notaryUserInfos, UploadSignInfoVO notaryUser,
- UploadSignInfoVO seal, InputStream file, String fileName) throws Exception {
- DictDetailService dictDetailService = SpringContextHolder.getBean(DictDetailService.class);
- Map<String, String> map = dictDetailService.getValueByName("signed_position");
- int i = 0;
- String signature = map.get("signature");
- JSONObject json = JSONObject.parseObject(signature);
- int start = json.getIntValue("start");
- int step = json.getIntValue("step");
- String keyword = json.getString("keyword");
- String offsetY = json.getString("offsetY");
- String width = json.getString("width");
- String height = json.getString("height");
- for (UploadSignInfoVO user : notaryUserInfos) {
- String x = String.valueOf(start + i * step);
- i++;
- SignKeywordVO keywordNotaryUser = getSignKeywork(keyword, x, offsetY, width, height);
- setSignInfoDefault(user, keywordNotaryUser);
- }
- // 公证员签名
- String signed = map.get("signed");
- json = JSONObject.parseObject(signed);
- keyword = json.getString("keyword");
- String offsetX = json.getString("offsetX");
- offsetY = json.getString("offsetY");
- width = json.getString("width");
- height = json.getString("height");
- // 公证员签名
- if (notaryUser != null) {
- SignKeywordVO keywordNotaryUser = getSignKeywork(keyword, offsetX, offsetY, width, height);
- setSignInfoDefault(notaryUser, keywordNotaryUser);
- notaryUserInfos.add(notaryUser);
- }
- // 盖章
- String stamp = map.get("stamp");
- json = JSONObject.parseObject(stamp);
- keyword = json.getString("keyword");
- offsetX = json.getString("offsetX");
- offsetY = json.getString("offsetY");
- width = json.getString("width");
- height = json.getString("height");
- if (seal != null) {
- SignKeywordVO keywordNotaryOffice = getSignKeywork(keyword, offsetX, offsetY, width, height);
- setSignInfoDefault(seal, keywordNotaryOffice);
- notaryUserInfos.add(seal);
- }
- UploadContractVO uploadContract = new UploadContractVO();
- uploadContract.setSignInfos(notaryUserInfos.toArray(new UploadSignInfoVO[0]));
- uploadContract.setContractTypeCode("WT");
- uploadContract.setContractName("赋强公证");
- HeadVO head = new HeadVO();
- head.setTxTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14));
- Tx3203ReqVO tx3203ReqVO = new Tx3203ReqVO();
- tx3203ReqVO.setHead(head);
- tx3203ReqVO.setUploadContract(uploadContract);
- String response = requestAPI(tx3203ReqVO, "3203", file, fileName);
- if (!isSucess(response)) {
- ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class);
- System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage());
- throw new Exception("安心签服务请求失败");
- }
- Tx3203ResVO tx3203ResVO = new ObjectMapper().readValue(response, Tx3203ResVO.class);
- return tx3203ResVO.getContract().getContractNo();
- }
- /**
- * 设置签名默认信息
- *
- * @param signInfo
- * @param keyword
- */
- public void setSignInfoDefault(UploadSignInfoVO signInfo, SignKeywordVO keyword) {
- signInfo.setIsProxySign(PROXY_SIGN_ON);
- signInfo.setProjectCode(PROJECT_CODE);
- signInfo.setIsCheckProjectCode(0);
- signInfo.setSignKeyword(keyword);
- }
- /**
- * 获得签名定位关键词对象
- *
- * @param keyword
- * @param offsetX
- * @param offsetY
- * @param width
- * @param height
- * @return
- */
- public SignKeywordVO getSignKeywork(String keyword, String offsetX, String offsetY, String width,
- String height) {
- SignKeywordVO signKeyword = new SignKeywordVO();
- signKeyword.setKeyword(keyword);
- signKeyword.setOffsetCoordX(offsetX);
- signKeyword.setOffsetCoordY(offsetY);
- signKeyword.setImageWidth(width);
- signKeyword.setImageHeight(height);
- return signKeyword;
- }
- /**
- * 判断请求是否成功
- *
- * @param response
- * @return
- */
- private boolean isSucess(String response) {
- if (response.indexOf("\"head\"") < 0) {
- return false;
- }
- return true;
- }
- }
|