当前位置:网站首页>微信公众号订阅消息 wx-open-subscribe 的实现及闭坑指南
微信公众号订阅消息 wx-open-subscribe 的实现及闭坑指南
2022-07-01 14:54:00 【a_靖】
先来个实现成功的效果图:
主要看图片底部 success 的日志 status 为 accept 时表示用户点击了授权,为 reject 时表示用户拒绝了授权。


下面来看实现,本文采用的是uniapp框架,运行在微信浏览器H5端
避坑指南:
1. wx-open-subscribe 标签,在安卓显示,IOS不显示?
解决方案:在 manifest.json 文件路由模式改为 hash 。并且在访问你的应用第一个页面的时候,就进行 wx.config 配置。
2. wx-open-subscribe 标签样式设置很麻烦?或者设置了不生效?
解决方案:在外层包裹一个div,复杂的样式写在外层,开放标签尽量写简单的样式,不生效的话可以借鉴我后面贴的代码。
3. wx-open-subscribe组件,怎样监听用户点击“取消”、“允许”?为什么官方的success事件返回没有例如:{ errMsg: "subscribe:ok", subscribeDetails: "{"TenvU22BA1jCp4YHfYEpRuESXYReQyDuhs4vbdWA99I":"{\"status\":\"accept\"}"},而是返回
解决方案:success里面接收的参数要写成success({detail})格式才行,写成success(e)就不会有e.detail。
<wx-open-subscribe id="subscribe-btn" ref="subscribeBtn"
style="position: fixed;z-idnex:9999; top: 0px, left: 0px;bottom:0px;right:0px;width: 100%;height: 100%;"
template="Qw2bWImUYSgS5y9UoAGa-fd3RUC2aXZ5m25MApxIpA4" id="subscribe-btn">
<!-- Qw2bWImUYSgS5y9UoAGa-fd3RUC2aXZ5m25MApxIpA4 测试 -->
<!-- NtmNRrJl5FMKND8FuyZhZxL_jbRLNg0-h-iNCr0z0ak 正式 -->
<script type="text/wxtag-template">
<div style="position: fixed;z-idnex:9999; top: 0px, left: 0px;bottom:0px;right:0px;width: 100%;height: 100%;" />
</script>
</wx-open-subscribe>
// wx-open-subscribe 原生绑定点击事件
this.$nextTick(() => {
var btn = this.$refs.subscribeBtn;
btn.addEventListener('success', ({detail}) => {
console.log('success1', detail);
});
btn.addEventListener('error', ({detail}) => {
console.log('fail1', detail);
});
})4. wx-open-subscribe组件外层的div,点击事件不生效?
解决方案:wx-open-subscribe 组件的事件不会冒泡,可以在 success 返回后做事件处理。
实现代码:
<view class="subscribeBlock" v-if="showSubscribe">
<wx-open-subscribe id="subscribe-btn" ref="subscribeBtn"
style="position: fixed;z-idnex:9999; top: 0px, left: 0px;bottom:0px;right:0px;width: 100%;height: 100%;"
template="Qw2bWImUYSgS5y9UoAGa-fd3RUC2aXZ5m25MApxIpA4" id="subscribe-btn">
<!-- Qw2bWImUYSgS5y9UoAGa-fd3RUC2aXZ5m25MApxIpA4 测试 -->
<!-- NtmNRrJl5FMKND8FuyZhZxL_jbRLNg0-h-iNCr0z0ak 正式 -->
<script type="text/wxtag-template">
<div style="position: fixed;z-idnex:9999; top: 0px, left: 0px;bottom:0px;right:0px;width: 100%;height: 100%;" />
</script>
</wx-open-subscribe>
</view>js
<script>
var that;
export default {
components: {
search
},
data() {
return {
showSubscribe: false,
};
},
onReady() {
// wx-open-subscribe 原生绑定点击事件
this.$nextTick(() => {
var btn = this.$refs.subscribeBtn;
btn.addEventListener('success', ({detail}) => {
console.log('success1', detail);
this.clickSubscribe()
});
btn.addEventListener('error', ({detail}) => {
console.log('fail1', detail);
this.clickSubscribe()
});
})
},
onLoad(option) {
},
methods: {
clickSubscribe() {
console.log('点击了订阅');
this.showSubscribe = false;
},
goTest() {
// let url = '/pages/test/test'
// this.goOtherPages(url)
},
}
};
</script>css
.subscribeBlock {
z-index: 99999;
position: fixed;
width: 100vw;
height: 100vh;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
}
jssdk封装文件: 需要node安装 jweixin-module ,具体安装可以百度查一下
import config from "./config.js";
import api from "./api.js";
import myRequest from "./request.js";
let jweixin = require('jweixin-module')
Vue.prototype.$wx = jweixin
var wxjssdk = (_wx) => {
return new Promise(rr => {
let url = location.href;
if (url.indexOf('http://localhost') !== -1) url = config.http_url
console.log('url', url)
let data = {
url: encodeURIComponent(url)
}
myRequest.request(api.sdk.wechatJsSdkTicket, data).then(res => {
console.log('wechatJsSdkTicket',res);
let configData = res.data;
console.log('configData',configData);
_wx.config({
// debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: configData.appId, // 必填,公众号的唯一标识
timestamp: configData.timestamp, // 必填,生成签名的时间戳
nonceStr: configData.nonceStr, // 必填,生成签名的随机串
signature: configData.signature, // 必填,签名
jsApiList: ['updateAppMessageShareData', 'getNetworkType',
'updateTimelineShareData', 'getLocation'
],
openTagList: ['wx-open-launch-weapp','wx-open-subscribe']
});
_wx.ready(function(res) {
console.log('wx-jssdk-ready', res)
rr(true)
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
});
_wx.error(function(err) {
console.log('wx-jssdk-error', err)
rr(false)
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
});
})
})
}
export default wxjssdk;
边栏推荐
- NPDP产品经理国际认证报名有什么要求?
- The State Administration of Chia Tai market supervision, the national development and Reform Commission and the China Securities Regulatory Commission jointly reminded and warned some iron ores
- How to view the state-owned enterprises have unloaded Microsoft office and switched to Kingsoft WPS?
- Minimum spanning tree and bipartite graph in graph theory (acwing template)
- Opencv mat class
- Written on the first day after Doris graduated
- Problem note - Oracle 11g uninstall
- Rearrangement of overloaded operators
- Build MySQL master-slave server under Ubuntu 14.04
- 2022-2-15 learning the imitation Niuke project - Section 3 post details
猜你喜欢

JVM second conversation -- JVM memory model and garbage collection

【牛客网刷题系列 之 Verilog快速入门】~ 多功能数据处理器、求两个数的差值、使用generate…for语句简化代码、使用子模块实现三输入数的大小比较

JVM第一话 -- JVM入门详解以及运行时数据区分析

Microservice development steps (Nacos)

炎炎夏日,这份安全用气指南请街坊们收好!

111. Minimum depth of binary tree

一波三折,终于找到src漏洞挖掘的方法了【建议收藏】

How to view the state-owned enterprises have unloaded Microsoft office and switched to Kingsoft WPS?

C learning notes (5) class and inheritance
![After twists and turns, I finally found the method of SRC vulnerability mining [recommended collection]](/img/ac/ab6053e6ea449beedf434d4cf07dbb.png)
After twists and turns, I finally found the method of SRC vulnerability mining [recommended collection]
随机推荐
TypeScript:const
使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器
Cannot link redis when redis is enabled
Official announcement: Apache Doris graduated successfully and became the top project of ASF!
Basic operations of SQL database
NPDP能给产品经理带来什么价值?你都知道了吗?
Opencv interpolation mode
MIT team used graph neural network to accelerate the screening of amorphous polymer electrolytes and promote the development of next-generation lithium battery technology
Pat 1065 a+b and C (64bit) (20 points) (16 points)
JVM第二话 -- JVM内存模型以及垃圾回收
241. 为运算表达式设计优先级
手把手带你入门 API 开发
建立自己的网站(14)
关于重载运算符的再整理
Today, with the popularity of micro services, how does service mesh exist?
Configuration of ZABBIX API and PHP
深度分析数据在内存中的存储形式
TypeScript: let
What if you are always bullied because you are too honest in the workplace?
The first technology podcast month will be broadcast soon