From 845a553097290cd2c77e021804de9f572c14bf9d Mon Sep 17 00:00:00 2001 From: shahaibo <1023316923@qq.com> Date: Mon, 13 Jan 2025 14:11:03 +0800 Subject: [PATCH] =?UTF-8?q?BUGFIX:=E5=BE=AE=E4=BF=A1=E5=90=8D=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E3=80=81toproductimage=20=E6=89=93=E5=85=89=E7=A7=AF?= =?UTF-8?q?=E5=88=86=E4=B8=8D=E8=B6=B3code=E7=A0=81=E4=BF=AE=E6=94=B9;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../da/service/impl/AccountServiceImpl.java | 21 +++++++++++++++++++ .../impl/UserLikeGroupServiceImpl.java | 5 +++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java b/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java index 1bf8ec30..889d969f 100644 --- a/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java @@ -49,6 +49,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.sql.DataSource; import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -2405,6 +2406,15 @@ public class AccountServiceImpl extends ServiceImpl impl RestTemplate restTemplate = new RestTemplate(); String response = restTemplate.getForObject(url, String.class); + // 强制校正编码为 UTF-8(如果返回的是 ISO-8859-1 编码) + if (!isUTF8(response)) { // 检查编码方法,下面会提供 + byte[] bytes = response.getBytes(StandardCharsets.ISO_8859_1); + response = new String(bytes, StandardCharsets.UTF_8); + } + + // 打印调试信息 + log.info("WeChat user info response: {}", response); + // 转换为 JSON 对象 JSONObject jsonResponse = JSONObject.parseObject(response); if (jsonResponse.containsKey("errcode")) { @@ -2414,6 +2424,17 @@ public class AccountServiceImpl extends ServiceImpl impl return jsonResponse; } + private boolean isUTF8(String text) { + try { + byte[] bytes = text.getBytes(StandardCharsets.ISO_8859_1); + String decoded = new String(bytes, StandardCharsets.UTF_8); + return decoded.equals(text); + } catch (Exception e) { + return false; + } + } + + private JSONObject getAccessTokenFromWeChat(String code) { // 构造微信接口请求 URL String url = String.format( diff --git a/src/main/java/com/ai/da/service/impl/UserLikeGroupServiceImpl.java b/src/main/java/com/ai/da/service/impl/UserLikeGroupServiceImpl.java index 87ced60b..ee988979 100644 --- a/src/main/java/com/ai/da/service/impl/UserLikeGroupServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/UserLikeGroupServiceImpl.java @@ -5,6 +5,7 @@ import com.ai.da.common.config.exception.BusinessException; import com.ai.da.common.constant.CommonConstant; import com.ai.da.common.context.UserContext; import com.ai.da.common.enums.CreditsEventsEnum; +import com.ai.da.common.response.ResultEnum; import com.ai.da.common.utils.*; import com.ai.da.mapper.primary.*; import com.ai.da.mapper.primary.entity.*; @@ -274,7 +275,7 @@ public class UserLikeGroupServiceImpl extends ServiceImpl