fix wxNickName charset

main
wangqing 8 months ago
parent aed8faee29
commit 1ee6e3a3e3

@ -3,6 +3,7 @@ package com.luoo.user.service;
import api.Result; import api.Result;
import api.StatusCode; import api.StatusCode;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.luoo.user.dao.UserInfoDao; import com.luoo.user.dao.UserInfoDao;
import com.luoo.user.dto.response.*; import com.luoo.user.dto.response.*;
import com.luoo.user.dto.request.WeChatBindReq; 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.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
@ -264,18 +267,38 @@ public class WeChatAouth2Service {
Aouth2WeChatJSAPIResponse aouth2WeChatJSAPIResponse = JSON.parseObject(string, Aouth2WeChatJSAPIResponse.class); Aouth2WeChatJSAPIResponse aouth2WeChatJSAPIResponse = JSON.parseObject(string, Aouth2WeChatJSAPIResponse.class);
return aouth2WeChatJSAPIResponse; 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); public String getWechatUsername(String accessToken,String openId) {
Aouth2WeChatUserinfoResponse aouth2WeChatTokenResponse = JSON.parseObject(string, Aouth2WeChatUserinfoResponse.class);
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<String> 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) { public Aouth2WeChatTokenResponse refreshAccessToken(String refresh_token) {

Loading…
Cancel
Save