/**
* @param
* @Description: 获取微信公众号用户登录信息
* @date 2022/04/22
*/
public JSONObject selectWxUser(String code) {
JSONObject jsonWxUser = new JSONObject();
Map map = new HashMap<>();
CloseableHttpClient httpclient = HttpClients.createDefault();
//调用微信接口
HttpGet httpget = new HttpGet(WxUtil.GET_ACCESS_TOKEN_URL + WxUtil.APP_ID
+ "&secret=" + WxUtil.APP_SECRET + "&code=" + code + "&grant_type=authorization_code");
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
JSONObject jsonObject = JSON.parseObject(EntityUtils.toString(entity));
//获取微信公众号用户openid返回的数据
String openid = jsonObject.getString("openid");
log.info("openid数据为:"+openid);
//根据openid 去数据库查询是否存在
String accesstToken = jsonObject.getString("access_token");
String scope = jsonObject.getString("scope");
log.info("scope:"+scope);
//通过openid以及accesstToken 获取用户信息
// 拉取用户信息(需scope为 snsapi_userinfo)
String infoUrl = WxUtil.ACCESS_TOKEN_URL_BY+accesstToken
+ "&openid="+openid + "&lang=zh_CN";
log.info("infoUrl:"+infoUrl);
JSONObject userInfo = WeChatUtils.doGetJson(infoUrl);
String nickName = userInfo.getString("nickname");
//用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
String headimgurl = userInfo.getString("headimgurl");
log.info("用户昵称:{}", nickName);
log.info("用户头像:{}", headimgurl);
map.put("phone", null);
map.put("openid", openid);
map.put("nickName", nickName);
map.put("headimgurl", headimgurl);
jsonWxUser.put("code", 0);
jsonWxUser.put("data", map);
} catch (IOException e) {
log.info("后台报错:{}", e.toString());
}
return jsonWxUser;
}
package com.rmsz.oa.utils;
/**
* @Author 微信工具类
* @Date 2022/04/22
*/
public class WxUtil {
public static final String APP_ID = "*******";
public static final String APP_SECRET = "*************";
/**
* 获取code后,请求以下链接获取access_token:
* https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE
* &grant_type=authorization_code
*/
public static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=";
public static final String ACCESS_TOKEN_URL_BY = "https://api.weixin.qq.com/sns/userinfo?access_token=";
}
package com.rmsz.oa.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class WeChatUtils {
/**
* @param url
* @return
* @throws Exception
*/
public static JSONObject doGetJson(String url){
JSONObject jsonObject =null;
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet =new HttpGet(url);
HttpResponse response = null;
try {
response = client.execute(httpGet);
HttpEntity entity =response.getEntity();
if(entity!=null)
{
//把返回的结果转换为JSON对象
String result = EntityUtils.toString(entity, "UTF-8");
jsonObject = JSON.parseObject(result);
}
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
/**
* shal加密
* @param src
* @return
*/
public static String shal(String src){
//获取一个加密对象
MessageDigest md = null;
try {
md = MessageDigest.getInstance("sha1");
char[] chars={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
StringBuilder sb = new StringBuilder();
//加密
byte[] digest = md.digest(src.getBytes());
//处理加密结果
for (byte b : digest) {
sb.append(chars[(b>>4)&15]) ;
sb.append(chars[b&15]);
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
}