doum
9 天以前 0201c32312f6478b2bde706607c8c6338e9e1d06
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
package com.doumee.core.device;
 
import cn.emay.sdk.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.doumee.core.device.model.*;
import com.doumee.core.device.model.request.*;
import com.doumee.core.device.model.response.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
 
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.*;
 
@Slf4j
public class ElectronicToolUtil {
    /**
     * 电表添加采集器
     */
    public static ElectronicBaseResponse collectorAdd(List<CollectorAddRequest> param){
        if(param ==null || param.size()==0){
            return null;
        }
        String url = ElectronicConstant.api2_url+"/Api_v2/collector/add";
        try {
            String request_content = JSON.toJSONString(param);
            String r = request(url, request_content);
            TypeReference typeReference =
                    new TypeReference< ElectronicBaseResponse<List<CollectorDealResponse>> >(){};
            ElectronicBaseResponse  result = JSONObject.parseObject(r, typeReference.getType());
            return  result;
        }catch (Exception e){
            log.error("电表==============添加采集器",e);
        }
        return null;
    }
    /**
     * 删除采集器
     */
    public static ElectronicBaseResponse  collectorDelete(List<CollectorAddRequest> param){
        if(param ==null || param.size()==0){
            return null;
        }
        String url = ElectronicConstant.api2_url+"/Api_v2/collector/delete";
        try {
            String request_content = JSON.toJSONString(param);
            String r = request(url, request_content);
            TypeReference typeReference =
                    new TypeReference< ElectronicBaseResponse<List<CollectorDealResponse>> >(){};
            ElectronicBaseResponse  result = JSONObject.parseObject(r, typeReference.getType());
            return  result;
        }catch (Exception e){
            log.error("电表==============删除采集器",e);
        }
        return null;
    }
    /**
     * 更新采集器
     */
    public static ElectronicBaseResponse collectorUpdate(List<CollectorUpdateRequest> param){
        if(param ==null || param.size()==0){
            return null;
        }
        String url = ElectronicConstant.api2_url+"/Api_v2/collector/renew";
        try {
            String request_content = JSON.toJSONString(param);
            String r = request(url, request_content);
            TypeReference typeReference =
                    new TypeReference< ElectronicBaseResponse<List<CollectorUpdateResponse>> >(){};
            ElectronicBaseResponse  result = JSONObject.parseObject(r, typeReference.getType());
            return  result;
        }catch (Exception e){
            log.error("电表==============更新采集器",e);
        }
        return null;
    }
 
