uniapp微信小程序webview嵌套H5页面分享笔记

1、H5端

1、index.html引入jweixin.js

html 复制代码
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>

2、需要分享的页面

javascript 复制代码
postMessageToMiniProgram (shareData) {
      // alert(JSON.stringify(window.wx))
      // 1. 判断是否在小程序 web-view 环境中
      const userAgent = window.navigator.userAgent.toLowerCase();
      const isInMiniProgram = userAgent.includes('miniprogram');
      // 2. 检查微信JS-SDK是否可用
      if (isInMiniProgram && window.wx.miniProgram) {
        try {
          window.wx.miniProgram.postMessage({
            data: shareData // 发送的数据对象
          });
          console.log('H5向小程序发送消息成功', shareData);
        } catch (error) {
          console.error('H5向小程序发送消息失败', error);
        }
      } else {
        console.warn('不在小程序web-view环境中,无法发送消息');
      }
        let guide = document.createElement('div');
      guide.id = 'share-guide';
      guide.innerHTML = `
         <div style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; 
            background: rgba(0,0,0,0.7); z-index: 9999; display: flex; 
            flex-direction: column; align-items: center; justify-content: flex-start;">
        <div style="margin-top: 1rem; color: white; text-align: center;">
          <div style="width: 1rem; height: 0.8rem; margin: 0 auto 20px;position: fixed;top: 0;right:0.6rem">
            <img src="${this.arrowImg}" 
                style="width: 100%; height: 100%; object-fit: contain;" 
                alt="分享指引箭头" />
          </div>
          <div style="font-size: 14px; margin-bottom: 20px;">请点击右上角. . .进行分享</div>
        </div>
        <div style="position: absolute; bottom: 50px; color: white; 
                    background: #1aad19; padding: 10px 20px; border-radius: 5px;
                    cursor: pointer;" onclick="document.getElementById('share-guide').remove()">
          我知道了
        </div>
      </div>
      `;
      document.body.appendChild(guide);
    },
    //点击分享
    goShareHall() {
      // 构造分享数据
      const shareInfo = {
        title: `${this.detail.name}的纪念馆`, // 分享标题
        imageUrl: this.defaultAvatar, // 分享图片,建议使用绝对路径
        path: window.location.href, // 分享的H5页面链接
        hallId:this.id
      };
      // 发送给小程序
      this.postMessageToMiniProgram(shareInfo);
    },

3、点击分享后的卡片进入H5项目

javascript 复制代码
 async created() {
    this.openId = this.getParams(window.location.href,'openId')
    this.hallId = this.getParams(window.location.href,'hallId')
    console.log(this.openId, 'openId')
    console.log(this.hallId, 'hallId')
    if(!this.openId) {
      this.$toast.fail('暂未获取到用户信息,请稍后再试');
      return
    }
    if(this.openId) {
      await this.passwordSubmit()
    }
    if(this.hallId){
      this.$router.push(`/memorialHall/${this.hallId}`);
    }
  },

2、小程序端

html 复制代码
<template>
	<view class="offical">
		<web-view  :src="src" @message="getMessage"></web-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				 src:``,
				 hallId:''
			};
		},
		onLoad(options) {
			console.log(options,'options')
			let openId=uni.getStorageSync('openId')
			if(options.hallId){
				this.src = `https://fwzx.cn/yjs?openId=${openId}&hallId=${options.hallId}`
			}else{
				this.src=`https://fwzx.cn/yjs?openId=${openId}`
			}
			
			console.log(this.src,'this.src')
			this.$forceUpdate()
		},
		onLaunch() {
			wx.showShareMenu({
			  withShareTicket: true
			})
		},
		// 分享到微信好友
		onShareAppMessage(options) {
		  return {
			title: '事业服务中心',
			path: `/pagesA/webView/webView?hallId=${this.hallId}`,
			imageUrl: '',
		  }
		},
		methods: {
			getMessage(e) {
				console.log(e,'H5传给小程序的参数')
				// alert(JSON.stringify(e))
				if(e.detail.data){
					this.hallId = e.detail.data[0].hallId;
					this.$forceUpdate()
				}
			}
		},
	}
</script>

<style lang="scss" scoped>
.offical{
	width: 100%;
	height: 100%;
}
</style>
相关推荐
航Hang*几秒前
第2章:进阶Linux系统——第4节:配置与管理NFS服务器
linux·运维·服务器·笔记·学习·vmware
ZhiqianXia11 分钟前
Pytorch 学习笔记(8): PyTorch FX
pytorch·笔记·学习
xuhaoyu_cpp_java33 分钟前
Boyer-Moore 投票算法
java·经验分享·笔记·学习·算法
雨浓YN1 小时前
OPC UA 通讯开发笔记 - 基于Opc.Ua.Client
笔记·c#
迷路爸爸1801 小时前
Docker 入门学习笔记 06:用一个可复现的 Python 项目真正理解 Dockerfile
笔记·学习·docker
Engineer邓祥浩1 小时前
JVM学习笔记(6) 第二部分 自动内存管理 第5章节 调优案例分析与实战
jvm·笔记·学习
ysa0510301 小时前
斐波那契上斐波那契【矩阵快速幂】
数据结构·c++·笔记·算法
派大星~课堂2 小时前
【力扣-94.二叉树的中序遍历】Python笔记
笔记·python·leetcode
ZhiqianXia2 小时前
PyTorch 学习笔记(10) : PyTorch torch.library
pytorch·笔记·学习
小陈phd2 小时前
多模态大模型学习笔记(三十一)—— 基于CCT(Compact Convolutional Transformers)实现中文车牌数据集微调
笔记·学习