uniapp 开发微信小程序订阅消息功能

时间:2021-6-11 作者:qvyue
代码如下:
async goApply() {
                const res = await uni.getSetting({ withSubscriptions: true })
                const go = () => {
                    this.sureApply() //接口
                }
                let s;
                try {
                    const [, { subscriptionsSetting: { itemSettings: _s } }] = await uni.getSetting({ withSubscriptions: true })
                    s = _s
                } catch(e) {
                    go()
                    return false
                }
                const tmplIds = [
                    't9_3hsUqanNeYNheYEDtsL26VWqCVRrFBaj7QLiSh2M',
                    '8R9SbxBeziYURpzOqMYQUyd1dCzBKQVuayrpeUr4PSQ',
                    'FjXnhSWmUSoCLf-eC1zkNxbojpD7bzoe0U9yVGRZR54'
                ]
                const t_alen = tmplIds.length
                let t_len = 0
                if(s) {
                    const s_k = Object.keys(s)
                    const s_v = Object.values(s)
                    t_len = tmplIds.filter(id => s_k.indexOf(id) !== -1).length
                }
                if(!s || t_len !== t_alen) {
                    uni.requestSubscribeMessage({
                        tmplIds,
                        success(res) {
                            const _o = Object.assign({}, res)
                            Reflect.deleteProperty(_o, 'errMsg')
                            const a_len = Object.values(_o).filter(s => s === 'accept').length
                            if(a_len !== t_alen){
                                uni.showModal({
                                    title: '提示',
                                    content: '请选择订阅服务,以便我们通知您活动信息',
                                    showCancel: false
                                })
                            } else {
                                go()
                            }
                        },
                        fail(err) {
                            uni.showModal({
                                title: '失败',
                                content: '打开订阅服务失败',
                                showCancel: false
                            })
                            go()
                        }
                    })
                } else {
                    go()
                }
            },
声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:qvyue@qq.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。