wwl hace 3 años
padre
commit
7ed6d0cc72

+ 13 - 13
twzd-service/src/main/java/com/miaxis/wx/service/impl/WxGzhServiceImpl.java

@@ -3,10 +3,13 @@ package com.miaxis.wx.service.impl;
 import com.alibaba.fastjson.JSONObject;
 import com.miaxis.common.constant.Constants;
 import com.miaxis.common.core.redis.RedisCache;
+import com.miaxis.common.utils.StringUtils;
 import com.miaxis.feign.service.IWxSendService;
 import com.miaxis.wx.service.IWxGzhService;
+import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
@@ -20,14 +23,12 @@ import java.util.concurrent.TimeUnit;
  */
 @Service
 @Slf4j
+@RequiredArgsConstructor
 public class WxGzhServiceImpl implements IWxGzhService {
 
+    private final RedisTemplate redisTemplate;
 
-    @Resource
-    private RedisCache redisCache;
-
-    @Resource
-    private IWxSendService wxSendService;
+    private final  IWxSendService wxSendService;
 
     @Value("${app.appid}")
     private String appid;
@@ -38,18 +39,17 @@ public class WxGzhServiceImpl implements IWxGzhService {
 
     @Override
     public String getGzhToken() {
-        Object cacheObject = redisCache.getCacheObject(Constants.GZH_MESSAGE_TOKEN);
-        //如果过期,重新获取并保存到redis
-        if (cacheObject == null){
+        String token = "";
+        if (redisTemplate.hasKey(Constants.GZH_MESSAGE_TOKEN)){
+            token = (String)redisTemplate.opsForValue().get(Constants.GZH_MESSAGE_TOKEN);
+        }else if (StringUtils.isEmpty(token)){
             String result = wxSendService.getAccessToken("client_credential",appid,secret);
             JSONObject json = JSONObject.parseObject(result);
-            String token = json.getString("access_token");
+            token = json.getString("access_token");
             int expiresIn = json.getIntValue("expires_in");
-            redisCache.setCacheObject(Constants.GZH_MESSAGE_TOKEN,token,expiresIn, TimeUnit.SECONDS);
-            return token;
-        }else {
-            return String.valueOf(cacheObject);
+            redisTemplate.opsForValue().set(Constants.GZH_MESSAGE_TOKEN,token,expiresIn, TimeUnit.SECONDS);
         }
 
+        return token;
     }
 }