    /**
     * 电能表添加
     */
    public static ElectronicBaseResponse eleMeterAdd(List<MeterAddRequest> param) {
        if(param ==null || param.size()==0){
            return null;
        }
        String url = ElectronicConstant.api2_url+"/Api_v2/ele_meter/add";
        try {
            String request_content = JSON.toJSONString(param);
            String r = request(url, request_content);
            TypeReference typeReference =
                    new TypeReference< ElectronicBaseResponse<List<MeterAddResponse>> >(){};
            ElectronicBaseResponse  result = JSONObject.parseObject(r, typeReference.getType());
            return  result;
        }catch (Exception e){
            log.error("电表==============新增电表",e);
        }
        return null;
    }
    /**
     * 电能表更新
     */
    public static ElectronicBaseResponse  eleMeterUpdate(List<MeterUpdateRequest> param) {
        if(param ==null || param.size()==0){
            return null;
        }
        String url = ElectronicConstant.api2_url+"/Api_v2/ele_meter/renew";
        try {
            String request_content = JSON.toJSONString(param);
            String r = request(url, request_content);
            TypeReference typeReference =
                    new TypeReference< ElectronicBaseResponse<List<MeterAddResponse>> >(){};
            ElectronicBaseResponse  result = JSONObject.parseObject(r, typeReference.getType());
            return  result;
        }catch (Exception e){
            log.error("电表==============新增电表",e);
        }
        return null;
    }
    /**
     * 电能表删除
     */
    public static ElectronicBaseResponse  eleMeterDelete(List<MeterAddRequest> param) {
        if(param ==null || param.size()==0){
            return null;
        }
        String url = ElectronicConstant.api2_url+"/Api_v2/ele_meter/delete";
        try {
            String request_content = JSON.toJSONString(param);
            String r = request(url, request_content);
            TypeReference typeReference =
                    new TypeReference< ElectronicBaseResponse<List<MeterAddResponse>> >(){};
            ElectronicBaseResponse  result = JSONObject.parseObject(r, typeReference.getType());
            return  result;
        }catch (Exception e){
            log.error("电表==============删除电表",e);
        }
        return null;
    }
    public static void main(String[] args) {
        QueryDataRequest param = new QueryDataRequest();
        param.setStart_time("2026-05-25 10:00::00");
        param.setEnd_time("2026-05-25 14:24:25");
        param.setLimit(1);
        param.setFunctionids("253");
        param.setOffset(0);
        queryDataRequest(param );
 
    }
    /**
     * 电表开户_表清零
     * 电表清零接口用于电表的清零操作,切换付费模式,通过该接口可以将电表的计量数据,余额清零,使得电表的计量数据重新从0开始。
     * 注意 预付费电表 清零时会 拉闸
     * 新预付费表开始启用时都要进行清零、开户、充值
     */
    public static ElectronicBaseResponse   eleSecurityReset(List<SecurityResetRequest> param) {
        if(param ==null || param.size()==0){
            return null;
        }
        String url = ElectronicConstant.api2_url+"/Api_v2/ele_security/reset";
 
        try {
            String request_content = JSON.toJSONString(param);
            String r = requestAsync(url, request_content);
            return parseAsyncMeterResponse(r);
        }catch (Exception e){
            log.error("电表==============开户",e);
        }
        return null;
    }
    /**
     * 电表开户_同步模式m
     */
    public static ElectronicBaseResponse   openAcount(List<OpenAccountRequest> param) {
        if(param ==null || param.size()==0){
            return null;
        }
        String url = ElectronicConstant.api2_url+"/Api_v2/ele_security/open_acount";
 
        try {
            String request_content = JSON.toJSONString(param);
            String r = requestAsync(url, request_content);
            return parseAsyncMeterResponse(r);
        }catch (Exception e){
            log.error("电表==============开户",e);
        }
        return null;
    }
    /**
     * 电表_电表充值
     */
    public static ElectronicBaseResponse   recharger(List<OpenAccountRequest> param) {
        if(param ==null || param.size()==0){
            return null;
        }
        String url = ElectronicConstant.api2_url+"/Api_v2/ele_security/recharge";
 
        try {
            String request_content = JSON.toJSONString(param);
            String r = requestAsync(url, request_content);
            return parseAsyncMeterResponse(r);
        }catch (Exception e){
            log.error("电表==============充值",e);
        }
        return null;
    }
    /**
     * 电表_拉合闸
     */
    public static ElectronicBaseResponse   eleControl(List<OpenAccountRequest> param) {
        if(param ==null || param.size()==0){
            return null;
        }
        String url = ElectronicConstant.api2_url+"/Api_v2/ele_control";
 
        try {
            String request_content = JSON.toJSONString(param);
            String r = requestAsync(url, request_content);
            return parseAsyncMeterResponse(r);
        }catch (Exception e){
            log.error("电表==============拉合闸",e);
        }
        return null;
    }
    /**
     * 查询异步操作任务状态(/Api_v2/request/status)。
     * request_content 为 JSON 数组,可合并传入多个 opr_id 一次查询。
     * 调用成功后建议 30s 首次查询,后续间隔递增(1h、2h、4h…),禁止频繁调用。
     * 响应 SUCCESS、FAIL、TIMEOUT、CANCELED、RESPONSE_FAIL 后无需再查询。
     */
    public static ElectronicBaseResponse requestStatus(List<RequestStatusRequest> param) {
        if (param == null || param.isEmpty()) {
            return null;
        }
        String url = ElectronicConstant.api2_url + "/Api_v2/request/status";
        try {
            String request_content = JSON.toJSONString(param);
            String r = request(url, request_content);
            return parseAsyncMeterResponse(r);
        } catch (Exception e) {
            log.error("电表==============查询异步任务状态", e);
        }
        return null;
    }
 
