当前位置: 首页 > news >正文

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

1、H5端

1、index.html引入jweixin.js

<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>

2、需要分享的页面

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项目

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、小程序端

<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>
http://www.jsqmd.com/news/493581/

相关文章:

  • Hunyuan MT模型术语干预怎么用?精准翻译部署教程
  • 【Docker】nscenter命令详解
  • 三大消息队列技术对比与应用指南
  • 市面上的可视挖耳勺怎么样?掏耳神器哪种最好用?耳勺品牌排行榜
  • 保姆级教程:Pentaho Kettle 10.2.0.0-222安装与MySQL连接全流程(附驱动下载)
  • CAD设计文档智能生成:Nanbeige 4.1-3B理解图纸并输出工艺说明
  • Qwen3-TTS新手入门:无需代码,WebUI界面快速合成多语言语音
  • 快速上手translategemma-27b-it:图文翻译模型部署与调用指南
  • Deformable DETR 实战解析:如何加速目标检测训练与提升小物体检测性能
  • PROJECT MOGFACE Python入门实战:10分钟完成你的第一个AI应用
  • CST仿真原理:让CST软件告诉你高速差分信号为什么要进行等长匹配
  • 自学python笔记心得--数据存储与运算2
  • Qwen3-ForcedAligner-0.6B在医疗转录中的应用:精准病历时间戳标注
  • dify节点HTTP 请求右击不显示文件或图片URL解决方式
  • 一级减速器成套CAD图【22CAD】
  • 注意力状态空间模块解析:为什么MambaIRv2在图像恢复任务中表现如此出色?
  • 氮化镓GaN FET/GaN HEMT功率驱动器选型一览表
  • 避坑指南:穿云箭量化平台HP_tdx股票代码转换的6种隐藏陷阱(附正确姿势)
  • 6 个让你悄悄发胖的坏习惯,第 3 个很多人天天在做
  • TensorRT Python API实战:从ONNX模型到高效推理引擎的完整流程
  • 微服务统一认证:Gateway集成JWT实战
  • GME-Qwen2-VL-2B-Instruct快速原型开发:利用CSDN开源项目加速应用落地
  • 第三届通信、信息与数字技术国际会议(CIDT 2026),SPIE出版论文
  • Xinference场景实战:用一行代码为你的AI应用快速切换大模型后端
  • 2026年口碑好的煤粉公司推荐:铸造煤粉公司口碑推荐 - 品牌宣传支持者
  • 搜索 会员中心 创作中心 干货整理!10 个适合自学网络安全的在线资源平台
  • Linux驱动开发理解指针与结构体
  • 记录一下uniapp项目中自己封装的组件开发环境特别卡的问题
  • Dify私有化上线倒计时72小时——这份由3家金融级客户联合验证的《灰度发布核验清单》正在紧急回收中(含自动巡检脚本)
  • 基于Halcon的距离变换与分水岭算法在骰子点数识别中的应用