Pu Zhibing
4 天以前 db0b7644a9a5a62ac2da3cf571fee41bb8b6974f
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
package com.ruoyi.jianguan.elutong.controller;
 
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.jianguan.elutong.config.AppServerConfig;
import com.ruoyi.jianguan.elutong.core.AESTools;
import com.ruoyi.jianguan.elutong.core.CommonRequest;
import com.ruoyi.jianguan.elutong.core.CommonResponse;
import com.ruoyi.jianguan.elutong.core.SignatureTools;
import com.ruoyi.integration.api.elutong.model.ConnectorStatusReq;
import com.ruoyi.integration.api.elutong.model.StationInfoReq;
import com.ruoyi.integration.api.elutong.model.StationsStatusReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
 
@Slf4j
@RestController
@RequestMapping("/elutong")
public class SampleController {
 
    @Autowired
    private AppServerConfig appServerConfig;
    @Autowired
    private RestTemplate restTemplate;
 
    /**
     * 签名
     *
     * @param request
     * @param privateKey
     * @return
     */
    public static CommonRequest sign(CommonRequest request, String privateKey) {
        String signStr = SignatureTools.buildSignStr(request);
        signStr = signStr + "&filename=" + request.getFilename();
        log.info("【E路通】signStr : {}", signStr);
        String sign = SignatureTools.rsa256Sign(signStr, privateKey);
        request.setSign(sign);
        return request;
    }
 
    /**
     * 验签
     *
     * @param response
     * @return
     */
    public boolean verifySign(CommonResponse response) {
        String signStr = SignatureTools.buildSignStr(response);
        log.info("【E路通】signStr : {}", signStr);
        log.info("【E路通】sign : {}", response.getSign());
        return SignatureTools.rsa256Verify(signStr, appServerConfig.getSignPub(), response.getSign());
    }
 
 
 
