竺森洋 преди 5 месеца
родител
ревизия
1d1cb6a29c

+ 26 - 3
src/main/java/io/renren/application/common/service/imp/SysPublicParamServiceImpl.java

@@ -13,6 +13,9 @@ import io.renren.application.common.vo.ChaoXiangParamVo;
 import io.renren.application.common.vo.Results;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Service;
 
 import java.io.IOException;
@@ -25,6 +28,8 @@ import java.util.List;
 @Service
 @RequiredArgsConstructor
 @Slf4j
+@Component
+@EnableScheduling
 public class SysPublicParamServiceImpl extends ServiceImpl<SysPublicParamMapper, SysPublicParam> implements SysPublicParamService {
 
     String CHAO_XIANG_PARAM_PRE = "CHAO_XIANG_PARAM_QRCODE_WEEK_";
@@ -164,14 +169,13 @@ public class SysPublicParamServiceImpl extends ServiceImpl<SysPublicParamMapper,
         if (Integer.valueOf(1).equals(checkTime)){
             id = getWeekParam().getData();
         }else{
-            if (publicParam ==null){
+            if (idParam !=null){
                 id = idParam.getPublicValue();
             }
         }
 
         try {
-            getWeekParam().getData();
-            log.info("开始查询库存 id:{}",getWeekParam().getData());
+            log.info("开始查询库存 id:{}",id);
             Integer stock = ChaoXiangUtils.getStock(id);
             if (stock!=null&&stock>0){
                 if (today.isAfter(endTime)){
@@ -186,4 +190,23 @@ public class SysPublicParamServiceImpl extends ServiceImpl<SysPublicParamMapper,
         return Results.success(-1);
     }
 
+    @Scheduled(cron = "0 0/10 * * * ? ")
+    public void checkToken() {
+        Boolean fail = true;
+        for (int i = 1; i <= 10 && fail; i++){
+            try {
+                String token = ChaoXiangUtils.login();
+                if (StringUtils.isNotBlank(token)) {
+                    log.info("超翔令牌获取成功.");
+                    fail = false;
+                } else {
+                    log.warn("超翔令牌获取失败.");
+                    fail = true;
+                }
+            } catch (Exception e) {
+                log.error("第{}次获取超翔令牌失败",i,e);
+            }
+        }
+    }
+
 }

+ 2 - 2
src/main/java/io/renren/application/common/util/ChaoXiangUtils.java

@@ -92,7 +92,7 @@ public class ChaoXiangUtils {
         return 0;
     }
 
-    private static String login() throws IOException {
+    public static String login() throws IOException {
         String randomStr = RandomUtil.randomString(4).toUpperCase();
         OkHttpClient client = new OkHttpClient();
         Request imageRequest = new Request.Builder()
@@ -162,7 +162,7 @@ public class ChaoXiangUtils {
         Response response = client.newCall(request).execute();
         String token = JSONObject.parseObject(response.body().string()).getJSONObject("data").getString("accessToken");
         cacheToken.setToken(token);
-        cacheToken.setExpireTime(LocalDateTime.now().plusHours(24));
+        cacheToken.setExpireTime(LocalDateTime.now().plusMinutes(24));
         log.info("登入信息:{}",cacheToken);
         return token;
     }