实现思路:前端访问微信授权链接,获取code,传给后端获取用户信息实现静默登录。
参考官方文档:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
示例代码:
<template>
<div>获取微信code静默登录</div>
</template>
<script>
export default {
name: 'login',
data() {
return {
code: '',
}
},
created() {
this.code = this.$route.query.code
if (this.code) {
this.login()
} else {
this.getWXcode()
}
},
methods: {
getWXcode() {
let appId = 'wx123456789' //示例,公众号appid
let redirectUri= encodeURIComponent(window.location.href)
//去微信公众号官方授权页根据appid获取code,之后会携带code值自行返回redirect_uri页面
window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appId + '&redirect_uri=' + redirectUri + '&response_type=code&scope=snsapi_base#wechat_redirect'
},
login() {
// 可以发起登录请求将code值传给后端,拿到用户信息实现静默登录
}
}
}
</script>