赞
踩
在正式开发工作之前,请优先熟读并查看微信开发文档。
需提前登录微信公众平台进入“公众号设置”的“功能设置”的“JS接口安全域名”、“业务域名”、“网页授权域名”内依次配置h5页面的相关域名地址(例如:www.baidu.com)这里不包含协议名称和端口,同时可在根目录上传MP_verify_cZv0a41uGOH2UNym.txt文件,如图:
如图:
不会的可以参考教程:https://jingyan.baidu.com/article/7908e85c70685bee481ad2b1.html
在需要调用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)如图:
与使用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']
如图:
<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>
username
为小程序原始ID。path
为跳转至小程序的路径,一定要加后缀.html
。还能携带参数,在微信小程序中通过 onLoad
的 options
接收。(代码如下)<wx-open-launch-weapp>
中必须用<template>
标签包裹。openTagList: ['wx-open-launch-weapp']
。wx.config
配置不正确。Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。