JSSDK开放标签使用 - 官方文档
1.vue3模板无法渲染script标签,报错
2.开放标签不显示问题
3.报错-config:fail,invalid signature
4.报错-config:fail,invalid url domai
框架:使用uni-app + vue3的模板项目
实现功能:在微信公众号中嵌入H5页面,需要在H5页面中唤起模板通知消息弹窗
(1) 由于我是使用模板开发,而不是CLI,所以直接在index.html文件直接应用script,代码如下:
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js " />
<script>
window.jWeixin = window.wx;
delete window.wx;
</script>
(2) 使用CLI的项目可以使用npm来下载文件
// 后端提供生成签名的接口
getWxConfig({ url: encodeURIComponent(location.href.split('#')[0]) }).then((result: any) => {
const { data } = result;
// jWeixin 是在第二步中全局赋值的参数
jWeixin.config({
debug: false,
appId: data.appid, // 后端返回公众号appId
timestamp: data.timestamp, // 后端返回用于生成签名的时间戳
nonceStr: data.nonceStr, // 后端返回用于生成签名的随机字符串
signature: data.signature, // 后端返回的签名
jsApiList: [], // 这里我没有注册api,所以留了空数组,如果有需要的api就添加,文档有提供可注册的api列表
openTagList: ['wx-open-subscribe'] // 注册需要用的开发标签,文档有提供可注册的标签列表
});
console.log(jWeixin);
document.addEventListener('WeixinOpenTagsError', function(e) {
console.error('开放标签的错误原因:', e.detail.errMsg); // 无法使用开放标签的错误原因,需回退兼容。仅无法使用开放标签,JS-SDK其他功能不受影响
});
});
<template>
<wx-open-subscribe template="这里填你需要推送的模板id" id="subscribe-btn">
<component is="style">.subscribe-btn { color: #fff; background-color: #07c160; }</component>
<component is="script">
<button class="subscribe-btn">一次性模版消息订阅</button>
</component>
</wx-open-subscribe>
</template>
<wx-open-subscribe template="这里填你需要推送的模板id" id="subscribe-btn">
<script type="text/wxtag-template" slot="style">
<style>
.subscribe-btn {
color: #fff;
background-color: #07c160;
}
</style>
</script>
<script type="text/wxtag-template">
<button class="subscribe-btn">
一次性模版消息订阅
</button>
</script>
</wx-open-subscribe>
vue3的模板下,script无法被渲染,使用vue的动态组件component来替换script,替换如下:
<wx-open-subscribe template="这里填你需要推送的模板id" id="subscribe-btn">
<component is="style">.subscribe-btn { color: #fff; background-color: #07c160; }</component>
<component is="script">
<button class="subscribe-btn">一次性模版消息订阅</button>
</component>
</wx-open-subscribe>