瀏覽代碼

Merge remote-tracking branch 'origin/master'

Ben 5 月之前
父節點
當前提交
7b4d345643

+ 12 - 1
feifan-module-mall/feifan-module-trade-biz/src/main/java/cn/newfeifan/mall/module/trade/utils/wechat/WcChatMessageUtils.java

@@ -83,7 +83,7 @@ public class WcChatMessageUtils {
             stringRedisTemplate.opsForValue().set(WX_TICK_KEY_APPID, accessToken, expireTime, java.util.concurrent.TimeUnit.SECONDS);
         }
 
-        log.info("===========Access_token:{}",accessToken);
+        log.info("===========Access_token:{}", accessToken);
 
         return accessToken;
     }
@@ -123,6 +123,10 @@ public class WcChatMessageUtils {
         message.setClient_msg_id(client_msg_id);
         message.setData(data);
 
+        sendWecChatMessage(userId, wechatMsgTemplateId, systemUserId, memberUserId, objectId, message, userOpenId ,1);
+    }
+
+    private void sendWecChatMessage(Long userId, String wechatMsgTemplateId, Long systemUserId, Long memberUserId, Long objectId, WeChatTemplateMessage message, String userOpenId, int maxRetries) {
         // 发送消息
         String returnMsg = HttpUtil.post(StrUtil.format("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}", getAccessToken()), message.toString());
         cn.hutool.json.JSONObject jsonObject2 = JSONUtil.parseObj(returnMsg);
@@ -147,6 +151,13 @@ public class WcChatMessageUtils {
         }
 
         messageLogService.createMessageLog(messageLogSaveReqVO);
+
+        // 如果微信的accessToken失效,则重新获取,最多重试3次
+        if (jsonObject2.getStr("errcode").equals("40001") && maxRetries <= 3) {
+            maxRetries++;
+            stringRedisTemplate.delete(WX_TICK_KEY_APPID);
+            sendWecChatMessage(userId, wechatMsgTemplateId, systemUserId, memberUserId, objectId, message, userOpenId, maxRetries);
+        }
     }
 
     /**