Эх сурвалжийг харах

互联互通对接调试:实现query_token接口等

skyline 1 жил өмнө
parent
commit
a1a91b491f

+ 1 - 0
entity/src/main/java/com/kym/entity/common/RedisKeys.java

@@ -7,6 +7,7 @@ package com.kym.entity.common;
  */
 public interface RedisKeys {
     String EN_PLUS_TOKEN = "EN_PLUS_TOKEN:";
+    String PLATFORM_TOKEN = "PLATFORM_TOKEN:";
     String EN_PLUS_SASS_TOKEN = "EN_PLUS_SASS_TOKEN";
     String OFFLINE = "OFFLINE:";
     String OFFLINE_EXPIRED = "OFFLINE_EXPIRED:";

+ 10 - 8
miniapp/src/main/java/com/kym/miniapp/config/SaTokenConfigure.java

@@ -2,6 +2,7 @@ package com.kym.miniapp.config;
 
 import cn.dev33.satoken.interceptor.SaInterceptor;
 import cn.dev33.satoken.stp.StpUtil;
+import org.springframework.context.annotation.Configuration;
 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
@@ -10,7 +11,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  * @description 权限校验
  * @date 2023-07-11 21:49
  */
-//@Configuration
+@Configuration
 public class SaTokenConfigure implements WebMvcConfigurer {
     // 注册拦截器
     @Override
@@ -24,13 +25,14 @@ public class SaTokenConfigure implements WebMvcConfigurer {
                         "/user/wxLogin",
                         "/user/refresh",
                         "/charge/pullEnStations/*",
-                        "/charge/notification_stationStatus",
-                        "/charge/notification_start_charge_result",
-                        "/charge/notification_start_charge_status",
-                        "/charge/notification_equip_charge_status",
-                        "/charge/notification_stop_charge_result",
-                        "/charge/notification_charge_order_info"
+                        "/charge/*/query_token",
+                        "/charge/*/notification_stationStatus",
+                        "/charge/*/notification_start_charge_result",
+                        "/charge/*/notification_start_charge_status",
+                        "/charge/*/notification_equip_charge_status",
+                        "/charge/*/notification_stop_charge_result",
+                        "/charge/*/notification_charge_order_info"
                 );
     }
 
-}
+}

+ 13 - 5
miniapp/src/main/java/com/kym/miniapp/controller/ChargerController.java

@@ -7,13 +7,13 @@ import com.kym.common.annotation.ApiLog;
 import com.kym.common.config.WxConfig;
 import com.kym.common.enums.WxApi;
 import com.kym.common.utils.HttpUtil;
-import com.kym.entity.enplus.response.PlatformResponse;
 import com.kym.entity.miniapp.ChargeOrder;
 import com.kym.entity.miniapp.queryParams.OrderQueryParams;
+import com.kym.entity.platform.response.PlatformResponse;
 import com.kym.service.admin.StationService;
-import com.kym.service.enplus.EnNotifyService;
 import com.kym.service.miniapp.ChargeOrderService;
 import com.kym.service.miniapp.ChargeService;
+import com.kym.service.platform.PlatformNotifyService;
 import lombok.SneakyThrows;
 import org.springframework.format.annotation.DateTimeFormat;
 import org.springframework.web.bind.annotation.*;
