1、在微信公众号平台:接口权限->网页服务->网页授权 点击修改进入功能设置。设置网页授权域名
2、下载txt文件,放入服务器,填写域名。注意: 域名/MP_***.txt(文件名) 要能访问到下载下来放到服务器上的txt文件
(可以直接将文件放在ROOT下面,注意spring里过滤的路径 或者在xml 里跳转)
3、uniapp开发公众号手机网站登录
首先
onLoad(){
//1. 页面一加载,判断有没有code(对h5,因为授权成功之后,会刷新当前页面,此时应该在页面一加载的时候判断有没有code,如果有code,直接把code传给后台服务器)
let code = this.getUrlCode('code')
console.log(code)
if(code != null && code != "") {
console.log(111)
this.getOpenidAndUserinfo(code)
}
},
在method 里
// 对H5,获取code
// 先判断url中有没有code,如果有code,表明已经授权,如果没有code,跳转微信授权链接
getCode () {
if(String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger") {//判断是不是微信浏览器
// 截取地址中的code,如果没有code就去微信授权,如果已经获取到code了就直接把code传给后台获取openId
let code = this.getUrlCode('code')
if (code === null || code === '') {
window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx********&redirect_uri=' + encodeURIComponent('https://********/h5/#/pages/public/login') + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'
// redirect_uri是授权成功后,跳转的url地址,微信会帮我们跳转到该链接,并且通过?的形式拼接code,这里需要用encodeURIComponent对链接进行处理。
// 如果配置参数一一对应,那么此时已经通过回调地址刷新页面后,你就会再地址栏中看到code了。
// http://127.0.0.1/pages/login/login?code=001BWV4J1lRz*********J1BWV4q&state=1
}
}
},
getUrlCode (name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null
},
getOpenidAndUserinfo(code) {
uni.request({
url: this.$url+'/api/weChatPublicLogin.do?code='+code,//将code传到后台或者在uniapp获取用户信息
success: (result) => {
if(result.data.state === 1){
this.login(result.data.data);
uni.switchTab({
url: '/pages/user/user'
})
}else{
this.$api.msg(result.data.message);
this.logining = false;
}
}
})
}
4、后台得倒code,然后获取到 openid 和 unionid,还可以获得微信用户信息,根据业务逻辑再继续