wx.config({ appId: ‘‘, debug: true, timestamp: ‘‘, nonceStr: ‘‘, signature: ‘‘, jsApiList: [ ‘onMenuShareTimeline‘, // 分享给好友 ‘onMenuShareAppMessage‘, // 分享到朋友圈 ], openTagList: [‘wx-open-launch-app’] // 获取开放标签权限 });
{errMsg: "config:ok”}
说明你已经接入JS-SDK成功了
遗憾的是截至2020年7月13号为止,微信开发者工具还是无法支持微信开放标签的调试功能,只能在手机上调试并且是在加了微信安全域名的服务器环境下调试,着实麻烦
// 忽略自定义元素标签抛出的报错 Vue.config.ignoredElements = [ ‘wx-open-launch-app‘, ]; new Vue({ el: ‘#app‘, template: ‘<app/>‘, components: { app } })
wx-open-launch-app 标签组件
<wx-open-launch-app :id="id" class="launch-btn" :appid="appId" :extinfo="extinfoStr" > <script type="text/wxtag-template"> <style>.btn {height: 96px;}</style> <div class="btn">打开app</div> </script> </wx-open-launch-app>
mounted(){ var btn = document.getElementById(this.id) btn.addEventListener(‘launch‘, e => { // 在此处可设置粘贴板内数据,数据是传递给 app 的参数进, console.log(‘success‘); }); btn.addEventListener(‘error‘, e => { // 在此处可设置粘贴板内数据,数据是传递给 app 的参数进, console.log(‘fail‘, e.detail); // 唤醒失败的情况下,可用于降级处理比如在此处跳转到应用宝 }); }
7、如果微信版本低于7.0.12 开放标签是无法使用的,所以最好在开放标签外套一层 DIV 用于点击事件,在事件中断段微信版本号如果低于,则降级到应用宝之类的方案
<div @click="launch"> <wx-open-launch-app :id="id" class="launch-btn" :appid="appId" :extinfo="extinfoStr" > <script type="text/wxtag-template"> <style>.btn {height: 96px;}</style> <div class="btn">打开app</div> </script> </wx-open-launch-app> </div>
launch(){ // 在此处可设置粘贴板内数据,数据是传递给 app 的参数进 if(!this.enable){ // 不支持标签降级处理 } }
四、最后当然是封装成项目中的一个组件
<template> <div @click="launch"> <wx-open-launch-app :id="id" class="launch-btn" :appid="appId" :extinfo="extinfoStr" > <script type="text/wxtag-template"> <style>.btn {height: 96px;}</style> <div class="btn">打开app</div> </script> </wx-open-launch-app> </div> </template> <script> import globalConfig from ‘@assets/js/config‘ import { copyToClipboard } from ‘@assets/js/utils‘ import { getWeixinVersion, onBridgeReady } from ‘@assets/js/weixin/weixin‘ let idIndex = 0 export default { name: ‘LaunchButton‘, props: { extinfo: { type: Object, default: ‘‘ }, }, watch: { extinfo: { handler(n){ this.extinfoStr = JSON.stringify(n) }, immediate: true } }, data() { idIndex++ return { id: ‘wxopenLanchAppId‘ + idIndex, appId: globalConfig.WEIXIN_APP_ID, enable: false, extinfoStr: ‘‘, } }, methods: { redirectToApp(){ setTimeout(()=>{ window.location.href = globalConfig.YING_YONG_BAO }, 400) }, setClipboard(){ console.log(‘start copy‘) let copyObject = { app: ‘yogo‘ } for(var k in this.extinfo){ copyObject[k] = this.extinfo[k] } copyObject = JSON.stringify(copyObject) copyToClipboard(copyObject) console.log(‘end copy‘) }, launch(){ this.setClipboard() if(!this.enable){ this.redirectToApp() } } }, created(){ // 微信版本号大于 7.0.12 开放标签才可进行 唤醒 app 跳转 const wxVersion = getWeixinVersion() if(wxVersion){ let v = wxVersion.split(‘.‘) if(v[0]>=7){ if(v[1]>=0){ if(v[2]>=12){ this.enable = true } } } } }, mounted(){ var btn = document.getElementById(this.id) btn.addEventListener(‘launch‘, e => { this.setClipboard() console.log(‘success‘); }); btn.addEventListener(‘error‘, e => { console.log(‘fail‘, e.detail); this.setClipboard() this.redirectToApp() }); } } </script> <style lang="scss" scoped> .launch-btn{ position: absolute; left: 0; top: 0; width: 100%; height: 100%; z-index: 1; opacity: 0; // background: red; } </style>
注:转载请注明出处博客园:sheldon(willian12345@126.com)
https://github.com/willian12345
微信浏览器内 h5 直接唤醒 app 之 微信开放标签 wx-open-launch-app
原文:https://www.cnblogs.com/willian/p/13293030.html