授权码code获取用户基本信息
上一遍已经获得微信回调的code,网址:回调获取code 那这篇通过code和其它参数去获得用户基本信息。
github源码地址:https://github.com/yudiandemingzi/spring-boot-wechat-login
1、UserServiceImpl关键代码
@Override public User saveWeChatUser(String code) { //1、通过openAppid和openAppsecret和微信返回的code,拼接URL String accessTokenUrl = String.format(WeChatConfig.getOpenAccessTokenUrl(),weChatConfig.getOpenAppid(),weChatConfig.getOpenAppsecret(),code); //2、通过URL再去回调微信接口 (使用了httpclient和gson工具) Map<String ,Object> baseMap = HttpUtils.doGet(accessTokenUrl); //3、回调成功后获取access_token和oppid if(baseMap == null || baseMap.isEmpty()){ return null; } String accessToken = (String)baseMap.get("access_token"); String openId = (String) baseMap.get("openid"); //4、去数据库查看该用户之前是否已经扫码登陆过(openid是用户唯一标志) User dbUser = userMapper.findByopenid(openId); if(dbUser!=null) { //如果用户已经存在,直接返回 return dbUser; } //4、access_token和openid拼接URL String userInfoUrl = String.format(WeChatConfig.getOpenUserInfoUrl(),accessToken,openId); //5、通过URL再去调微信接口获取用户基本信息 Map<String ,Object> baseUserMap = HttpUtils.doGet(userInfoUrl); if(baseUserMap == null || baseUserMap.isEmpty()){ return null; } //6、获取用户姓名、性别、城市、头像等基本信息 String nickname = (String)baseUserMap.get("nickname"); Double sexTemp = (Double) baseUserMap.get("sex"); int sex = sexTemp.intValue(); String province = (String)baseUserMap.get("province"); String city = (String)baseUserMap.get("city"); String country = (String)baseUserMap.get("country"); String headimgurl = (String)baseUserMap.get("headimgurl"); StringBuilder sb = new StringBuilder(country).append("||").append(province).append("||").append(city); String finalAddress = sb.toString(); try { //解决用户名乱码 nickname = new String(nickname.getBytes("ISO-8859-1"), "UTF-8"); finalAddress = new String(finalAddress.getBytes("ISO-8859-1"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } //7、新用户存入数据库 User user = new User(); user.setName(nickname); user.setHeadImg(headimgurl); user.setCity(finalAddress); user.setOpenid(openId); user.setSex(sex); user.setCreateTime(new Date()); userMapper.save(user); return user; }
2、HttpUtils工具类
(1)首先需要httpclient、gson相关jar包
<!--httpclient相关jar包--> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.5.2</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> </dependency> <!-- gson工具,封装http的时候使用 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.0</version> </dependency>
(2)代码部分
/** * 封装http get post */ public class HttpUtils { private static final Gson gson = new Gson(); /** * get方法 * @param url * @return */ public static Map<String,Object> doGet(String url){ Map<String,Object> map = new HashMap<>(); CloseableHttpClient httpClient = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000) //连接超时 .setConnectionRequestTimeout(5000)//请求超时 .setSocketTimeout(5000) .setRedirectsEnabled(true) //允许自动重定向 .build(); HttpGet httpGet = new HttpGet(url); httpGet.setConfig(requestConfig); try{ HttpResponse httpResponse = httpClient.execute(httpGet); if(httpResponse.getStatusLine().getStatusCode() == 200){ String jsonResult = EntityUtils.toString( httpResponse.getEntity()); map = gson.fromJson(jsonResult,map.getClass()); } }catch (Exception e){ e.printStackTrace(); }finally { try { httpClient.close(); }catch (Exception e){ e.printStackTrace(); } } return map; } /** * 封装post * @return */ public static String doPost(String url, String data,int timeout){ CloseableHttpClient httpClient = HttpClients.createDefault(); //超时设置 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout) //连接超时 .setConnectionRequestTimeout(timeout)//请求超时 .setSocketTimeout(timeout) .setRedirectsEnabled(true) //允许自动重定向 .build(); HttpPost httpPost = new HttpPost(url); httpPost.setConfig(requestConfig); httpPost.addHeader("Content-Type","text/html; chartset=UTF-8"); if(data != null && data instanceof String){ //使用字符串传参 StringEntity stringEntity = new StringEntity(data,"UTF-8"); httpPost.setEntity(stringEntity); } try{ CloseableHttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); if(httpResponse.getStatusLine().getStatusCode() == 200){ String result = EntityUtils.toString(httpEntity); return result; } }catch (Exception e){ e.printStackTrace(); }finally { try{ httpClient.close(); }catch (Exception e){ e.printStackTrace(); } } return null; } }
HttpUtils
参考
微信官方文档
1、通过code获取access_token
2、通过access_token获取微信用户头像和昵称等基本信息
我只是偶尔安静下来,对过去的种种思忖一番。那些曾经的旧时光里即便有过天真愚钝,也不值得谴责。毕竟,往后的日子,还很长。不断鼓励自己,
天一亮,又是崭新的起点,又是未知的征程(上校14)
相关文章
暂无评论...