获取失败问题

This commit is contained in:
wangw 2025-10-20 15:02:53 +08:00
parent e0fc346f26
commit c361fe651d
2 changed files with 16 additions and 8 deletions

View File

@ -139,9 +139,12 @@ public class UserAuthorizationServiceImpl implements UserAuthorizationService {
private void initAc(UserInfo userInfo) {
if (StrUtil.isBlank(userInfo.getWechatAcOpenId()) &&
(userInfo.getAcQrcodeValidTime() == null || userInfo.getAcQrcodeValidTime().isBefore(LocalDateTime.now()))) {
userInfo.setWechatAcQrcode(acAccountUtil.createQrCode(userInfo.getId()));
userInfo.setAcQrcodeValidTime(LocalDateTime.now().plusDays(29));
userInfoService.updateById(userInfo);
String qrCode = acAccountUtil.createQrCode(userInfo.getId());
if (StrUtil.isNotBlank(qrCode)) {
userInfo.setWechatAcQrcode(qrCode);
userInfo.setAcQrcodeValidTime(LocalDateTime.now().plusDays(29));
userInfoService.updateById(userInfo);
}
}
}
}

View File

@ -49,9 +49,10 @@ public class AcAccountUtil {
* 获取临时二维码用于关注公众号 30天即2592000秒后过期
*/
public String createQrCode(Long userId) {
String accessToken = Convert.toStr(redisService.get("accessToken"));
if (StrUtil.isNotEmpty(accessToken)) {
return accessToken;
String accessToken = getAccessToken();
if (StrUtil.isBlank(accessToken)) {
log.error("获取 access_token 失败");
return "";
}
JSONObject bodyJson = new JSONObject();
//二维码有效时间最大2592000仅临时二维码需要
@ -64,7 +65,7 @@ public class AcAccountUtil {
actionInfo.put("scene", scene);
bodyJson.put("action_info", actionInfo);
String resp = HttpUtil.post(StrUtil.format("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={}", getAccessToken()), bodyJson);
String resp = HttpUtil.post(StrUtil.format("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={}", accessToken), bodyJson);
/**
* {
* "ticket": "gQH47joAAAAAAAAAASxodHRwOi8vd2VpeGlu...",
@ -73,6 +74,10 @@ public class AcAccountUtil {
* }
*/
JSONObject respInfo = JSONObject.parseObject(resp);
if (respInfo.get("url") == null) {
log.error("创建临时二维码失败, 响应内容: {}", resp);
return "";
}
return respInfo.get("url").toString();
}
@ -88,7 +93,7 @@ public class AcAccountUtil {
JSONObject respInfo = JSONObject.parseObject(resp);
if (!respInfo.containsKey("access_token")) {
log.warn("公众号获取token失败, 响应内容: {}", resp);
throw new RuntimeException(resp);
return "";
}
accessToken = respInfo.getString("access_token");
int expiresIn = respInfo.getInteger("expires_in");