1.场景描述:当在小程序中打开h5页面时,希望小程序的转发出去的标题,图片,跳转link可以通过h5通信实现自定义。

2.实现方式:通过h5给小程序通信,发送标题,图片,跳转link等信息,让小程序设置分享。

h5发送给小程序通信代码引用微信js1.3.2以上才支持,scripttype="text/javascript"src="//res.wx.qq.com/open/js/jweixin-1.3.2.js"/script判断h5是在小程序环境中打开代码://判断是否是小程序环境functiondoMiniProgram(callback){try{//小程序环境设置分享varua=window.navigator.userAgent.toLowerCase();//判断是否是微信环境if(ua.match(/MicroMessenger/i)=='micromessenger'){//微信环境wx.miniProgram.getEnv(function(res){if(res.miniprogram){callback();}})}}catch(ex){console.log(ex);}}

3.给小程序发送数据设置小程序分享:

//设置小程序分享functionsetMiniProgramShare(shareTitle,imageUrl,shareUrl){try{doMiniProgram(function(){//小程序环境下逻辑wx.miniProgram.postMessage({data:{title:shareTitle,path:shareUrl,imageUrl:imageUrl}})});}catch(ex){console.log(ex);}}小程序接收&处理逻辑h5页面:web-viewsrc="{{url}}"bindmessage="message"/js逻辑:Page({data:{shareData:{}},onShareAppMessage(options){returnthis.shareData},message(e){varthat=thisconsole.log(e)that.shareData=e.detail.data[0]}})

TAG标签: 微信小程序小程序分享页面设置小程序分享

http://www.xiaochengxub.com/read-98187.html