From 1ee6e3a3e34cd2c113b9b41ad2f4aee8ad2b8126 Mon Sep 17 00:00:00 2001 From: wangqing <408564126@qq.com> Date: Tue, 2 Apr 2024 16:11:47 +0800 Subject: [PATCH] fix wxNickName charset --- .../user/service/WeChatAouth2Service.java | 41 +++++++++++++++---- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java b/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java index 009db53..fc4520e 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java +++ b/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java @@ -3,6 +3,7 @@ package com.luoo.user.service; import api.Result; import api.StatusCode; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.luoo.user.dao.UserInfoDao; import com.luoo.user.dto.response.*; import com.luoo.user.dto.request.WeChatBindReq; @@ -22,7 +23,9 @@ import org.apache.http.impl.client.DefaultHttpClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; import java.io.BufferedReader; import java.io.IOException; @@ -264,18 +267,38 @@ public class WeChatAouth2Service { Aouth2WeChatJSAPIResponse aouth2WeChatJSAPIResponse = JSON.parseObject(string, Aouth2WeChatJSAPIResponse.class); return aouth2WeChatJSAPIResponse; } +// +// public String getWechatUsername(String access_token,String openid){ +// StringBuffer url = new StringBuffer(); +// url.append(Constants.HTTPS_API_WEIXIN_QQ_COM_SNS_USERINFO); +// url.append("?access_token=").append(urlEncode(access_token)); +// url.append("&openid=").append(openid); +// String string = new String(get(url.toString())); +// +// log.info("getWechatName:" + string); +// Aouth2WeChatUserinfoResponse aouth2WeChatTokenResponse = JSON.parseObject(string, Aouth2WeChatUserinfoResponse.class); +// +// return new String(aouth2WeChatTokenResponse.getNickname().getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8); +// } - public String getWechatUsername(String access_token,String openid){ - StringBuffer url = new StringBuffer(); - url.append(Constants.HTTPS_API_WEIXIN_QQ_COM_SNS_USERINFO); - url.append("?access_token=").append(urlEncode(access_token)); - url.append("&openid=").append(openid); - String string = new String(get(url.toString())); - log.info("getWechatName:" + string); - Aouth2WeChatUserinfoResponse aouth2WeChatTokenResponse = JSON.parseObject(string, Aouth2WeChatUserinfoResponse.class); + public String getWechatUsername(String accessToken,String openId) { - return new String(aouth2WeChatTokenResponse.getNickname().getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8); + String url = "https://api.weixin.qq.com/sns/userinfo?" + + "access_token=" + accessToken + + "&openid=" + openId + + "lang=zh_CN"; + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity response = restTemplate.getForEntity(url,String.class); + String responseBody = response.getBody(); + JSONObject jsonObject = (JSONObject) JSON.parse(responseBody); + String nickName = ""; + if (StringUtils.isNotBlank(responseBody)) { + nickName = jsonObject.getString("nickname"); + nickName = new String(nickName.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8); + + } + return nickName; } public Aouth2WeChatTokenResponse refreshAccessToken(String refresh_token) {