版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
其实文档已经真的很详细了。下面是我简单捋的 静默授权的。
<script type="text/javascript">
var that;
var vm = new Vue({
el: '#app',
data: {
msg: "测试测试",
appid: '', // 公众号的 appid
redirect_uri: '', // 公众号下的 从定向地址
response_type: 'code',
scope: 'snsapi_base',
state: '', // 传不传都行
secret: '', // 公众号的appsecret
code: '' // 连接获取到的上面得到的
},
methods: {
getOpenId() {
//1. 拿到code
window.location.reload = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid +
'&redirect_uri=' + redirect_uri + '&response_type=' + response_type + '&scope=' + scope + '&state=' + state + '#wechat_redirect';
if (code) {
that.code = code;
// 2. 通过code得到 openId
window.location.reload = ' https://api.weixin.qq.com/sns/oauth2/access_token?appid=' + appid + '&secret='++'&code=' + that.code + '&grant_type=authorization_code';
if (openId) {
console.log('拿到openId');
}else{
console.log('拿到openId,失败');
that.getOpenId();
}
}
}
},
mounted() {
this.getOpenId();
}
})
</script>
----完。