一、微信开放平台 注册开发者账号、绑定公众号、小程序
二、小程序端获取unionid
1获取code
wx.login({
success: res => {
console.log("getCode", res.code)
this.getOpenId(res.code)
}
})
2通过code调用后台方法获取openid,unionid
小程序端
getOpenId: function (code) {
let url = config.apiUrl + "/wx/user/getOpenId";
var param = {
"code": code
};
util.httpPost(url, param).then((res) => {
console.log(res);
})
},
java端
@RequestMapping(value = "/getOpenId", method = RequestMethod.POST)
public Result<?> callBack(@RequestBody WeixinModel model) throws ServletException, IOException {
System.out.println("start");
String code = model.code;
//第二步:通过code换取网页授权access_token
String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+ appid1
+ "&secret="+ secret1
+ "&js_code="+code
+ "&grant_type=authorization_code";
System.out.println("url:"+url);
JSONObject jsonObject = WeixinHelper.doGetJson(url);
return Result.ok(jsonObject);
}
效果
三、公众号通过openid获取unionid
1获取accss_token
public static String getNewAccessToken() {
String access_token = "";
String grant_type = "client_credential";//获取access_token填写client_credential
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=" + grant_type + "&appid=" + appId + "&secret=" + secret;
try {
URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
http.setRequestMethod("GET"); // 必须是get方式请求
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String message = new String(jsonBytes, "UTF-8");
JSONObject demoJson = JSONObject.parseObject(message);
System.out.println("JSON字符串:" + demoJson);
access_token = demoJson.getString("access_token");
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return access_token;
}
2通过openid获取unionid
private static String getUnionId(String openId) {
String result = null;
String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openId + "&lang=zh_CN";//这个url链接和参数不能变
try {
URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
http.setRequestMethod("GET"); // 必须是get方式请求
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String message = new String(jsonBytes, "UTF-8");
JSONObject demoJson = JSONObject.parseObject(message);
System.out.println("JSON字符串:" + demoJson);
result = demoJson.getString("unionid");
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
测试
public static void main(String[] args) throws ClientException, InterruptedException {
WeixinHelper.setAppId("wx88e7974f9***8be");
WeixinHelper.setSecret("0b73d127be64****416cd6eb105c20dc");
String accessToken = getAccessToken();
System.out.println(accessToken);
String unioinId = getUnionId("ots5M6wVAgsx5B****eJrfjajiE");
}
效果