jiangping
2024-12-30 aa3e62b0594db5b713a36fb8027af24adb25cf7a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
package com.doumee.service.business.third;
 
import cn.emay.sdk.client.SmsSDKClient;
import cn.emay.sdk.util.exception.SDKParamsException;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.utils.Constants;
import com.doumee.dao.business.model.InterfaceLog;
import com.doumee.service.business.InterfaceLogService;
import com.jzq.JzqHttpApiTool;
import com.jzq.common.ResultInfo;
import com.jzq.common.bean.sign.SignatoryReq;
import com.jzq.common.http.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import java.util.Date;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.UUID;
 
/**
 * <ul>
 * <li>项目名称 : 后台服务</li>
 * <li>文件名称 : JzqHttpApiTest</li>
 * <li>创建时间 : 2019/8/23 9:14</li>
 * <li>描 述 :  用于jzq的api服请求
 * </ul>
 *
 * @author yfx
 * @title 用于jzq的api服请求
 */
@Slf4j
@Service
public class SignService {
    /**
     * 君子签测试环境key和接口地址:
     * appKey:dcb4bd535a09df3c
     * appSecret:b87c346edcb4bd535a09df3ca8c45d9a
     * services_url:https://api.sandbox.junziqian.com
     * 开发文档: https://s.junziqian.com/api_doc/index.html
     */
    private static String SERVICE_URL="https://api.sandbox.junziqian.com";
    private static  String APP_KEY="dcb4bd535a09df3c";
    private static  String APP_SECRET="b87c346edcb4bd535a09df3ca8c45d9a";
    @Value("${debug_model}")
    private Boolean debugModel;
 
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
    @PostConstruct//启动初始化
    public int initClient(){
        SERVICE_URL = systemDictDataBiz.queryByCode(Constants.SIGN,Constants.SIGN_URL).getCode();
        APP_KEY = systemDictDataBiz.queryByCode(Constants.SIGN,Constants.SIGN_APPKEY).getCode();
        APP_SECRET = systemDictDataBiz.queryByCode(Constants.SIGN,Constants.SIGN_APPSECRET).getCode();
        initParams();
        return 0;
    }
    //请求的body内参数
    private static  Map<String, Object> bodyParams;
    @Autowired
    public InterfaceLogService interfaceLogService = null;
    private   void saveInterfaceLog(String path,String name,String req ,String res) {
        if(interfaceLogService !=null){
            InterfaceLog hkMonitoryLogDO=new InterfaceLog();
            hkMonitoryLogDO.setType(0);
            hkMonitoryLogDO.setCreateDate(new Date());
            hkMonitoryLogDO.setIsdeleted(0);
            hkMonitoryLogDO.setRequest(req);
            hkMonitoryLogDO.setRepose(res);
            hkMonitoryLogDO.setName(name);
            hkMonitoryLogDO.setUrl(path);
            interfaceLogService.create(hkMonitoryLogDO);
        }
    }
 
    public void initParams(){
        long ts=System.currentTimeMillis()/1000;
        String nonce=DigestUtils.md5Hex(System.currentTimeMillis()+"");
        String sign=DigestUtils.sha256Hex("nonce"+nonce+"ts"+ts+"app_key"+APP_KEY+"app_secret"+APP_SECRET);
        bodyParams=new IdentityHashMap<>();
        bodyParams.put("ts",System.currentTimeMillis()/1000);
        bodyParams.put("app_key",APP_KEY);
        bodyParams.put("sign",sign);
        bodyParams.put("nonce",nonce);//这只只是为了生成一个随机值
    }
 
