以下内容也是我测试过的内容,主要也是通过官方的文档发现的这个API,如果不仔细阅读可能也不会发现这个功能,会让我的项目进度卡住!!!
以下就是官方文档的API:微信网页开发 / 网页授权 (qq.com)
主要就是通过这个网页授权的功能去获取到微信用户的openID,其实就像我们平时进入一个页面啊什么的,就会弹出一个授权的页面一样。
具体的内容就看文档即可,主要就是需要去配置自己的域名,必须是HTTPS的请求,不然不生效。
注:这些功能的前提条件是我最开始写微信公众号的那篇文章的内容,必须先通过微信验证!!!
文章地址:微信公众号模板消息接口开发实现_微信公众号模板接口-CSDN博客
然后下面就是开发代码(简单):
//生成授权链接
@GetMapping("/×××××")
@ApiOperation(value = "获取用户授权地址")
public ResultBean<String> getWxUrl() throws IOException {
String urlString = URLEncoder.encode("填写自己需要回调的地址接口", "UTF-8");
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=填写自己的appid&redirect_uri=" + urlString + "&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
return new ResultBean<>(url);
}
//回调的地址接口
@GetMapping("×××××")
@ApiOperation(value = "获取用户的openId")
public ResultBean<String> geUserOpenId(@RequestParam("code") String code,
@RequestParam("state") String state) throws IOException {
Map<String,String> map=new HashMap<>();
String access_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=填写自己的appid&secret=填写自己的secret&code=" + code + "&grant_type=authorization_code";
String post = HttpRestUtils.post(access_url, "");
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(post);
String openid = jsonObject.getString("openid");
map.put("openid",openid);
String access_token = jsonObject.getString("access_token");
String urls = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN";
String posts = HttpRestUtils.post(urls, "");
com.alibaba.fastjson.JSONObject postsJsonObject = com.alibaba.fastjson.JSONObject.parseObject(posts);
String nickname = postsJsonObject.getString("nickname");//微信昵称
//根据自己的需求去获取数据并且返回即可
return new ResultBean<>(posts);
}
然后发现下来其实这个功能非常简单。关联起来之后呢,把openId与用户进行对照,就可以根据需求给某一个用户进行消息通知的,是会方便许多。
好滴,谢谢大家的持续关注!!!