/**
* 获取OPENID 在小程序中获取code
*/
@PostMapping("/getOpenId")
@ResponseBody
public Map<String, Object> getOpenId(@RequestParam(name = "code") String code) {
return appletService.getOpenId(code,taskNo);
}
public Map<String,Object> getOpenId(String code) {
//登录凭证不能为空
if (code == null || "".equals(code)) {
Map map = new HashMap(3);
map.put("status" , "ERROR");
map.put("code" , "02");
map.put("msg" , "code 不能为空");
return map;
}
//小程序appid
String wxspAppid = "";
//小程序密钥
String wxspSecret ="b";
String grant_type = "authorization_code";
//1、向微信服务器 使用登录凭证 code 获取 session_key 和 openid
//请求参数
String params = "appid=" + wxspAppid + "&secret=" + wxspSecret + "&js_code=" + code + "&grant_type=" + grant_type;
//发送请求
String sr = HttpUtil.sendGet("https://api.weixin.qq.com/sns/jscode2session" , params);
JSONObject objects = JSONObject.parseObject(sr);
Object openId = objects.get("openid");
//获取到的openid
System.out.println(openId);
}
微信小程序获取openid
最新推荐文章于 2023-08-08 15:50:04 发布