LinkDataController.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package com.zsElectric.boot.charging.controller;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.zsElectric.boot.charging.service.ChargingReceptionService;
  4. import com.zsElectric.boot.common.annotation.ApiRateLimit;
  5. import com.zsElectric.boot.common.annotation.Log;
  6. import com.zsElectric.boot.common.constant.ConnectivityConstants;
  7. import com.zsElectric.boot.common.enums.LogModuleEnum;
  8. import com.zsElectric.boot.common.util.AESCryptoUtils;
  9. import com.zsElectric.boot.common.util.HmacMD5Util;
  10. import com.zsElectric.boot.common.util.electric.QueryTokenResponseData;
  11. import com.zsElectric.boot.common.util.electric.RequestParmsEntity;
  12. import com.zsElectric.boot.common.util.electric.ResponseParmsEntity;
  13. import com.zsElectric.boot.common.util.electric.queryToken.JwtTokenUtil;
  14. import com.zsElectric.boot.common.util.electric.queryToken.QueryTokenRequestParms;
  15. import io.swagger.v3.oas.annotations.Operation;
  16. import io.swagger.v3.oas.annotations.tags.Tag;
  17. import lombok.RequiredArgsConstructor;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.springframework.web.bind.annotation.*;
  20. @Slf4j
  21. @RestController
  22. @RequiredArgsConstructor
  23. @Tag(name = "充电业务相关接口_第三方")
  24. @RequestMapping("/charge-business/v1/linkData")
  25. public class LinkDataController {
  26. private final ChargingReceptionService chargingReceptionService;
  27. private final JwtTokenUtil jwtTokenUtil;
  28. // @ApiRateLimit(
  29. // prefix = "third_party:query_token", // Redis key前缀
  30. // limitType = ApiRateLimit.LimitType.IP, // 限流类型:IP/GLOBAL/USER
  31. // count = 200, // 时间窗口内允许的最大请求次数
  32. // time = 60, // 时间窗口大小(秒)
  33. // message = "获取Token请求过于频繁,请稍后再试" // 限流提示信息
  34. // )
  35. /**
  36. * 获取Token
  37. * Token作为全局唯一凭证,调用各接口时均需要使用,该接口作为双方获取Token的接口,双方均需要实现。
  38. * @author wzq
  39. * @param request,参数为类则包含{@link RequestParmsEntity}
  40. * @return ResponseParmsEntity{@link ResponseParmsEntity}
  41. */
  42. @Operation(summary = "获取token")
  43. @PostMapping("/query_token")
  44. @Log(value = "获取token", module = LogModuleEnum.PARKING, params = true, result = true)
  45. // @ApiRateLimit(prefix = "third_party:query_token", limitType = ApiRateLimit.LimitType.IP, count = 200, time = 60, message = "获取Token请求过于频繁,请稍后再试")
  46. public ResponseParmsEntity getToken(@RequestBody RequestParmsEntity request) throws Exception {
  47. ResponseParmsEntity responseParmsEntity = new ResponseParmsEntity();
  48. try {
  49. //验证签名
  50. if (!HmacMD5Util.verify(request.getOperatorID() + request.getData() + request.getTimeStamp() + request.getSeq(),
  51. ConnectivityConstants.SIG_SECRET, request.getSig())) {
  52. responseParmsEntity.setRet(4001);
  53. responseParmsEntity.setMsg("签名验证失败");
  54. responseParmsEntity.setData("");
  55. responseParmsEntity.setSig(HmacMD5Util.genSign(responseParmsEntity.getRet(), responseParmsEntity.getMsg(), responseParmsEntity.getData(),
  56. ConnectivityConstants.SIG_SECRET));
  57. return responseParmsEntity;
  58. }
  59. String data = request.getData();
  60. String string = AESCryptoUtils.decrypt(data, ConnectivityConstants.DATA_SECRET, ConnectivityConstants.DATA_SECRET_IV);
  61. ObjectMapper objectMapper = new ObjectMapper();
  62. QueryTokenRequestParms queryTokenRequestParms = objectMapper.readValue(string, QueryTokenRequestParms.class);
  63. if (queryTokenRequestParms == null || queryTokenRequestParms.getOperatorID() == null || queryTokenRequestParms.getOperatorSecret() == null) {
  64. responseParmsEntity.setRet(4003);
  65. responseParmsEntity.setMsg("参数错误");
  66. responseParmsEntity.setData("");
  67. responseParmsEntity.setSig(HmacMD5Util.genSign(responseParmsEntity.getRet(), responseParmsEntity.getMsg(), responseParmsEntity.getData(),
  68. ConnectivityConstants.SIG_SECRET));
  69. return responseParmsEntity;
  70. }
  71. //判断运营商ID与密钥是否正确
  72. if (!queryTokenRequestParms.getOperatorID().equals(ConnectivityConstants.PLATFORM_OPERATOR_ID) || !queryTokenRequestParms.getOperatorSecret().equals(ConnectivityConstants.OPERATOR_SECRET)) {
  73. responseParmsEntity.setRet(4004);
  74. responseParmsEntity.setMsg("OperatorID或OperatorSecret错误!");
  75. responseParmsEntity.setData("");
  76. responseParmsEntity.setSig(HmacMD5Util.genSign(responseParmsEntity.getRet(), responseParmsEntity.getMsg(), responseParmsEntity.getData(),
  77. ConnectivityConstants.SIG_SECRET));
  78. return responseParmsEntity;
  79. }
  80. //redis获取token,不存在则创建
  81. String accessToken = jwtTokenUtil.generateToken(queryTokenRequestParms.getOperatorID());
  82. Integer remainingTTL = jwtTokenUtil.getRemainingTTL(accessToken).intValue();
  83. //构建Data(token信息)
  84. QueryTokenResponseData queryTokenResponseData = new QueryTokenResponseData();
  85. queryTokenResponseData.setOperatorID(queryTokenRequestParms.getOperatorID());
  86. queryTokenResponseData.setAccessToken(accessToken);
  87. queryTokenResponseData.setTokenAvailableTime(remainingTTL);
  88. queryTokenResponseData.setSuccStat(0);
  89. queryTokenResponseData.setFailReason(0);
  90. log.info("生成token信息:{}", objectMapper.writeValueAsString(queryTokenResponseData));
  91. String encodeData = AESCryptoUtils.encrypt(objectMapper.writeValueAsString(queryTokenResponseData), ConnectivityConstants.DATA_SECRET,
  92. ConnectivityConstants.DATA_SECRET_IV);
  93. responseParmsEntity.setRet(0);
  94. responseParmsEntity.setMsg("成功");
  95. responseParmsEntity .setData(encodeData);
  96. responseParmsEntity.setSig(HmacMD5Util.genSign(responseParmsEntity.getRet(), responseParmsEntity.getMsg(), responseParmsEntity.getData(),
  97. ConnectivityConstants.SIG_SECRET));
  98. return responseParmsEntity;
  99. } catch (Exception e) {
  100. log.error("系统错误:{}", e.getMessage());
  101. responseParmsEntity .setRet(500);
  102. responseParmsEntity.setMsg("系统错误");
  103. responseParmsEntity.setData("");
  104. responseParmsEntity.setSig(HmacMD5Util.genSign(responseParmsEntity.getRet(), responseParmsEntity.getMsg(), responseParmsEntity.getData(),
  105. ConnectivityConstants.SIG_SECRET));
  106. return responseParmsEntity;
  107. }
  108. }
  109. /**
  110. * <p>2.4 推送启动充电结果</p>
  111. * @author SheepHy
  112. * @param requestDTO,参数为类则包含{@link RequestParmsEntity}
  113. * @return 推送启动充电结果VO
  114. */
  115. @Operation(summary = "推送启动充电结果")
  116. @PostMapping("/notification_start_charge_result")
  117. @Log(value = "推送启动充电结果", module = LogModuleEnum.PARKING, params = true, result = true)
  118. // @ApiRateLimit(prefix = "third_party:start_charge", limitType = ApiRateLimit.LimitType.IP, count = 300, time = 60, message = "启动充电请求过于频繁,请稍后再试")
  119. public ResponseParmsEntity chargeResponse(@RequestBody RequestParmsEntity requestDTO){
  120. return chargingReceptionService.chargeResponse(requestDTO);
  121. }
  122. /**
  123. * 2.6 推送充电状态
  124. * */
  125. @Operation(summary = "推送充电状态")
  126. @PostMapping("/notification_equip_charge_status")
  127. @Log(value = "推送充电状态", module = LogModuleEnum.PARKING, params = true, result = true)
  128. // @ApiRateLimit(prefix = "third_party:charge_status", limitType = ApiRateLimit.LimitType.IP, count = 500, time = 60, message = "充电状态推送过于频繁,请稍后再试")
  129. public ResponseParmsEntity chargeStatusResponse(@RequestBody RequestParmsEntity requestDTO){
  130. return chargingReceptionService.chargeStatusResponse(requestDTO);
  131. }
  132. /**
  133. * 2.8 推送停止充电结果
  134. * */
  135. @Operation(summary = "推送停止充电结果")
  136. @PostMapping("/notification_stop_charge_result")
  137. @Log(value = "推送停止充电结果", module = LogModuleEnum.PARKING, params = true, result = true)
  138. // @ApiRateLimit(prefix = "third_party:stop_charge", limitType = ApiRateLimit.LimitType.IP, count = 300, time = 60, message = "停止充电请求过于频繁,请稍后再试")
  139. public ResponseParmsEntity stopChargeResponse(@RequestBody RequestParmsEntity requestDTO){
  140. return chargingReceptionService.stopChargeResponse(requestDTO);
  141. }
  142. /**
  143. * 2.9 推送充电订单信息
  144. * */
  145. @Operation(summary = "推送充电订单信息")
  146. @PostMapping("/notification_charge_order_info")
  147. @Log(value = "推送充电订单信息", module = LogModuleEnum.PARKING, params = true, result = true)
  148. // @ApiRateLimit(prefix = "third_party:charge_order", limitType = ApiRateLimit.LimitType.IP, count = 200, time = 60, message = "订单信息推送过于频繁,请稍后再试")
  149. public ResponseParmsEntity chargeOrderResponse(@RequestBody RequestParmsEntity requestDTO) throws Exception {
  150. return chargingReceptionService.chargeOrderResponse(requestDTO);
  151. }
  152. /**
  153. * 3.2 设备状态变化推送
  154. * @param requestDTO
  155. * @return
  156. */
  157. @Operation(summary = "设备状态变化推送")
  158. @PostMapping("/notification_stationStatus")
  159. @Log(value = "设备状态变化推送", module = LogModuleEnum.PARKING, params = true, result = true)
  160. // @ApiRateLimit(prefix = "third_party:station_status", limitType = ApiRateLimit.LimitType.IP, count = 500, time = 60, message = "设备状态推送过于频繁,请稍后再试")
  161. public ResponseParmsEntity stationStatus(@RequestBody RequestParmsEntity requestDTO){
  162. return chargingReceptionService.stationStatus(requestDTO);
  163. }
  164. }