    /**
     * 1.ping服务
     */
    public boolean ping(){
        try {
            Map<String, Object>  params=bodyParams;
            String url=SERVICE_URL+"/v2/ping";
            //所有参数装入了body中
            String str= HttpClientUtils.init().getPost(url,null,params,false);
            log.info("返回结果为:"+str);
            ResultInfo ri= JSONObject.parseObject(str,ResultInfo.class);
            saveInterfaceLog(url,"【电子签】ping服务",JSONObject.toJSONString(params),str);
            return ri.isSuccess();
        }catch (Exception e){
 
        }
        return false;
    }
 
 
    /**
     * 发起企业认证申请
     * @param fullname
     * @param creditCode
     * @param legalName
     * @param email
     * @param businessimgPath
     * @param notifyUrl
     * @return
     */
    public boolean organizationCreate (String fullname,String creditCode,String legalName,String email,String businessimgPath, String notifyUrl){
        try {
            initParams();
            File  businessimg =netFileToLoclFile(businessimgPath);
            if(businessimg == null || !businessimg.isFile() || !businessimg.exists()){
                return false;
            }
            Map<String, Object>  params=bodyParams;
            String url=SERVICE_URL+"/v2/user/organizationCreate";
            params.put("name",fullname);
            params.put("identificationType",1);
            params.put("organizationRegNo",creditCode);
            params.put("organizationType",0);
            params.put("organizationCode",creditCode);
            params.put("organizationRegImg",businessimg);
            params.put("notifyUrl",notifyUrl);
            params.put("legalName",legalName);
            params.put("emailOrMobile",email);
            String str= HttpClientUtils.init().getPost(url,null,params,true);
            businessimg.delete();
            saveInterfaceLog(url,"【电子签】发起企业认证申请",JSONObject.toJSONString(params),str);
            JSONObject json = JSONObject.parseObject(str);
            if(json!=null && json.getBoolean("success")){
                return  true;
            }
            System.out.println(str);
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
 
    }
    /**
     *重新发起企业认证申请
     * @param fullname
     * @param creditCode
     * @param legalName
     * @param email
     * @param businessimgPath
     * @param notifyUrl
     * @return
     */
    public boolean organizationReApply(String fullname,String creditCode,String legalName,String email,String businessimgPath,String notifyUrl){
        try {
 
            File  businessimg = new File(new URI(businessimgPath));
            if(businessimg == null|| !businessimg.isFile() || !businessimg.exists()){
                return false ;
            }
            initParams();
            Map<String, Object>  params=bodyParams;
            String url=SERVICE_URL+"/v2/user/organizationReapply";
            params.put("name",fullname);
            params.put("identificationType",1);
            params.put("organizationRegNo",creditCode);
            params.put("organizationType",0);
            params.put("organizationCode",creditCode);
            params.put("organizationRegImg",businessimg);
            params.put("notifyUrl",notifyUrl);
            params.put("legalName",legalName);
            params.put("emailOrMobile",email);
            String str= HttpClientUtils.init().getPost(url,null,params,true);
            businessimg.delete();
            saveInterfaceLog(url,"【电子签】重新发起企业认证申请",JSONObject.toJSONString(params),str);
            JSONObject json = JSONObject.parseObject(str);
            if(json!=null && json.getBoolean("success")){
                return  true;
            }
            System.out.println(str);
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
 
    }
 
    /**
     * 网络文件转本地临时文件
     * @param fileUrl
     * @return
     */
    public static File netFileToLoclFile(String fileUrl)  {
 
        try {
            URL url = new URL(fileUrl);
            InputStream inputStream = url.openConnection().getInputStream();
 
            File file = File.createTempFile( "temp",""+UUID.randomUUID().toString()+fileUrl.substring(fileUrl.lastIndexOf("."))); // 指定保存到本地的文件名及路径
            OutputStream outputStream = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
 
            outputStream.close();
            inputStream.close();
            return file;
        }catch (Exception e){
 
        }
        return null;
    }
    /**
     * 查询企业签约状态 审核状态,0正在申请1通过2驳回
     * @param email
     * @return
     */
    public int  organizationAuditStatus (String email){
        try {
            initParams();
            Map<String, Object>  params=bodyParams;
            String url=SERVICE_URL+"/v2/user/organizationAuditStatus";
            params.put("emailOrMobile",email);
            String str= HttpClientUtils.init().getPost(url,null,params,true);
            System.out.println(str);
            saveInterfaceLog(url,"【电子签】查询企业签约状态",JSONObject.toJSONString(params),str);
            JSONObject json = JSONObject.parseObject(str);
            if(json!=null && json.getBoolean("success")){
                return  json.getJSONObject("data").getIntValue("status");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  -1;//查询失败
    }
 
    /**
     * 发起签约
     * @param name
     * @param fileUrl
     * @param fullname
     * @param creditCoe
     * @param email
     * @param postionJson
     */
    public String applySignWidthQifengSet(String name,String fileUrl,String fullname,String creditCoe,String email,String postionJson,String singId,String notifyUrl,Float qifengSet){
      return  applySignDoByParam(name,fileUrl,fullname,creditCoe,email,postionJson,singId,notifyUrl,qifengSet);
    }
    /**
     * 发起签约
     * @param name
     * @param fileUrl
     * @param fullname
     * @param creditCoe
     * @param email
     * @param postionJson
     */
    public String applySign(String name,String fileUrl,String fullname,String creditCoe,String email,String postionJson,String singId,String notifyUrl,Float qifengSet){
      return  applySignDoByParam(name,fileUrl,fullname,creditCoe,email,postionJson,singId,notifyUrl,null);
    }
    /**
     * 发起签约
     * @param name
     * @param fileUrl
     * @param fullname
     * @param creditCoe
     * @param email
     * @param postionJson
     */
    public String applySignDoByParam(String name,String fileUrl,String fullname,String creditCoe,String email,String postionJson,String singId,String notifyUrl,Float qifengSet){
      try {
          initParams();
          String url=SERVICE_URL+"/v2/sign/applySign";
          Map<String, Object>  params=bodyParams;
          params.put("contractName",name); //合同名称
          params.put("serverCa",1); //使用云证书
//          params.put("file",file);
          params.put("url",fileUrl);
          params.put("dealType",5); //指定合同文件签署方式 5 为部分自动签
//          params.put("positionType",2); //指定通过表单域方式设置签字位置
          params.put("positionType",0); //指定公章位置类型:0或null使用签字座标位置或不指定签字位置;1表单域定位(表单域如果上传为pdf时,需pdf自行定义好表单域,html及url及tmpl等需定义好input标签);2关键字定义
          params.put("fileType",1);
          params.put("needQifengSign",1);
          params.put("notifyUrl",notifyUrl);
          JSONArray signatories=new JSONArray();
          SignatoryReq sReq=new SignatoryReq();
          sReq.setFullName(fullname); //企业姓名
          sReq.setIdentityType(11); //证件类型
          sReq.setIdentityCard(creditCoe);//营业执照号
          sReq.setEmail(email); //在君子签注册认证的邮箱
//        sReq.setChapteJson("[{\"page\":0,\"chaptes\":[{\"offsetX\":0.12,\"offsetY\":0.23}]},{\"page\":1,\"chaptes\":[{\"offsetX\":0.45,\"offsetY\":0.67}]}]");
//          sReq.setChapteJson(postionJson);//表单域定位 对应positionType=1
//          sReq.setSearchKey(postionJson);//关键字 对应positionType=2
          sReq.setSignId(singId);
          sReq.setNoNeedVerify(1);
          sReq.setQiFengOffset(qifengSet);
          signatories.add(sReq);
          params.put("signatories",signatories.toJSONString());
          System.out.println("================"+JSONObject.toJSONString(params));
          String str= HttpClientUtils.init().getPost(url,null,params,true);
          System.out.println(str);
          saveInterfaceLog(url,"【电子签】发起签约",JSONObject.toJSONString(params),str);
          JSONObject json = JSONObject.parseObject(str);
          if(json!=null && json.getBoolean("success")){
              return  json.getString("data");
          }
      }catch (Exception e){
          e.printStackTrace();
      }
      return null;
 
    }
    /**
     * 发起签约
     * @param name
     * @param file
     * @param creditCoe
     * @param email
     * @param postionJson
     */
    public String applySignLocalFile(String name,String fullname,String file,String creditCoe,String email,String postionJson,String singId,String notifyUrl){
      try {    initParams();
          String url=SERVICE_URL+"/v2/sign/applySign";
          Map<String, Object>  params=bodyParams;
          params.put("contractName",name); //合同名称
          params.put("serverCa",1); //使用云证书
          params.put("file",new File(file));
          params.put("dealType",5); //指定合同文件签署方式 5 为部分自动签
          params.put("positionType",0); //指定通过表单域方式设置签字位置
          params.put("fileType",0);
          params.put("needQifengSign",1);
          params.put("notifyUrl",notifyUrl);
          JSONArray signatories=new JSONArray();
          SignatoryReq sReq=new SignatoryReq();
          sReq.setFullName(fullname); //企业姓名
          sReq.setIdentityType(11); //证件类型
          sReq.setIdentityCard(creditCoe);//营业执照号
          sReq.setEmail(email); //在君子签注册认证的邮箱
//        sReq.setChapteJson("[{\"page\":0,\"chaptes\":[{\"offsetX\":0.12,\"offsetY\":0.23}]},{\"page\":1,\"chaptes\":[{\"offsetX\":0.45,\"offsetY\":0.67}]}]");
//          sReq.setChapteJson(postionJson);
//          sReq.setSearchKey(postionJson);
          sReq.setSignId(singId);
          sReq.setSearchConvertExtend("{\"fixX\":-80,\"fixY\":-80}");
          sReq.setNoNeedVerify(1);
          signatories.add(sReq);
          params.put("signatories",signatories.toJSONString());
          System.out.println(signatories.toJSONString());
          String str= HttpClientUtils.init().getPost(url,null,params,true);
          System.out.println(str);
          saveInterfaceLog(url,"【电子签】发起签约",JSONObject.toJSONString(params),str);
          JSONObject json = JSONObject.parseObject(str);
          if(json!=null && json.getBoolean("success")){
              return  json.getString("data");
          }
      }catch (Exception e){
          e.printStackTrace();
 
      }
      return null;
 
    }
 
 
    /**
     * 获取签署链接地址(签署页面地址)
     * @param applyNo
     * @param name
     * @param creditCode
     */
    public String signLink(String applyNo,String name,String creditCode) {
        try {
            //延迟2秒
//            Thread.sleep(2*1000);
            initParams();
            Map<String, Object> params = bodyParams;
            String url = SERVICE_URL + "/v2/sign/link";
            params.put("applyNo",applyNo); //发起合同签署接口返回的APL编号
            params.put("fullName",name); //发起合同签署接口需要手动签署对象的姓名
            params.put("identityCard",creditCode); //发起合同签署接口中需要手动签署对象的证件号
            params.put("identityType",11); //证件类型,个人1 ,企业11
            String str= HttpClientUtils.init().getPost(url,null,params,true);
            System.out.println(str);
            saveInterfaceLog(url,"【电子签】获取签署链接地址",JSONObject.toJSONString(params),str);
            JSONObject json = JSONObject.parseObject(str);
            if(json!=null && json.getBoolean("success")){
                String link =json.getString("data");
                if(!debugModel){
                    link = link.replace("https://h5","https://h7");
                }
                return link;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }
    /**
     * 获取签署链接地址(返回文件地址,签署后才是文件地址,否则跳转到签署页面)
     * @param applyNo
     */
    public String  linkFileStatus(String applyNo) {
 
        try {    initParams();
            Map<String, Object> params = bodyParams;
            String url = SERVICE_URL + "/v2/sign/status";
            //构建请求参数
            params.put("applyNo",applyNo); //发起合同签署接口返回的APL编号
//            params.put("fullName",fullName); //签约人名称
//            params.put("identityCard",identityCard); //签约人证件号
//            params.put("identityType",12); //证件类型 1身份证, 2护照, 3台胞证, 4港澳居民来往内地通行证, 11营业执照, 12统一社会信用代码, 20子账号, 99其他
            String str= HttpClientUtils.init().getPost(url,null,params,true);
            System.out.println(str);
            saveInterfaceLog(url,"【电子签】签约状态查询",JSONObject.toJSONString(params),str);
            JSONObject json = JSONObject.parseObject(str);
            if(json!=null && json.getBoolean("success")){
                //0 :未签 1 :已签 2:拒签 3:已保全
                return  json.getString("data");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }
    /**
     * 获取签署链接地址(返回文件地址,签署后才是文件地址,否则跳转到签署页面)
     * @param applyNo
     */
    public String  linkFile(String applyNo) {
 
        try {    initParams();
            Map<String, Object> params = bodyParams;
            String url = SERVICE_URL + "/v2/sign/linkFile";
            //构建请求参数
            params.put("applyNo",applyNo); //发起合同签署接口返回的APL编号
            String str= HttpClientUtils.init().getPost(url,null,params,true);
            System.out.println(str);
            saveInterfaceLog(url,"【电子签】获取签署链接地址",JSONObject.toJSONString(params),str);
            JSONObject json = JSONObject.parseObject(str);
            if(json!=null && json.getBoolean("success")){
                return  json.getString("data");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }
    /**
     * 上传企业自定义印章(返回印章ID)
     * @param signName
     * @param path
     */
    public String  uploadEntSign(String signName,String path,String email ) {
 
        try {    initParams();
            File signImgFile =netFileToLoclFile(path);
            Map<String, Object> params = bodyParams;
            String url = SERVICE_URL + "/v2/user/uploadEntSign";
            //构建请求参数
            params.put("signName",signName);
            params.put("signImgFile",signImgFile);
            params.put("email",email);
            String str= HttpClientUtils.init().getPost(url,null,params,true);
            System.out.println(str);
            saveInterfaceLog(url,"【电子签】上传企业自定义印章",JSONObject.toJSONString(params),str);
            JSONObject json = JSONObject.parseObject(str);
            if(json!=null && json.getBoolean("success")){
                return  json.getString("data");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }
    /**
     * 删除企业自定义印章
     * @param signId
     */
    public boolean  deleteEntSign(String signId ) {
 
        try {    initParams();
            Map<String, Object> params = bodyParams;
            String url = SERVICE_URL + "/v2/user/deleteEntSign";
            //构建请求参数
            params.put("signId",signId);
            String str= HttpClientUtils.init().getPost(url,null,params,true);
            System.out.println(str);
            saveInterfaceLog(url,"【电子签】删除企业自定义印章",JSONObject.toJSONString(params),str);
            JSONObject json = JSONObject.parseObject(str);
            if(json!=null && json.getBoolean("success")){
                return  json.getBoolean("success");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  false;
    }
 
}