h5跳转微信小程序方案及注意事项(vue方向)

1、准备

在正式开发工作之前,请优先熟读并查看微信开发文档

2、绑定域名 (在微信公众平台设置)

需提前登录微信公众平台进入“公众号设置”的“功能设置”的“JS接口安全域名”、“业务域名”、“网页授权域名”内依次配置h5页面的相关域名地址(例如:www.baidu.com)这里不包含协议名称和端口,同时可在根目录上传MP_verify_cZv0a41uGOH2UNym.txt文件,如图:

![在这里插入图片描述](https://img-blog.csdnimg.cn/96d6fce2a041469eaf7e57d9c0ef769e.png

3、IP白名单(在微信公众平台设置)

如图:

![](https://img-blog.csdnimg.cn/02ddb45aff2a45b6b0885f556825a27a.png

4、将小程序和H5公众号进行关联 (在微信公众平台设置)

不会的可以参考教程:https://jingyan.baidu.com/article/7908e85c70685bee481ad2b1.html

5、引入JS文件

在需要调用JS接口的页面(index.html)引入如下JS文件:
http://res.wx.qq.com/open/js/jweixin-1.6.0.js (支持https)
如需进一步提升服务稳定性,当上述资源不可访问时,可改访问:http://res2.wx.qq.com/open/js/jweixin-1.6.0.js (支持https)如图:

在这里插入图片描述

6、通过config接口注入权限验证配置并申请所需开放标签

与使用JS-SDK配置方式相同,所有需要使用开放标签的页面必须先注入配置信息,并通过openTagList字段申请所需要的开放标签,否则将无法使用(同一个url仅需调用一次)。开放标签的申请和JS接口的申请相互独立,因此是可以同时申请的。

wx.config({
  debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印
  appId: '', // 必填,公众号的唯一标识
  timestamp: , // 必填,生成签名的时间戳
  nonceStr: '', // 必填,生成签名的随机串
  signature: '',// 必填,签名
  jsApiList: ['showMenuItems'], // 必填,需要使用的JS接口列表,不能为空,为空的话安卓会有问题
  openTagList: ['wx-open-launch-weapp'] // 可选,需要使用的开放标签列表,例如['wx-open-launch-app']
});

记得在main.js页面添加相关配置

Vue.config.ignoredElements = ['wx-open-launch-weapp']

7、传参(VUE、小程序页面)

如图:

在这里插入图片描述
在这里插入图片描述

8、全部代码

<template>
  <div class="app">
    <p class="test-text" v-if="isWxBtn">点击打开微信小程序</p>
    <wx-open-launch-weapp
      id="launch-btn"
      :username="wx_username"
      :path="wx_path"
      v-if="isWxBtn"
    >
      <script type="text/wxtag-template">
        <style>
          .test-btn {
            position:fixed;
            margin:auto;
            left:0;
            right:0;
            top:0;
            bottom:0;
            display: block;
            width: 80%;
            font-size: 18px;
            color: #2973ba !important;
            height: 48px;
            line-height: 48px;
            background-color: #fff;
            border-top: 1px solid #ddd;
            border-bottom-left-radius: 5px;
            border-bottom-right-radius: 5px;
            text-align: center;
          }
        </style>
        <div class="test-btn">确定</div>
      </script>
    </wx-open-launch-weapp>
  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {
        username:'',
        password:''
      },

      isWxBtn:false,
      wx_username: 'gh_xxxxxxxxxxxx', // gh_ 开头的原始小程序ID
      wx_path: 'pages/index/index.html', // 一定要以 .html 结尾
      token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    }
  },
  created() {},
  methods:{
	getShopWxConfig() {
      let that = this;
      let url = window.location.href.split('#')[0];
      api.getWxConfig(url).then(res => {
        wx.config({
          debug: true, // 验证结果弹窗控制(成功或者失败)
          appId: res.data.appId, // 公众号唯一appid
          nonceStr: res.data.noncestr,
          timestamp: res.data.timestamp,
          signature: res.data.signature,
          jsApiList: [''], // 必填,需要使用的JS接口列表
          openTagList: ['wx-open-launch-weapp'],
        });
        wx.ready(function () {
          that.isWxBtn = true;
          console.log('111111',success);
        });
        wx.error(function (err) {
          // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
          console.log('000000', error);
        });
      });
    },
  },
  mounted() {
    this.wx_path = this.wx_path + "?token=" + this.token;
	this.getShopWxConfig();
  }
</script>

<style lang="scss" scoped>
.app{
  background: url(../../assets/images/img.jpg) no-repeat center center;
  position: fixed;
  background-size:cover;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  .test-text {
    position: fixed;
    margin: auto;
    left: 0;
    right: 0;
    top: -108px;
    bottom: 0;
    text-align: center;
    width: 80%;
    height: 60px;
    line-height: 60px;
    font-size: 18px;
    color: #2b2b2b;
    z-index: 99999;
    background: #fff;
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
  }
  #launch-btn {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100vh;
    line-height: 100vh;
    text-align: center;
    background: rgba(0, 0, 0, 0.5);
    display: block;
  }
}
</style>

9、注意事项 ( 按钮不显示、点击按钮没反应,请对照以下事项逐一排查 )

  • username为小程序原始ID。
  • path为跳转至小程序的路径,一定要加后缀.html。还能携带参数,在微信小程序中通过 onLoadoptions接收。(代码如下)
  • <wx-open-launch-weapp>中必须用<template>标签包裹。
  • config配置中一定要加入openTagList: ['wx-open-launch-weapp']
  • 微信版本要求为:7.0.12及以上。 系统版本要求为:iOS 10.3及以上、Android 5.0及以上。
  • 引入js至少是1.6以上版本。
  • 若按钮不显示,多半是wx.config配置不正确。
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值