    /**
     * 按 opr_id 列表合并查询异步任务状态。
     */
    public static ElectronicBaseResponse requestStatusByOprIds(List<String> oprIds) {
        if (oprIds == null || oprIds.isEmpty()) {
            return null;
        }
        List<RequestStatusRequest> param = new ArrayList<>();
        for (String oprId : oprIds) {
            if (StringUtils.isNotBlank(oprId)) {
                RequestStatusRequest req = new RequestStatusRequest();
                req.setOpr_id(oprId.trim());
                param.add(req);
            }
        }
        return param.isEmpty() ? null : requestStatus(param);
    }
 
    /**
     * 查询单个异步操作任务状态。
     */
    public static ElectronicBaseResponse requestStatus(String oprId) {
        if (StringUtils.isBlank(oprId)) {
            return null;
        }
        RequestStatusRequest req = new RequestStatusRequest();
        req.setOpr_id(oprId.trim());
        return requestStatus(Collections.singletonList(req));
    }
 
    /**
     * 异步任务是否已到达终态,平台后续不再处理,无需再轮询。
     */
    public static boolean isAsyncStatusFinal(String status) {
        if (StringUtils.isBlank(status)) {
            return false;
        }
        String s = status.trim().toUpperCase(Locale.ROOT);
        return "SUCCESS".equals(s) || "FAIL".equals(s) || "TIMEOUT".equals(s)
                || "CANCELED".equals(s) || "RESPONSE_FAIL".equals(s);
    }
 
    /**
     * 电表_立即抄表
     */
    public static ElectronicBaseResponse   eleRead(List<EleReadRequest> param) {
        if(param ==null || param.size()==0){
            return null;
        }
        String url = ElectronicConstant.api2_url+"/Api_v2/ele_read";
 
        try {
            String request_content = JSON.toJSONString(param);
            String r = requestAsync(url, request_content);
            return parseAsyncMeterResponse(r);
        }catch (Exception e){
            log.error("电表==============立即抄表",e);
        }
        return null;
    }
    /**
     * Api v1 数据类接口(/Api/Meter、/Api/Collector 等)成功标识:文档为 status=1,少数返回 SUCCESS
     */
    public static boolean isDataApiSuccess(ElectronicDataResponse<?> response) {
        if (response == null || StringUtils.isBlank(response.getStatus())) {
            return false;
        }
        String status = response.getStatus().trim();
        return "1".equals(status) || "SUCCESS".equalsIgnoreCase(status);
    }
 
    public static String dataApiErrorMessage(ElectronicDataResponse<?> response, String defaultMsg) {
        if (response != null && StringUtils.isNotBlank(response.getError_msg())) {
            return response.getError_msg();
        }
        if (response != null && StringUtils.isNotBlank(response.getStatus())) {
            return defaultMsg + "(status=" + response.getStatus() + ")";
        }
        return defaultMsg;
    }
 
