H5--公众号页面如何唤起小程序

一、整体流程

  1. 认证服务号
  2. 绑定 JS接口安全域名
  3. 配置IP白名单
  4. 将H5和小程序进行关联
  5. 引入 微信sdk jweixin-1.6.0.js
  6. 需要跳转的小程序页面path和原始ID(gh_xxx)

二、检查流程配置是否正确

先查看官方的文档需求:

一步到位「开发前必读文档」
在这里插入图片描述
一步到位「微信 SDK说明文档」
在这里插入图片描述

三、配置

1. 认证服务号;

指微信公众号,不是订阅号,在做之前一定要检查清楚;

2. 绑定JS接口安全域名;

这个需要在微信公众号后台配置,不要找错地方哦
在这里插入图片描述

3. 配置IP白名单

IP白单也是在公众号后台配置:进入公众号》设置与开发》安全中心》找到IP白名单区设置》
在这里插入图片描述

4. 将小程序和H5公众号进行关联

微信公众后台链接:https://mp.weixin.qq.com/
关联公众号登录 “公众号管理后台-小程序管理” 完成关联
查看关联公众号在小程序后台配置:进入小程序后台》设置》关联设置》关联的公众号》

在这里插入图片描述

5. 引入微信jweixin-1.6.0.js

在这里插入图片描述

<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
6. 跳转小程序基本信息

● 小程序名称
● 小程序的AppId
● 跳转小程序的path
● usename (所需跳转的小程序原始id,即小程序对应的以gh_开头的id(跳转时,有 appid 会优先使用appid,没有 appid 才会使用username))

四、开发工作

main.js

Vue.config.ignoredElements = ["wx-open-launch-app"];
<template>
  <div class="navi-container">
    <div class="fake-btn" id="weappBtn">跳转小程序</div>
    <wx-open-launch-weapp v-if="isWechat" @launch="launch" appid="xxx" path="pages/index/index?bxChannel=gansuyuanxinhuibao" style="position: absolute;top: 0;left: 0;width: 100%;height: 50px;z-index: 10;">
      <script type="text/wxtag-template">
        <style>.btn {width: 100%;height: 50px;position: absolute;left: 0;top: 0;z-index: 99;background: transparent;border: none;}</style>
        <button class="btn" id="NaviToMini"></button>
      </script>
    </wx-open-launch-weapp>
  </div>
</template>
<script>
import { getHBJsApiConfig } from '@/api/wx'
import { getDeviceInfo } from '@/util/tools' // 工具类
export default {
  data() {
    return {
      isWechat: getDeviceInfo().env === 'wechat'
    }
  },
  async mounted() {
    try {
      if (this.isWechat) {
        let wxConfigInfo = ''
        // 初始化 微信sdk 获取初始化相关密钥
        let sigData = await getHBJsApiConfig()
        wxConfigInfo = JSON.parse(sigData.data)
        window.wx.config({
          debug: false,
          appId: wxConfigInfo.appId,
          timestamp: wxConfigInfo.timestamp,
          nonceStr: wxConfigInfo.nonceStr,
          signature: wxConfigInfo.signature,
          jsApiList: ["scanQRCode","updateAppMessageShareData","updateTimelineShareData"],
          openTagList: ["wx-open-launch-weapp"],
        })
        window.wx.ready(function () {
          console.log("---------ready---------")
        });
        window.wx.error(function (res) {
          console.log("--------fail--------", res)
        });
      }
    } catch (e) {
      // 接口报错 打印错误信息
    }
  },
  methods: {
    launch(e) {
      console.log("获取小程序 允许 按钮 操作回调", e)
    }
  }
}
</script>
<style lang="less"  scoped>
.navi-container {
  position: relative;
  width: 305px;
  height: 50px;
  margin: 18px auto;
  .fake-btn {
    width: 305px;
    height: 50px;
    position: absolute;
    left: 0;
    top: 0;
    border-radius: 25px;
    border: 1px solid rgba(246, 31, 30, 1);
    color: #fff;
    font-size: 16px;
    text-align: center;
    line-height: 50px;
    z-index: 1;
    background: linear-gradient(to right, #DB0003, #FF6C61);
  }
}
</style>

五、注意事项

● 系统版本要求为:iOS 10.3及以上、Android 5.0及以上
● 微信版本要求为:7.0.12及以上

六、参考文档

官方文档
H5跳转小程序成功案例

要在H5页面中实现唤起微信小程序支付,你需要通过微信提供的JS-SDK来完成。以下是步骤: 1. **引入JS-SDK**: 首先,在HTML文件中引入微信的`wx.min.js`库,确保你的H5页面运行在支持微信公众号小程序环境。 ```html <script src="https://res.wx.qq.com/open/js/jweixin-1.7.0.js" async></script> ``` 2. **配置微信AppID**: 在你的H5项目中,需要有微信小程序对应的AppID,并在`jsconfig.json`文件中配置好: ```json { "debug": false, " appId": "your_app_id", "timestamp": "", "nonceStr": "", "signature": "", "jsApiList": [ "checkJsApi", "onMenuShareTimeline", "onMenuShareAppMessage" ] } ``` 3. **初始化JS-SDK**: 在H5页面加载完成后,调用`wx.config()`函数配置支付所需的参数: ```javascript function initWechatPay() { var config = { debug: false, // 开启调试模式 appId: 'your_app_id', timestamp: getTimestamp(), // 时间戳 nonceStr: generateNonceStr(), // 随机字符串 signature: getSignature(), // 签名 jsApiList: ['wx.requestPayment'] // 要使用的API列表 }; wx.config(config); } // 获取时间戳、随机字符串和签名生成函数省略... ``` 4. **发起支付请求**: 当用户确认支付时,调用`wx.requestPayment()`方法,传入支付详情: ```javascript function payOrder(orderInfo) { if (!wx.ready) { wx.ready(function () { wx.requestPayment({ // 支付信息 title: '订单标题', desc: '订单描述', total_fee: orderInfo.totalFee, // 总金额(单位:分) detail: '商品详情', success: function (res) { console.log('支付成功', res); // 处理支付成功的回调 }, fail: function (err) { console.log('支付失败', err); // 处理支付失败的回调 } }); }); } else { wx.requestPayment(...); // 如果已经ready,直接发起请求 } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值