提交 b5f97239 authored 作者: 吕本才's avatar 吕本才

feat(config): 更换accesstoken接口

上级 7ea99eea
...@@ -58,7 +58,7 @@ public class SubscribeMessageService { ...@@ -58,7 +58,7 @@ public class SubscribeMessageService {
requestBody.put("data", dataMap); requestBody.put("data", dataMap);
// 获取access_token,拼接完整URL // 获取access_token,拼接完整URL
String accessToken = tokenUtil.getAccessToken(); String accessToken = tokenUtil.getStableAccessToken();
// 发送订阅通知的API地址 // 发送订阅通知的API地址
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+ accessToken; String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+ accessToken;
......
...@@ -2,16 +2,25 @@ package com.wangxiaolu.promotion.utils; ...@@ -2,16 +2,25 @@ package com.wangxiaolu.promotion.utils;
import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.wangxiaolu.promotion.common.constant.Constants; import com.wangxiaolu.promotion.common.constant.Constants;
import com.wangxiaolu.promotion.common.redis.service.RedisCache; import com.wangxiaolu.promotion.common.redis.service.RedisCache;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Component @Component
@Slf4j @Slf4j
...@@ -21,6 +30,7 @@ public class WechatAccessTokenUtil { ...@@ -21,6 +30,7 @@ public class WechatAccessTokenUtil {
@Value("${wx.cx_miniapp.app_secret}") @Value("${wx.cx_miniapp.app_secret}")
private String secret; private String secret;
private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"; private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
private static final String STABLE_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/stable_token";
// 缓存access_token,避免频繁调用 // 缓存access_token,避免频繁调用
private String accessToken; private String accessToken;
...@@ -61,4 +71,42 @@ public class WechatAccessTokenUtil { ...@@ -61,4 +71,42 @@ public class WechatAccessTokenUtil {
return accessToken; return accessToken;
} }
public String getStableAccessToken() throws IOException {
String accessTokenRedisCache = redisCache.get(Constants.REDIS_ACCESS_TOKEN);
// 检查是否过期,未过期直接返回
if (ObjectUtil.isNotEmpty(accessTokenRedisCache)) {
return accessTokenRedisCache;
}
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(STABLE_TOKEN_URL);
// 2. 构建请求参数(普通模式,force_refresh=false)
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("grant_type", "client_credential");
paramMap.put("appid", appid);
paramMap.put("secret", secret);
// 普通模式,避免频繁刷新
paramMap.put("force_refresh", false);
// 3. 设置请求头(JSON格式)
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
httpPost.setEntity(new StringEntity(JSON.toJSONString(paramMap), "UTF-8"));
// 4. 发送请求并解析响应
String response = EntityUtils.toString(httpClient.execute(httpPost).getEntity());
Map<String, Object> resultMap = JSON.parseObject(response, Map.class);
// 5. 处理响应结果(成功则更新缓存,失败则抛异常)
if (resultMap.get("errcode") != null && !"0".equals(resultMap.get("errcode").toString())) {
throw new RuntimeException("获取 access_token 失败:" + resultMap.get("errmsg"));
}
accessToken = resultMap.get("access_token").toString();
int expiresIn = Integer.parseInt(resultMap.get("expires_in").toString());
log.info("获取微信access_token,url:{},response:{}",STABLE_TOKEN_URL,response);
redisCache.addToSeconds(Constants.REDIS_ACCESS_TOKEN,accessToken,expiresIn - 300);
return accessToken;
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论