Bläddra i källkod

Merge branch 'han' of uskycloud/usky-modules into server-165

hanzhengyi 1 år sedan
förälder
incheckning
3ea10ec32d

+ 25 - 16
service-issue/service-issue-biz/src/main/java/com/usky/issue/annotation/CheckSignAspect.java

@@ -1,12 +1,16 @@
 package com.usky.issue.annotation;
 
+import com.alibaba.fastjson.JSONObject;
+import com.usky.common.core.bean.ApiResult;
 import com.usky.common.core.utils.StringUtils;
+import com.usky.issue.mapper.SpProjectConfigMapper;
 import com.usky.issue.service.util.SignUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.aspectj.lang.ProceedingJoinPoint;
 import org.aspectj.lang.annotation.Around;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Pointcut;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.web.context.request.RequestAttributes;
@@ -25,7 +29,8 @@ import java.util.Objects;
 @Configuration
 @Slf4j
 public class CheckSignAspect {
-
+    @Autowired
+    private SpProjectConfigMapper spProjectConfigMapper;
     @Value("${sign.expireTime}")
     private long expireTime;//接口签名验证超时时间
     @Value("${sign.secretKey}")
@@ -42,36 +47,40 @@ public class CheckSignAspect {
         try {
             ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
             HttpServletRequest request = Objects.requireNonNull(sra).getRequest();
-
-            String timestamp = request.getHeader("timestamp");//获取timestamp参数
-            String sign = request.getHeader("sign");//获取sign参数
-
-            if (StringUtils.isBlank(timestamp) || StringUtils.isBlank(sign)) {
-                return "timestamp和sign参数不能为空";
+            Object[] args = joinPoint.getArgs();
+            String originalValue = (String) args[0];
+            JSONObject requestBody = JSONObject.parseObject(originalValue);
+            String timestamp = requestBody.get("timestamp").toString();//获取timestamp参数
+            String sign = requestBody.get("sign").toString();//获取sign参数
+            String appKey = request.getHeader("appKey");//获取appKey参数
+            if (StringUtils.isBlank(timestamp) || StringUtils.isBlank(sign) || StringUtils.isBlank(appKey)) {
+                return ApiResult.success("timestamp和sign、appKey参数不能为空");
             }
             long requestTime = Long.valueOf(timestamp);
             long now = System.currentTimeMillis() / 1000;
-            log.info("now={}", now);
             // 请求发起时间与当前时间超过expireTime,则接口请求过期
             if (now - requestTime > expireTime) {
-                return "接口请求过期";
+                return ApiResult.success("接口请求过期");
             }
 
-            String generatedSign = generatedSignature(request, timestamp);
+            String generatedSign = generatedSignature(request, timestamp,appKey);
             if (!generatedSign.equals(sign)) {
-                return "签名校验错误";
+                return ApiResult.success("签名校验错误");
             }
-
-            Object result = joinPoint.proceed();
+            String companyCode = spProjectConfigMapper.getCompanyCode(appKey);
+            StringBuilder newString = new StringBuilder(originalValue);
+            newString.insert(newString.length()-1, ",\"companyCode\":\""+companyCode+"\"");
+            args[0] = newString.toString();
+            Object result = joinPoint.proceed(args);
             return result;
         } catch (Throwable t) {
-            return "签名校验异常";
+            return ApiResult.success("签名校验异常");
         }
 
     }
 
     //获取请求参数并生成签名
-    private String generatedSignature(HttpServletRequest request, String timestamp) {
+    private String generatedSignature(HttpServletRequest request, String timestamp,String appKey) {
         //获取RequestBody参数,此处需要配合过滤器处理request后才能获取
         String bodyParam = null;
         if (request instanceof ContentCachingRequestWrapper) {
@@ -86,7 +95,7 @@ public class CheckSignAspect {
         Map<String, String> requestPathMap = (Map<String, String>) webRequest.getAttribute(
                 HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
 
-        return SignUtil.sign(bodyParam, requestParameterMap, requestPathMap, secretKey, timestamp);
+        return SignUtil.sign(bodyParam, requestParameterMap, requestPathMap, secretKey, timestamp,appKey);
     }
 
 }

+ 9 - 2
service-issue/service-issue-biz/src/main/java/com/usky/issue/controller/web/YtDeviceStatusController.java

@@ -27,6 +27,12 @@ public class YtDeviceStatusController {
     @Autowired
     private YtDeviceStatusService ytDeviceStatusService;
 
+    @CheckSign
+    @PostMapping("hello")
+    ApiResult<String> hello(@RequestBody String requestBody){
+        return ApiResult.success(requestBody);
+    }
+
     /**
      * 离线设备查询
      * @param requestVO
@@ -53,9 +59,10 @@ public class YtDeviceStatusController {
      * @param
      * @return
      */
+    @CheckSign
     @PostMapping("deviceList")
-    ApiResult <List<Map<String, Object>>> deviceList(@RequestBody YtDeviceStatusRequestVO requestVO){
-        return ApiResult.success(ytDeviceStatusService.deviceList(requestVO));
+    ApiResult <List<Map<String, Object>>> deviceList(@RequestBody String requestBody){
+        return ApiResult.success(ytDeviceStatusService.deviceList(requestBody));
     }
 
 }

+ 1 - 1
service-issue/service-issue-biz/src/main/java/com/usky/issue/service/YtDeviceStatusService.java

@@ -25,5 +25,5 @@ public interface YtDeviceStatusService extends CrudService<YtDeviceStatus> {
     List<YtDeviceStatus> export();
 
     //根据单位编号查询离线设备
-    List<Map<String, Object>> deviceList(YtDeviceStatusRequestVO requestVO);
+    List<Map<String, Object>> deviceList(String requestBody);
 }

+ 56 - 46
service-issue/service-issue-biz/src/main/java/com/usky/issue/service/impl/YtDeviceStatusServiceImpl.java

@@ -1,6 +1,9 @@
 package com.usky.issue.service.impl;
 
 
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.usky.common.core.bean.CommonPage;
 import com.usky.common.core.exception.BusinessException;
 import com.usky.issue.domain.YtDeviceStatus;
@@ -71,53 +74,60 @@ public class YtDeviceStatusServiceImpl extends AbstractCrudService<YtDeviceStatu
     根据传入单位编号查询所有设备信息
     */
     @Override
-    public List<Map<String, Object>> deviceList(YtDeviceStatusRequestVO requestVO) {
-        String companyCode = requestVO.getCompanyCode();
+    public List<Map<String, Object>> deviceList(String requestBody) {
+        JSONObject requestVO = JSONObject.parseObject(requestBody);
+        String companyCode = requestVO.get("companyCode").toString();
+        QueryWrapper<YtDeviceStatus> queryWrapper = Wrappers.query();
+        queryWrapper.select("device_type AS deviceType","COUNT(*) AS deviceTypeCounts","COUNT(difference < 48 OR " +
+                        "NULL) AS onlineCount","COUNT(difference >= 48 OR NULL) AS offlineCount")
+                .eq("company_code",companyCode)
+                .groupBy("device_type");
+        List<Map<String, Object>> deviceStatusList = this.listMaps(queryWrapper);
         // 查询各类型设备的总数以及在线、离线设备数量
-        List<Map<String, Object>> statusCounts = ytDeviceStatusMapper.deviceList(companyCode);
-        Map<String, Map<String, Integer>> statusMap = new HashMap<>();
-        for (Map<String, Object> statusCount : statusCounts) {
-            String deviceType = (String) statusCount.get("deviceType");
-            Integer onlineCount = ((Number) statusCount.get("onlineCount")).intValue();
-            Integer offlineCount = ((Number) statusCount.get("offlineCount")).intValue();
-            statusMap.computeIfAbsent(deviceType, k -> new HashMap<>())
-                    .put("onlineCount", onlineCount);
-            statusMap.computeIfAbsent(deviceType, k -> new HashMap<>())
-                    .put("offlineCount", offlineCount);
-            int totalCount = ((Number) statusCount.get("deviceTypeCounts")).intValue();
-            statusMap.computeIfAbsent(deviceType, k -> new HashMap<>()).put("deviceTypeCounts", totalCount);
-        }
-        // 统计设备总数和在线、离线设备数
-        Map<String, Integer> deviceTypeCounts = new HashMap<>();
-        Map<String, Integer> onlineDeviceCount = new HashMap<>();
-        Map<String, Integer> offlineDeviceCount = new HashMap<>();
-
-        // 设置设备状态并统计设备数量
-        List<Map<String, Object>> deviceList = ytDeviceStatusMapper.deviceList(companyCode);
-        for (Map<String, Object> device : deviceList) {
-            String deviceType = (String) device.get("deviceType");
-            deviceTypeCounts.merge(deviceType, (Integer) statusMap.getOrDefault(deviceType, Collections.emptyMap()).getOrDefault("deviceTypeCounts", 0), Integer::sum);
-
-            Integer onlineCount = statusMap.getOrDefault(deviceType, Collections.emptyMap())
-                    .getOrDefault("onlineCount", 0);
-            Integer offlineCount = statusMap.getOrDefault(deviceType, Collections.emptyMap())
-                    .getOrDefault("offlineCount", 0);
-            Integer currentOnlineCount = onlineDeviceCount.getOrDefault(deviceType, 0);
-            Integer currentOfflineCount = offlineDeviceCount.getOrDefault(deviceType, 0);
-            onlineDeviceCount.put(deviceType, currentOnlineCount + onlineCount);
-            offlineDeviceCount.put(deviceType, currentOfflineCount + offlineCount);
-        }
-
-        // 添加设备类型、设备总数和在线、离线设备数到结果列表
-        List<Map<String, Object>> deviceStatusList = new ArrayList<>();
-        for (String deviceType : deviceTypeCounts.keySet()) {
-            Map<String, Object> deviceInfo = new HashMap<>();
-            deviceInfo.put("deviceType", deviceType);
-            deviceInfo.put("deviceTypeCounts", deviceTypeCounts.getOrDefault(deviceType, 0));
-            deviceInfo.put("onlineDeviceCount", onlineDeviceCount.getOrDefault(deviceType, 0));
-            deviceInfo.put("offlineDeviceCount", offlineDeviceCount.getOrDefault(deviceType, 0));
-            deviceStatusList.add(deviceInfo);
-        }
+//        List<Map<String, Object>> statusCounts = ytDeviceStatusMapper.deviceList(companyCode);
+//        Map<String, Map<String, Integer>> statusMap = new HashMap<>();
+//        for (Map<String, Object> statusCount : statusCounts) {
+//            String deviceType = (String) statusCount.get("deviceType");
+//            Integer onlineCount = ((Number) statusCount.get("onlineCount")).intValue();
+//            Integer offlineCount = ((Number) statusCount.get("offlineCount")).intValue();
+//            statusMap.computeIfAbsent(deviceType, k -> new HashMap<>())
+//                    .put("onlineCount", onlineCount);
+//            statusMap.computeIfAbsent(deviceType, k -> new HashMap<>())
+//                    .put("offlineCount", offlineCount);
+//            int totalCount = ((Number) statusCount.get("deviceTypeCounts")).intValue();
+//            statusMap.computeIfAbsent(deviceType, k -> new HashMap<>()).put("deviceTypeCounts", totalCount);
+//        }
+//        // 统计设备总数和在线、离线设备数
+//        Map<String, Integer> deviceTypeCounts = new HashMap<>();
+//        Map<String, Integer> onlineDeviceCount = new HashMap<>();
+//        Map<String, Integer> offlineDeviceCount = new HashMap<>();
+//
+//        // 设置设备状态并统计设备数量
+//        List<Map<String, Object>> deviceList = ytDeviceStatusMapper.deviceList(companyCode);
+//        for (Map<String, Object> device : deviceList) {
+//            String deviceType = (String) device.get("deviceType");
+//            deviceTypeCounts.merge(deviceType, (Integer) statusMap.getOrDefault(deviceType, Collections.emptyMap()).getOrDefault("deviceTypeCounts", 0), Integer::sum);
+//
+//            Integer onlineCount = statusMap.getOrDefault(deviceType, Collections.emptyMap())
+//                    .getOrDefault("onlineCount", 0);
+//            Integer offlineCount = statusMap.getOrDefault(deviceType, Collections.emptyMap())
+//                    .getOrDefault("offlineCount", 0);
+//            Integer currentOnlineCount = onlineDeviceCount.getOrDefault(deviceType, 0);
+//            Integer currentOfflineCount = offlineDeviceCount.getOrDefault(deviceType, 0);
+//            onlineDeviceCount.put(deviceType, currentOnlineCount + onlineCount);
+//            offlineDeviceCount.put(deviceType, currentOfflineCount + offlineCount);
+//        }
+//
+//        // 添加设备类型、设备总数和在线、离线设备数到结果列表
+//        List<Map<String, Object>> deviceStatusList = new ArrayList<>();
+//        for (String deviceType : deviceTypeCounts.keySet()) {
+//            Map<String, Object> deviceInfo = new HashMap<>();
+//            deviceInfo.put("deviceType", deviceType);
+//            deviceInfo.put("deviceTypeCounts", deviceTypeCounts.getOrDefault(deviceType, 0));
+//            deviceInfo.put("onlineDeviceCount", onlineDeviceCount.getOrDefault(deviceType, 0));
+//            deviceInfo.put("offlineDeviceCount", offlineDeviceCount.getOrDefault(deviceType, 0));
+//            deviceStatusList.add(deviceInfo);
+//        }
 
         return deviceStatusList;
     }

+ 25 - 24
service-issue/service-issue-biz/src/main/java/com/usky/issue/service/util/SignUtil.java

@@ -26,30 +26,31 @@ public class SignUtil {
         return sortMap;
     }
 
-    public static String sign(String body, Map<String, String[]> params, Map<String, String> requestPathMap, String secretKey, String timestamp) {
-        StringBuilder sb = new StringBuilder();
-        if (StringUtils.isNotBlank(body)) {
-            sb.append(body).append('#');
-        }
-
-        if (!CollectionUtils.isEmpty(params)) {
-            params.entrySet()
-                    .stream()
-                    .sorted(Map.Entry.comparingByKey())
-                    .forEach(paramEntry -> {
-                        String paramValue = String.join(",", Arrays.stream(paramEntry.getValue()).sorted().toArray(String[]::new));
-                        sb.append(paramEntry.getKey()).append("=").append(paramValue).append('#');
-                    });
-        }
-
-        if (!CollectionUtils.isEmpty(requestPathMap)) {
-            for (String key : requestPathMap.keySet()) {
-                String value = requestPathMap.get(key);
-                sb.append(key).append("=").append(value).append('#');
-            }
-
-        }
-        String a = String.join("#", secretKey, timestamp, sb.toString());
+    public static String sign(String body, Map<String, String[]> params, Map<String, String> requestPathMap,
+                              String secretKey, String timestamp,String appKey) {
+//        StringBuilder sb = new StringBuilder();
+//        if (StringUtils.isNotBlank(body)) {
+//            sb.append(body).append('#');
+//        }
+
+//        if (!CollectionUtils.isEmpty(params)) {
+//            params.entrySet()
+//                    .stream()
+//                    .sorted(Map.Entry.comparingByKey())
+//                    .forEach(paramEntry -> {
+//                        String paramValue = String.join(",", Arrays.stream(paramEntry.getValue()).sorted().toArray(String[]::new));
+//                        sb.append(paramEntry.getKey()).append("=").append(paramValue).append('#');
+//                    });
+//        }
+//
+//        if (!CollectionUtils.isEmpty(requestPathMap)) {
+//            for (String key : requestPathMap.keySet()) {
+//                String value = requestPathMap.get(key);
+//                sb.append(key).append("=").append(value).append('#');
+//            }
+//
+//        }
+        String a = String.join("#", secretKey, timestamp, appKey);
         return SecureUtil.md5(a);
     }
 }