当前位置:   article > 正文

h5跳转微信小程序方案及注意事项(vue方向)_vue 浏览器跳转到微信小程序

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']
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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

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

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123

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配置不正确。
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号