    @GetMapping("/test")
    public String test() {
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String filename = "CHARGE_DATA_STATIONINFO_REQ"+"_"+appServerConfig.getAppId()+"_"+format.format(date)+".json";
        //String filename = "SAIS_SERVICEAREA_LPAGE_REQ" + "_" + appServerConfig.getAppId() + "_" + format.format(date) + ".json";
 
 
        CommonRequest req = new CommonRequest();
        req.setEncryptType("AES");
        req.setSignType("RSA256");
        req.setFilename(filename);
        format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        req.setTimestamp(format.format(date));
 
        String requestJson = "{\n" +
        "\"operatorId\":\"91530xxxxK3LK15Y\",\n" +
                "\"operatorInfo\":{\n" +
                "\"operatorId\":\"91530xxxxK3LK15Y\",\n" +
                "\"operatorName\":\"xxx产业发展有限公司\",\n" +
                "\"operatorTel1\":\"0871-61408801\",\n" +
                "\"operatorTel2\":\"0871-61408801\",\n" +
                "\"operatorRegAddress\":\"xxx西山区前卫街道前福路231号\",\n" +
                "\"operatorNote\":null\n" +
                "},\n" +
                "\"stationId\":\"G56-111111-220-2-02819-0-CD-01025001\",\n" +
                "\"serAreaCode\":\"G56-111111-220-2-02819-0\",\n" +
                "\"itemSize\":1,\n" +
                "\"stationInfos\":[\n" +
                "{\n" +
                "\"serAreaCode\":\"G56-111111-220-2-02819-0\",\n" +
                "\"stationId\":\"G56-111111-220-2-02819-0-CD-01025001\",\n" +
                "\"stationMode\":\"CD\",\n" +
                "\"operatorId\":\"915300016K3LK15Y\",\n" +
                "\"stationName\":\"黄草xxx车区上行\",\n" +
                "\"areaCode\":\"510100\",\n" +
                "\"serviceTel\":\"0871-68408801\",\n" +
                "\"stationType\":1,\n" +
                "\"stationStatus\":50,\n" +
                "\"parkNums\":2,\n" +
                "\"stationLng\":98.7116251,\n" +
                "\"stationLat\":24.6117291,\n" +
                "\"siteGuide\":null,\n" +
                "\"construction\":11,\n" +
                "\"pictures\":null,\n" +
                "\"matchCars\":null,\n" +
                "\"businessHours\":\"周一至周日00:00-24:00\",\n" +
                "\"electricityFee\":null,\n" +
                "\"serviceFee\":null,\n" +
                "\"parkFee\":null,\n" +
                "\"payment\":null,\n" +
                "\"supportOrder\":null,\n" +
                "\"equipmentInfos\":[\n" +
                "{\n" +
                "\"equipmentId\":\"02025001\",\n" +
                "\"manufacturerId\":\"11\",\n" +
                "\"manufacturerName\":null,\n" +
                "\"equipmentModel\":null,\n" +
                "\"productionDate\":null,\n" +
                "\"equipmentType\":1,\n" +
                "\"connectorInfos\":[\n" +
                "{\n" +
                "\"connectorId\":\"0202500101\",\n" +
                "\"connectorName\":null,\n" +
                "\"connectorType\":1,\n" +
                "\"voltageUpperLimits\":750,\n" +
                "\"voltageLowerLimits\":150,\n" +
                "\"current\":40,\n" +
                "\"power\":30.0,\n" +
                "\"parkNo\":null,\n" +
                "\"nationalStandard\":2\n" +
                "}\n" +
                "],\n" +
                "\"equipmentLng\":null,\n" +
                "\"equipmentLat\":null,\n" +
                "\"power\":37.0,\n" +
                "\"equipmentName\":null\n" +
                "}\n" +
                "],\n" +
                "\"remark\":null\n" +
                "}\n" +
                "]\n" +
                "}\n";
        //String requestJson = "{\"pageNum\":1,\"pageSize\":700}";
        System.out.println("queryParam : " + requestJson);
        String bizContentJson = AESTools.encrypt(requestJson, appServerConfig.getAesKey());
 
        req.setBizContent(bizContentJson);
        System.out.println("encrypt CommonRequest toJson : " + JSONObject.toJSONString(req));
        req = sign(req, appServerConfig.getPriKey());
        System.out.println("sign : " + req.getSign());
        System.out.println("request body : " + JSONObject.toJSONString(req));
 
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        String bodyStr = JSONObject.toJSONString(req);
        HttpEntity<String> request = new HttpEntity<String>(bodyStr, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(appServerConfig.getServerUrl(), request, String.class);
        String result = "";
        if (response.getStatusCodeValue() == 200) {
            result = response.getBody();
            System.out.println("response result : " + result);
            CommonResponse commonResponse = JSONObject.parseObject(result, CommonResponse.class);
            if ("200".equals(commonResponse.getStatusCode())) {
                boolean rsa256Verify = verifySign(commonResponse);
                System.out.println("verify sign result : " + rsa256Verify);
            }
        }
        return result;
    }
 
 
    /**
     * 推送充电站静态信息
     * @param stationInfoReq
     * @return
     */
    @ResponseBody
    @PostMapping("/pushStationInfo")
    public R pushStationInfo(@RequestBody StationInfoReq stationInfoReq){
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String filename = "CHARGE_DATA_STATIONINFO_REQ_" + appServerConfig.getAppId() + "_" + format.format(date) + ".json";
 
        CommonRequest req = new CommonRequest();
        req.setEncryptType("AES");
        req.setSignType("RSA256");
        req.setFilename(filename);
        format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        req.setTimestamp(format.format(date));
 
        String requestJson = JSON.toJSONString(stationInfoReq);
        log.info("【E路通-推送充电站静态信息】queryParam : {}", requestJson);
        String bizContentJson = AESTools.encrypt(requestJson, appServerConfig.getAesKey());
 
        req.setBizContent(bizContentJson);
        log.info("【E路通-推送充电站静态信息】encrypt CommonRequest toJson : {}", JSONObject.toJSONString(req));
        req = sign(req, appServerConfig.getPriKey());
        log.info("【E路通-推送充电站静态信息】sign : {}", req.getSign());
        log.info("【E路通-推送充电站静态信息】request body : {}", JSONObject.toJSONString(req));
 
        HttpRequest post = HttpUtil.createPost(appServerConfig.getServerUrl());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        post.header(headers);
        String bodyStr = JSONObject.toJSONString(req);
        post.body(bodyStr);
        HttpResponse execute = post.execute();
        String result = "";
        if (execute.getStatus() == 200) {
            result = execute.body();
            log.info("【E路通-推送充电站静态信息】response result : {}", result);
            CommonResponse commonResponse = JSONObject.parseObject(result, CommonResponse.class);
            if ("200".equals(commonResponse.getStatusCode())) {
                boolean rsa256Verify = verifySign(commonResponse);
                log.info("【E路通-推送充电站静态信息】verify sign result : {}", rsa256Verify);
                return R.ok();
            }else{
                return R.fail(commonResponse.getErrorMsg());
            }
        }
        return R.fail("推送接口请求失败");
    }
 
 
    /**
     * 推送充电站状态信息
     * @param stationsStatusReq
     * @return
     */
    @ResponseBody
    @PostMapping("/pushStationsStatus")
    public R pushStationsStatus(@RequestBody StationsStatusReq stationsStatusReq){
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String filename = "CHARGE_DATA_STATIONSTATUS_REQ_" + appServerConfig.getAppId() + "_" + format.format(date) + ".json";
 
        CommonRequest req = new CommonRequest();
        req.setEncryptType("AES");
        req.setSignType("RSA256");
        req.setFilename(filename);
        format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        req.setTimestamp(format.format(date));
 
        String requestJson = JSON.toJSONString(stationsStatusReq);
        log.info("【E路通-推送充电站状态信息】queryParam : {}", requestJson);
        String bizContentJson = AESTools.encrypt(requestJson, appServerConfig.getAesKey());
 
        req.setBizContent(bizContentJson);
        log.info("【E路通-推送充电站状态信息】encrypt CommonRequest toJson : {}", JSONObject.toJSONString(req));
        req = sign(req, appServerConfig.getPriKey());
        log.info("【E路通-推送充电站状态信息】sign : {}", req.getSign());
        log.info("【E路通-推送充电站状态信息】request body : {}", JSONObject.toJSONString(req));
 
        HttpRequest post = HttpUtil.createPost(appServerConfig.getServerUrl());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        post.header(headers);
        String bodyStr = JSONObject.toJSONString(req);
        post.body(bodyStr);
        HttpResponse execute = post.execute();
        String result = "";
        if (execute.getStatus() == 200) {
            result = execute.body();
            log.info("【E路通-推送充电站状态信息】response result : {}", result);
            CommonResponse commonResponse = JSONObject.parseObject(result, CommonResponse.class);
            if ("200".equals(commonResponse.getStatusCode())) {
                boolean rsa256Verify = verifySign(commonResponse);
                log.info("【E路通-推送充电站状态信息】verify sign result : {}", rsa256Verify);
                return R.ok();
            }else{
                return R.fail(commonResponse.getErrorMsg());
            }
        }
        return R.fail("推送接口请求失败");
    }
 
 
    /**
     * 推送设备接口状态信息
     * @param connectorStatusReq
     * @return
     */
    @ResponseBody
    @PostMapping("/pushConnectorStatus")
    public R pushConnectorStatus(@RequestBody ConnectorStatusReq connectorStatusReq){
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String filename = "CHARGE_NOTIFICATION_CONNECTORSTAUS_REQ_" + appServerConfig.getAppId() + "_" + format.format(date) + ".json";
 
        CommonRequest req = new CommonRequest();
        req.setEncryptType("AES");
        req.setSignType("RSA256");
        req.setFilename(filename);
        format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        req.setTimestamp(format.format(date));
 
        String requestJson = JSON.toJSONString(connectorStatusReq);
        log.info("【E路通-推送设备接口状态信息】queryParam : {}", requestJson);
        String bizContentJson = AESTools.encrypt(requestJson, appServerConfig.getAesKey());
 
        req.setBizContent(bizContentJson);
        log.info("【E路通-推送设备接口状态信息】encrypt CommonRequest toJson : {}", JSONObject.toJSONString(req));
        req = sign(req, appServerConfig.getPriKey());
        log.info("【E路通-推送设备接口状态信息】sign : {}", req.getSign());
        log.info("【E路通-推送设备接口状态信息】request body : {}", JSONObject.toJSONString(req));
 
        HttpRequest post = HttpUtil.createPost(appServerConfig.getServerUrl());
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        post.header(headers);
        String bodyStr = JSONObject.toJSONString(req);
        post.body(bodyStr);
        HttpResponse execute = post.execute();
        String result = "";
        if (execute.getStatus() == 200) {
            result = execute.body();
            log.info("【E路通-推送设备接口状态信息】response result : {}", result);
            CommonResponse commonResponse = JSONObject.parseObject(result, CommonResponse.class);
            if ("200".equals(commonResponse.getStatusCode())) {
                boolean rsa256Verify = verifySign(commonResponse);
                log.info("【E路通-推送设备接口状态信息】verify sign result : {}", rsa256Verify);
                return R.ok();
            }else{
                return R.fail(commonResponse.getErrorMsg());
            }
        }
        return R.fail("推送接口请求失败");
    }
 
}