以下是使用 web-view 并配配合微信公众号提供的 网页授权 来实现
onLoad() {
this.setData({
src: 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=`自己公众号openid`&redirect_uri=自己跳转的H5页面&response_type=code&scope=snsapi_base&state=123#wechat_redirect',
})
},
redirect_uri:必须是https开头,并且需要如下配置
location.search
获取code
;返回微信小程序,需要安装weixin-js-sdk
<web-view src="{{src}}" />
Page({
data: {
src: '',
},
onLoad() {
this.setData({
src: 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=自己的公众号openid&redirect_uri=需要跳转的H5页面&response_type=code&scope=snsapi_base&state=123#wechat_redirect',
})
}
});
<template>
<div class="wx-h5"></div>
</template>
<script>
import wx from "weixin-js-sdk";
import { showFullScreenLoading } from "@/utils/loading";
export default {
name: "WXh5",
mounted() {
// 页面loading
showFullScreenLoading("页面跳转中...", "", "wx-text-color");
// 跳转到tarBar页面
// wx.miniProgram.switchTab({ url: "****" });
if (this.getUrlCode().code) {
wx.miniProgram.redirectTo({url: `/subpackages/extension/index?code=${this.getUrlCode().code}`})
}
},
methods: {
getUrlCode() {
// 截取url中的code方法
var url = location.search
// eslint-disable-next-line no-new-object
var theRequest = new Object()
if (url.indexOf('?') != -1) {
var str = url.substr(1)
var strs = str.split('&')
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1]
}
}
return theRequest
}
}
};
</script>
<style lang="scss">
.wx-text-color {
width: 90px;
height: 105px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border-radius: 10px;
.circular {
.path {
stroke: #fff;
}
}
.el-loading-text {
color: #fff;
font-size: 12px;
}
}
</style>
/subpackages/extension/index
页面的onLoad
接收参数code
,便可根据code获取公众号的openId微信小程序的web-view页面只能独立出来使用,不能在
/subpackages/extension/index
页面使用,不然返回到/subpackages/extension/index
页面后,该页面的事件都会失效。