@@ -38,14 +38,14 @@ public class ChargerController {
 
     private final ChargeOrderService chargeOrderService;
 
-    private final EnNotifyService enNotifyService;
+    private final PlatformNotifyService enNotifyService;
 
     private final WxConfig wxConfig;
 
 
     public ChargerController(StationService stationService, ChargeService chargeService,
                              ChargeOrderService chargeOrderService,
-                             EnNotifyService enNotifyService, WxConfig wxConfig) {
+                             PlatformNotifyService enNotifyService, WxConfig wxConfig) {
         this.stationService = stationService;
         this.chargeService = chargeService;
         this.chargeOrderService = chargeOrderService;
@@ -196,6 +196,15 @@ public class ChargerController {
 
     //====================================================以下是EN+推送接口==============================================================
 
+    /**
+     * 【互联互通平台】提供Token
+     */
+    @ApiLog("互联互通平台推送请求Token")
+    @PostMapping("/{platformName}/query_token")
+    PlatformResponse queryToken(@PathVariable(value = "platformName", required = false) String platformName) {
+        return new PlatformResponse(enNotifyService.queryToken(platformName));
+    }
+
     /**
      * 【互联互通平台】推送场站设备状态变化
      *
@@ -204,7 +213,6 @@ public class ChargerController {
      */
     @ApiLog("推送场站设备状态变化")
     @PostMapping("/{platformName}/notification_stationStatus")
-    //todo 通知en+更新推送地址,加上platformName
     PlatformResponse notificationStationStatus(@PathVariable(value = "platformName", required = false) String platformName, @RequestBody JSONObject json) {
         return new PlatformResponse(enNotifyService.handleNotificationStationStatus(platformName, json));
     }

+ 1 - 0
service/src/main/java/com/kym/service/platform/PlatformNotifyService.java

@@ -19,4 +19,5 @@ public interface PlatformNotifyService {
     String handleNotificationChargeOrderInfo(String platformName, JSONObject json);
 
     String handleNotificationStationStatus(String platformName, JSONObject json);
+    String queryToken(String platformName);
 }

+ 3 - 0
service/src/main/java/com/kym/service/platform/impl/PlatformApiServiceImpl.java

@@ -264,6 +264,7 @@ public class PlatformApiServiceImpl implements PlatformApiService {
      */
     @Override
     @DynamicCache(spel = "#connectorId")
+    @PlatformConvert
     public JSONObject queryEquipBusinessPolicy(@PlatformName String platformName, String equipBizSeq, @ConnectorID String connectorId) {
         var param = """
                 {
@@ -286,6 +287,7 @@ public class PlatformApiServiceImpl implements PlatformApiService {
      * @return
      */
     @Override
+    @PlatformConvert
     public JSONObject queryStartCharge(@PlatformName String platformName, String startChargeSeq, @ConnectorID String connectorId, String qrCode, Integer amount) {
         var param = """
                 {
@@ -346,6 +348,7 @@ public class PlatformApiServiceImpl implements PlatformApiService {
      * @return
      */
     @Override
+    @PlatformConvert
     public JSONObject queryStopCharge(@PlatformName String platformName, String startChargeSeq, String connectorId) {
         var param = """
                 {

+ 36 - 7
service/src/main/java/com/kym/service/platform/impl/PlatformNotifySServiceImpl.java → service/src/main/java/com/kym/service/platform/impl/PlatformNotifyServiceImpl.java

@@ -1,23 +1,26 @@
 package com.kym.service.platform.impl;
 
+import cn.hutool.core.util.IdUtil;
 import cn.hutool.extra.mail.MailUtil;
 import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.dynamic.datasource.annotation.DS;
 import com.baomidou.dynamic.datasource.annotation.DSTransactional;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.kym.common.utils.CommUtil;
 import com.kym.common.utils.PlatformConvertUtil;
 import com.kym.entity.admin.ConnectorInfo;
 import com.kym.entity.admin.EquipmentInfo;
 import com.kym.entity.admin.MonitorLog;
 import com.kym.entity.common.RedisKeys;
-import com.kym.entity.platform.PlatformConnectorStatusInfo;
 import com.kym.entity.miniapp.Account;
 import com.kym.entity.miniapp.ChargeOrder;
 import com.kym.entity.miniapp.WalletDetail;
+import com.kym.entity.platform.PlatformConnectorStatusInfo;
 import com.kym.service.admin.ConnectorInfoService;
 import com.kym.service.admin.EquipmentInfoService;
 import com.kym.service.admin.MonitorLogService;
 import com.kym.service.cache.KymCache;
+import com.kym.service.cache.PlatformCache;
 import com.kym.service.factory.DiscountStrategyFactory;
 import com.kym.service.miniapp.*;
 import com.kym.service.platform.PlatformApiService;
@@ -34,6 +37,7 @@ import java.math.BigDecimal;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
 /**
@@ -42,8 +46,8 @@ import java.util.stream.Collectors;
  */
 
 @Service
-public class PlatformNotifySServiceImpl implements PlatformNotifyService {
-    private static final Logger LOGGER = LoggerFactory.getLogger(PlatformNotifySServiceImpl.class);
+public class PlatformNotifyServiceImpl implements PlatformNotifyService {
+    private static final Logger LOGGER = LoggerFactory.getLogger(PlatformNotifyServiceImpl.class);
     public final StringRedisTemplate redisTemplate;
     private final PlatformApiService enPlusService;
     private final ChargeOrderService chargeOrderService;
@@ -58,10 +62,10 @@ public class PlatformNotifySServiceImpl implements PlatformNotifyService {
     @Value("${kym.notify-email}")
     private String notifyEmail;
 
-    public PlatformNotifySServiceImpl(PlatformApiService enPlusService, ChargeOrderService chargeOrderService,
-                                      ChargeService chargeService, AccountService accountService, WalletDetailService walletDetailService,
-                                      MonitorLogService monitorLogService, EquipmentInfoService equipmentInfoService,
-                                      ConnectorInfoService connectorInfoService, StringRedisTemplate redisTemplate, UserStationService userStationService) {
+    public PlatformNotifyServiceImpl(PlatformApiService enPlusService, ChargeOrderService chargeOrderService,
+                                     ChargeService chargeService, AccountService accountService, WalletDetailService walletDetailService,
+                                     MonitorLogService monitorLogService, EquipmentInfoService equipmentInfoService,
+                                     ConnectorInfoService connectorInfoService, StringRedisTemplate redisTemplate, UserStationService userStationService) {
         this.enPlusService = enPlusService;
         this.chargeOrderService = chargeOrderService;
         this.chargeService = chargeService;
@@ -79,6 +83,30 @@ public class PlatformNotifySServiceImpl implements PlatformNotifyService {
         KymCache.INSTANCE.putConnectorId2Status(connectorInfoService.list().stream().collect(Collectors.toMap(ConnectorInfo::getConnectorId, ConnectorInfo::getStatus)));
     }
 
+    @Override
+    public String queryToken(String platformName) {
+        var platform = PlatformCache.INSTANCE.getPlatformByName(platformName);
+        // 查询缓存,存在则返回值和过期时间,不存在则重新生成并缓存
+        var token = redisTemplate.opsForValue().get(RedisKeys.PLATFORM_TOKEN + platformName);
+        // 过期时间
+        var ttl = redisTemplate.getExpire(RedisKeys.PLATFORM_TOKEN + platformName);
+        if (CommUtil.isEmptyOrNull(token)) {
+            token = IdUtil.simpleUUID();
+            ttl = 7 * 24 * 3600L;
+            redisTemplate.opsForValue().set(RedisKeys.PLATFORM_TOKEN + platformName, token, ttl, TimeUnit.SECONDS);
+        }
+
+        return """
+                {
+                    "OperatorID":"%s",
+                    "SuccStat":%d,
+                    "AccessToken":"%s",
+                    "TokenAvailableTime":%d,
+                    "FailReason":%d
+                }
+                """.formatted(platform.getOperatorId(), 0, token, ttl, 0);
+    }
+
     /**
      * EN+ 充电站设备状态变化推送
      *
@@ -155,6 +183,7 @@ public class PlatformNotifySServiceImpl implements PlatformNotifyService {
                 """.formatted(0);
     }
 
+
     /**
      * EN+ 推送启动充电结果
      *