准备工作:小程序和公众号需要绑定在同一开放平台下(这样会有unionid,用来验证是否为同一用户);公众号设置好白名单,网页授权域名(这个要和小程序的api访问域名一致)
步骤:1.准备一个跳转的html页面,放在用网页授权域名可以访问到的地址
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>公众号信息网页授权</title>
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<script type="text/javascript">
let url = window.location.href;
if(url.includes('code')) {
if (wx.miniProgram) {
wx.miniProgram.reLaunch({
url: `跳转回的小程序页面`,
success: function() {
},
fall: function() {
}
})
}
} else {
window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=公众号的appid&redirect_uri=当前这个html的访问地址.html&response_type=code&scope=snsapi_userinfo(这是可以获取用户信息的code,不需要可以使用静默授权snsapi_base)&state=#wechat_redirect"
}
}
</script>
</head>
<body>
</body>
</html>
2.小程序中新增一个web-view页面,url放html的地址
<template>
<view>
<web-view src="html的访问地址"></web-view>
</view>
</template>
<script>
export default {
components: { },
data() {
return {
}
},
}
</script>