liren123.cn
1.微信小程序設(shè)置
index.wxml
<view class="container">
<web-view src="{{webUrl}}" bindmessage="msgHandler"></web-view>? ?//通過web-view綁定現(xiàn)有的H5網(wǎng)站頁面
</view>
index.js
msgHandler: function (e) { //(h5像小程序傳遞參數(shù))
? ? console.log(e.detail.data) //獲取到來自也頁面的數(shù)據(jù)
? ? var info = (e.detail.data)[e.detail.data.length-1]
? ? this.setData({
? ? ? value: info.value? ?
? ? });
? ? //console.log('msgHandler:value: info.value:' + this.data.value)
? ? this.setData({
? ? ? title: info.title
? ? });
? ? //console.log('msgHandler:value: info.title:' + this.data.title)
? },
onLoad: function (options){
? ? var webUrl = '';
? ? if (options.value) {//獲取轉(zhuǎn)發(fā)過來的參數(shù)
? ? ? webUrl =? options.value;
? ? ? console.log('onLoad:webUrl =? options.value'+webUrl)
? ? } else {
? ? ? webUrl = "https://liren123.cn";? ? ? ? ??
? ? ? console.log('onLoad:webUrl = "https://liren123.cn' + webUrl)
? ? }
? ? this.setData({
? ? ? webUrl: webUrl
? ? })
? },?
//轉(zhuǎn)發(fā)
? onShareAppMessage: function (options){? ?
? ? var that = this;
? ? //console.log('onShareAppMessage:data.title1=' + that.data.title);
? ? //console.log('onShareAppMessage:data.link1=' + that.data.value);
? ? return {
? ? ? title: that.data.title,
? ? ? path: '/pages/index/index?value=' + that.data.value,//小程序像h5傳遞參數(shù)
? ? ? success: function (res) { }
? ? }
? }
2.H5頁面設(shè)置
<script type="text/javascript">
var ua = window.navigator.userAgent.toLowerCase();
? if (ua.match(/MicroMessenger/i) == 'micromessenger') {? //判斷是不是微信環(huán)境
wx.miniProgram.getEnv(function (res) {
? if (res.miniprogram) {
? ? var info = {
? ? ? title: '測試',
? ? ? value: window.location.href,?
? ? };
? ? wx.miniProgram.postMessage({
? ? ? data: info
? ? });
? } else {
//如果不是...可以忽略,或者去掉這部分
window.location.href ='www.baidu.com'
? }
})
? } else {
//如果不是...可以忽略,或者去掉這部分
window.location.href ='liren123.cn'?
? }
? </script>