【微信小程序】webview向h5页面传值

实现功能:webview向h5页面传值

代码:
index.wxml

<web-view src="{{url}}"></web-view>

index.js

  onLoad: function() {
    let that = this;
    let openId = wx.getStorageSync('openId');
    that.setData({
      url: '地址/index.html?id=' + openId
    })
  },

index.html

var id=window.location.href.split("=")[1];//从小程序webview链接上获取id 

思路:通过url带参数的方式,h5页面去取参数
如果是带多个参数,可通过&符号去分别取

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是微信小程序webview嵌入h5页面调用微信支付的完整代码: HTML页面: ``` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>微信支付demo</title> </head> <body> <button onclick="callPay()">微信支付</button> <script type="text/javascript"> function callPay() { // 构造支付参数 var params = { appId: '你的appId', timeStamp: '时间戳', nonceStr: '随机字符串', package: '预支付交易会话标识', signType: 'MD5', paySign: '支付签名' }; // 调用微信支付接口 WeixinJSBridge.invoke('getBrandWCPayRequest', params, function(res){ // 支付结果处理 if(res.err_msg == "get_brand_wcpay_request:ok"){ // 支付成功 }else{ // 支付失败 } }); } // 判断是否在微信内置浏览器中打开 if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', callPay, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', callPay); document.attachEvent('onWeixinJSBridgeReady', callPay); } }else{ callPay(); } </script> </body> </html> ``` 小程序页面: ``` <web-view src="https://你的H5页面地址"></web-view> ``` 小程序JS代码: ``` Page({ onReady: function () { // 获取web-view组件 var webview = this.selectComponent("#webview"); // 监听web-view中的页面加载完成事件 webview.onMessage(function(e){ if(e.detail == 'loadFinish'){ // web-view中的页面加载完成后,调用支付接口 wx.request({ url: 'https://你的支付接口地址', method: 'POST', data: { // 构造支付参数 appId: '你的appId', timeStamp: '时间戳', nonceStr: '随机字符串', package: '预支付交易会话标识', signType: 'MD5', paySign: '支付签名' }, success: function(res){ // 将支付参数传递给web-view中的页面 webview.postMessage({type: 'pay', data: res.data}); } }) } }) } }) ``` 需要注意的是,在小程序中调用微信支付接口需要在小程序后台配置支付授权目录,否则会出现调用失败的情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值