    /**
     * 电表_查询设备列表和当前状态
     */
    public static ElectronicDataResponse   meterList( ) {
 
        String url = ElectronicConstant.api_url+"/Api/Meter";
 
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("auth",ElectronicConstant.auth_code);
            String urlParams = getUrlParams(params);
            url = url + "?" + urlParams;
//            String request_content = JSON.toJSONString(param);
            String r = HttpClientGet(url);
            log.info("meterList=========================:"+r);
            TypeReference typeReference =
                    new TypeReference< ElectronicDataResponse<List<MeterInfoResponse>> >(){};
            ElectronicDataResponse  result = JSONObject.parseObject(r, typeReference.getType());
            return  result;
        }catch (Exception e){
            log.error("电表==============查询设备列表和当前状态",e);
        }
        return null;
    }
    /**
     * 电表_查询采集器列表和当前状态
     */
    public static ElectronicDataResponse   collectorStatus( ) {
 
        String url = ElectronicConstant.api_url+"/Api/Collector";
 
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("auth",ElectronicConstant.auth_code);
            String urlParams = getUrlParams(params);
            url = url + "?" + urlParams;
//            String request_content = JSON.toJSONString(param);
            String r = HttpClientGet(url);
            log.info("meterList=========================:"+r);
            TypeReference typeReference =
                    new TypeReference< ElectronicDataResponse<List<CollectorStatusResponse>> >(){};
            ElectronicDataResponse  result = JSONObject.parseObject(r, typeReference.getType());
            return  result;
        }catch (Exception e){
            log.error("电表==============查询设备列表和当前状态",e);
        }
        return null;
    }
    /**
     * 电表_查询参数档案
     */
    public static ElectronicDataResponse   paramList() {
 
        String url = ElectronicConstant.api_url+"/Api/Param";
 
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("auth",ElectronicConstant.auth_code);
            String urlParams = getUrlParams(params);
            url = url + "?" + urlParams;
            String r = HttpClientGet(url);
            log.error("paramList=========================:"+r);
            TypeReference typeReference =
                    new TypeReference< ElectronicDataResponse<List<ParamInfoResponse>> >(){};
            ElectronicDataResponse  result = JSONObject.parseObject(r, typeReference.getType());
            return  result;
        }catch (Exception e){
            log.error("电表==============查询参数档案",e);
        }
        return null;
    }
 
    /**
     * 查询历史数据接口
     */
    public static ElectronicDataResponse queryDataRequest() {
        return queryDataRequest(null);
    }
 
    /**
     * 查询历史数据接口
     */
    public static ElectronicDataResponse queryDataRequest(QueryDataRequest param) {
        // 查询历史数据接口
        try {
            String url =ElectronicConstant.api_url+"/Api/DataRequest";
            Map<String, Object> params = new HashMap<>();
            params.put("auth",         ElectronicConstant.auth_code);
            if(param!=null){
                params.put("type",         param.getType());
                params.put("functionids",  param.getFunctionids());
                params.put("start_time",   param.getStart_time());
                params.put("end_time",     param.getEnd_time());
                params.put("offset",        param.getOffset());
                params.put("limit",         param.getLimit());
            }
            String urlParams = getUrlParams(params);
            url = url + "?" + urlParams;
            String r = HttpClientGet(url);
            log.error("查询历史数据接口=============:"+r);
            TypeReference typeReference =
                    new TypeReference< ElectronicDataResponse<List<QueryDataInfoResponse>> >(){};
            ElectronicDataResponse  result = JSONObject.parseObject(r, typeReference.getType());
            return  result;
        }catch (Exception e){
            log.error("电表==============查询历史数据接口",e);
        }
        return null;
 
    }
    /**
     * 报警信息查询接口
     */
    public static ElectronicDataResponse warningList(WarningListRequest param) {
        // 查询历史数据接口
        try {
            String url =ElectronicConstant.api_url+"/Api/Warning";
            Map<String, Object> params = new HashMap<>();
            params.put("auth",         ElectronicConstant.auth_code);
            if(param!=null){
                params.put("device_type", param.getDevice_type());
                params.put("warning_def_id",  param.getWarning_def_id());
            }
            String urlParams = getUrlParams(params);
            url = url + "?" + urlParams;
            String r = HttpClientGet(url);
            log.error("报警信息查询接口========:"+r);
            TypeReference typeReference =
                    new TypeReference< ElectronicDataResponse<List<WarningInfoResponse>> >(){};
            ElectronicDataResponse  result = JSONObject.parseObject(r, typeReference.getType());
            return  result;
        }catch (Exception e){
            log.error("电表==============报警信息查询接口",e);
        }
        return null;
    }
    public static void simpleRequestPrint(String url, String resp) {
        String name=Thread.currentThread().getStackTrace()[2].getMethodName();
        System.out.println(name);
 
        System.out.println("请求参数:" + url);
        System.out.println("返回数据:" + resp);
    }
 
    public static String HttpClientGet(String url) {
        try {
            CloseableHttpClient client = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(url);
            CloseableHttpResponse Response = client.execute(httpGet);
            HttpEntity entity = Response.getEntity();
            String resp = EntityUtils.toString(entity, "UTF-8");
            Response.close();
            return  resp;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return "";
        }
    }
 
 
 
    public static String getUrlParams(Map<String, Object> map) {
        if (map == null || map.size() == 0) {
            return "";
        }
        List<String> list = new ArrayList<>();
        map.forEach((key, value)->{
            try {
                if(value!=null && StringUtils.isNotBlank(value.toString())){
                    list.add(key + "=" + URLEncoder.encode(value.toString(), "UTF-8"));
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        });
        return String.join("&", list);
    }
 
 
 
    public static void testApiAsync(String url, String request_content)
    {
        String name=Thread.currentThread().getStackTrace()[2].getMethodName();
        System.out.println(name);
 
        String response = requestAsync(url, request_content);
        printResponse(response);
    }
 
    // 打印响应内容
    /**
     * 解析 Api_v2 异步电表操作响应。
     * 平台 response_content 可能是 JSON 数组,也可能是字符串形式的 JSON 数组。
     */
    public static ElectronicBaseResponse parseAsyncMeterResponse(String r) {
        if (StringUtils.isBlank(r)) {
            return null;
        }
        try {
            JSONObject jsonObject = JSON.parseObject(r);
            if (jsonObject == null) {
                return null;
            }
            ElectronicBaseResponse<List<MeterDealResponse>> result = new ElectronicBaseResponse<>();
            result.setStatus(jsonObject.getString("status"));
            result.setTimestamp(jsonObject.getString("timestamp"));
            result.setError_msg(jsonObject.getString("error_msg"));
            result.setSign(jsonObject.getString("sign"));
            result.setResponse_content(parseResponseContentList(jsonObject.get("response_content"), MeterDealResponse.class));
            return result;
        } catch (Exception e) {
            log.error("parse async meter response failed, raw={}", r, e);
            return null;
        }
    }
 
    private static <T> List<T> parseResponseContentList(Object content, Class<T> clazz) {
        if (content == null) {
            return null;
        }
        if (content instanceof JSONArray) {
            return ((JSONArray) content).toJavaList(clazz);
        }
        if (content instanceof JSONObject) {
            T one = ((JSONObject) content).toJavaObject(clazz);
            return one != null ? Collections.singletonList(one) : null;
        }
        String text = String.valueOf(content);
        if (StringUtils.isBlank(text) || "null".equalsIgnoreCase(text.trim())) {
            return null;
        }
        text = text.trim();
        if (text.startsWith("[")) {
            return JSON.parseArray(text, clazz);
        }
        if (text.startsWith("{")) {
            T one = JSON.parseObject(text, clazz);
            return one != null ? Collections.singletonList(one) : null;
        }
        return null;
    }
 
    public static void printResponse(String response) {
        JSONObject jsonObject = JSON.parseObject(response);
 
        String status = jsonObject.getString("status");
        if(!"SUCCESS".equals(status)) {
            System.out.println(jsonObject.getString("error_msg"));
        } else {
            String response_content = jsonObject.getString("response_content");
            System.out.println("response_content: " + response_content);
            JSONArray contentArray = JSON.parseArray(response_content);
            int index = 1;
            System.out.println("返回结果:");
            for(int i = 0; i < contentArray.size(); ++i) {
                System.out.println("[" + index++ + "]");
                JSONObject contentObject = contentArray.getJSONObject(i);
                Set<String> keySet = contentObject.keySet();
                for(String key: keySet) {
                    System.out.println(key + ": " + contentObject.get(key));
                }
 
            }
        }
    }
 
    // 请求接口
    public static  String request(String url, String request_content ){
        // 时间戳
        String timestamp = String.valueOf(new Date().getTime()/1000);
 
        // 用于签名的内容
        Map<String, String> data = new HashMap<>();
        data.put("timestamp", timestamp);
        data.put("auth_code",  ElectronicConstant.auth_code);
        data.put("request_content", request_content);
 
        // 获取签名
        String sign = getSign(data);
 
        data.put("sign", sign);
 
        try {
            String r =  sendHttpRequest(url, data);
            return r;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
    // 请求接口
    public static String requestAsync(String url, String request_content){
        // 时间戳
        String timestamp = String.valueOf(new Date().getTime()/1000);
 
        // 用于签名的内容
        Map<String, String> data = new HashMap<>();
        data.put("timestamp", timestamp);
        data.put("auth_code", ElectronicConstant.auth_code);
        data.put("request_content", request_content);
        data.put("notify_url", ElectronicConstant.notify_url);
 
        // 获取签名
        String sign = getSign(data);
 
        data.put("sign", sign);
 
        try {
            return sendHttpRequest(url, data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
    // 生成签名字符串
    public static String getSign(Map<String, String> data)
    {
        // 获取关键字列表
        List<String> keys = new ArrayList<>(data.keySet());
        // 关键字列表排序
        keys.sort(Comparator.naturalOrder());
        StringBuilder sb = new StringBuilder();
        for (String key : keys)
        {
            // 取各个字段内容拼接字符串
            sb.append(data.get(key));
        }
        // 加上双方约定随机字符串
        String txt = sb.toString() + ElectronicConstant.nonce;
 
        // 计算哈希值
        return getMD5(txt);
    }
 
    public static boolean verifyNotifySign(String responseContent, String timestamp, String sign) {
        if (StringUtils.isBlank(responseContent) || StringUtils.isBlank(timestamp) || StringUtils.isBlank(sign)) {
            return false;
        }
        Map<String, String> withAuth = new HashMap<>();
        withAuth.put("auth_code", ElectronicConstant.auth_code);
        withAuth.put("response_content", responseContent);
        withAuth.put("timestamp", timestamp);
        if (sign.equalsIgnoreCase(getSign(withAuth))) {
            return true;
        }
        Map<String, String> withoutAuth = new HashMap<>();
        withoutAuth.put("response_content", responseContent);
        withoutAuth.put("timestamp", timestamp);
        return sign.equalsIgnoreCase(getSign(withoutAuth));
    }
 
    // md5加密
    public static String getMD5(String password) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        byte[] byteArray = password.getBytes(StandardCharsets.UTF_8);
 
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuilder hexValue = new StringBuilder();
        for (byte md5Byte : md5Bytes) {
            int val = ((int) md5Byte) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
 
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }
 
    // 发送http请求
    public static String sendHttpRequest(String url, Map<String, String> bodyMap) throws Exception {
        System.out.println("请求地址:" + url);
        System.out.println("发送参数:" + bodyMap.toString());
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost postRequest = new HttpPost(url);
 
        List<NameValuePair> nvps = new ArrayList<>();
 
        for(String key : bodyMap.keySet()) {
            nvps.add(new BasicNameValuePair(key,bodyMap.get(key)));
        }
        postRequest.setEntity(new UrlEncodedFormEntity(nvps,"utf-8"));
 
 
        int retry = 3;
        HttpResponse execute = null;
        while(retry-- > 0) {
            try {
                execute = client.execute(postRequest);
                break;
            } catch (Exception e) {
                Thread.sleep(5000);
            }
        }
        if(execute == null) {
            throw new Exception("接口请求失败");
        }
        String resp = EntityUtils.toString(execute.getEntity(), "UTF-8");
        System.out.println("接口返回:" + resp);
        return resp;
    }
 
}