美文网首页
uniapp 开发微信小程序订阅消息功能

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

作者: 吃肉肉不吃肉肉 | 来源:发表于2021-04-23 14:04 被阅读0次
代码如下:
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()
                }
            },

相关文章

网友评论

      本文标题:uniapp 开发微信小程序订阅消息功能

      本文链接:https://www.haomeiwen.com/subject/ufpcrltx.html