微信授权登录
注意:微信授权登录测试用户必须已经关注测试号,否则会获取不到用户信息
相关配置
使用内网穿透,配置相关端口,在JS接口安全域名和网页授权登录处配置穿透的网址,
注意:配置域名去掉http://
获取code值
在前台界面访问的url,判断是否有code值,
if (this.wxcode == null || this.wxcode == '') {
let url =
"https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + this.$common.wx.appID +
"&redirect_uri=" + 'http://ffktu3.natappfree.cc/user_signup' +
"&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect";
window.location.href = url;
}
//this.$common.wx.appID这个appId使用的是自己微信测试号里的appId
//&redirect_uri=后面拼接的是自己的重定向路径,注意这里要用到内网穿透的地址
注意:&redirect_uri=后面拼接的路径要使用可以访问的路径,本地接口locahost:8080的话会出出错,"&redirect_uri"错误,必须使用本地测试的话,推荐使用内网穿透
关于appId:参考https://blog.csdn.net/qq_45587153/article/details/116428667
内网穿透:https://blog.csdn.net/qq_45587153/article/details/116587789
截取url中code的方法
//截取code值
getUrlParam(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
let url = window.location.href.split('#')[0]
let search = url.split('?')[1]
if (search) {
var r = search.substr(0).match(reg)
if (r !== null)
return unescape(r[2])
return null
} else
return null
}
//调用:
this.wxcode = this.getUrlParam('code')
拉取用户信息
后台java代码
@GetMapping("/getUserInfo/{code}")
public ResultJson getUserInfo(