什么是微信开放标签
微信开放标签是微信公众平台面向网页开发者提供的扩展标签集合。通过使用微信开放标签,网页开发者可安全便捷地使用微信或系统的能力,为微信用户提供更优质的网页体验。
如何使用微信开放标签
小程序跳转按钮:
<wx-open-launch-weapp>
App跳转按钮:<wx-open-launch-app>
config注入
wx.config({
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印
appId: '', // 必填,公众号的唯一标识
timestamp: , // 必填,生成签名的时间戳
nonceStr: '', // 必填,生成签名的随机串
signature: '',// 必填,签名
jsApiList: [], // 必填,需要使用的JS接口列表
openTagList: [] // 可选,需要使用的开放标签列表,例如['wx-open-launch-app']
});
标准h5使用
<wx-open-launch-weapp
id="launch-btn"
username="gh_xxxxxxxx"
path="pages/home/index.html?user=123&action=abc"
>
<template>
<style>.btn { padding: 12px }</style>
<button class="btn">打开小程序</button>
</template>
</wx-open-launch-weapp>
<script>
var btn = document.getElementById('launch-btn');
btn.addEventListener('launch', function (e) {
console.log('success');
});
btn.addEventListener('error', function (e) {
console.log('fail', e.detail);
});
</script>
React使用
import React from 'react';
import { initWxConfig } from '@/helpers/wxConfig';
export default class TestWechat extends React.Component {
async componentDidMount() {
await initWxConfig();
if (document) {
this.btnBox = document.querySelector('#openWechat');
this.btnBox.innerHTML = `<wx-open-launch-weapp
id="launch-btn"
username="gh_***"
path="pages/index/index.html"
style="padding: 30px;background-color: #ff5500;color: #007aff;font-weight: bold;border-radius: 10px;position: fixed;right: 20px;bottom: 30px;"
>
<template>
<style>.open-btn{color: #ffffff;}</style>
<div class="open-btn">打开app</div>
</template>
</wx-open-launch-weapp>`;
}
}
componentWillUnmount() {
if (document) {
this.btnBox.innerHTML = '';
}
}
render() {
return <div id="openWechat" />;
}
}