chunping hace 2 años
padre
commit
6c08f5713b

+ 23 - 34
mp-service/src/main/java/com/qs/mp/framework/service/impl/AppTokenServiceImpl.java

@@ -2,6 +2,7 @@ package com.qs.mp.framework.service.impl;
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.qs.mp.common.core.redis.RedisCache;
 import com.qs.mp.common.exception.ServiceException;
 import com.qs.mp.common.utils.DateUtils;
 import com.qs.mp.common.utils.LogUtil;
@@ -13,6 +14,7 @@ import com.qs.mp.framework.redis.RedisKey;
 import com.qs.mp.framework.redis.RedisUtil;
 import com.qs.mp.framework.service.IAppTokenService;
 import java.util.Date;
+import java.util.concurrent.TimeUnit;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -28,7 +30,7 @@ public class AppTokenServiceImpl implements IAppTokenService {
 
 
   @Autowired
-  private RedisUtil redis;
+  private RedisCache redisCache;
 
   @Override
   public AppToken selectByPrimaryKey(String appId) {
@@ -37,25 +39,20 @@ public class AppTokenServiceImpl implements IAppTokenService {
 
   @Override
   public String getAccessToken(String appId) {
-//    String cacheKey = RedisKey.build(RedisKey.WX_APP_TOKEN_KEY, appId);
+    String cacheKey = RedisKey.build(RedisKey.WX_APP_TOKEN_KEY, appId);
 
-//    boolean updateCache = false;
-
-//    String appInfo = redis.get(cacheKey);
-    AppToken token = null;
-//    if (StringUtils.isBlank(appInfo)) {
-      token = selectByPrimaryKey(appId);
-    /*  if (null == token) {
-        LogUtil.error(logger, "app_token记录不存在,appId:{0}", appId);
-        throw new ServiceException();
-      }
-      updateCache = true;
-    } else {
-      token = JSON.parseObject(appInfo, AppToken.class);
+    String cacheToken = redisCache.getCacheObject(cacheKey);
+    if (StringUtils.isNotBlank(cacheToken)) {
+      return cacheToken;
     }
-*/
+    AppToken token = selectByPrimaryKey(appId);
+    if (null == token) {
+      LogUtil.error(logger, "app_token记录不存在,appId:{0}", appId);
+      throw new ServiceException();
+    }
+
    // LogUtil.info(logger, "token: {0}", JSON.toJSONString(token));
-   // if (token.getExpireTimestamp().getTime() < System.currentTimeMillis()) {
+    if (new Date().after(token.getExpireTimestamp())) {
       String res = wxGetAccessToken(appId, token.getAppSecret());
       if (StringUtils.isEmpty(res)) {
         LogUtil.error(logger, "获取access_token为空,appId:{0}, res:{1}", appId,
@@ -64,29 +61,21 @@ public class AppTokenServiceImpl implements IAppTokenService {
       }
       JSONObject jsonObject = JSONObject.parseObject(res);
       String accessToken = jsonObject.getString("access_token");
-      return accessToken;
-      /*Integer expiresIn = jsonObject.getInteger("expires_in");
+
+      Integer expiresIn = jsonObject.getInteger("expires_in");
       if (StringUtils.isEmpty(accessToken)) {
         LogUtil.error(logger, "获取access_token为空,appId:{0}, res:{1}",
             appId, res);
-        throw new ServiceException();
+        throw new ServiceException("access_token获取为空");
       }
       token.setAccessToken(accessToken);
-      // 防止时间不同步,此处提前100s超时
-      token.setExpireTimestamp(DateUtils.addSeconds(new Date(), expiresIn - 100));
-      appTokenMapper.updateByPrimaryKeySelective(token);*/
- //     updateCache = true;
+      // 防止时间不同步,此处提前300s超时
+      token.setExpireTimestamp(DateUtils.addSeconds(new Date(), expiresIn - 300));
+      appTokenMapper.updateByPrimaryKeySelective(token);
 
-   // }
-/*
-    if (updateCache) {
-      boolean result = redis.set(cacheKey, JSON.toJSONString(token));
-      if (!result) {
-        LogUtil.error(logger, "appsecret key set error.cacheKey:", cacheKey);
-      }
-    }*/
-
-   // return token.getAccessToken();
+    }
+    redisCache.setCacheObject(cacheKey, token.getAccessToken(), 1, TimeUnit.MINUTES);
+    return token.getAccessToken();
   }
 
   private String wxGetAccessToken(String appId, String appSecret) {