智能仲裁后端服务

BestsignOpenApiClient.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package com.ruoyi.bestsign.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.ruoyi.bestsign.domain.ArbitrSignatuVO;
  6. import lombok.Data;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.stereotype.Component;
  10. import javax.validation.constraints.NotNull;
  11. import java.io.IOException;
  12. /**
  13. * 上上签混合云SDK客户端
  14. */
  15. @Component
  16. @Data
  17. @Slf4j
  18. public class BestsignOpenApiClient {
  19. /**
  20. * 开发者id
  21. */
  22. @Value("${ssq.developerId}")
  23. private String developerId;
  24. /**
  25. * 开发者私钥
  26. */
  27. @Value("${ssq.privateKey}")
  28. private String privateKey;
  29. /**
  30. * Host地址
  31. */
  32. @Value("${ssq.serverHost}")
  33. private String serverHost;
  34. /**
  35. * 签名参数
  36. */
  37. private static String urlSignParams = "?developerId=%s&rtick=%s&signType=rsa&sign=%s";
  38. // public BestsignOpenApiClient(String developerId, String privateKey,
  39. // String serverHost) {
  40. // this.developerId = developerIds;
  41. // this.privateKey = privateKey;
  42. // this.serverHost = serverHost;
  43. // }
  44. /**
  45. * POST方法示例
  46. * 个人用户注册
  47. *
  48. * @param account 用户账号
  49. * @param name 姓名
  50. * @param mail 用来接收通知邮件的电子邮箱
  51. * @param mobile 用来接收通知短信的手机号码
  52. * @param identity 证件号码
  53. * @param identityType 枚举值:0-身份证,目前仅支持身份证
  54. * @param contactMail 电子邮箱
  55. * @param contactMobile 手机号码
  56. * @param province 省份
  57. * @param city 城市
  58. * @param address 地址
  59. * @param method 地址
  60. * @return 异步申请任务单号
  61. * @throws IOException
  62. */
  63. public JSONObject userPersonalReg(String account, String name, String mail,
  64. String mobile, String identity, String identityType,
  65. String contactMail, String contactMobile, String province,
  66. String city, String address, @NotNull String method) throws Exception {
  67. //body参数
  68. JSONObject requestBody = new JSONObject();
  69. //用户帐号
  70. requestBody.put("account", account);
  71. //用户名称
  72. requestBody.put("name", name);
  73. //用户类型
  74. requestBody.put("userType", "1");
  75. //用户邮箱
  76. requestBody.put("mail", mail);
  77. //用户手机号
  78. requestBody.put("mobile", mobile);
  79. //用户证件信息对象
  80. JSONObject credential = new JSONObject();
  81. //用户证件号
  82. credential.put("identity", identity);
  83. //用户证件类型
  84. credential.put("identityType", identityType);
  85. requestBody.put("credential", credential);
  86. //是否申请证书
  87. requestBody.put("applyCert", "1");
  88. // 生成一个时间戳参数
  89. String rtick = RSAUtils.getRtick();
  90. // 计算参数签名
  91. String paramsSign = RSAUtils.calcRsaSign(this.developerId,
  92. this.privateKey, this.serverHost, method, rtick, null,
  93. requestBody.toJSONString());
  94. // 签名参数追加为url参数
  95. String fullUrlParams = String.format(urlSignParams, this.developerId,
  96. rtick, paramsSign);
  97. // 发送POST请求
  98. String responseBody = HttpClientSender.sendHttpPost(this.serverHost, method,
  99. fullUrlParams, requestBody.toJSONString());
  100. System.out.println(responseBody);
  101. // 返回结果解析
  102. JSONObject userObj = JSON.parseObject(responseBody);
  103. System.out.println(JSON.toJSONString(userObj));
  104. return userObj;
  105. // 返回errno为0,表示成功,其他表示失败
  106. // if (userObj.getIntValue("errno") == 0) {
  107. // JSONObject data = userObj.getJSONObject("data");
  108. // if (data != null) {
  109. // //对返回data进行处理
  110. // String taskId = data.getString("taskId");
  111. // return taskId;
  112. // }
  113. // return null;
  114. // } else {
  115. // //接口返回异常
  116. // System.out.println(userObj.getIntValue("errno"));
  117. // System.out.println(userObj.getString("errmsg"));
  118. // throw new Exception(userObj.getIntValue("errno") + ":"
  119. // + userObj.getString("errmsg"));
  120. // }
  121. }
  122. /**
  123. * GET方法示例
  124. * 下载合同PDF文件
  125. *
  126. * @param contractId 合同编号
  127. * @return
  128. * @throws Exception
  129. */
  130. public byte[] contractDownload(String contractId) throws Exception {
  131. String host = this.serverHost;
  132. String method = "/storage/contract/download/";
  133. // 组装url参数
  134. String urlParams = "contractId=" + contractId;
  135. // 生成一个时间戳参数
  136. String rtick = RSAUtils.getRtick();
  137. // 计算参数签名
  138. String paramsSign = RSAUtils.calcRsaSign(this.developerId,
  139. this.privateKey, host, method, rtick, urlParams, null);
  140. // 签名参数追加为url参数
  141. urlParams = String.format(urlSignParams, this.developerId, rtick,
  142. paramsSign) + "&" + urlParams;
  143. // 发送请求
  144. byte[] responseBody = HttpClientSender.sendHttpGet(host, method,
  145. urlParams);
  146. // 返回结果解析
  147. return responseBody;
  148. }
  149. public String selectApplyStatus(String account, String taskId) throws Exception {
  150. String methodInvoke = "/user/async/applyCert/status/";
  151. JSONObject requestParam = new JSONObject();
  152. //用户账号
  153. requestParam.put("account", account);
  154. //任务单号
  155. requestParam.put("taskId", taskId);
  156. String timestamsParam = RSAUtils.getRtick();
  157. // 计算参数签名
  158. String paramsSign = RSAUtils.calcRsaSign(this.developerId,
  159. this.privateKey, this.serverHost, methodInvoke, timestamsParam, null,
  160. requestParam.toJSONString());
  161. // 签名参数追加为url参数
  162. String fullUrlParams = String.format(urlSignParams, this.developerId,
  163. timestamsParam, paramsSign);
  164. String responseResult = HttpClientSender.sendHttpPost(this.serverHost, methodInvoke,
  165. fullUrlParams, requestParam.toJSONString());
  166. JSONObject responseObj = JSON.parseObject(responseResult);
  167. // 返回errno为0,表示成功,其他表示失败
  168. if (responseObj.getIntValue("errno") == 0) {
  169. JSONObject data = responseObj.getJSONObject("data");
  170. if (data != null) {
  171. String message = data.getString("message");
  172. String status = data.getString("status");
  173. return status;
  174. }
  175. } else {
  176. log.error("查询申请状态异常:" + responseObj.toJSONString());
  177. }
  178. return responseObj.toJSONString();
  179. }
  180. /**
  181. * 上传文件
  182. *
  183. * @param arbitrSignatuVO
  184. * @return
  185. * @throws Exception
  186. */
  187. public String uploadContact(ArbitrSignatuVO arbitrSignatuVO) throws Exception {
  188. String methodInvoke = "/storage/contract/upload/";
  189. JSONObject requestParam = new JSONObject();
  190. requestParam.put("account", arbitrSignatuVO.getAccount());
  191. requestParam.put("fmd5", arbitrSignatuVO.getFileMd5());
  192. requestParam.put("fdata", arbitrSignatuVO.getFileData());
  193. requestParam.put("fpages", arbitrSignatuVO.getFilePages());
  194. requestParam.put("fname", arbitrSignatuVO.getFileName());
  195. requestParam.put("ftype", arbitrSignatuVO.getFileType());
  196. requestParam.put("title", arbitrSignatuVO.getContractTitle());
  197. requestParam.put("expireTime", arbitrSignatuVO.getPeriodValidity());
  198. String timestamsParam = RSAUtils.getRtick();
  199. // 计算参数签名
  200. String paramsSign = RSAUtils.calcRsaSign(this.developerId,
  201. this.privateKey, this.serverHost, methodInvoke, timestamsParam, null,
  202. requestParam.toJSONString());
  203. // 签名参数追加为url参数
  204. String fullUrlParams = String.format(urlSignParams, this.developerId,
  205. timestamsParam, paramsSign);
  206. String responseResult = HttpClientSender.sendHttpPost(this.serverHost, methodInvoke,
  207. fullUrlParams, requestParam.toJSONString());
  208. JSONObject responseObj = JSON.parseObject(responseResult);
  209. // 返回errno为0,表示成功,其他表示失败
  210. if (responseObj.getIntValue("errno") == 0) {
  211. JSONObject data = responseObj.getJSONObject("data");
  212. if (data != null) {
  213. String contractId = data.getString("contractId");
  214. return contractId;
  215. }
  216. } else {
  217. log.error("上传文件异常:" + responseObj.toJSONString());
  218. }
  219. return responseObj.toJSONString();
  220. }
  221. /**
  222. * 获取签署链接
  223. *
  224. * @param arbitrSignatuVO
  225. * @return
  226. * @throws Exception
  227. */
  228. public String getSignLink(ArbitrSignatuVO arbitrSignatuVO) throws Exception {
  229. String methodInvoke = "/contract/send/";
  230. JSONObject requestParam = new JSONObject();
  231. requestParam.put("contractId", arbitrSignatuVO.getContractNum());
  232. requestParam.put("signer", arbitrSignatuVO.getSignerAccout());
  233. JSONArray signatureArray = new JSONArray();
  234. JSONObject signatuItem = new JSONObject();
  235. signatuItem.put("pageNum", arbitrSignatuVO.getPageNumSigner());
  236. //x坐标
  237. signatuItem.put("x", arbitrSignatuVO.getPageNumSignerX());
  238. //y坐标
  239. signatuItem.put("y", arbitrSignatuVO.getPageNumSignerY());
  240. signatureArray.add(signatuItem);
  241. requestParam.put("signaturePositions", signatureArray);
  242. String timestamsParam = RSAUtils.getRtick();
  243. // 计算参数签名
  244. String paramsSign = RSAUtils.calcRsaSign(this.developerId,
  245. this.privateKey, this.serverHost, methodInvoke, timestamsParam, null,
  246. requestParam.toJSONString());
  247. // 签名参数追加为url参数
  248. String fullUrlParams = String.format(urlSignParams, this.developerId,
  249. timestamsParam, paramsSign);
  250. String responseResult = HttpClientSender.sendHttpPost(this.serverHost, methodInvoke,
  251. fullUrlParams, requestParam.toJSONString());
  252. JSONObject responseObj = JSON.parseObject(responseResult);
  253. // 返回errno为0,表示成功,其他表示失败
  254. if (responseObj.getIntValue("errno") == 0) {
  255. JSONObject data = responseObj.getJSONObject("data");
  256. if (data != null) {
  257. String signLink = data.getString("url");
  258. return signLink;
  259. }
  260. } else {
  261. log.error("获取签署链接异常:" + responseObj.toJSONString());
  262. }
  263. return responseObj.toJSONString();
  264. }
  265. /**
  266. * 完成签署
  267. *
  268. * @param arbitrSignatuVO
  269. * @return
  270. * @throws Exception
  271. */
  272. public String finishSign(ArbitrSignatuVO arbitrSignatuVO) throws Exception {
  273. String methodInvoke = "/storage/contract/lock/";
  274. JSONObject requestParam = new JSONObject();
  275. requestParam.put("contractId", arbitrSignatuVO.getContractNum());
  276. String timestamsParam = RSAUtils.getRtick();
  277. // 计算参数签名
  278. String paramsSign = RSAUtils.calcRsaSign(this.developerId,
  279. this.privateKey, this.serverHost, methodInvoke, timestamsParam, null,
  280. requestParam.toJSONString());
  281. // 签名参数追加为url参数
  282. String fullUrlParams = String.format(urlSignParams, this.developerId,
  283. timestamsParam, paramsSign);
  284. String responseResult = HttpClientSender.sendHttpPost(this.serverHost, methodInvoke,
  285. fullUrlParams, requestParam.toJSONString());
  286. JSONObject responseObj = JSON.parseObject(responseResult);
  287. // 返回errno为0,表示成功,其他表示失败
  288. if (responseObj.getIntValue("errno") == 0) {
  289. return "完成签名";
  290. } else {
  291. log.error("完成签署异常:" + responseObj.toJSONString());
  292. }
  293. return responseObj.toJSONString();
  294. }
  295. }