通过微信code获取微信openid
@GetMapping("/getOpenId")
public R getOpenId(String code) {
String openId = wxPayService.getOpenId(code);
logger.info("通过code获取到的openId:"+openId);
if (openId == null) {
return R.error("500", "code失效,请传入最新的code");
}
return R.ok().put("openId", openId);
}
public String getOpenId(String code) {
String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + wxAppId + "&secret="
+ wxSecret + "&code=" + code + "&grant_type=authorization_code";
String openIdStr = HttpUtils.sendGet(requestUrl);
JSONObject jsonObject = JSONObject.parseObject(openIdStr);
return jsonObject.getString("openid");
}