Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
promotion-service
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
promotion
promotion-service
Commits
b5f97239
提交
b5f97239
authored
11月 18, 2025
作者:
吕本才
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(config): 更换accesstoken接口
上级
7ea99eea
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
49 行增加
和
1 行删除
+49
-1
SubscribeMessageService.java
...vice/activity/temporary/impl/SubscribeMessageService.java
+1
-1
WechatAccessTokenUtil.java
...com/wangxiaolu/promotion/utils/WechatAccessTokenUtil.java
+48
-0
没有找到文件。
src/main/java/com/wangxiaolu/promotion/service/activity/temporary/impl/SubscribeMessageService.java
浏览文件 @
b5f97239
...
@@ -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
.
get
Stable
AccessToken
();
// 发送订阅通知的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
;
...
...
src/main/java/com/wangxiaolu/promotion/utils/WechatAccessTokenUtil.java
浏览文件 @
b5f97239
...
@@ -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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论