소스 검색

优化代码

java110 5 년 전
부모
커밋
b6417f1bc9

+ 2 - 2
service-job/src/main/java/com/java110/job/adapt/hcIot/IotConstant.java

@@ -31,12 +31,12 @@ public class IotConstant {
     public static final String IOT_APP_ID = "APP_ID"; // 物联网域
     public static final String IOT_APP_SECRET = "APP_SECRET"; // 物联网域
 
-    private static final String DEFAULT_IOT_URL = "https://things.homecommunity.cn/";
+    private static final String DEFAULT_IOT_URL = "https://things.homecommunity.cn";
 
     private static final String DEFAULT_APP_ID = "e86a6a373c354927bea5fd21a0bec617";
     private static final String DEFAULT_APP_SECRET = "ead9a2f67f96e2b8ed2fe38cc9709463";
 
-    public static final String GET_TOKEN_URL = " /extApi/auth/getAccessToken?appId=APP_ID&appSecret=APP_SECRET";
+    public static final String GET_TOKEN_URL = "/extApi/auth/getAccessToken?appId=APP_ID&appSecret=APP_SECRET";
 
     //添加小区
     public static final String ADD_COMMUNITY_URL = "/extApi/community/addCommunity";

+ 26 - 1
service-job/src/main/java/com/java110/job/adapt/hcIot/asyn/impl/IotSendAsynImpl.java

@@ -76,10 +76,26 @@ public class IotSendAsynImpl implements IIotSendAsyn {
         HttpHeaders httpHeaders = new HttpHeaders();
         httpHeaders.add("access_token", GetToken.get(outRestTemplate));
         //httpHeaders.add("Content-Type", "application/x-www-form-urlencoded");
-
         return httpHeaders;
     }
 
+    private void refreshAccessToken(ResponseEntity<String> responseEntity) {
+        if (responseEntity == null) {
+            return;
+        }
+
+        if (responseEntity.getStatusCode() != HttpStatus.OK) {
+            return;
+        }
+
+        JSONObject paramOut = JSONObject.parseObject(responseEntity.getBody());
+
+        if (paramOut.getInteger("code") == ResultVo.CODE_UNAUTHORIZED) {
+            //重新刷新token
+            GetToken.get(outRestTemplate);
+        }
+    }
+
     @Override
     @Async
     public void addCommunity(JSONObject postParameters) {
@@ -111,6 +127,7 @@ public class IotSendAsynImpl implements IIotSendAsyn {
             saveTranslateError(machineTranslateDto, postParameters.toJSONString(), responseEntity != null ? responseEntity.getBody() : "");
         } finally {
             saveTranslateLog(machineTranslateDto);
+            refreshAccessToken(responseEntity);
         }
     }
 
@@ -147,6 +164,7 @@ public class IotSendAsynImpl implements IIotSendAsyn {
 
         } finally {
             saveTranslateLog(machineTranslateDto);
+            refreshAccessToken(responseEntity);
         }
     }
 
@@ -182,6 +200,7 @@ public class IotSendAsynImpl implements IIotSendAsyn {
             saveTranslateError(machineTranslateDto, postParameters.toJSONString(), responseEntity != null ? responseEntity.getBody() : "");
         } finally {
             saveTranslateLog(machineTranslateDto);
+            refreshAccessToken(responseEntity);
         }
     }
 
@@ -236,6 +255,7 @@ public class IotSendAsynImpl implements IIotSendAsyn {
             return;
         } finally {
             saveTranslateLog(machineTranslateDto);
+            refreshAccessToken(responseEntity);
         }
 
         MachineDto machinePo = new MachineDto();
@@ -292,6 +312,7 @@ public class IotSendAsynImpl implements IIotSendAsyn {
             saveTranslateError(machineTranslateDto, postParameters.toJSONString(), responseEntity != null ? responseEntity.getBody() : "");
         } finally {
             saveTranslateLog(machineTranslateDto);
+            refreshAccessToken(responseEntity);
         }
     }
 
@@ -335,6 +356,7 @@ public class IotSendAsynImpl implements IIotSendAsyn {
             saveTranslateError(machineTranslateDto, postParameters.toJSONString(), responseEntity != null ? responseEntity.getBody() : "");
         } finally {
             saveTranslateLog(machineTranslateDto);
+            refreshAccessToken(responseEntity);
         }
     }
 
@@ -381,6 +403,7 @@ public class IotSendAsynImpl implements IIotSendAsyn {
 
         } finally {
             saveTranslateLog(machineTranslateDto);
+            refreshAccessToken(responseEntity);
         }
     }
 
@@ -425,6 +448,7 @@ public class IotSendAsynImpl implements IIotSendAsyn {
 
         } finally {
             saveTranslateLog(machineTranslateDto);
+            refreshAccessToken(responseEntity);
         }
     }
 
@@ -472,6 +496,7 @@ public class IotSendAsynImpl implements IIotSendAsyn {
 
         } finally {
             saveTranslateLog(machineTranslateDto);
+            refreshAccessToken(responseEntity);
         }
     }